/*
|
|
* ZMHW Modector
|
|
*
|
|
* Digital High outputs TCP Packet to ZMTrigger
|
|
* Steak Electronics Company 2019
|
|
*
|
|
*
|
|
*/
|
|
|
|
/*
|
|
|
|
Same as default library but uses UIPEthernet. Requires UIPEthernet.
|
|
Manage Libraries - Download UIP Ethernet library
|
|
|
|
*/
|
|
|
|
/*
|
|
Web Server
|
|
|
|
A simple web server that shows the value of the analog input pins.
|
|
using an Arduino Wiznet Ethernet shield.
|
|
|
|
Circuit:
|
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
|
* Analog inputs attached to pins A0 through A5 (optional)
|
|
|
|
created 18 Dec 2009
|
|
by David A. Mellis
|
|
modified 9 Apr 2012
|
|
by Tom Igoe
|
|
modified 02 Sept 2015
|
|
by Arturo Guadalupi
|
|
|
|
*/
|
|
|
|
#include <SPI.h>
|
|
#include <UIPEthernet.h>
|
|
|
|
#define NORMALMODECT 1 // For a digital High motion sensor.
|
|
#define TWOSERVERS 1
|
|
|
|
// Enter a MAC address and IP address for your controller below.
|
|
// The IP address will be dependent on your local network:
|
|
byte mac[] = {
|
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x50
|
|
};
|
|
IPAddress ip(192, 168, 1, 50);
|
|
|
|
// Initialize the Ethernet server library
|
|
// with the IP address and port you want to use
|
|
// (port 80 is default for HTTP):
|
|
#define LISTENPORT 80
|
|
EthernetServer serverHOST(LISTENPORT);
|
|
|
|
|
|
//Client
|
|
byte server[] = { 192, 168, 1, 115 };
|
|
//byte serverB[] = { 192, 168, 1, 116 }; //can't be server2, as that is already set for ethernetserver below
|
|
String host="192.168.1.115";
|
|
char* monnum = "32"; //monitor number
|
|
char* LOCATIONOFSENSOR = "FtLab";
|
|
#define ZMTRIGGERPORT 6802
|
|
EthernetClient client;
|
|
|
|
//IO
|
|
#define MODECTPIN 8
|
|
|
|
//GLOBALS
|
|
uint8_t AlarmActive = 0;
|
|
char* ZMTriggerMessage = "1234567890123456789012345"; //Initialize this with dummy data
|
|
uint8_t TEMPERATUREVALUE = 0;
|
|
uint8_t TEMPERATUREVALUE2 = 0;
|
|
uint8_t MODECTVALUE = 0;
|
|
char* onoff = "on"; //command to send to zmtrigger.
|
|
char* timealarm = "10"; //time to set monitor to alarm
|
|
char* score = "77"; //score to assign
|
|
char* source = "ZMHW";//source. Add details as needed (e.g. Hallway sensor)
|
|
uint8_t ActivateServer2 = 0;
|
|
|
|
//Interrupt / Timer
|
|
uint16_t timer1 = 0;
|
|
uint16_t timer1_counter = 0;
|
|
uint8_t debouncetime = 0;
|
|
uint8_t first_interrupt = 0;
|
|
|
|
//for debounce
|
|
ISR(TIMER1_OVF_vect){
|
|
timer1++;
|
|
if (first_interrupt == 1 ){
|
|
debouncetime++;
|
|
}
|
|
if (debouncetime > 2) {
|
|
first_interrupt = 0;
|
|
debouncetime = 0;
|
|
AlarmActive = 0;
|
|
}
|
|
}
|
|
|
|
void TimerInit(void){
|
|
//Crystal of Uno is == Mega (16MHz)
|
|
//this timer init is flawed, but doesn't matter, as we only
|
|
//need two or three counts between alarms. TODO: clean up
|
|
|
|
TCCR1A = 0; //Clear existing registers
|
|
TCCR1B = 0;
|
|
|
|
// Set timer1_counter to the correct value for our interrupt interval
|
|
//timer1_counter = 10000; // 62500 for one second if using 256 prescaler. can't be over 16 bit value (timer1 is 16bit limited)
|
|
//timer1_counter = 10;
|
|
//TCNT1 = timer1_counter; // TCNT1 is what we are overflowing on
|
|
TCCR1B |= (1 << CS12); // 256 prescaler (divide 16mhz/256 = 62500)
|
|
TCCR1B |= 00000101; // https://web.archive.org/web/20170707164930/http://www.avrbeginners.net:80/architecture/timers/timers.html
|
|
// search tccr1b
|
|
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt (if goes over timer, interrupt flagged)
|
|
sei(); //timer needs interrupts, enable (set) interrupts
|
|
}
|
|
|
|
void ReadModect(void){
|
|
if (NORMALMODECT){
|
|
delay(10);
|
|
MODECTVALUE = digitalRead(MODECTPIN);
|
|
delay(10);
|
|
//Serial.print(F("Motion Detector Value (normal): "));
|
|
//Serial.println(String(MODECTVALUE));
|
|
//Serial.println(MODECTVALUE);
|
|
delay(30);
|
|
}
|
|
}
|
|
|
|
void DigiModectChkandSend(void){
|
|
//Digital High Motion Sensor
|
|
if(NORMALMODECT){
|
|
|
|
if (MODECTVALUE==HIGH && AlarmActive == 0){
|
|
Serial.println(F("Motion Detected on Normal Sensor"));
|
|
//firstpacketsend = 0;
|
|
cli();
|
|
//some of this may be redundant, need to check
|
|
AlarmActive = 1;
|
|
first_interrupt = 1;
|
|
debouncetime = 0;
|
|
sei();
|
|
|
|
delay(10);
|
|
Serial.println(F("Connecting To Cam Srv"));
|
|
if (client.connect(server, ZMTRIGGERPORT)) {
|
|
//beware that the buffer in snprintf is big enough to hold everything
|
|
snprintf(ZMTriggerMessage, 56, "%s|%s+%s|%s|%s||", monnum, onoff, timealarm, score, source);
|
|
Serial.print(F("the TCP Packet being sent:"));
|
|
Serial.println(String(ZMTriggerMessage));
|
|
client.println(String(ZMTriggerMessage)); //required
|
|
Serial.println(F("TCP packet sent to ZMTrigger"));
|
|
client.stop();
|
|
if(TWOSERVERS){
|
|
ActivateServer2 = 1;
|
|
}
|
|
}
|
|
else {
|
|
//NOTE: If you are not connected to the network
|
|
//the device will currently freeze up, and not timeout.
|
|
//Need to implement a watchdog.
|
|
//If you ARE connected to the network, and server is not available
|
|
//then it will timeout.
|
|
Serial.println(F("Connection to Server failed"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void SendtoServer2(void){
|
|
server[0] = 192;
|
|
server[1] = 168;
|
|
server[2] = 1;
|
|
server[3] = 116;
|
|
host="192.168.1.116";
|
|
delay(10);
|
|
Serial.println(F("Connecting To Cam Srv"));
|
|
if (client.connect(server, ZMTRIGGERPORT)) {
|
|
client.println(String(ZMTriggerMessage)); //required
|
|
Serial.println(F("TCP packet sent to Server Two ZMTrigger"));
|
|
client.stop();
|
|
}
|
|
server[0] = 192;
|
|
server[1] = 168;
|
|
server[2] = 1;
|
|
server[3] = 115;
|
|
host="192.168.1.115";
|
|
ActivateServer2 = 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
// Open serial communications and wait for port to open:
|
|
Serial.begin(9600);
|
|
while (!Serial) {
|
|
; // wait for serial port to connect. Needed for native USB port only
|
|
}
|
|
Serial.println(F("ZMHW Project"));
|
|
Serial.println(F("Motion Sensor"));
|
|
|
|
|
|
// start the Ethernet connection and the server:
|
|
Ethernet.begin(mac, ip);
|
|
serverHOST.begin();
|
|
Serial.print(F("server is at "));
|
|
Serial.println(Ethernet.localIP());
|
|
|
|
pinMode(MODECTPIN, INPUT);
|
|
|
|
TimerInit();
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
ReadModect();
|
|
DigiModectChkandSend();
|
|
|
|
if(TWOSERVERS && ActivateServer2){
|
|
SendtoServer2();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// listen for incoming clients
|
|
EthernetClient client = serverHOST.available();
|
|
if (client) {
|
|
Serial.println("new client");
|
|
// an http request ends with a blank line
|
|
boolean currentLineIsBlank = true;
|
|
while (client.connected()) {
|
|
if (client.available()) {
|
|
char c = client.read();
|
|
Serial.write(c);
|
|
// if you've gotten to the end of the line (received a newline
|
|
// character) and the line is blank, the http request has ended,
|
|
// so you can send a reply
|
|
if (c == '\n' && currentLineIsBlank) {
|
|
// send a standard http response header
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
//client.println("Connection: close"); // the connection will be closed after completion of the response
|
|
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
|
|
client.println();
|
|
client.println("<!DOCTYPE HTML>");
|
|
client.println("<html><pre>");
|
|
client.println("<b>Steak Electronics</b>");
|
|
client.println("\"Eat Steak, Use Birth Control\"");
|
|
client.println("");
|
|
client.print("Sensor Location:");
|
|
client.println(LOCATIONOFSENSOR);
|
|
client.print("Type::");
|
|
client.println("HFS-DC06H");
|
|
client.print("Monitor: ");
|
|
client.println(monnum);
|
|
client.print("Temp:");
|
|
client.println(TEMPERATUREVALUE2);
|
|
client.println("</pre></html>");
|
|
break;
|
|
}
|
|
if (c == '\n') {
|
|
// you're starting a new line
|
|
currentLineIsBlank = true;
|
|
} else if (c != '\r') {
|
|
// you've gotten a character on the current line
|
|
currentLineIsBlank = false;
|
|
}
|
|
}
|
|
}
|
|
// give the web browser time to receive the data
|
|
delay(1);
|
|
// close the connection:
|
|
client.stop();
|
|
Serial.println("client disconnected");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
ZMTrigger Command to Send
|
|
|
|
B<id>|B<action>|B<score>|B<cause>|B<text>|B<showtext>
|
|
which in this code is:
|
|
monnum | onoff + timealarm | score | source
|
|
|
|
e.g.
|
|
2|on+5|100|ZoneAVR||
|
|
|
|
This will send a command to ZMTrigger.pl to turn monitor #2 ON (alarm state) for five seconds, with a score of 100
|
|
and the source of ZoneAVR. The text field, and show text are not setup here.
|
|
|
|
*/
|