/** Code developed by Antonio Vaz * Triggers BasicAuthentication and validates default user and password * Requires: * 1. Ethernet Shield by Arduino */ #include #include #include "AM2320.h" // AM2320 drivers #include "DHT.h" // DHT drivers #define DHTPIN 5 // what pin we're connected to for DHT sensor #define DHTTYPE DHT11 // DHT 22 (AM2302) #define LOW_INTERIOR 4 #define HIGH_INTERIOR 3 #define OFF_INTERIOR 6 #define LOW_EXTERIOR 7 #define HIGH_EXTERIOR 8 #define OFF_EXTERIOR 9 #define AUTO_INTERIOR A0 #define AUTO_EXTERIOR A1 #define THRESH_INSIDE_H_LOW 50 #define THRESH_INSIDE_H_HIGH 60 #define THRESH_INSIDE_T_LOW 30 #define THRESH_INSIDE_T_HIGH 40 #define THRESH_OUTSIDE_H_LOW 50 #define THRESH_OUTSIDE_H_HIGH 60 #define THRESH_OUTSIDE_T_LOW 30 #define THRESH_OUTSIDE_T_HIGH 40 void enableAutoInterior(); void enableAutoExterior(); void enableOffInterior(); void enableOffExterior(); void enableLowInterior(); void enableHighInterior(); void enableLowExterior(); void enableHighExterior(); void readActivateSensores(); DHT dht(DHTPIN, DHTTYPE); AM2320 th; EthernetServer server(93); int debug = 1; float h = 0, t = 0, hExt = 0, tExt = 0; boolean autoModeInside = true; boolean autoModeOutside = true; boolean offModeInside = false; boolean offModeOutside = false; boolean forceLowInside = false; boolean forceHighInside = false; boolean forceLowOutside = false; boolean forceHighOutside = false; void setup() { byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x03 }; Serial.begin(9600); while(!Serial); pinMode(LOW_INTERIOR, OUTPUT); // low interior pinMode(HIGH_INTERIOR, OUTPUT); // HIGH interior pinMode(OFF_INTERIOR, OUTPUT); // OFF Interior pinMode(LOW_EXTERIOR, OUTPUT); // low exterior pinMode(HIGH_EXTERIOR, OUTPUT); //high exterior pinMode(OFF_EXTERIOR, OUTPUT); // off exterior pinMode(AUTO_INTERIOR, OUTPUT); pinMode(AUTO_EXTERIOR, OUTPUT); if(Ethernet.begin(mac) != 1){ while(true) { delay(1000); } } if(debug) Serial.println(Ethernet.localIP()); dht.begin(); Wire.begin(); server.begin(); } void loop() { char linebuf[80]; int charcount=0; boolean authentificated=false; readActivateSensores(); EthernetClient client = server.available(); if (client) { boolean currentLineIsBlank = true; memset(linebuf,0,sizeof(linebuf)); charcount=0; authentificated=false; while (client.connected()) { if (client.available()) { char c = client.read(); if(debug) Serial.print(c); linebuf[charcount]=c; if (charcount")); client.println(F("")); client.println(F("

Sensores

Refresh
")); client.println(F("")); client.print(F("Interior H: ")); client.print(h); client.print(F(" | T: ")); client.println(t); client.print(F("
Exterior H: ")); client.print(hExt); client.print(F(" | T: ")); client.println(tExt); client.println(F("

Saidas

")); client.print(F("")); client.print(F("
AutoOffLowHigh
Interior")); client.print((digitalRead(AUTO_INTERIOR)>=1)?F("1"):F("0")); client.print(F("")); client.print((digitalRead(OFF_INTERIOR)>=1)?F("1"):F("0")); client.print(F("")); client.print((digitalRead(LOW_INTERIOR)>=1)?F("1"):F("0")); client.print(F("")); client.print((digitalRead(HIGH_INTERIOR)>=1)?F("1"):F("0")); client.print(F("
Exterior")); client.print((digitalRead(AUTO_EXTERIOR)>=1)?F("1"):F("0")); client.print(F("")); client.print((digitalRead(OFF_EXTERIOR)>=1)?F("1"):F("0")); client.print(F("")); client.print((digitalRead(LOW_EXTERIOR)>=1)?F("1"):F("0")); client.print(F("")); client.print((digitalRead(HIGH_EXTERIOR)>=1)?F("1"):F("0")); client.print(F("
")); client.println(F("

Interior

")); client.print(F("Auto
")); client.print(F("Off
")); client.print(F("Low
")); client.print(F("High
")); client.println(F("

Exterior

