-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidoforHeRunSunPowerAtLewei50.ino
367 lines (303 loc) · 12.1 KB
/
MidoforHeRunSunPowerAtLewei50.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/***************************************************
* This is an example for the DFRobot Wido - Wifi Integrated IoT lite sensor and control node
* Product Page & More info: http://www.dfrobot.com.cn/goods-997.html
* Designed specifically to work with the DFRobot Wido products:
*
* The library is forked from Adafruit
*
* Written by Lauren
* BSD license, all text above must be included in any redistribution
*
* Modfiy by bill deng
****************************************************/
/*
This example code is used to connect the Lewei50 cloud service (Official homepage: www.lewei50.com).
The device required is just:
1.
2. And Wido
Note: Please don't forget to change the setting below before using!
1. WLAN_SSID & WlAN_PASS
2. userkey
3. GATE ID
*/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <avr/wdt.h>
//#include <SHT1x.h>
//#include "utility/debug.h"
#define Wido_IRQ 7
#define Wido_VBAT 5
#define Wido_CS 10
#define LED_PIN 13
Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define WLAN_SSID "herunsun" // cannot be longer than 32 characters!
#define WLAN_PASS "qazwsxedc" // For connecting router or AP, don't forget to set the SSID and password here!!
#define TCP_TIMEOUT 30000
//#define CC3000_TINY_DRIVER
#define WEBSITE "www.lewei50.com"
#define userkey "dab6a862154c4ebfab05b845eb4b5652" // Update Your API Key. To get your API Key, please check the link below
//https://www.lewei50.com/user/clientindex
#define analogInPin A0 // Analog input pin that the potentiometer is attached to
static unsigned long widoruntimeStamp = 0;
void softReset() {
asm volatile ("jmp 0");
}
void setup() {
// wdt_enable(WDTO_8S); //看门狗使能 并设置超时时间 8s
wdt_disable(); //看门狗禁止
// wdt_reset(); //看门狗复位
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH); // sets the LED off
Serial.begin(9600); //USB ON Serial
Serial1.setTimeout(3000); //set Serial1 Timeout value=3000ms
Serial1.begin(9600); //concet to UPS
delay(5000);
widoruntimeStamp = millis() ;
Serial.print(F("Initialise the CC3000..........."));
if (!Wido.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
digitalWrite(LED_PIN, HIGH); // sets the LED on
delay(1500);
digitalWrite(LED_PIN, LOW); // sets the LED off
delay(200);
softReset();
}
Serial.println(F("OK"));
for (byte i = 0; i < 10; i++) {
digitalWrite(LED_PIN, HIGH); // sets the LED on
delay(200);
digitalWrite(LED_PIN, LOW); // sets the LED off
delay(500);
}
// Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
// Serial.print(F("\nAttempting to connect to "));
Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode!
By default connectToAP will retry indefinitely, however you can pass an
optional maximum number of retries (greater than zero) as the fourth parameter.
*/
if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY, 3)) {
Serial.println(F("Connect To AP Failed!"));
for (byte i = 0; i < 20; i++) {
digitalWrite(LED_PIN, HIGH); // sets the LED on
delay(100);
digitalWrite(LED_PIN, LOW); // sets the LED off
delay(300);
}
softReset();
}
Serial.println(F("Connected!"));
wdt_enable(WDTO_8S); //看门狗使能 并设置超时时间 8s
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!Wido.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
}
uint32_t ip = 0;
unsigned int BatteryVoltage = 0;
unsigned int OutACVoltage = 0;
//unsigned int InACVoltage = 0;
int BatteryAmp = 0;
unsigned int LOAD = 0;
//unsigned int UPState = 0;
boolean Updata = false ; //数据更新标志:=0无更新数据
unsigned int ADConter = 0;
//float Temperature = 0;
//float Humidity = 0;
void loop() {
static Adafruit_CC3000_Client WidoClient;
static unsigned long RetryMillis = 0;
static unsigned long uploadtStamp = 0;
static unsigned long sensortStamp = 0;
static unsigned long CnnectedStamp = 0;
unsigned int IncominDataLength = 0;
char IncominBuffer[15] = ""; //{"/x01/x10/x80/x00/x00/x08/xB2/x02/x1F/x00/x64/xF4/x0D/xA8"} ;
wdt_reset(); //看门狗复位
if ((millis() - widoruntimeStamp) > 3600000) {
Serial.print(F("Time To Reset......"));
Serial.println(millis());
delay(1000);
softReset();
}
if (millis() - sensortStamp > 3000) { //DATA I/O SCAN
sensortStamp = millis();
// Temperature = sht1x.readTemperatureC();
// Humidity = sht1x.readHumidity();
digitalWrite(LED_PIN, HIGH); // sets the LED on
Serial1.print("\x01\x10\x01\xEC"); //Poll UPS StateCommand
IncominDataLength = Serial1.readBytes(IncominBuffer, 14);
if ((IncominDataLength == 14 ) && (IncominBuffer[0] == 1)) {
BatteryVoltage = (unsigned char)(IncominBuffer[7]) ;
BatteryVoltage <<= 8;
BatteryVoltage += (unsigned char)(IncominBuffer[8]);
OutACVoltage = (unsigned char)(IncominBuffer[5]);
OutACVoltage <<= 8;
OutACVoltage += (unsigned char)(IncominBuffer[6]);
//InACVoltage = (unsigned char)(IncominBuffer[3]);
//InACVoltage <<= 8;
//InACVoltage += (unsigned char)(IncominBuffer[4]);
//UPState = (unsigned char)(IncominBuffer[2]) ;
// read the analog in value:
BatteryAmp += analogRead(analogInPin);
ADConter ++ ;
Updata = true ;
}
Serial1.print("\x59\x0D"); //Poll UPS StateCommand
IncominDataLength = Serial1.readBytes(IncominBuffer, 8);
if ((IncominDataLength == 8 ) && (IncominBuffer[7] == 0x0D)) {
LOAD = (unsigned char)(IncominBuffer[4]) * 6 ; //(LOAD% * 6KW )
}
digitalWrite(LED_PIN, LOW); // sets the LED off
}
wdt_reset(); //看门狗复位
if (!WidoClient.connected() && (millis() - RetryMillis > TCP_TIMEOUT)) {
// Update the time stamp
RetryMillis = millis();
Serial.println(F("Try to connect the cloud server"));
WidoClient.close();
// Get Lewei50 IP address
Serial.print(F("www.lewei50.com -> "));
while (ip == 0) {
if (!Wido.getHostByName(WEBSITE, &ip)) { // Get the server IP address based on the domain name
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
Wido.printIPdotsRev(ip);
Serial.println(F(""));
// Connect to the Lewei50 Server
WidoClient = Wido.connectTCP(ip, 80); // Try to connect cloud server
if (WidoClient.connected()) {
CnnectedStamp = millis();
}
if ((millis() - CnnectedStamp) > 180000 ) {
Serial.print(F("Time To Reset......"));
Serial.println(millis());
delay(1000);
softReset();
}
}
else { //检查连接丢失是否超过了3分钟
WidoClient.close(); // Close the service connection
}
wdt_reset(); //看门狗复位
if (WidoClient.connected() && (millis() - uploadtStamp > 30000) && Updata ) {
Updata = false;
uploadtStamp = millis();
// If the device is connected to the cloud server, upload the data every 30s.
wdt_reset(); //看门狗复位
// Prepare Http Package for Lewei50 & get length
int length = 0;
char lengthstr[4] = "";
char BVChar[6] = "", OVChar[4] = "", BIChar[7] = "", LAChar[5] = ""; //USChar[4] = "";
itoa(BatteryVoltage / 10, BVChar, 10); // push the data to the http data package
strcat(BVChar, ".");
itoa(BatteryVoltage % 10, BVChar + strlen(BVChar), 10);
length += strlen(BVChar);
itoa(OutACVoltage / 10, OVChar, 10); // push the data to the http data package
length += strlen(OVChar);
BatteryAmp = BatteryAmp / ADConter;
BatteryAmp = BatteryAmp - 519; // Zero Ponint calc
BatteryAmp = BatteryAmp * 24; // convet to Amp 20mv = 1A
// push the data to the http data package
itoa(BatteryAmp / 100, BIChar, 10);
strcat(BIChar, ".");
itoa(abs(BatteryAmp / 10) % 10, BIChar + strlen(BIChar), 10);
length += strlen(BIChar);
BatteryAmp = 0 ;
ADConter = 0 ;
itoa(LOAD / 100, LAChar, 10); // push the data to the http data package
strcat(LAChar, ".");
itoa(LOAD % 100, LAChar + strlen(LAChar), 10);
length += strlen(LAChar);
//itoa(UPState, USChar, 10); // push the data to the http data package
//length += strlen(USChar);
Serial.println(F("Connected to Lewei50 server."));
// Send headers
Serial.print(F("Sending headers"));
WidoClient.fastrprint(F("POST /api/V1/gateway/UpdateSensors/01"));
//Please change your Gateway ID here, after creating
//Please check the link:https://www.lewei50.com/user/clientindex
WidoClient.fastrprintln(F(" HTTP/1.1"));
Serial.print(F("."));
WidoClient.fastrprintln(F("Host: www.lewei50.com"));
Serial.print(F("."));
WidoClient.fastrprint(F("userkey: "));
WidoClient.fastrprintln(userkey);
Serial.print(F("."));
length += 101; // get the length of data package
itoa(length, lengthstr, 10); // convert int to char array for posting
Serial.print(F("Length = "));
Serial.println(length);
WidoClient.fastrprint("Content-Length: ");
WidoClient.fastrprintln(lengthstr);
WidoClient.fastrprintln("");
Serial.print(F("."));
Serial.println(F(" done."));
// Send data
/***************************************************************************************************
* make a DataPakg:
* [
* {"Name":"BV","Value":"55.6"},
* {"Name":"OV","Value":"220"},
* {"Name":"IV","Value":"220"},
* {"Name":"LA","Value":"1.58"},
* {"Name":"SV","Value":"160"}
* ]
***************************************************************************************************/
WidoClient.fastrprint(F("[{\"Name\":\"BV\",\"Value\":\""));
WidoClient.fastrprint(BVChar); //23
WidoClient.fastrprint(F("\"},{\"Name\":\"OV\",\"Value\":\""));
WidoClient.fastrprint(OVChar); //25
WidoClient.fastrprint(F("\"},{\"Name\":\"A1\",\"Value\":\""));
WidoClient.fastrprint(BIChar); //25
WidoClient.fastrprint(F("\"},{\"Name\":\"LA\",\"Value\":\""));
WidoClient.fastrprint(LAChar);
WidoClient.fastrprintln(F("\"}]")); //28
//WidoClient.fastrprint(F("\"},{\"Name\":\"SV\",\"Value\":\""));
//WidoClient.fastrprint(USChar);
//WidoClient.fastrprintln(F("\"}]")); //28
// strcat(httpPackage,"{\"Name\":\"T1\",\"Value\":\"");
// dtostrf(Temperature,5,2,httpPackage+strlen(httpPackage));
// strcat(httpPackage,"\"},");
// strcat(httpPackage,"{\"Name\":\"H1\",\"Value\":\"");
// dtostrf(Humidity,5,2,httpPackage+strlen(httpPackage)); // push the data to the http data package
// strcat(httpPackage,"\"}]");
Serial.println(F("Sending data"));
Serial.println(F(" done."));
/********** Get the http page feedback ***********/
unsigned long rTimer = millis();
Serial.println(F("Reading Cloud Response!!!\r\n"));
while (millis() - rTimer < 2000) {
while (WidoClient.connected() && WidoClient.available()) {
char c = WidoClient.read();
Serial.print(c);
}
}
Serial.println(F(" done."));
wdt_reset(); //看门狗复位
delay(1000); // Wait for 1s to finish posting the data stream
WidoClient.close(); // Close the service connection
digitalWrite(LED_PIN, HIGH); // sets the LED on
delay(200);
digitalWrite(LED_PIN, LOW); // sets the LED off
delay(100);
digitalWrite(LED_PIN, HIGH); // sets the LED on
delay(200);
digitalWrite(LED_PIN, LOW); // sets the LED off
delay(100);
digitalWrite(LED_PIN, HIGH); // sets the LED on
delay(200);
digitalWrite(LED_PIN, LOW); // sets the LED off
RetryMillis = millis(); // Reset the timer stamp for applying the connection with the service
}
}