I’m back … forced home by the Seveso flood (a river here in Milan) I take this opportunity to update the blog.
In previous post we have :
- read motion sensors data
- read sensor noise data
- connected the ethernet shield and sent an email
At this point we can put all together.
But i want also to use the webcam I have at home (model D-Link DCS-930L).
I don’t want to let on the webcam the whole day, I want to turn on only if a movement/noise is detected and I want to be able to turn on or off from intenet.
I decided to buy a wireless power strip like this.
I can connect webcam to power strip and turn or/off the power strip (and then the webcam) with Arduino.
I have only to know the power strip sensor code as done in the previous post with the movement sensor.
Using the power strip remote I read this codes:
power strip turn on code | power strip turn off code |
1394001 | 1394004 |
Now I have all the codes I need. Now I have to connect wireless trasmitter to Arduino.
Here is the final result:
I had to modify the connections made in the previous posts because ethernet shield use exclusively pin 10,11,12 and 13 .
Here we are the new connections between arduino and trasmitter and receiver modules.
Noise sensor
Vcc | 5 V |
GND | GND |
SNG | Pin 5 (digital) |
Receiver sensor at 433 Mhz
Receiver Pin | Pin Arduino |
Vcc | 5 V |
GND | GND |
DATA | Pin 2 (digital) |
Trasmitter Sensor 433 Mhz
Trasmitter Pin | Pin Arduino |
Vcc | 5 V |
GND | GND |
DATA | Pin 9 (digital) |
Here comes the complete code :
#include <String.h> #include <RCSwitch.h> #include <Time.h> #include <SPI.h> #include <Ethernet.h> /* Informazioni Ethernet*/ byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; int SERIAL_BAUD = 9600; int TRASMIT_PIN = 9; int RECEIVE_PIN = 0; int NOISE_DIGITAL_PIN = 5; //Sensori long CODE_DOOR_SENSOR = 3557625; //sendor code long CODE_KITCHEN_SENSOR = 10521177; long CODE_WEBCAM_ON_SENSOR = 1394001; long CODE_WEBCAM_OFF_SENSOR= 1394004; EthernetClient client; EthernetServer server(8081); char smtpServer[] = "smtpcorp.com"; RCSwitch mySwitch = RCSwitch(); void setup() { Serial.begin(SERIAL_BAUD); pinMode(SERIAL_BAUD, INPUT); mySwitch.enableReceive(RECEIVE_PIN); mySwitch.enableTransmit(TRASMIT_PIN); setupComm(); } void loop() { getClientConnection(); if (detectNoise()){ Serial.print("Noise detected"); email("Attention, noise at home!"); accendiCam(CODE_WEBCAM_ON_SENSOR) ; } if (mySwitch.available()) { int value = mySwitch.getReceivedValue(); Serial.print(value); if (value == 0) { Serial.print("Unknown encoding"); Serial.print("n"); } else { long receivedValue = mySwitch.getReceivedValue(); if (receivedValue == CODE_DOOR_SENSOR) { Serial.print("Attention! Door sensor activated!"); Serial.print("n"); email("Attention, Door sensor activated!"); accendiCam(CODE_WEBCAM_ON_SENSOR) ; delay(1000); } else if(receivedValue == CODE_KITCHEN_SENSOR) { Serial.print("Attenzione! kitchen sensor activated!"); Serial.print("n"); email("Attenzione, kitchen sensor activated!"); accendiCam(CODE_WEBCAM_ON_SENSOR) ; delay(1000); } Serial.print("Received "); Serial.print( receivedValue); Serial.print(" / "); Serial.print( mySwitch.getReceivedBitlength() ); Serial.print("bit "); Serial.print("Protocol: "); Serial.println( mySwitch.getReceivedProtocol() ); } mySwitch.resetAvailable(); } } bool detectNoise () { bool rit = false; if (digitalRead(NOISE_DIGITAL_PIN) == HIGH) { rit = true; } return rit; } void accendiCam(long value) { mySwitch.send(value, 24); mySwitch.send(value, 24); mySwitch.send(value, 24); mySwitch.send(value, 24); mySwitch.send(value, 24); } void setupComm() { Serial.println("Trying to connect"); Serial.print("n"); if (!Ethernet.begin(mac)){ Serial.println("Failed to DHCP"); while(true); } Serial.print("My IP address: "); for (byte thisByte = 0; thisByte < 4; thisByte++) { Serial.print(Ethernet.localIP()[thisByte], DEC); Serial.print("."); } Serial.println("fine"); } // Call to send an email. bool email(char* text) { bool success = false; Serial.println("Sending email..."); Serial.print("n"); Serial.println("SMTP server..."); Serial.print(smtpServer); if (client.connect(smtpServer, 2525)){ Serial.println("connected"); delay(100); client.println("EHLO arduino"); for(int i=0; i<999; ++i){ if(client.read() > 0) break; } Serial.println("responded"); Serial.print("n"); client.println("AUTH LOGIN"); client.println("xxxxxxx"); //see explanation on last post client.println("yyyyyy"); //see explanation on last post client.println("MAIL FROM:<dumm@gmail.com>"); for(int i=0; i<999; ++i){ if(client.read() > 0) break; } client.println("RCPT TO:<giuseppe.scola@gmail.com>"); for(int i=0; i<999; ++i){ if(client.read() > 0) break; } client.println("DATA"); for(int i=0; i<999; ++i){ if(client.read() > 0) break; } client.println("from: giuseppe.scola@gmail.com"); client.println("to: giuseppe.scola@gmail.com"); client.println("SUBJECT: ArduAlarm"); client.println(""); client.println(text); client.println("."); client.println("QUIT"); for(int i=0; i<999; ++i){ if(i > 998){ Serial.println("error: No response"); } if(client.read() > 0) break; } success = true; client.println(); Serial.println("end"); Serial.print("n"); } else { Serial.println("Failed"); Serial.print("n"); client.println("QUIT"); } client.stop(); return success; } void getClientConnection(){ EthernetClient client = server.available(); if (client) { String postString =""; Serial.println("new request"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); //postString.concat(c); if(postString.length()<10){ postString +=c; } // Serial.write(c); if (c == 'n' && currentLineIsBlank) { //if(readString.indexOf("id=1") > 0){ 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>"); //client.println("<h1>Settaggi</h1><br>"); client.println("<h1>AllarDuino</h1>"); client.print("<br>"); client.println("<a href=\"./?on\">CAM On</a>"); client.println("<a href=\"./?off\">CAM Off</a>"); client.println("</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; } } } //fine client.connected Serial.println("-------------"); Serial.println(postString); Serial.println("-------------"); if(postString.indexOf("?on") > 0){ Serial.println("CAM On"); accendiCam(CODE_WEBCAM_ON_SENSOR); client.println("<br/>"); client.println("<p>Cam On</p>"); } if(postString.indexOf("?off") > 0){ accendiCam(CODE_WEBCAM_OFF_SENSOR); client.println("<br/>"); client.println("<p>Cam Off</p>"); } delay(1); // close the connection: client.stop(); Serial.println("client disonnected"); } }
In the next days I’ll try to have time to comment the code expecially the html generation.