-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_serial_lcd.ino
733 lines (603 loc) · 21.2 KB
/
esp32_serial_lcd.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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#include <WiFi.h>
#include <WiFiMulti.h>
#include <esp_wifi.h>
#include <PubSubClient.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include <EEPROM.h>
//OTA
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>
#include <stdio.h> //you must include this library to get the atoi function
#include <stdlib.h>
//WEB
#include <WebServer.h>
#include <HTTPClient.h>
#include "index.h" //Web page header file
//MQTT
#include <PubSubClient.h>
WebServer server(80);
//--------------CONFIGURATION
//
//WIFIMULTI Handle the WIFI Stuff....!!
//MQTT and Chorus Client use same server IP
#define WIFI_AP_NAME1 "Chorus32 LapTimer" //Dont´t change... reserved for Chorus32 connection
#define WIFI_AP_NAME2 "Laptimer" //my laptimer ssid on the field
#define WIFI_AP_NAME3 "A1-7FB051" //my dev ssid
#define WIFI_AP_PASSWORD1 "" //Chorus32 Laptimer password
#define WIFI_AP_PASSWORD2 "laptimer" //you know ... your password
#define WIFI_AP_PASSWORD3 "blablabla" //you know ... your password
#define MQTTSERVER1 "192.168.4.1" //MQTT or TCP Client for Chorus32 Port 9000
#define MQTTSERVER2 "192.168.0.141"
#define MQTTSERVER3 "10.0.0.50"
#define UART_TX 1
#define UART_RX 3 //4 normal esp32 14 esp32-cam
#define PROXY_PREFIX 'P' // used to configure any proxy (i.e. this device ;))
#define PROXY_CONNECTION_STATUS 'c'
#define PROXY_WIFI_STATUS 'w'
#define PROXY_WIFI_RSSI 't'
#define PROXY_WIFI_IP 'i'
#define EEPROM_SIZE 32
#define delaytime 70
#define MAX_BUF 1500
char buf[MAX_BUF];
uint32_t buf_pos = 0;
String global;
int mqtt = 0;
long lastReconnectAttempt = 0;
String mqttserver;
const char* mqtt_server = "10.0.0.81";
String ipa = "10.0.0.50";
int mqttid = 2; //Change THIS .... MQTTID!!
#include <U8x8lib.h>
//U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
uint8_t oled_line = 0;
String pad_str(String str) {
int len = str.length();
for (int i = 0; i < (16 - len); i++)
str += ' ';
return str;
}
void lcdlog (String str) {
u8x8.setFont(u8x8_font_8x13B_1x2_f);
u8x8.drawString(0, oled_line, pad_str(str).c_str());
oled_line=oled_line+2;
//oled_line++;
if (oled_line >= 8) {
oled_line = 0;
}
}
//TEST
//#include <SPort.h> //Include the SPort library
//SPortHub hub(0x12, 0); //Hardware ID 0x12, Software serial pin 3
//SimpleSPortSensor sensor(0x5900); //Sensor with ID 0x5900
WiFiClient client;
WiFiMulti wifiMulti;
WiFiClient espClient;
PubSubClient client2(espClient);
int real_rssi = 0;
//Rapidfire Station
String serverName = "http://radio0/setLED?LEDstate";
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
void handleADC() {
//String adcValue = String(WiFi.localIP());
server.send(200, "text/plane", global); //Send ADC value only to client ajax request
}
unsigned int hexToDec(String hexString) {
unsigned int decValue = 0;
int nextInt;
for (int i = 0; i < hexString.length(); i++) {
nextInt = int(hexString.charAt(i));
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain(nextInt, 0, 15);
decValue = (decValue * 16) + nextInt;
}
return decValue;
}
String chorus_ms(String chorustime){
String outdata;
String stringone;
uint8_t buf;
int len = chorustime.length();
stringone = chorustime.substring(5, len);
stringone.trim();
int i = atoi(stringone.c_str());
outdata=hexToDec(stringone);
return outdata;
}
String ms_time(String chorustime){
String outdata;
String stringone;
int i = atoi(chorustime.c_str());
int minutesa;
int secondsa;
minutesa=abs(i/60000);
if(minutesa>=1){
secondsa=abs(i/1000);
}else{
secondsa=abs(i/1000);
}
outdata=String(minutesa)+":"+String(secondsa);
outdata=chorustime;
return outdata;
}
void callback(char* topic, byte* message, unsigned int length) { //MQTT Callback
//Serial.print("Message arrived on topic: ");
//Serial.print(topic);
//Serial.print(". Message: ");
String line;
String rxvid = WiFi.macAddress();
String topicmsg;
int bandn;
rxvid.replace(":", ""); //remove : from mac
for (int i = 0; i < length; i++) {
//Serial.print((char)message[i]);
line += (char)message[i];
}
global = global + line + "<br>";
//Serial.println("Topic:");
//Serial.println(String(topic));
//Serial.println(line);
//Rotorhazard send \n first
// -> 29UML1:Callsign 2 L1: 0:06.451%
line.trim();
//SWITCH CHANNEL
if (line.charAt(0) == '0' && line.charAt(1) == '9' && line.charAt(2) == 'B' && line.charAt(3) == 'C') {
//channel(line.charAt(4)+1);
global = global + "CHANNEL: " + line.charAt(4) + "<br>";
}
//SWITCH BAND
if (line.charAt(0) == '0' && line.charAt(1) == '9' && line.charAt(2) == 'B' && line.charAt(3) == 'G') {
bandn = line.charAt(4);
if (bandn == 2) {
bandn = 2; //raceband
}
if (bandn == 4) {
bandn = 1; //fatshark
}
if (bandn == 3) {
bandn = 3; //e
}
if (bandn == 5) {
bandn = 6; //low
}
if (bandn == 1) {
bandn = 4; //b
}
if (bandn == 0) {
bandn = 5; //a
}
//band(bandn);
global = global + "BAND: " + line.charAt(4) + "<br>";
}
//09BC7% 09B=command change freq.. c=raceband channel 7
//Change RotorHazard vrx id rx/cv1/cmd_esp_target/CV_246F28166140
if (line.indexOf('node_number')) { //<<command
mqttid = line.substring(12, 13).toInt();
if (mqttid > 0) {
EEPROM.write(0, mqttid);
EEPROM.commit();
//String stringnr="5";
client2.unsubscribe("rx/cv1/cmd_node/0");
client2.unsubscribe("rx/cv1/cmd_node/1");
client2.unsubscribe("rx/cv1/cmd_node/2");
client2.unsubscribe("rx/cv1/cmd_node/3");
client2.unsubscribe("rx/cv1/cmd_node/4");
client2.unsubscribe("rx/cv1/cmd_node/5");
client2.unsubscribe("rx/cv1/cmd_node/6");
client2.unsubscribe("rx/cv1/cmd_node/7");
client2.unsubscribe("rx/cv1/cmd_node/8");
topicmsg = "rx/cv1/cmd_node/" + line.substring(12, 13);
client2.subscribe(topicmsg.c_str());
global = global + topicmsg + "<br>";
char JSONmessageBuffer[100];
StaticJsonBuffer<300> JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject();
//JSONencoder["node_number"] = String(6);
JSONencoder["node_number"] = String(line.substring(12, 13));
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
topicmsg = "status_variable/CV_" + rxvid;
client2.publish(topicmsg.c_str(), JSONmessageBuffer);
global = global + "CHANGE TO" + String(mqttid) + "<br>";
global = global + "CHANGE NODE_NUMBER" + "<br>";
//mqttid=current_nr;
//reconnect();
}
}
//rx/cv1/cmd_esp_target/CV_00000001
if (line.charAt(0) == '0' && line.charAt(1) == '9' && line.charAt(2) == 'U') { //09UMFinish%
//Serial.println("Send text mqtt");
String stringOne;
stringOne = "T=" + line.substring(4, line.lastIndexOf('%') + 3); //8
//stringOne="T="+line; //8
int len = strlen(stringOne.c_str());
String fill;
int i;
for (int i = len; i < 24; i++) {
fill = fill + " ";
}
stringOne = stringOne + fill;
stringOne.replace("%", " "); //replace % with space
//text(stringOne); //not working
global = global + stringOne + "<br>";
}
if (line.charAt(0) == '2' || line.charAt(0) == '0' && line.charAt(1) == '9' && line.charAt(2) == 'U') { //Push in laptimes
//Serial.println("Send text mqtt");
String stringOne;
stringOne = "T=" + line.substring(5, line.lastIndexOf('%') + 3); //8
int len = strlen(stringOne.c_str());
//fill with spaces
String fill;
int i;
for (int i = len; i < 24; i++) {
fill = fill + " ";
}
stringOne = stringOne + fill;
stringOne.replace("%", " "); //replace % with space
stringOne = stringOne.substring(0, line.lastIndexOf('/')); //extract only my time not others
/*
int firsttime = stringOne.indexOf(':');
stringOne=stringOne.substring(firsttime+1,stringOne.length());
firsttime = stringOne.indexOf(':');
stringOne=stringOne.substring(firsttime+1,stringOne.length());
firsttime = stringOne.indexOf(':');
int lasttime = stringOne.lastIndexOf(':');
*/
int lappos = stringOne.lastIndexOf('L');
int lap = String(stringOne.charAt(lappos + 1), DEC).toInt();
int lasttime = stringOne.lastIndexOf(':'); //found time position
stringOne = stringOne.substring(lasttime - 1, lasttime + 7);
global = global + stringOne; //-1=0:233 //full time string in 0:00.000
global = global + "<br>";
int minutechar = String(stringOne.charAt(0), DEC).toInt(); //esctract only minutes char
int minute = minutechar - 48; //esctract only minutes int
stringOne = stringOne.substring(1, stringOne.length()); //extract only seconds
stringOne.replace(":", ""); //remove :
stringOne.replace(".", ""); //remove .
int seconds = stringOne.toInt(); //convert to int
int sum = seconds + (60000 * minute); //calculate minute and second
global = global + "lap" + lap + "<br>";
global = global + "minute" + minute + "<br>";
global = global + "seconds" + seconds + "<br>";
global = global + "summery" + sum + "<br>";
global = global + "<br>";
if (sum > 0 && lap != 97 && lap != 84) { //Only send positiv values
String stringtwo = String(sum, HEX);
lap = lap - 48;
String stringlap = String(lap, DEC);
Serial.println("S1L0" + stringlap + """000" + stringtwo);
global = global + "S1L0" + stringlap + """000" + stringtwo + "<br>";
}
}
if (line.charAt(0) == 'T') { //Normal mqtt osd text working!!
String t_state = line;
t_state.trim();
int len = strlen(t_state.c_str());
String fill;
int i;
for (int i = len; i < 26; i++) {
fill = fill + " ";
}
t_state = t_state + fill;
t_state.replace("%", " "); //replace % with space
//text(t_state); //not working
//text(line);
}
//LED Light
if (line.charAt(0) == 'L') {
if (line.charAt(2) == '0') {
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
}
if (line.charAt(2) == '1') {
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
}
}
//Rapidfire CMD
if (line.charAt(0) == 'P') {
String stringOne;
stringOne = line.substring(2, line.length());
Serial.println(stringOne);
lcdlog(chorus_ms(stringOne));
global = global + "Forwarding Taranis" + stringOne + "<br>";
}
//Rapidfire CMD SPORT
if(line.charAt(0) == 'X') {
String stringOne;
stringOne=line.substring(2,line.length());
lcdlog(ms_time(stringOne));
global=global+"Forwarding Taranis"+stringOne+"<br>";
}
if (line.charAt(0) == 'S') {
//buzzer();
//Serial.println("BUZZER");
global = global + "MQTTBUZZER" + "<br>";
}
if (line.charAt(0) == 'O') {
//osdmode(line.charAt(2));
//Serial.println("OSD");
}
if (line.charAt(0) == 'C') {
//channel(line.charAt(2));
//Serial.println("Channel");
}
if (line.charAt(0) == 'B') {
//band(line.charAt(2));
//Serial.println("Band");
}
}
boolean reconnect() { //MQTT Connect and Reconnect
// Loop until we're reconnected
mqttid = EEPROM.read(0);
mqtt = 1;
ipa = server.arg("ip");
String rxvid = WiFi.macAddress();
String topic;
rxvid.replace(":", ""); //remove : from mac
lcdlog(rxvid);
global = global + rxvid + "<br>";
if (ipa.length() >= 5) {
lcdlog("New MQTT conn...");
mqttserver = ipa;
} else {
if (WiFi.SSID() == WIFI_AP_NAME1) {
mqttserver = MQTTSERVER1;
global = global + "try mqttconnect<br>";
}
if (WiFi.SSID() == WIFI_AP_NAME2) {
mqttserver = MQTTSERVER2;
global = global + "try mqttconnect<br>";
}
if (WiFi.SSID() == WIFI_AP_NAME3) {
mqttserver = MQTTSERVER3;
global = global + "try mqttconnect<br>";
}
lcdlog("-MQTT reconnect-");
}
client2.setServer(mqttserver.c_str(), 1883);
client2.setCallback(callback);
lcdlog("Trying MQTT...");
lcdlog(mqttserver.c_str());
// Attempt to connect
topic = "opentx_" + rxvid;
if (client2.connect(topic.c_str())) {
lcdlog("connected");
// Subscribe
//client2.subscribe("esp32/output");
//client2.subscribe("rx/cv1/cmd_esp_all");
client2.subscribe("rx/cv1/cmd_all");
topic = "rx/cv1/cmd_esp_target/CV_" + rxvid;
client2.subscribe(topic.c_str()); //change id new via mac
global = global + "TOPIC: " + String(topic) + "<br>";
//client.subscribe("rx/cv1/cmd_esp_target/CV_00000001"); //change id old
global = global + "MQTT Connected" + "<br>";
//String topic = "rxcn/CV_" + rxvid; why two times? bottom....
//client.publish(topic.c_str(), "1");
StaticJsonBuffer<300> JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject();
JSONencoder["dev"] = "rx";
JSONencoder["ver"] = "todover";
JSONencoder["fw"] = "todover";
JSONencoder["nn"] = "taranis";
//JsonArray& values = JSONencoder.createNestedArray("values");
char JSONmessageBuffer[100];
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
//Serial.println("Sending message to MQTT topic..");
//Serial.println(JSONmessageBuffer);
JSONencoder["node_number"] = "1";
topic = "rxcn/CV_" + rxvid;
client2.publish(topic.c_str(), "1");
delay(500);
topic = "status_static/CV_" + rxvid;
client2.publish(topic.c_str(), JSONmessageBuffer);
char JSONmessageBuffer2[100];
JSONencoder.printTo(JSONmessageBuffer2, sizeof(JSONmessageBuffer2));
//Serial.println("Sending message to MQTT topic..");
//Serial.println(JSONmessageBuffer2);
delay(500);
topic = "status_variable/CV_" + rxvid;
client2.publish(topic.c_str(), JSONmessageBuffer2);
//rx/cv1/cmd_node/1
String topicmsg;
client2.unsubscribe("rx/cv1/cmd_node/0");
client2.unsubscribe("rx/cv1/cmd_node/1");
client2.unsubscribe("rx/cv1/cmd_node/2");
client2.unsubscribe("rx/cv1/cmd_node/3");
client2.unsubscribe("rx/cv1/cmd_node/4");
client2.unsubscribe("rx/cv1/cmd_node/5");
client2.unsubscribe("rx/cv1/cmd_node/6");
client2.unsubscribe("rx/cv1/cmd_node/7");
client2.unsubscribe("rx/cv1/cmd_node/8");
topicmsg = "rx/cv1/cmd_node/" + String(mqttid);
global = global + "TOPIC: " + String(topicmsg) + "<br>";
client2.subscribe(topicmsg.c_str());
JSONencoder["node_number"] = mqttid; // mqttid );<<<<<<<<<<<<<<<
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
topicmsg = "status_variable/CV_" + rxvid;
global = global + "TOPIC: " + String(topicmsg) + "<br>";
client2.publish(topicmsg.c_str(), JSONmessageBuffer);
} else {
mqtt = 0;
//Serial.print("failed, rc=");
//Serial.print(client2.state());
global = global + "MQTT not Connected" + "<br>";
//Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(500);
}
return client2.connected();
}
void handleLED() {
String t_state = server.arg("LEDstate"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
Serial.println(t_state);
//Serial.println(t_state);
lcdlog(chorus_ms(t_state));
global = global + t_state + "<br>";
server.send(200, "text/plane", t_state); //Send web page
}
void handleTime() {
//int t_state = server.arg("time").toInt(); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
int timems = server.arg("time").toInt();
lcdlog(String(timems, DEC));
String stringOne = String(timems, HEX);
Serial.println("S1L01000" + stringOne);
//server.send(200, "text/plane", t_state); //Send web page
}
void chorus_connect() {
if (WiFi.isConnected()) {
if (WiFi.SSID() == WIFI_AP_NAME1) {
if (client.connect(MQTTSERVER1, 9000)) {
//Serial.println("Connected to chorus via tcp 192.168.4.1");
}
}
if (WiFi.SSID() == WIFI_AP_NAME2) {
if (client.connect(MQTTSERVER2, 9000)) {
//Serial.println("Connected to chorus via tcp 192.168.0.141");
}
}
if (WiFi.SSID() == WIFI_AP_NAME3) {
if (client.connect(MQTTSERVER3, 9000)) {
//Serial.println("Connected to chorus via tcp 10.0.0.50");
}
}
}
}
void setup() {
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
lcdlog("Start");
//hub.registerSensor(sensor); //Add sensor to the hub
//hub.begin(); //Start listening
EEPROM.begin(EEPROM_SIZE);
mqttid = EEPROM.read(0);
//Serial.begin(115200, SERIAL_8N1, 3, 1, true); //ESP32
Serial.begin(115200, SERIAL_8N1, UART_RX, UART_TX, true);
//WiFi.begin(WIFI_AP_NAME);
wifiMulti.addAP(WIFI_AP_NAME1, WIFI_AP_PASSWORD1);
wifiMulti.addAP(WIFI_AP_NAME2, WIFI_AP_PASSWORD2);
wifiMulti.addAP(WIFI_AP_NAME3, WIFI_AP_PASSWORD3);
lcdlog("Connecting Wifi:");
if (wifiMulti.run() == WL_CONNECTED) {
//Serial.println("");
lcdlog("WiFi connected");
lcdlog("IP address: ");
MDNS.begin("radio0");
lcdlog(WiFi.localIP().toString());
}
delay(500);
server.on("/", handleRoot); //This is display page
server.on("/readADC", handleADC);//To get update of ADC Value only
server.on("/setLED", handleLED);
server.on("/setTime", handleTime);
server.on("/reconnect", reconnect);
server.on("/reconnect2", chorus_connect);
server.begin(); //Start server
//Serial.println("HTTP server started");
ArduinoOTA.begin();
lcdlog((String)mqttid);
global = global + "node_number: " + mqttid + "<br>";
global = global + "Try boot up mqtt connect...<br>";
reconnect();
}
void loop() {
ArduinoOTA.handle();
real_rssi = WiFi.RSSI();
server.handleClient();
//sensor.value = 1234; //Set the sensor value
//hub.handle(); //Handle new data
delay(1);
if (wifiMulti.run() != WL_CONNECTED) {
//Serial.println("WiFi not connected!");
delay(1000);
}
while (client.connected() && client.available()) {
// Doesn't include the \n itself
String line = client.readStringUntil('\n');
// Only forward specific messages for now
//if(line[2] == 'L') {
Serial.println(line);
//Serial.printf("Forwarding to taranis: %s\n", line.c_str());
//}
}
IPAddress myip = WiFi.localIP();
while (Serial.available()) {
if (buf_pos >= MAX_BUF - 1 ) {
buf_pos = 0; // clear buffer when full
break;
}
buf[buf_pos++] = Serial.read();
if (buf[buf_pos - 1] == '\n') {
global = global + buf[buf_pos - 1];
// catch proxy command
if (buf[0] == 'P') {
switch (buf[3]) {
case PROXY_WIFI_STATUS:
Serial.printf("%cS*%c%1x\n", PROXY_PREFIX, PROXY_WIFI_STATUS, WiFi.status());
break;
case PROXY_WIFI_RSSI:
Serial.printf("%cS*%c%2x\n", PROXY_PREFIX, PROXY_WIFI_RSSI, abs(real_rssi * -1));
//Serial.println(real_rssi);
break;
case PROXY_CONNECTION_STATUS:
Serial.printf("%cS*%c%1x\n", PROXY_PREFIX, PROXY_CONNECTION_STATUS, client.connected());
break;
}
}
else if ((buf[0] == 'E' && (buf[1] == 'S' || buf[1] == 'R')) || buf[0] == 'S' || buf[0] == 'R') {
//Serial.print("Forwarding to chorus: ");
Serial.write((uint8_t*)buf, buf_pos);
client.write(buf, buf_pos);
//int a = analogRead(A0);
}
String adcValue = String(buf);
if (buf[0] == 'C') {
HTTPClient http;
String serverPath = serverName + "=C=" + (buf[1] - 47);
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
}
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
buf_pos = 0;
}
}
mqtt = 1;
if (!client2.connected() && mqtt == 1) {
long now = millis();
if (now - lastReconnectAttempt > 10000) { // Try to reconnect.
lastReconnectAttempt = now;
if (reconnect()) { // Attempt to reconnect.
lastReconnectAttempt = 0;
}
}
} else {
mqtt == 1; // Connected.
client2.loop();
//client.publish(channelName,"Hello world!"); // Publish message.
//Serial.println(mqtt);
}
if (WiFi.SSID() == "Chorus32 LapTimer") {
mqttserver = MQTTSERVER1;
global = global + "try chorus connect to 192.168.4.1<br>";
if (!client.connected()) {
delay(1000);
Serial.println("Lost tcp connection! Trying to reconnect");
Serial.println(WiFi.localIP());
Serial.println(WiFi.SSID());
chorus_connect();
}
}
}