-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLinkNodeR8.ino
304 lines (288 loc) · 7.43 KB
/
LinkNodeR8.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "FS.h"
#include <ESP8266WiFi.h>
#include <WString.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
String data;
String deviceID;
String FactoryKey;
String apikey;
bool config_flag = true;
WiFiClient client;
const char* server = "www.linksprite.io";
void config_wifi();
void query();
void updatetime(String a);
int RegisterDevice();
void config_gpio();
void setup()
{
Serial.begin(115200);
config_gpio();
/*Read the factory_key and deviceID first*/
bool result = SPIFFS.begin();
Serial.println("SPIFFS opened: " + result);
/*this opens the file "data.txt" in read-mode*/
File f = SPIFFS.open("/data.txt", "r");
if (!f) {
Serial.println("File doesn't exist yet. Creating it");
/* Configure the factory_key and deviceID via serial
* and save it to data.txt
* example data:
* 0080000003b124b62d-03a4-4087-bb30-fd118ad10ff
*/
while(config_flag){
Serial.println("Start to configure...");
if(Serial.available() > 0)
{
data = Serial.readStringUntil('\n');
}
/*check the '-' character, if get '-', then configuration is done!*/
if(data.lastIndexOf('-')!=-1){
config_flag = false;
Serial.println("Configuration is finished!");
}
delay(1000);
}
/* open the file in write mode*/
File f = SPIFFS.open("/data.txt", "w");
if (!f) {
Serial.println("file creation failed");
}
/* now write two lines in key/value style with end-of-line characters*/
f.println(data);
} else {
// we could open the file
while(f.available()) {
//Lets read line by line from the file
data = f.readStringUntil('\n');
}
}
/*parse the device ID and factory from data.txt file*/
deviceID = data.substring(0,10);
String va = data.substring(10);
FactoryKey = va.substring(0,36);
Serial.println("deviceID:");
Serial.println(deviceID);
Serial.println("FactoryKey:");
Serial.println(FactoryKey);
f.close();
config_wifi();
while(RegisterDevice()!=0);
}
void loop()
{
query();
delay(1000);
}
void config_gpio()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(16, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(15, OUTPUT);
pinMode(10, OUTPUT);
}
void config_wifi()
{
WiFiManager wifiManager;
wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
wifiManager.autoConnect("LinkNodeAP");
Serial.print("WiFi Connected ...\n");
Serial.println("WiFi connected");
}
void query()
{
if (client.connect(server,80))
{
String postStr ="{";
postStr +="\"action\":\"query\",";
postStr +="\"apikey\":\"";
postStr += apikey;
postStr +="\",";
postStr +="\"deviceid\":\"";
postStr += deviceID;
postStr +="\",";
postStr += "\"params\":";
postStr += "[";
postStr += "\"relays\"";
postStr +="]";
postStr +="}";
client.print("POST /api/http HTTP/1.1\n");
client.print("Host: ");
client.print(server);
client.print("\nContent-Type: application/json\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
delay(200);
Serial.println("Store response...");
String request = "";
while (client.available())
{
char c = client.read();
request +=c;
}
if (request!= NULL)
{
int index1 = request.indexOf(":{");
int index2 = request.indexOf("},");
int index3 = request.indexOf("GMT");
String tim = request.substring(index3-9,index3);
String param = request.substring(index1, index2 + 1);
Serial.print("The param is:");
Serial.println(param);
Serial.print("The time is:");
Serial.println(tim);
if(param[12]=='1')
digitalWrite(12,HIGH);
else
digitalWrite(12,LOW);
if(param[13]=='1')
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
if(param[14]=='1')
digitalWrite(14,HIGH);
else
digitalWrite(14,LOW);
if(param[15]=='1')
digitalWrite(16,HIGH);
else
digitalWrite(16,LOW);
if(param[16]=='1')
digitalWrite(4,HIGH);
else
digitalWrite(4,LOW);
if(param[17]=='1')
digitalWrite(5,HIGH);
else
digitalWrite(5,LOW);
if(param[18]=='1')
digitalWrite(15,HIGH);
else
digitalWrite(15,LOW);
if(param[19]=='1')
digitalWrite(10,HIGH);
else
digitalWrite(10,LOW);
Serial.println(param[12]);
Serial.println(param[13]);
Serial.println(param[14]);
Serial.println(param[15]);
Serial.println(param[16]);
Serial.println(param[17]);
Serial.println(param[18]);
Serial.println(param[19]);
client.stop();
Serial.println("Waiting...");
updatetime(tim);
delay(500);
}
}
void updatetime(String a)
{
if (client.connect(server,80))
{
String postStr1 ="{";
postStr1 +="\"action\":\"update\",";
postStr1 +="\"apikey\":\"";
postStr1 += apikey;
postStr1 +="\",";
postStr1 +="\"deviceid\":\"";
postStr1 += deviceID;
postStr1 +="\",";
postStr1 +="\"params\":";
postStr1 +="{";
postStr1 +="\"time\":\"";
postStr1 +=a;
postStr1 +="\"\r\n";
postStr1 +="}";
postStr1 +="}";
client.print("POST /api/http HTTP/1.1\n");
client.print("Host: ");
client.print(server);
client.print("\nContent-Type: application/json\n");
client.print("Content-Length: ");
client.print(postStr1.length());
client.print("\n\n");
client.print(postStr1);
}
delay(200);
Serial.println("Store response...");
String request = "";
while (client.available())
{
char c = client.read();
request +=c;
}
if (request!= NULL)
{
int index1 = request.indexOf(":{");
int index2 = request.indexOf("},");
String param = request.substring(index1, index2 + 1);
Serial.print("The update param is:");
Serial.println(param);
client.stop();
}
}
int RegisterDevice()
{
if (client.connect(server,80))
{
String postStr2 ="{";
postStr2 +="\"action\":\"register\",";
postStr2 +="\"deviceid\":\"";
postStr2 += deviceID;
postStr2 +="\",";
postStr2 +="\"apikey\":\"";
postStr2 += FactoryKey;
postStr2 +="\"}";
client.print("POST /api/http HTTP/1.1\n");
client.print("Host: ");
client.print(server);
client.print("\nContent-Type: application/json\n");
client.print("Content-Length: ");
client.print(postStr2.length());
client.print("\n\n");
client.print(postStr2);
Serial.print(postStr2);
}
delay(200);
Serial.println("Store response...");
String request = "";
while (client.available())
{
char c = client.read();
request +=c;
}
if (request!= NULL)
{
int index1 = request.indexOf(":{");
int index2 = request.indexOf("},");
String param = request.substring(index1, index2 + 1);
Serial.println(param);
int index3 = param.indexOf("apikey");
int index4 = param.indexOf("deviceid");
apikey = param.substring(index3+9, index3+45);
String id = param.substring(index4+11, index4 +21);
Serial.print("KEY:");
Serial.println(apikey);
Serial.print("ID:");
Serial.println(id);
client.stop();
int len=param.length();
if(len>=250)
{
Serial.println("RegisterDevice OK!\n");
return 0;
}
}
return -1;
}