-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasmotaInvita.ino
559 lines (413 loc) · 9.49 KB
/
TasmotaInvita.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <LittleFS.h>
#include <list>
#define csprintf(...) { Serial.printf(__VA_ARGS__ ); }
//create your own WiFi-setup.h with your user name and password (see WiFi-setup.h.sample)
// or use the #define statements below.
#include "WiFi-setup.h"
#define UPLOAD_FILE
//#define WIFI_SSID "linksys-e2000"
//#define WIFI_PASS "12345678"
//The LED very gently breathes in the background without annoying your peripheral vision, but lights up brightly when a new device is found and invited.
#define LED_PIN 14
//#define INVERT_LED
#define PWM_RANGE 16383
#define DIM_BRIGHTNESS ((int) (PWM_RANGE * 0.01))
WiFiClient httpWifiClient;
HTTPClient http;
int HttpRequestInternal(const char * url, String * post)
{
http.setTimeout(5000);
http.begin(httpWifiClient,url); //Specify request destination
int httpCode = 0;
if(post)
{
httpCode=http.POST(*post);
}
else
{
httpCode=http.GET(); //Send the request
}
if (httpCode > 0)
{ //Check the returning code
// String payload = http.getString(); //Get the request response payload
// Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
return httpCode;
}
int HttpRequest(const char * url, int retries)
{
while(retries)
{
int ret=HttpRequestInternal(url,NULL);
if(ret>=0) return ret;
retries--;
delay(20);
}
return -999;
}
int HttpPost(String & url, String & payload, int retries)
{
while(retries)
{
int ret=HttpRequestInternal(url.c_str(),&payload);
if(ret>=0) return ret;
retries--;
delay(20);
}
return -999;
}
void SetLed(bool bLight)
{
#ifdef INVERT_LED
analogWrite(LED_PIN, bLight ? 0 : PWM_RANGE);
#else
analogWrite(LED_PIN, bLight ? PWM_RANGE : 0);
#endif
}
void DimLed(int level)
{
#ifdef INVERT_LED
analogWrite(LED_PIN, PWM_RANGE - level);
#else
analogWrite(LED_PIN, level);
#endif
}
class IgnoreItem
{
public:
String strSSID;
uint32_t ulTimestamp;
};
std::list<IgnoreItem> listIgnore;
//The setup function is called once at startup of the sketch
void setup()
{
// Add your initialization code here
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
analogWriteFreq(120);
analogWriteRange(PWM_RANGE);
pinMode(LED_PIN, OUTPUT);
SetLed(0);
#ifdef UPLOAD_FILE
csprintf("\n### Tasmota-Converta by Leif Claesson ###\n");
#else
csprintf("\n### Tasmota-Invita by Leif Claesson ###\n");
#endif
}
bool bFirst = true;
int iStreak = 0;
bool DoHttpUpload()
{
//csprintf("HttpRequest test!\n");
const char * szFilename="Lightbulb.bin";
if(!LittleFS.begin())
{
csprintf("couldn't open littlefs\n");
return false;
}
File myfile=LittleFS.open(szFilename, "r");
if(!myfile)
{
csprintf("Unable to open %s\n",szFilename);
return false;
}
csprintf("File %s opened! size %u\n",szFilename,myfile.size());
// String url="http://172.22.22.40:7381/dev_upload";
char use_url[256];
sprintf(use_url, "http://%s/u2", WiFi.gatewayIP().toString().c_str());
http.setTimeout(5000);
http.begin(httpWifiClient,use_url); //Specify request destination
csprintf("\nUploading %s to %s ...\n",szFilename,use_url);
int httpCode = http.sendRequest("POST",&myfile,myfile.size(),szFilename,"application/octet-stream","data");
// httpCode will be negative on error
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
http.end();
return true;
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
return false;
}
void loop()
{
SetLed(0);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#ifdef UPLOAD_FILE
Serial.printf("\nScanning for AP-mode tasmota devices to convert.\n\n");
#else
Serial.printf("\nScanning for AP-mode tasmota devices to invite to network %s.\n\n",WIFI_SSID);
#endif
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
unsigned char network_flag[256];
memset(network_flag, 0, sizeof(network_flag));
if(n > 255)
{
n = 255;
}
reuse_list:
int tasmotas = 0;
// Serial.println("scan done");
if(n == 0)
{
Serial.println("no networks found");
iStreak++;
}
else
{
Serial.print(n);
Serial.println(" networks found");
/*
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
}
*/
Serial.print(" Ch BSSID RSSI Encr SSID\n");
tasmotas = 0;
int best_idx = -1;
int best_rssi = -1000;
for(int i = 0; i < n; i++)
{
if(network_flag[i])
{
continue;
}
std::list<IgnoreItem>::iterator iter;
for(iter=listIgnore.begin();iter!=listIgnore.end();)
{
IgnoreItem & item=*iter;
int age=(millis()-item.ulTimestamp);
if(age>30000)
{
//too old
//csprintf("%s too old to ignore (%i ms), erasing\n",item.strSSID.c_str(),age);
iter=listIgnore.erase(iter);
}
else
{
if(item.strSSID==WiFi.SSID(i))
{
//csprintf("Ignoring %s age %i!\n",item.strSSID.c_str(),age);
network_flag[i]=true;
}
iter++;
}
}
if(network_flag[i])
{
continue;
}
int ch = WiFi.channel(i);
Serial.print(ch < 10 ? " " : " ");
Serial.print(ch);
Serial.print(" ");
Serial.print(WiFi.BSSIDstr(i));
Serial.print(" ");
Serial.print(WiFi.RSSI(i));
Serial.print(" ");
switch(WiFi.encryptionType(i))
{
case ENC_TYPE_WEP:
Serial.print("WEP ");
break;
case ENC_TYPE_TKIP:
Serial.print("TKIP");
break;
case ENC_TYPE_CCMP:
Serial.print("WPA ");
break;
default:
Serial.print(WiFi.encryptionType(i));
Serial.print(" ");
break;
case ENC_TYPE_NONE:
Serial.print("None");
break;
case ENC_TYPE_AUTO:
Serial.print("Auto");
break;
}
// Serial.print((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
Serial.print(" ");
Serial.print(WiFi.SSID(i));
Serial.println("");
/*int spaces=32-WiFi.SSID(i).length();
for(int x=0;x<spaces;x++)
{
Serial.printf(" ");
}
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
*/
delay(0);
if(!strncasecmp(WiFi.SSID(i).c_str(), "tasmota", 7))
{
if(best_rssi < WiFi.RSSI(i))
{
best_rssi = WiFi.RSSI(i);
best_idx = i;
}
tasmotas++;
}
else
{
network_flag[i] = true;
}
}
if(best_idx >= 0)
{
network_flag[best_idx] = true;
int counter = 30;
SetLed(1);
Serial.printf("%i found.\n", tasmotas);
Serial.printf("Connecting to %s (%i)", WiFi.SSID(best_idx).c_str(), WiFi.RSSI(best_idx));
WiFi.begin(WiFi.SSID(best_idx), "");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
counter--;
if(!counter)
{
Serial.printf("timed out.\n");
//delay(5000);
return;
}
}
csprintf("Connected!\n");
for(int x = 0; x < 4; x++)
{
SetLed(0);
delay(80);
SetLed(1);
delay(80);
}
//delay(1000);
csprintf("Our IP is %s, Gateway is %s\n", WiFi.localIP().toString().c_str(), WiFi.gatewayIP().toString().c_str());
#ifdef UPLOAD_FILE
char use_url[256];
sprintf(use_url, "http://%s/cm?cmnd=SetOption78%%201", WiFi.gatewayIP().toString().c_str());
int result = HttpRequest(use_url, 3);
//int result=200;
if(result == 200)
{
csprintf("SetOption78 success!\n");
DimLed(PWM_RANGE>>3);
bool bSuccess=DoHttpUpload();
IgnoreItem ignore;
ignore.strSSID=WiFi.SSID();
ignore.ulTimestamp=millis();
listIgnore.push_back(ignore);
if(bSuccess)
{
for(int i=0;i<3;i++)
{
SetLed(1);
delay(500);
SetLed(0);
delay(500);
}
}
else
{
SetLed(0);
delay(200);
SetLed(1);
delay(2000);
SetLed(0);
}
}
#else
char use_url[256];
sprintf(use_url, "http://%s/wi?s1=%s&p1=%s&save=", WiFi.gatewayIP().toString().c_str(), WIFI_SSID, WIFI_PASS);
//sprintf(use_url,"http://%s",WiFi.gatewayIP().toString().c_str());
csprintf("Requesting set SSID %s...", WIFI_SSID);
int result = HttpRequest(use_url, 3);
//int result=200;
if(result == 200)
{
csprintf("success!\n");
WiFi.disconnect();
for(int b = 0; b < 2; b++)
{
for(float a = 0.0f; a < 1.0f; a += 0.01f)
{
DimLed(pow(a, 3.0f)*PWM_RANGE);
delay(5);
}
for(float a = 1.0f; a > 0.0f; a -= 0.01f)
{
DimLed(pow(a, 3.0f)*PWM_RANGE);
delay(5);
}
}
}
else
{
SetLed(0);
csprintf("error.\n");
}
#endif
SetLed(0);
iStreak++;
}
else
{
Serial.printf("No tasmotas found.\n");
iStreak = 0;
}
}
WiFi.disconnect();
if(iStreak >= 1 && tasmotas > 1)
{
Serial.printf("Reusing list\n");
delay(500);
goto reuse_list;
}
if(bFirst)
{
bFirst=false;
}
else
{
// Wait a bit before scanning again
delay(3000);
}
if(!iStreak)
{
SetLed(0);
for(float a = 0.0f; a < 1.0f; a += 0.002f)
{
DimLed(pow(a, 1.5f)*DIM_BRIGHTNESS);
delay(5);
}
for(float a = 1.0f; a > 0.0f; a -= 0.002f)
{
DimLed(pow(a, 1.5f)*DIM_BRIGHTNESS);
delay(5);
}
SetLed(0);
}
}