Using a Sick WS15-D1130 Infrared Diode pair to act as a hardware motion detection sensor for Zoneminder. Also testing out an Omrom photo electric sensor, the E3F2-R2C4. This is a tripwire alarm sensor. When the beam is blocked, the alarm is activated.
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.

338 lines
7.2 KiB

6 years ago
  1. /*
  2. *
  3. * ZoneMinder Hardware Project
  4. * Laser Sensor
  5. *
  6. *
  7. * A sensor communicating
  8. * via ENC28J60 to ZMTrigger daemon
  9. *
  10. *
  11. */
  12. /*
  13. * What it does:
  14. *
  15. *
  16. */
  17. /*
  18. *
  19. *
  20. *
  21. * Directions:
  22. *
  23. * Use Arduino Uno
  24. * Connect Pin 3 to Reset pin (may not be needed)
  25. * Connect ENC28J60 using these instructions:
  26. * https://github.com/ntruchsess/arduino_uip
  27. * http://web.archive.org/save/https://create.arduino.cc/projecthub/Sourcery/how-to-connect-the-enc28j60-to-an-arduino-efd0dd
  28. * CS for ENC and UIP library is 10 by default on UNO. Not 8, like that link says.
  29. *
  30. * Connect microwave motion sensor such as HB100, or laser diode to A1
  31. * Add a speaker for audible debugging
  32. * LED can also be added
  33. *
  34. *
  35. *
  36. *
  37. */
  38. #include <UIPEthernet.h>
  39. #include <EEPROM.h>
  40. //Edit the below values
  41. #define DEBUGMODE 1 // 1 == on. 0 == off.
  42. /***************Ethernet ENC28J60***************/
  43. //Mac must be unique
  44. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xEA, 0x11 };
  45. //IP of Arduino
  46. byte ip[] = { 192, 168, 1, 177 };
  47. //IP of zm server
  48. byte server[] = { 192, 168, 1, 178 };
  49. //ZM server ip to put in requests.
  50. //maybe you can use hostname, not sure. TODO: test hostnames
  51. String host="192.168.1.178";
  52. //username and password to login to Zoneminder Server.
  53. //If you don't have authentication, you will need to edit the
  54. //script.
  55. //NOTE: not needed for ZMTrigger. Only API.
  56. String username="username";
  57. String password="password";
  58. EthernetClient client;
  59. #define ZMTRIGGERPORT 6802
  60. /***************Pins***************/
  61. #define SPEAKER_PIN 6
  62. #define LED_PIN 9
  63. #define RESETPIN 2 //may not be needed here
  64. #define SENSORPIN A1
  65. /***************Variables************/
  66. int MotionSensorRead = 0;
  67. uint8_t AlarmActive = 0;
  68. char* ZMTriggerMessage = "1234567890123456789012345678901234"; //Initialize this with dummy data
  69. // Upper and lower limit for ADC to register motion
  70. // The HB100 outputs a wave that chaotically moves up and down. If the wave reaches a
  71. // high or low point, we register an alarm.
  72. // These should be tuned depending on your setup and how sensitive you want motion detected
  73. // without getting false alarms. Easiest to test with Serial output
  74. #define UPPERLIMIT 900
  75. #define LOWERLIMIT 100
  76. /*
  77. ZMTrigger Command to Send
  78. B<id>|B<action>|B<score>|B<cause>|B<text>|B<showtext>
  79. which in this code is:
  80. monnum | onoff + timealarm | score | source
  81. e.g.
  82. 2|on+5|100|ZoneAVR||
  83. This will send a command to ZMTrigger.pl to turn monitor #2 ON (alarm state) for five seconds, with a score of 100
  84. and the source of ZoneAVR. The text field, and show text are not setup here.
  85. */
  86. char* monnum = "25"; //monitor number
  87. char* onoff = "on"; //command to send to zmtrigger.
  88. char* timealarm = "10"; //time to set monitor to alarm
  89. char* score = "100"; //score to assign
  90. char* source = "ZMHW MotionSensor"; //source
  91. //Do not need to edit below
  92. void chime(int freq){
  93. tone(SPEAKER_PIN, freq, 50);
  94. delay(50);
  95. }
  96. void chimefast(int freq, int fast){
  97. tone(SPEAKER_PIN, freq, fast);
  98. delay(fast);
  99. }
  100. //timer/interrupt
  101. uint16_t timer1;
  102. uint16_t timer1_counter;
  103. uint8_t debouncetime;
  104. uint8_t first_interrupt = 0;
  105. //timer for debounce
  106. ISR(TIMER1_OVF_vect){
  107. timer1++;
  108. if (first_interrupt == 1 ){
  109. debouncetime++;
  110. }
  111. if (debouncetime > 2) {
  112. first_interrupt = 0;
  113. debouncetime = 0;
  114. AlarmActive = 0;
  115. }
  116. }
  117. void setup()
  118. {
  119. Serial.begin(9600);
  120. Serial.println("ZMHW Project");
  121. Serial.println("Motion Sensor");
  122. pinMode(SENSORPIN, INPUT);
  123. pinMode(SPEAKER_PIN, OUTPUT);
  124. pinMode(RESETPIN, OUTPUT);
  125. Ethernet.begin(mac, ip);
  126. //timer 1, setup
  127. //Crystal of Uno is == Mega (16MHz)
  128. //this timer is all bodged up, but doesn't matter, as we only
  129. //need two or three counts between alarms. Not going to fix atm.
  130. //and it works.
  131. //Clear existing registers
  132. TCCR1A = 0;
  133. TCCR1B = 0;
  134. // Set timer1_counter to the correct value for our interrupt interval
  135. //timer1_counter = 10000; // 62500 for one second if using 256 prescaler. can't be over 16 bit value (timer1 is 16bit limited)
  136. //timer1_counter = 10;
  137. //TCNT1 = timer1_counter; // TCNT1 is what we are overflowing on
  138. TCCR1B |= (1 << CS12); // 256 prescaler (divide 16mhz/256 = 62500)
  139. TCCR1B |= 00000101; // https://web.archive.org/web/20170707164930/http://www.avrbeginners.net:80/architecture/timers/timers.html
  140. // search tccr1b
  141. TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt (if goes over timer, interrupt flagged)
  142. //end timer1
  143. sei(); //timer needs interrupts, enable interrupts
  144. tone(SPEAKER_PIN, 1000, 200);
  145. delay(100);
  146. tone(SPEAKER_PIN, 2000, 200);
  147. delay(100);
  148. tone(SPEAKER_PIN, 2200, 200);
  149. delay(100);
  150. }
  151. void loop()
  152. {
  153. //The SICK infrared laser requires a transistor to output
  154. //high or low, but we will cheat and instead, put it through
  155. //a serial diode, and then use the ADC instead of a digital pin
  156. //saves a few seconds to put a diode on the end instead of transistor
  157. MotionSensorRead = analogRead(SENSORPIN);
  158. Serial.print("Motion Sensor Value: ");
  159. Serial.println(String(MotionSensorRead));
  160. //Serial.println(String(timer1));
  161. if(DEBUGMODE){
  162. delay(10);
  163. }
  164. //upon boot, values are around 400 sometimes, so only alert at higher
  165. if (MotionSensorRead > 500 && AlarmActive == 0){
  166. Serial.println("Motion Detected");
  167. //firstpacketsend = 0;
  168. cli();
  169. //some of this may be redundant, need to check
  170. AlarmActive = 1;
  171. first_interrupt = 1;
  172. debouncetime = 0;
  173. sei();
  174. //Want the chime to be only noticeable if you know what to listen
  175. //for. Make it a high freq. sound that is easy to miss.
  176. chime(13000);
  177. Serial.println("Connecting...");
  178. if (client.connect(server, ZMTRIGGERPORT)) {
  179. chime(13000);
  180. //beware that the buffer in snprintf is big enough to hold everything
  181. snprintf(ZMTriggerMessage, 56, "%s|%s+%s|%s|%s||", monnum, onoff, timealarm, score, source);
  182. Serial.print("the TCP Packet being sent:");
  183. Serial.println(String(ZMTriggerMessage));
  184. client.println(String(ZMTriggerMessage)); //required
  185. Serial.println("TCP packet sent to ZMTrigger");
  186. client.stop();
  187. }
  188. else {
  189. //NOTE: If you are not connected to the network
  190. //the device will currently freeze up, and not timeout.
  191. //Need to implement a watchdog.
  192. //If you ARE connected to the network, and server is not available
  193. //then it will timeout.
  194. Serial.println("Connection to ZM Server failed");
  195. chime(50);
  196. delay(100);
  197. chime(50);
  198. delay(100);
  199. chime(50);
  200. delay(100);
  201. }
  202. }
  203. //disconnect
  204. if (!client.connected()) {
  205. client.stop();
  206. }
  207. //write to eeprom if connection failed, and try again upon reboot
  208. //EEPROM.write(EEPROM_RETRY, switch_pressed);
  209. } //end main loop