-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhelperFunctions.ino
90 lines (80 loc) · 2.12 KB
/
helperFunctions.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
//-------------------------------- Help functions ---------------------------
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 3; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void otaCountown(){
if(otaCount>0 && otaFlag==1) {
otaCount--;
Serial.println(otaCount);
}
}
#ifdef WEBOTA
void ota(){
//Debugln("OTA.");
if (OTA.parsePacket()) {
IPAddress remote = OTA.remoteIP();
int cmd = OTA.parseInt();
int port = OTA.parseInt();
int size = OTA.parseInt();
Serial.print("Update Start: ip:");
Serial.print(remote);
Serial.printf(", port:%d, size:%d\n", port, size);
uint32_t startTime = millis();
WiFiUDP::stopAll();
if(!Update.begin(size)){
Serial.println("Update Begin Error");
return;
}
WiFiClient clientWiFi;
if (clientWiFi.connect(remote, port)) {
uint32_t written;
while(!Update.isFinished()){
written = Update.write(clientWiFi);
if(written > 0) clientWiFi.print(written, DEC);
}
Serial.setDebugOutput(false);
if(Update.end()){
clientWiFi.println("OK");
Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
ESP.restart();
} else {
Update.printError(clientWiFi);
Update.printError(Serial);
}
} else {
Serial.printf("Connect Failed: %u\n", millis() - startTime);
}
}
//IDE Monitor (connected to Serial)
if (TelnetServer.hasClient()){
if (!Telnet || !Telnet.connected()){
if(Telnet) Telnet.stop();
Telnet = TelnetServer.available();
} else {
WiFiClient toKill = TelnetServer.available();
toKill.stop();
}
}
if (Telnet && Telnet.connected() && Telnet.available()){
while(Telnet.available())
Serial.write(Telnet.read());
}
if(Serial.available()){
size_t len = Serial.available();
uint8_t * sbuf = (uint8_t *)malloc(len);
Serial.readBytes(sbuf, len);
if (Telnet && Telnet.connected()){
Telnet.write((uint8_t *)sbuf, len);
yield();
}
free(sbuf);
}
}
#endif