/
cs1-iot.ino
112 lines (97 loc) · 2.72 KB
/
cs1-iot.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
ESP8266WebServer server;
char* ssid = "YOUR_SSID";
char* password = "YOUR_SSID_PASSWORD";
const char INDEX_HTML[] =
"<!DOCTYPE HTML>"
"<html>"
"<head>"
"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
"<title>ESP8266</title>"
"<style>"
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: Maroon; }"
"</style>"
"</head>"
"<body>"
"<image src='https://raw.githubusercontent.com/EricEisaman/EricEisaman.github.io/master/images/eddies.png'/>"
"<h1>ESP8266 Demo</h1>"
"<button onclick='toggle()'>Toggle LED</button>"
"<script>"
"function toggle(){"
"fetch('/toggle').then(data=>console.log(data))"
"}"
"</script>"
"</body>"
"</html>";
int POLLING_PERIOD=200;
int intervalStartMS=millis();
void setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
Serial.begin(9600);
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("IP Address: ");
Serial.print(WiFi.localIP());
Serial.println("");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN,LOW);//Turn on LED
server.on("/",sendIndex);
server.on("/toggle", toggleLED);
server.begin();
}
void loop()
{
server.handleClient();
if( millis() > intervalStartMS + POLLING_PERIOD ) {
pollServer();
intervalStartMS = millis();
}
}
void sendIndex(){
server.send(200,"text/html",INDEX_HTML);
}
void toggleLED(){
digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
//server.send(204,"");
server.send(200,"text/plain","Toggle!\n");
}
void pollServer(){
HTTPClient http;
http.begin("http://your-project.glitch.me/iot-get/?key=aW49mH15Bo&prop=LED");
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
Serial.println("");
setLED(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
String on = String("on");
void setLED(String state){
if(state == on){
Serial.println("TURNING THE LED ON.");
Serial.println("");
digitalWrite(LED_BUILTIN,LOW);
}else{
Serial.println("TURNING THE LED OFF.");
Serial.println("");
digitalWrite(LED_BUILTIN,HIGH);
}
}