forked from incmve/roomba-esp8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Roomba-eps8266.ino
716 lines (637 loc) · 21.5 KB
/
Roomba-eps8266.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// Includes
#include <Time.h>
#include <TimeLib.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <EEPROM.h>
#include <SoftwareSerial.h>
#include "Base64.h"
#include <FS.h>
String roombotVersion = "0.3.8";
String WMode = "1";
#define SERIAL_RX D5 // pin for SoftwareSerial RX
#define SERIAL_TX D6 // pin for SoftwareSerial TX
#define Wake_Pin D1
SoftwareSerial mySerial(SERIAL_RX, SERIAL_TX); // (RX, TX. inverted, buffer)
// Div
File UploadFile;
String fileName;
String BSlocal = "0";
int FSTotal;
int FSUsed;
//-------------- FSBrowser application -----------
//format bytes
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
// WIFI
String ssid = "your ssid";
String password = "password";
String espName = "Roombot";
// webserver
ESP8266WebServer server(80);
MDNSResponder mdns;
WiFiClient client;
// AP mode when WIFI not available
const char *APssid = "Roombot";
const char *APpassword = "thereisnospoon";
// Pimatic settings
String host = "192.168.x.x";
const int httpPort = 80;
String Username = "admin";
String Password = "password";
String chargevar = "chargestatus";
String distancevar = "distance";
char authVal[40];
char authValEncoded[40];
String ClientIP;
#define BASE64_LEN 40
char unameenc[BASE64_LEN];
// HTML
String header = "<html lang='en'><head><title>Roombot control panel</title><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1'><link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'><script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script><script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js'></script></head><body>";
String navbar = "<nav class='navbar navbar-default'><div class='container-fluid'><div class='navbar-header'><a class='navbar-brand' href='/'>Roombot control panel</a></div><div><ul class='nav navbar-nav'><li><a href='/'><span class='glyphicon glyphicon-info-sign'></span> Status</a></li><li class='dropdown'><a class='dropdown-toggle' data-toggle='dropdown' href='#'><span class='glyphicon glyphicon-cog'></span> Tools<span class='caret'></span></a><ul class='dropdown-menu'><li><a href='/updatefwm'><span class='glyphicon glyphicon-upload'></span> Firmware</a></li><li><a href='/filemanager.html'><span class='glyphicon glyphicon-file'></span> File manager</a></li><li><a href='/fupload'> File upload</a></li></ul></li><li><a href='https://github.com/incmve/roomba-eps8266/wiki' target='_blank'><span class='glyphicon glyphicon-question-sign'></span> Help</a></li></ul></div></div></nav>";
String containerStart = "<div class='container'><div class='row'>";
String containerEnd = "<div class='clearfix visible-lg'></div></div></div>";
String siteEnd = "</body></html>";
String panelHeaderName = "<div class='col-md-4'><div class='page-header'><h1>";
String panelHeaderEnd = "</h1></div>";
String panelEnd = "</div>";
String panelBodySymbol = "<div class='panel panel-default'><div class='panel-body'><span class='glyphicon glyphicon-";
String panelBodyName = "'></span> ";
String panelBodyValue = "<span class='pull-right'>";
String panelcenter = "<div class='row'><div class='span6' style='text-align:center'>";
String panelBodyEnd = "</span></div></div>";
String inputBodyStart = "<form action='' method='POST'><div class='panel panel-default'><div class='panel-body'>";
String inputBodyName = "<div class='form-group'><div class='input-group'><span class='input-group-addon' id='basic-addon1'>";
String inputBodyPOST = "</span><input type='text' name='";
String inputBodyClose = "' class='form-control' aria-describedby='basic-addon1'></div></div>";
String roombacontrol = "<a href='/roombastart'<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-play' aria-hidden='true'></span> Start</button></a><a href='/roombamax'<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-play' aria-hidden='true'></span> Max Clean</button></a><a href='/roombastop'<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-stop' aria-hidden='true'></span> Stop</button></a><a href='/roombaspot'<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-cleaning' aria-hidden='true'></span> Spot</button></a><a href='/roombadock'<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-home' aria-hidden='true'></span> Dock</button></a></div>";
// ROOT page
void handle_root()
{
// get IP
IPAddress ip = WiFi.localIP();
ClientIP = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
delay(500);
String title1 = panelHeaderName + String("Roombot Settings") + panelHeaderEnd;
String IPAddClient = panelBodySymbol + String("globe") + panelBodyName + String("IP Address") + panelBodyValue + ClientIP + panelBodyEnd;
String ClientName = panelBodySymbol + String("tag") + panelBodyName + String("Client Name") + panelBodyValue + espName + panelBodyEnd;
String Version = panelBodySymbol + String("info-sign") + panelBodyName + String("Roombot Version") + panelBodyValue + roombotVersion + panelBodyEnd;
String Uptime = panelBodySymbol + String("time") + panelBodyName + String("Uptime") + panelBodyValue + hour() + String(" h ") + minute() + String(" min ") + second() + String(" sec") + panelBodyEnd + panelEnd;
String title2 = panelHeaderName + String("Pimatic server") + panelHeaderEnd;
String IPAddServ = panelBodySymbol + String("globe") + panelBodyName + String("IP Address") + panelBodyValue + host + panelBodyEnd;
String User = panelBodySymbol + String("user") + panelBodyName + String("Username") + panelBodyValue + Username + panelBodyEnd + panelEnd;
String title3 = panelHeaderName + String("Commands") + panelHeaderEnd;
String commands = panelBodySymbol + panelBodyName + panelcenter + roombacontrol + panelBodyEnd;
server.send ( 200, "text/html", header + navbar + containerStart + title1 + IPAddClient + ClientName + Version + Uptime + title3 + commands + containerEnd + siteEnd);
}
// Setup
void setup(void)
{
Serial.begin(115200);
mySerial.begin(115200);
pinMode(SERIAL_RX, INPUT);
pinMode(SERIAL_TX, OUTPUT);
pinMode(Wake_Pin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(Wake_Pin,LOW);
// Check if SPIFFS is OK
if (!SPIFFS.begin())
{
Serial.println("SPIFFS failed, needs formatting");
handleFormat();
delay(500);
ESP.restart();
}
else
{
FSInfo fs_info;
if (!SPIFFS.info(fs_info))
{
Serial.println("fs_info failed");
}
else
{
FSTotal = fs_info.totalBytes;
FSUsed = fs_info.usedBytes;
}
}
//mySerial.begin(115200); // uncomment this line to use SoftSerial
WiFi.begin(ssid.c_str(), password.c_str());
int i = 0;
while (WiFi.status() != WL_CONNECTED && i < 31)
{
delay(1000);
Serial.print(".");
++i;
}
if (WiFi.status() != WL_CONNECTED && i >= 30)
{
WiFi.disconnect();
delay(1000);
Serial.println("");
Serial.println("Couldn't connect to network :( ");
Serial.println("Setting up access point");
Serial.println("SSID: ");
Serial.println(APssid);
Serial.println("password: ");
Serial.println(APpassword);
WiFi.mode(WIFI_AP);
WiFi.softAP(APssid, APpassword);
WMode = "AP";
Serial.print("Connected to ");
Serial.println(APssid);
IPAddress myIP = WiFi.softAPIP();
Serial.print("IP address: ");
Serial.println(myIP);
}
else
{
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Hostname: ");
Serial.println(espName);
}
server.on ( "/format", handleFormat );
server.on("/", handle_root);
server.on("/", handle_fupload_html);
server.on("/api", handle_api);
server.on("/updatefwm", handle_updatefwm_html);
server.on("/fupload", handle_fupload_html);
server.on("/filemanager_ajax", handle_filemanager_ajax);
server.on("/delete", handleFileDelete);
server.on("/roombastart", handle_roomba_start);
server.on("/roombamax", handle_roomba_max);
server.on("/roombastop", handle_roomba_stop);
server.on("/roombaspot", handle_roomba_spot);
server.on("/roombadock", handle_roomba_dock);
server.on("/restart", handle_esp_restart);
// Upload firmware:
server.on("/updatefw2", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []()
{
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START)
{
fileName = upload.filename;
Serial.setDebugOutput(true);
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
{
Update.printError(Serial);
}
}
else if (upload.status == UPLOAD_FILE_END)
{
if (Update.end(true)) //true to set the size to the current progress
{
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
}
else
{
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
});
// upload file to SPIFFS
server.on("/fupload2", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START)
{
fileName = upload.filename;
Serial.setDebugOutput(true);
//fileName = upload.filename;
Serial.println("Upload Name: " + fileName);
String path;
if (fileName.indexOf(".css") >= 0)
{
path = "/css/" + fileName;
}
else if (fileName.indexOf(".js") >= 0)
{
path = "/js/" + fileName;
}
else if (fileName.indexOf(".otf") >= 0 || fileName.indexOf(".eot") >= 0 || fileName.indexOf(".svg") >= 0 || fileName.indexOf(".ttf") >= 0 || fileName.indexOf(".woff") >= 0 || fileName.indexOf(".woff2") >= 0)
{
path = "/fonts/" + fileName;
}
else
{
path = "/" + fileName;
}
UploadFile = SPIFFS.open(path, "w");
// already existing file will be overwritten!
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (UploadFile)
UploadFile.write(upload.buf, upload.currentSize);
Serial.println(fileName + " size: " + upload.currentSize);
}
else if (upload.status == UPLOAD_FILE_END)
{
Serial.print("Upload Size: ");
Serial.println(upload.totalSize); // need 2 commands to work!
if (UploadFile)
UploadFile.close();
}
yield();
});
//called when the url is not defined here
//use it to load content from SPIFFS
server.onNotFound([]() {
if (!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
if (!mdns.begin(espName.c_str(), WiFi.localIP())) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
server.begin();
Serial.println("HTTP server started");
}
String getContentType(String filename) {
if (server.hasArg("download")) return "application/octet-stream";
else if (filename.endsWith(".htm")) return "text/html";
else if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".png")) return "image/png";
else if (filename.endsWith(".gif")) return "image/gif";
else if (filename.endsWith(".jpg")) return "image/jpeg";
else if (filename.endsWith(".ico")) return "image/x-icon";
else if (filename.endsWith(".xml")) return "text/xml";
else if (filename.endsWith(".pdf")) return "application/x-pdf";
else if (filename.endsWith(".zip")) return "application/x-zip";
else if (filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path)
{
Serial.println("handleFileRead: " + path);
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path))
{
if (SPIFFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
if ( (path.startsWith("/css/") || path.startsWith("/js/") || path.startsWith("/fonts/")) && !path.startsWith("/js/insert"))
{
server.sendHeader("Cache-Control", " max-age=31104000");
}
else
{
server.sendHeader("Connection", "close");
}
size_t sent = server.streamFile(file, contentType);
size_t contentLength = file.size();
file.close();
return true;
}
else
{
//Serial.println(path);
}
return false;
}
// LOOP
void loop(void)
{
if (mySerial.available()) {
Serial.print(mySerial.read());
}
server.handleClient();
}
// handles
void handle_api()
{
// Get vars for all commands
String action = server.arg("action");
String value = server.arg("value");
String api = server.arg("api");
if (action == "clean" && value == "start")
{
handle_roomba_start();
}
if (action == "dock" && value == "home")
{
handle_roomba_dock();
}
if (action == "reset" && value == "true")
{
server.send ( 200, "text/html", "Reset ESP OK");
delay(500);
Serial.println("RESET");
ESP.restart();
}
}
void handle_updatefwm_html()
{
server.send ( 200, "text/html", "<form method='POST' action='/updatefw2' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form><br<b>For firmware only!!</b>");
}
void handle_fupload_html()
{
String HTML = "<br>Files on flash:<br>";
Dir dir = SPIFFS.openDir("/");
while (dir.next())
{
fileName = dir.fileName();
size_t fileSize = dir.fileSize();
HTML += fileName.c_str();
HTML += " ";
HTML += formatBytes(fileSize).c_str();
HTML += " , ";
HTML += fileSize;
HTML += "<br>";
//Serial.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
}
server.send ( 200, "text/html", "<form method='POST' action='/fupload2' enctype='multipart/form-data'><input type='file' name='update' multiple><input type='submit' value='Update'></form><br<b>For webfiles only!!</b>Multiple files possible<br>" + HTML);
}
void handle_update_upload()
{
if (server.uri() != "/update2") return;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
}
void handle_update_html2()
{
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}
void handleFileDelete()
{
if (server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
if (!path.startsWith("/")) path = "/" + path;
Serial.println("handleFileDelete: " + path);
if (path == "/")
return server.send(500, "text/plain", "BAD PATH");
if (!SPIFFS.exists(path))
return server.send(404, "text/plain", "FileNotFound");
SPIFFS.remove(path);
server.send(200, "text/plain", "");
path = String();
}
void handleFormat()
{
server.send ( 200, "text/html", "OK");
Serial.println("Format SPIFFS");
if (SPIFFS.format())
{
if (!SPIFFS.begin())
{
Serial.println("Format SPIFFS failed");
}
}
else
{
Serial.println("Format SPIFFS failed");
}
if (!SPIFFS.begin())
{
Serial.println("SPIFFS failed, needs formatting");
}
else
{
Serial.println("SPIFFS mounted");
}
}
void handle_filemanager_ajax()
{
String form = server.arg("form");
if (form != "filemanager")
{
String HTML;
Dir dir = SPIFFS.openDir("/");
while (dir.next())
{
fileName = dir.fileName();
size_t fileSize = dir.fileSize();
HTML += String("<option>") + fileName + String("</option>");
}
// Glue everything together and send to client
server.send(200, "text/html", HTML);
}
}
void handle_roomba_wake(){
digitalWrite(Wake_Pin, HIGH);
delay(100);
digitalWrite(Wake_Pin, LOW);
delay(500);
digitalWrite(Wake_Pin, HIGH);
delay(100);
}
void handle_roomba_start()
{
handle_roomba_wake();
Serial.println("Starting");
mySerial.write(128);
delay(50);
mySerial.write(131);
delay(50);
mySerial.write(135);
Serial.println("I will clean master");
handle_root();
}
void handle_roomba_max()
{
handle_roomba_wake();
Serial.println("Starting");
mySerial.write(128);
delay(50);
mySerial.write(131);
delay(50);
mySerial.write(136);
Serial.println("Maximum cleaning");
handle_root();
}
void handle_roomba_spot()
{
handle_roomba_wake();
mySerial.write(128);
delay(50);
mySerial.write(131);
delay(50);
mySerial.write(134);
Serial.println("Spot cleaning");
handle_root();
}
void handle_roomba_stop()
{
handle_roomba_wake();
mySerial.write(128);
delay(50);
mySerial.write(131);
delay(50);
mySerial.write(133);
Serial.println("STOP");
handle_root();
}
void handle_roomba_dock()
{
handle_roomba_wake();
mySerial.write(128);
delay(50);
mySerial.write(131);
delay(50);
mySerial.write(143);
Serial.println("Thank you for letting me rest, going home master");
handle_root();
}
void handle_esp_restart() {
ESP.restart();
}
void handle_esp_charging() {
int charge = 0;
// int data;
mySerial.write(142);
delay(50);
mySerial.write(21);
delay(50);
if (mySerial.available()) {
charge = Serial.read();
Serial.println("..");
Serial.print(charge);
switch (charge) {
case 0:{
//do something when var equals 1
String data = String(charge);
handle_esp_pimatic(data, chargevar);
break;}
case 1:{
//do something when var equals 2
String data = String(charge);
handle_esp_pimatic(data, chargevar);
break;}
case 2:{
//do something when var equals 2
String data = String(charge);
handle_esp_pimatic(data, chargevar);
break;}
case 3:{
//do something when var equals 2
String data = String(charge);
handle_esp_pimatic(data, chargevar);
break;}
case 4:{
//do something when var equals 2
String data = String(charge);
handle_esp_pimatic(data, chargevar);
break;}
case 5:{
//do something when var equals 2
String data = String(charge);
String variable = String(charge);
handle_esp_pimatic(data, chargevar);
break;}
default:
// if nothing else matches, do the default
// default is optional
break;
}
}
Serial.println("Charging status");
}
void handle_esp_distance() {
mySerial.write(142);
delay(50);
mySerial.write(19);
delay(50);
if (mySerial.available()) {
Serial.println("..");
Serial.print(Serial.read());
}
String data = String(Serial.read());
Serial.println("Distance traveled");
handle_esp_pimatic(data, distancevar);
}
void handle_esp_pimatic(String data, String variable) {
String yourdata;
char uname[BASE64_LEN];
String str = String(Username) + ":" + String(Password);
str.toCharArray(uname, BASE64_LEN);
memset(unameenc, 0, sizeof(unameenc));
// base64_encode(unameenc, uname, strlen(uname));
base64 encoder;
String auth = Username;
auth += ":";
auth += Password;
if (!client.connect(host.c_str(), httpPort))
{
Serial.println("connection failed");
return;
}
yourdata = "{\"type\": \"value\", \"valueOrExpression\": \"" + data + "\"}";
client.print("PATCH /api/variables/");
client.print(variable);
client.print(" HTTP/1.1\r\n");
client.print("Authorization: Basic ");
client.print(unameenc);
client.print("\r\n");
client.print("Host: " + host +"\r\n");
client.print("Content-Type:application/json\r\n");
client.print("Content-Length: ");
client.print(yourdata.length());
client.print("\r\n\r\n");
client.print(yourdata);
delay(500);
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
//Serial.print(line);
}
}