You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
executaveis/lavandaria/1 - central exaustao/sketch_dec13a/sketch_dec13a.ino

356 lines
11 KiB
C++

/** Code developed by Antonio Vaz
* Triggers BasicAuthentication and validates default user and password
* Requires:
* 1. Ethernet Shield by Arduino
*/
#include <Ethernet.h>
#include <Wire.h>
#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<sizeof(linebuf)-1) charcount++;
if (c == '\n' && currentLineIsBlank) {
if (authentificated) {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Connnection: close"));
client.println();
client.println(F("<html>"));
client.println(F("<script type='text/javascript'> "));
client.println(F("<!-- "));
client.println(F("function logout(){ var str = '?'; var xmlhttp; "));
client.println(F(" if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); else xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); "));
client.println(F("xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) window.close(); } "));
client.println(F("xmlhttp.open('GET',str,true); xmlhttp.setRequestHeader('Authorization','Basic YXNkc2E6');xmlhttp.send(); return false; } "));
client.println(F("--> "));
client.println(F("</script>"));
client.println(F("<h1>Sensores</h1><a href='?'>Refresh</a><br/>"));
client.println(F("<input type='button' onclick='logout();' value='Logout' />"));
client.print(F("Interior H: "));
client.print(h);
client.print(F(" | T: "));
client.println(t);
client.print(F("<br/>Exterior H: "));
client.print(hExt);
client.print(F(" | T: "));
client.println(tExt);
client.println(F("<h1>Saidas</h1>"));
client.print(F("<table><tr><td></td><td>Auto</td><td>Off</td><td>Low</td><td>High</td></tr><tr><td>Interior</td><td>"));
client.print((digitalRead(AUTO_INTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td><td>"));
client.print((digitalRead(OFF_INTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td><td>"));
client.print((digitalRead(LOW_INTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td><td>"));
client.print((digitalRead(HIGH_INTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td></tr>"));
client.print(F("<tr><td>Exterior</td><td>"));
client.print((digitalRead(AUTO_EXTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td><td>"));
client.print((digitalRead(OFF_EXTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td><td>"));
client.print((digitalRead(LOW_EXTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td><td>"));
client.print((digitalRead(HIGH_EXTERIOR)>=1)?F("1"):F("0"));
client.print(F("</td></tr></table><form></form>"));
client.println(F("<h1>Interior</h1>"));
client.print(F("<a href='?eai'>Auto</a><br/>"));
client.print(F("<a href='?eoi'>Off</a><br/>"));
client.print(F("<a href='?eli'>Low</a><br/>"));
client.print(F("<a href='?ehi'>High</a><br/>"));
client.println(F("<h1>Exterior</h1>"));
client.print(F("<a href='?eae'>Auto</a><br/>"));
client.print(F("<a href='?eoe'>Off</a><br/>"));
client.print(F("<a href='?ele'>Low</a><br/>"));
client.print(F("<a href='?ehe'>High</a><br/>"));
client.println(F("</html>"));
} 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("<HTML><HEAD><TITLE>Error</TITLE>"));
client.println(F("</HEAD><BODY><H1>401 Unauthorized.</H1></BODY> </HTML>"));
}
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);}
}
}