forked from yasheena/telnetspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicOTA_and_TelnetSpy.ino
112 lines (99 loc) · 2.98 KB
/
BasicOTA_and_TelnetSpy.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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <TelnetSpy.h>
const char* ssid = ".....";
const char* password = ".....";
TelnetSpy SerialAndTelnet;
//#define SERIAL Serial
#define SERIAL SerialAndTelnet
void waitForConnection() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
SERIAL.print(".");
}
SERIAL.println(" Connected!");
}
void waitForDisconnection() {
while (WiFi.status() == WL_CONNECTED) {
delay(500);
SERIAL.print(".");
}
SERIAL.println(" Disconnected!");
}
void telnetConnected() {
SERIAL.println("Telnet connection established.");
}
void telnetDisconnected() {
SERIAL.println("Telnet connection closed.");
}
void setup() {
SerialAndTelnet.setWelcomeMsg("Welcome to the TelnetSpy example\n\n");
SerialAndTelnet.setCallbackOnConnect(telnetConnected);
SerialAndTelnet.setCallbackOnDisconnect(telnetDisconnected);
SERIAL.begin(74880);
delay(100); // Wait for serial port
SERIAL.setDebugOutput(false);
SERIAL.print("\n\nConnecting to WiFi ");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
waitForConnection();
// During updates "over the air" the telnet session will be closed.
// So the operations of ArduinoOTA cannot be seen via telnet.
// So we only use the standard "Serial" for logging.
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
SERIAL.println("Ready");
SERIAL.print("IP address: ");
SERIAL.println(WiFi.localIP());
SERIAL.println("\nType 'C' for WiFi connect.\nType 'D' for WiFi disconnect.\nType 'R' for WiFi reconnect.");
SERIAL.println("All other chars will be echoed. Play around...\n");
}
void loop() {
SerialAndTelnet.handle();
ArduinoOTA.handle();
if (SERIAL.available() > 0) {
char c = SERIAL.read();
switch (c) {
case '\r':
SERIAL.println();
break;
case 'C':
SERIAL.print("\nConnecting ");
WiFi.begin(ssid, password);
waitForConnection();
break;
case 'D':
SERIAL.print("\nDisconnecting ...");
WiFi.disconnect();
waitForDisconnection();
break;
case 'R':
SERIAL.print("\nReconnecting ");
WiFi.reconnect();
waitForDisconnection();
waitForConnection();
break;
default:
SERIAL.print(c);
break;
}
}
}