-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRemotePowerAndSerial.ino
More file actions
206 lines (176 loc) · 6.04 KB
/
RemotePowerAndSerial.ino
File metadata and controls
206 lines (176 loc) · 6.04 KB
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
/****************************************************************************
This code merges functionality from two ESP8266 examples.
The remote-serial-over-netcat one is by far the most complex,
and is originally called:
WiFiTelnetToSerial
Example Transparent UART to Telnet Server for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP8266WiFi library for Arduino environment.
The other example is the simple Web server. Both of these are originally
licensed as LGPL - so this simple amalgamation of the two also follows the
LGPL.
The crude surgery to build this Frankenstein-y thing, was performed by
Thanassis Tsiodras, in late July 2019. The details of the HW that this was
used for are documented in his blog post here:
https://www.thanassis.space/remoteserial.html
...containing pictures and videos of the HW and SW in action; remotely
commanding an AtomicPI Single Board Computer, from power on, to BIOS,
to a remote install of OpenBSD on a USB stick.
***************************************************************************/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "Your SSID goes here";
const char* password = "Your WiFi pass goes here";
// how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 1
// Remote-serial related globals
WiFiServer serverSerial(2345);
WiFiClient serverClients[MAX_SRV_CLIENTS];
// Web-server related globals
ESP8266WebServer serverWeb(80);
const int controlPin = 0;
bool poweredUp = false;
// Web-server callbacks
void handleRoot()
{
String response ="<html>";
response += "<head><title>Power up the AtomicPI!</title></head>\n";
response += "<body><h1>Power up the AtomicPI!</h1><div style=\"font-size:48px\"><p>Current status: ";
response += poweredUp?"<b><font color=\"red\">ON</font></b>":"OFF";
if (!poweredUp) {
response += "<p>Click <a href=\"/poweron\">here</a> to power up";
} else {
response += "<p>Click <a href=\"/poweroff\">here</a> to power off";
}
response += "</div>";
serverWeb.send(200, "text/html", response);
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += serverWeb.uri();
message += "\nMethod: ";
message += (serverWeb.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += serverWeb.args();
message += "\n";
for (uint8_t i=0; i<serverWeb.args(); i++){
message += " " + serverWeb.argName(i) + ": " + serverWeb.arg(i) + "\n";
}
serverWeb.send(404, "text/plain", message);
}
void setupWifi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial1.print("\nConnecting to "); Serial1.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
if(i == 21) {
Serial1.print("Could not connect to"); Serial1.println(ssid);
while(1)
delay(500);
}
Serial1.begin(115200);
Serial1.print("Connected to ");
Serial1.println(ssid);
Serial1.print("IP address: ");
Serial1.println(WiFi.localIP());
}
void setupRemoteSerial()
{
// Remote serial
Serial.begin(115200);
serverSerial.begin();
serverSerial.setNoDelay(true);
Serial1.print("Ready! Use 'telnet ");
Serial1.print(WiFi.localIP());
Serial1.println(" 2345' to connect");
}
void setupWebServer(void)
{
pinMode(controlPin, OUTPUT);
digitalWrite(controlPin, 0);
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
serverWeb.on("/", handleRoot);
auto redirect = []() {
serverWeb.sendHeader("Location", String("http://") + WiFi.localIP() + String("/"), true);
serverWeb.send(302, "text/plain", "");
};
serverWeb.on("/poweron", [&]() {
if (!poweredUp) {
poweredUp = true;
digitalWrite(controlPin, 1);
}
redirect();
});
serverWeb.on("/poweroff", [&]() {
if (poweredUp) {
poweredUp = false;
digitalWrite(controlPin, 0);
}
redirect();
});
serverWeb.onNotFound(handleNotFound);
serverWeb.begin();
Serial.println("HTTP server started");
}
void setup()
{
// As soon as possible, close the relay!
pinMode(controlPin, OUTPUT);
digitalWrite(controlPin, 0);
setupWifi();
setupWebServer();
setupRemoteSerial();
}
void loop()
{
uint8_t i;
// check if there are any new clients
if (serverSerial.hasClient()) {
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
// find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()) {
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = serverSerial.available();
Serial1.print("New client: "); Serial1.print(i);
break;
}
}
// no free/disconnected spot so reject
if ( i == MAX_SRV_CLIENTS) {
WiFiClient serverClient = serverSerial.available();
serverClient.stop();
Serial1.println("Connection rejected ");
}
}
// check clients for data
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
if(serverClients[i].available()) {
// get data from the telnet client and push it to the UART
while(serverClients[i].available()) Serial.write(serverClients[i].read());
}
}
}
// check UART for data
if(Serial.available()) {
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
// push UART data to all connected telnet clients
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write(sbuf, len);
delay(1);
}
}
}
serverWeb.handleClient();
}