這是一個用ESP8266開發的網路控制器
即使人不在家也可以靠手機去開關家裡的冷氣
這個算是我這幾次研究的集大成
在這個專案中有遇到一些問題
像是網站有時候會掛掉
我們後來在!client.available()之中加了一個計數器
若計數器滿了即會重置
有點類似watchdog
#ifndef UNIT_TEST
#include
#endif
#include
#include
#include
const char* ssid = "SSID";
const char* password = "PW";
const char* host = ""; //it will tell you the IP once it starts up
//just write it here afterwards and upload
#define IR_LED 4
IRsend irsend(IR_LED);
uint16_t irSignal[439] = {3500, 1750, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 10320, 3500, 1750, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 1300, 435};
uint16_t irSignalcl[439] = {3500, 1750, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 10320, 3500, 1750, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 1300, 435, 1300, 435, 435, 435, 435, 435, 1300, 435};
WiFiServer server(301); //just pick any port number you like
uint16_t temp = 26;
uint16_t wind = 10;
String onoff;
int ccon = 0;
void setup() {
Serial.begin(115200);
irsend.begin();
delay(10);
Serial.println(WiFi.localIP());
// prepare GPIO2
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("."); }
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while (!client.available()) {
delay(1);
ccon = ccon + 1;
if (ccon > 100) {
break;
}
}
ccon = 0;
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();
// Match the request
if (req.indexOf("") != -10) { //checks if you're on the main page
if (req.indexOf("/16") != -1) {
temp = 16 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/17") != -1) {
temp = 17 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/18") != -1) {
temp = 18 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/19") != -1) {
temp = 19 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/20") != -1) {
temp = 20 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/21") != -1) {
temp = 21 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/22") != -1) {
temp = 22;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/23") != -1) {
temp = 23 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/24") != -1) {
temp = 24 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/25") != -1) {
temp = 25 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/26") != -1) {
temp = 26 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/27") != -1) {
temp = 27 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/28") != -1) {
temp = 28 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/29") != -1) {
temp = 29 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/30") != -1) {
temp = 30 ;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/w1") != -1) {
wind = 1;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/w2") != -1) {
wind = 2;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/w3") != -1) {
wind = 3;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/w4") != -1) {
wind = 4;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/w5") != -1) {
wind = 5;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/wa") != -1) {
wind = 8;
irchange(temp, wind);
iremit();
}
if (req.indexOf("/off") != -1) {
irsend.sendRaw(irSignalcl, sizeof(irSignalcl) / sizeof(irSignalcl[0]), 38);
onoff = "off";
}
else {
onoff = "on";
}
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Prepare the response
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "\r\n\r\n";
s += ("Temperature : ");
s += temp;
s += "
";
s += ("Wind : ");
s += wind;
s += "
";
s += ("Status:");
s += onoff;
s += "
";
s += "";
s += "";
s += "";
s += "";
s += "
";
s += "";
s += "";
s += "";
s += "";
s += "
";
s += "";
s += "";
s += "";
s += "";
s += "
";
s += "";
s += "";
s += "";
s += "";
s += "
";
s += "
";
s += "\n";
client.flush();
// Send the response to the client
client.print(s);
delay(1);
}
void irchange(int temp, int wind) {
int tempa [4] = {0, 1, 0, 1};
int winda [4] = {0, 1, 0, 1};
int scorea [7] = {0, 0, 0, 0, 0, 0, 0};
int score = temp + 8 * wind - 12;
int tempb = temp - 16;
int windc = wind + 2;
for (int i = 6; i >= 0; i--) {
if (score >= pow(2, i )) {
scorea[i] = 1;
score = score - pow(2, i );
}
else {
scorea[i] = 0;
}
}
for (int i = 3; i >= 0; i--) {
if (tempb >= pow(2, i )) {
tempa[i] = 1;
tempb = tempb - pow(2, i );
}
else {
tempa[i] = 0;
}
}
for (int i = 3; i >= 0; i--) {
if (windc >= pow(2, i )) {
winda[i] = 1;
windc = windc - pow(2, i );
}
else {
winda[i] = 0;
}
}
Serial.println();
Serial.print("score: ");
for (int i = 0; i < 7; i++) {
Serial.print(scorea[i]);
Serial.print(",");
}
Serial.println();
Serial.print("temp: ");
for (int i = 0; i <= 3; i++) {
Serial.print("i=");
Serial.print(i);
Serial.print(" ");
Serial.print(tempa[i]);
Serial.print(",");
}
Serial.println();
Serial.print("wind: ");
for (int i = 0; i <= 3; i++) {
Serial.print(winda[i]);
Serial.print(",");
}
Serial.println();
if (tempa[0] == 0) {
irSignal[233] = 435;
} else {
irSignal[233] = 1300;
}
if (tempa[1] == 0) {
irSignal[235] = 435;
} else {
irSignal[235] = 1300;
}
if (tempa[2] == 0) {
irSignal[237] = 435;
} else {
irSignal[237] = 1300;
}
if (tempa[3] == 0) {
irSignal[239] = 435;
} else {
irSignal[239] = 1300;
}
if (winda[0] == 0) {
irSignal[271] = 435;
} else {
irSignal[271] = 1300;
}
if (winda[1] == 0) {
irSignal[273] = 435;
} else {
irSignal[273] = 1300;
}
if (winda[2] == 0) {
irSignal[275] = 435;
} else {
irSignal[275] = 1300;
}
if (winda[3] == 0) {
irSignal[277] = 435;
} else {
irSignal[277] = 1300;
}
if (scorea[0] == 0) {
irSignal[425] = 435;
} else {
irSignal[425] = 1300;
}
if (scorea[1] == 0) {
irSignal[427] = 435;
} else {
irSignal[427] = 1300;
}
if (scorea[2] == 0) {
irSignal[429] = 435;
} else {
irSignal[429] = 1300;
}
if (scorea[3] == 0) {
irSignal[431] = 435;
} else {
irSignal[431] = 1300;
}
if (scorea[4] == 0) {
irSignal[433] = 435;
} else {
irSignal[433] = 1300;
}
if (scorea[5] == 0) {
irSignal[435] = 435;
} else {
irSignal[435] = 1300;
}
if (scorea[6] == 0) {
irSignal[437] = 435;
} else {
irSignal[437] = 1300;
}
for (int i = 0; i < 439; i++) {
Serial.print(irSignal[i]);
Serial.print(",");
}
}
void iremit() {
irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), 38);
}