")); client.print(F("Auto
")); client.print(F("Off
")); client.print(F("Low
")); client.print(F("High
")); client.println(F("")); } else { client.println(F("HTTP/1.1 401 Authorization Required")); client.println(F("WWW-Authenticate: Basic realm=\"Secure Area\"")); client.println(F("Content-Type: text/html")); client.println(F("Connnection: close")); client.println(); client.println(F("Error")); client.println(F("

401 Unauthorized.

")); } break; } // End IF if (c == '\n') { // you're starting a new line currentLineIsBlank = true; // denariu, @123qwe@ if (strstr(linebuf,"Authorization: Basic")>0){ if(strstr(linebuf,"ZGVuYXJpdTpAMTIzcXdlQA==")>0) authentificated=true; else authentificated=false; } if (strstr(linebuf,"favicon.ico")>0) { client.println(F("HTTP/1.1 404 Not Found")); client.println(F("Content-Type: text/html")); client.println(F("Connnection: close")); client.println(); } else { if (strstr(linebuf,"GET /?eai")>0) {enableAutoInterior();} else if (strstr(linebuf,"GET /?eae")>0) {enableAutoExterior();} else if (strstr(linebuf,"GET /?eoi")>0) {enableOffInterior();} else if (strstr(linebuf,"GET /?eoe")>0) {enableOffExterior();} else if (strstr(linebuf,"GET /?eli")>0) {enableLowInterior();} else if (strstr(linebuf,"GET /?ehi")>0) {enableHighInterior();} else if (strstr(linebuf,"GET /?ele")>0) {enableLowExterior();} else if (strstr(linebuf,"GET /?ehe")>0) {enableHighExterior();} } memset(linebuf,0,sizeof(linebuf)); charcount=0; } else if (c != '\r') {currentLineIsBlank = false;} } } // End While delay(1); // poor CPU let it run for 1 ms client.stop(); // End of session } delay(1); // generously free 1 ms } void enableAutoInterior(){ offModeInside = false; autoModeInside = true; forceLowInside = false; forceHighInside = false; readActivateSensores(); } void enableAutoExterior(){ offModeOutside = false; autoModeOutside = true; forceHighOutside = false; forceLowOutside = false; readActivateSensores(); } void enableOffInterior(){ offModeInside = true; autoModeInside = false; forceLowInside = false; forceHighInside = false; readActivateSensores(); } void enableOffExterior(){ offModeOutside = true; autoModeOutside = false; forceLowOutside = false; forceHighOutside = false; readActivateSensores(); } void enableLowInterior(){ autoModeInside = false; offModeInside = false; forceLowInside = true; forceHighInside = false; readActivateSensores(); } void enableHighInterior(){ autoModeInside = false; offModeInside = false; forceHighInside = true; forceLowInside = false; readActivateSensores(); } void enableLowExterior(){ autoModeOutside = false; offModeOutside = false; forceLowOutside = true; forceHighOutside = false; readActivateSensores(); } void enableHighExterior(){ autoModeOutside = false; offModeOutside = false; forceHighOutside = true; forceLowOutside = false; readActivateSensores(); } void readActivateSensores(){ h = dht.readHumidity(); t = dht.readTemperature(); hExt = 0; tExt = 0; if(th.Read() == 0){ hExt = th.h; tExt = th.t; } boolean lowInsideOn = (h >= THRESH_INSIDE_H_LOW && h < THRESH_INSIDE_H_HIGH) || (t >= THRESH_INSIDE_T_LOW && t < THRESH_INSIDE_T_HIGH); boolean highInsideOn = (h >= THRESH_INSIDE_H_HIGH) || (t >= THRESH_INSIDE_T_HIGH); boolean lowOutsideOn = (hExt >= THRESH_OUTSIDE_H_LOW && hExt < THRESH_OUTSIDE_H_HIGH) || (tExt >= THRESH_OUTSIDE_T_LOW && tExt < THRESH_OUTSIDE_T_HIGH); boolean highOutsideOn = (hExt >= THRESH_OUTSIDE_H_HIGH) || (tExt >= THRESH_OUTSIDE_T_HIGH); if(highInsideOn && lowInsideOn) lowInsideOn = false; if(highOutsideOn && lowOutsideOn) lowOutsideOn = false; digitalWrite(AUTO_INTERIOR, autoModeInside?HIGH:LOW); digitalWrite(AUTO_EXTERIOR, autoModeOutside?HIGH:LOW); digitalWrite(OFF_INTERIOR, offModeInside?HIGH:LOW); digitalWrite(OFF_EXTERIOR, offModeOutside?HIGH:LOW); if(autoModeInside) { digitalWrite(LOW_INTERIOR, (lowInsideOn)?HIGH:LOW); digitalWrite(HIGH_INTERIOR, (highInsideOn)?HIGH:LOW); } if(autoModeOutside){ digitalWrite(LOW_EXTERIOR, (lowOutsideOn)?HIGH:LOW); digitalWrite(HIGH_EXTERIOR, (highOutsideOn)?HIGH:LOW); } if(offModeInside) { digitalWrite(LOW_INTERIOR, LOW); digitalWrite(HIGH_INTERIOR, LOW); } else { if(forceLowInside){ digitalWrite(LOW_INTERIOR, HIGH); digitalWrite(HIGH_INTERIOR, LOW);} if(forceHighInside){ digitalWrite(HIGH_INTERIOR, HIGH); digitalWrite(LOW_INTERIOR, LOW);} } if(offModeOutside) { digitalWrite(LOW_EXTERIOR, LOW); digitalWrite(HIGH_EXTERIOR, LOW); } else { if(forceLowOutside){ digitalWrite(LOW_EXTERIOR, HIGH); digitalWrite(HIGH_EXTERIOR, LOW);} if(forceHighOutside) { digitalWrite(HIGH_EXTERIOR, HIGH); digitalWrite(LOW_EXTERIOR, LOW);} } }