You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
8.4 KiB

4 years ago
  1. /*
  2. * ZMHW Modector
  3. *
  4. * Digital High outputs TCP Packet to ZMTrigger
  5. * Steak Electronics Company 2019
  6. *
  7. *
  8. */
  9. /*
  10. Same as default library but uses UIPEthernet. Requires UIPEthernet.
  11. Manage Libraries - Download UIP Ethernet library
  12. */
  13. /*
  14. Web Server
  15. A simple web server that shows the value of the analog input pins.
  16. using an Arduino Wiznet Ethernet shield.
  17. Circuit:
  18. * Ethernet shield attached to pins 10, 11, 12, 13
  19. * Analog inputs attached to pins A0 through A5 (optional)
  20. created 18 Dec 2009
  21. by David A. Mellis
  22. modified 9 Apr 2012
  23. by Tom Igoe
  24. modified 02 Sept 2015
  25. by Arturo Guadalupi
  26. */
  27. #include <SPI.h>
  28. #include <UIPEthernet.h>
  29. #define NORMALMODECT 1 // For a digital High motion sensor.
  30. #define TWOSERVERS 1
  31. // Enter a MAC address and IP address for your controller below.
  32. // The IP address will be dependent on your local network:
  33. byte mac[] = {
  34. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x50
  35. };
  36. IPAddress ip(192, 168, 1, 50);
  37. // Initialize the Ethernet server library
  38. // with the IP address and port you want to use
  39. // (port 80 is default for HTTP):
  40. #define LISTENPORT 80
  41. EthernetServer serverHOST(LISTENPORT);
  42. //Client
  43. byte server[] = { 192, 168, 1, 115 };
  44. //byte serverB[] = { 192, 168, 1, 116 }; //can't be server2, as that is already set for ethernetserver below
  45. String host="192.168.1.115";
  46. char* monnum = "32"; //monitor number
  47. char* LOCATIONOFSENSOR = "FtLab";
  48. #define ZMTRIGGERPORT 6802
  49. EthernetClient client;
  50. //IO
  51. #define MODECTPIN 8
  52. //GLOBALS
  53. uint8_t AlarmActive = 0;
  54. char* ZMTriggerMessage = "1234567890123456789012345"; //Initialize this with dummy data
  55. uint8_t TEMPERATUREVALUE = 0;
  56. uint8_t TEMPERATUREVALUE2 = 0;
  57. uint8_t MODECTVALUE = 0;
  58. char* onoff = "on"; //command to send to zmtrigger.
  59. char* timealarm = "10"; //time to set monitor to alarm
  60. char* score = "77"; //score to assign
  61. char* source = "ZMHW";//source. Add details as needed (e.g. Hallway sensor)
  62. uint8_t ActivateServer2 = 0;
  63. //Interrupt / Timer
  64. uint16_t timer1 = 0;
  65. uint16_t timer1_counter = 0;
  66. uint8_t debouncetime = 0;
  67. uint8_t first_interrupt = 0;
  68. //for debounce
  69. ISR(TIMER1_OVF_vect){
  70. timer1++;
  71. if (first_interrupt == 1 ){
  72. debouncetime++;
  73. }
  74. if (debouncetime > 2) {
  75. first_interrupt = 0;
  76. debouncetime = 0;
  77. AlarmActive = 0;
  78. }
  79. }
  80. void TimerInit(void){
  81. //Crystal of Uno is == Mega (16MHz)
  82. //this timer init is flawed, but doesn't matter, as we only
  83. //need two or three counts between alarms. TODO: clean up
  84. TCCR1A = 0; //Clear existing registers
  85. TCCR1B = 0;
  86. // Set timer1_counter to the correct value for our interrupt interval
  87. //timer1_counter = 10000; // 62500 for one second if using 256 prescaler. can't be over 16 bit value (timer1 is 16bit limited)
  88. //timer1_counter = 10;
  89. //TCNT1 = timer1_counter; // TCNT1 is what we are overflowing on
  90. TCCR1B |= (1 << CS12); // 256 prescaler (divide 16mhz/256 = 62500)
  91. TCCR1B |= 00000101; // https://web.archive.org/web/20170707164930/http://www.avrbeginners.net:80/architecture/timers/timers.html
  92. // search tccr1b
  93. TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt (if goes over timer, interrupt flagged)
  94. sei(); //timer needs interrupts, enable (set) interrupts
  95. }
  96. void ReadModect(void){
  97. if (NORMALMODECT){
  98. delay(10);
  99. MODECTVALUE = digitalRead(MODECTPIN);
  100. delay(10);
  101. //Serial.print(F("Motion Detector Value (normal): "));
  102. //Serial.println(String(MODECTVALUE));
  103. //Serial.println(MODECTVALUE);
  104. delay(30);
  105. }
  106. }
  107. void DigiModectChkandSend(void){
  108. //Digital High Motion Sensor
  109. if(NORMALMODECT){
  110. if (MODECTVALUE==HIGH && AlarmActive == 0){
  111. Serial.println(F("Motion Detected on Normal Sensor"));
  112. //firstpacketsend = 0;
  113. cli();
  114. //some of this may be redundant, need to check
  115. AlarmActive = 1;
  116. first_interrupt = 1;
  117. debouncetime = 0;
  118. sei();
  119. delay(10);
  120. Serial.println(F("Connecting To Cam Srv"));
  121. if (client.connect(server, ZMTRIGGERPORT)) {
  122. //beware that the buffer in snprintf is big enough to hold everything
  123. snprintf(ZMTriggerMessage, 56, "%s|%s+%s|%s|%s||", monnum, onoff, timealarm, score, source);
  124. Serial.print(F("the TCP Packet being sent:"));
  125. Serial.println(String(ZMTriggerMessage));
  126. client.println(String(ZMTriggerMessage)); //required
  127. Serial.println(F("TCP packet sent to ZMTrigger"));
  128. client.stop();
  129. if(TWOSERVERS){
  130. ActivateServer2 = 1;
  131. }
  132. }
  133. else {
  134. //NOTE: If you are not connected to the network
  135. //the device will currently freeze up, and not timeout.
  136. //Need to implement a watchdog.
  137. //If you ARE connected to the network, and server is not available
  138. //then it will timeout.
  139. Serial.println(F("Connection to Server failed"));
  140. }
  141. }
  142. }
  143. }
  144. void SendtoServer2(void){
  145. server[0] = 192;
  146. server[1] = 168;
  147. server[2] = 1;
  148. server[3] = 116;
  149. host="192.168.1.116";
  150. delay(10);
  151. Serial.println(F("Connecting To Cam Srv"));
  152. if (client.connect(server, ZMTRIGGERPORT)) {
  153. client.println(String(ZMTriggerMessage)); //required
  154. Serial.println(F("TCP packet sent to Server Two ZMTrigger"));
  155. client.stop();
  156. }
  157. server[0] = 192;
  158. server[1] = 168;
  159. server[2] = 1;
  160. server[3] = 115;
  161. host="192.168.1.115";
  162. ActivateServer2 = 0;
  163. }
  164. void setup() {
  165. // Open serial communications and wait for port to open:
  166. Serial.begin(9600);
  167. while (!Serial) {
  168. ; // wait for serial port to connect. Needed for native USB port only
  169. }
  170. Serial.println(F("ZMHW Project"));
  171. Serial.println(F("Motion Sensor"));
  172. // start the Ethernet connection and the server:
  173. Ethernet.begin(mac, ip);
  174. serverHOST.begin();
  175. Serial.print(F("server is at "));
  176. Serial.println(Ethernet.localIP());
  177. pinMode(MODECTPIN, INPUT);
  178. TimerInit();
  179. }
  180. void loop() {
  181. ReadModect();
  182. DigiModectChkandSend();
  183. if(TWOSERVERS && ActivateServer2){
  184. SendtoServer2();
  185. }
  186. // listen for incoming clients
  187. EthernetClient client = serverHOST.available();
  188. if (client) {
  189. Serial.println("new client");
  190. // an http request ends with a blank line
  191. boolean currentLineIsBlank = true;
  192. while (client.connected()) {
  193. if (client.available()) {
  194. char c = client.read();
  195. Serial.write(c);
  196. // if you've gotten to the end of the line (received a newline
  197. // character) and the line is blank, the http request has ended,
  198. // so you can send a reply
  199. if (c == '\n' && currentLineIsBlank) {
  200. // send a standard http response header
  201. client.println("HTTP/1.1 200 OK");
  202. client.println("Content-Type: text/html");
  203. //client.println("Connection: close"); // the connection will be closed after completion of the response
  204. //client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  205. client.println();
  206. client.println("<!DOCTYPE HTML>");
  207. client.println("<html><pre>");
  208. client.println("<b>Steak Electronics</b>");
  209. client.println("\"Eat Steak, Use Birth Control\"");
  210. client.println("");
  211. client.print("Sensor Location:");
  212. client.println(LOCATIONOFSENSOR);
  213. client.print("Type::");
  214. client.println("HFS-DC06H");
  215. client.print("Monitor: ");
  216. client.println(monnum);
  217. client.print("Temp:");
  218. client.println(TEMPERATUREVALUE2);
  219. client.println("</pre></html>");
  220. break;
  221. }
  222. if (c == '\n') {
  223. // you're starting a new line
  224. currentLineIsBlank = true;
  225. } else if (c != '\r') {
  226. // you've gotten a character on the current line
  227. currentLineIsBlank = false;
  228. }
  229. }
  230. }
  231. // give the web browser time to receive the data
  232. delay(1);
  233. // close the connection:
  234. client.stop();
  235. Serial.println("client disconnected");
  236. }
  237. }
  238. /*
  239. ZMTrigger Command to Send
  240. B<id>|B<action>|B<score>|B<cause>|B<text>|B<showtext>
  241. which in this code is:
  242. monnum | onoff + timealarm | score | source
  243. e.g.
  244. 2|on+5|100|ZoneAVR||
  245. This will send a command to ZMTrigger.pl to turn monitor #2 ON (alarm state) for five seconds, with a score of 100
  246. and the source of ZoneAVR. The text field, and show text are not setup here.
  247. */