This repository was archived by the owner on Apr 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathespThing.ino
132 lines (108 loc) · 3.48 KB
/
espThing.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
char* ssid = "ssid";
char* wifiPwd = "passCode";
String jwt = "Insert JWT string here";
char* thingUser = "myUser";
char* thingPwd = "myPwd";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
WiFi.begin(ssid, wifiPwd); //Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED) { //Wait for connection
delay(500);
Serial.println("Waiting to connect…");
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //Print the local IP
//Define the handling function for the config response
server.on("/things/esp", []() {
if(!server.authenticate(thingUser, thingPwd))
return server.requestAuthentication();
Serial.println("Getting thing");
String configThing = "[\
{\
\"name\": \"ESP8266 Thing 1\",\
\"type\": \"thing\",\
\"description\": \"myESP8266\",\
\"properties\": {\
\"clock\": {\
\"type\": \"number\",\
\"unit\": \"Ticks\",\
\"description\": \"The short millisec clock count\",\
\"href\":\"/properties/clock\"\
},\
\"led\": {\
\"type\": \"boolean\",\
\"description\": \"The onboard LED\",\
\"href\":\"/properties/led\"\
}\
}\
},\
{\
\"name\": \"ESP8266 Thing 2\",\
\"type\": \"thing\",\
\"description\": \"myESP8266\",\
\"properties\": {\
\"anotherclock\": {\
\"type\": \"number\",\
\"unit\": \"Ticks\",\
\"description\": \"The long millisec clock count\",\
\"href\":\"/properties/anotherclock\"\
}\
}\
}\
]";
server.send(200, "text/json", configThing);
});
// respond to led property
server.on("/things/esp/properties/led", []() {
Serial.println("led thing property");
if(!server.authenticate(thingUser, thingPwd))
return server.requestAuthentication();
char respondThing[1024];
int nIndex = -1;
for (int i = 0; i < server.args(); i++)
if( server.argName(i) =="led" )
nIndex = i;
if( nIndex > -1 ) {
if( server.arg("led")=="true" )
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
}
if( digitalRead(LED_BUILTIN)==HIGH )
sprintf(respondThing, "{\"led\":true}");
else
sprintf(respondThing, "{\"led\":false}");
server.send(200, "text/json", respondThing);
});
// respond to clock property
server.on("/things/esp/properties/clock", []() {
Serial.println("led thing property");
if(!server.authenticate(thingUser, thingPwd))
return server.requestAuthentication();
char respondThing[1024];
// clock can only be read, not set
// truncate to 8 bits as an example
sprintf(respondThing, "{\"clock\":\"%d\"}", millis()&0xff);
server.send(200, "text/json", respondThing);
});
// respond to clock property
server.on("/things/esp/properties/anotherclock", []() {
Serial.println("led thing property");
if(!server.authenticate(thingUser, thingPwd))
return server.requestAuthentication();
char respondThing[1024];
// clock can only be read, not set
sprintf(respondThing, "{\"anotherclock\":\"%d\"}", millis());
server.send(200, "text/json", respondThing);
});
server.begin(); //Start the server
Serial.println("Server listening");
}
void loop() {
server.handleClient(); //Handling of incoming requests
}