forked from hallard/WebSocketToSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketToSerial.ino
More file actions
176 lines (148 loc) · 5 KB
/
WebSocketToSerial.ino
File metadata and controls
176 lines (148 loc) · 5 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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <FS.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "**********";
const char* password = "************";
String inputString = "";
bool serialSwap = false;
// SKETCH BEGIN
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
if(type == WS_EVT_CONNECT){
os_printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf("Hello Client %u :)", client->id());
client->ping();
} else if(type == WS_EVT_DISCONNECT){
os_printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
} else if(type == WS_EVT_ERROR){
os_printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
} else if(type == WS_EVT_PONG){
os_printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
} else if(type == WS_EVT_DATA){
AwsFrameInfo * info = (AwsFrameInfo*)arg;
char buff[3];
String str = "";
// Grab all data
for(size_t i=0; i < info->len; i++) {
if (info->opcode == WS_TEXT ) {
str += (char) data[i];
} else {
sprintf(buff, "%02x ", (uint8_t) data[i]);
str += buff ;
}
}
// Custom coommand to talk to device
if (str == "ping") {
client->printf("received your ping, here is my pong");
} else if (str == "swap") {
Serial.swap();
client->printf("Swapped UART pins, now using ");
if ( serialSwap ) {
serialSwap = false;
client->printf("RX-GPIO3 TX-GPIO1");
} else {
serialSwap = true;
client->printf("RX-GPIO13 TX-GPIO15");
}
} else if (str == "heap") {
client->printf("Free Heap %ld bytes", ESP.getFreeHeap());
// Send all other to serial
} else if (str!="") {
Serial.print(info->opcode==WS_TEXT?"":"Binary 0x:");
Serial.println(str);
}
}
}
void setup(){
Serial.begin(115200);
Serial.println(F("\r\nWebSocket2Serial"));
SPIFFS.begin();
WiFi.mode(WIFI_STA);
// No empty sketch SSID, try connect
if (*ssid!='*' && *password!='*' ) {
Serial.printf("connecting to %s with psk %s\r\n", ssid, password );
WiFi.begin(ssid, password);
} else {
// empty sketch SSID, try autoconnect with SDK saved credentials
Serial.println(F("No SSID/PSK defined in sketch\r\nConnecting with SDK ones if any"));
}
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println(F("Failed to connect, retrying!"));
WiFi.disconnect(false);
delay(2000);
// No empty sketch SSID, try connect
if (*ssid!='*' && *password!='*' ) {
WiFi.begin(ssid, password);
}
}
Serial.print(F("Connected with "));
Serial.println(WiFi.localIP());
ArduinoOTA.begin();
ws.onEvent(onEvent);
server.addHandler(&ws);
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server.serveStatic("/", SPIFFS, "/index.htm", "max-age=86400");
server.serveStatic("/", SPIFFS, "/", "max-age=86400");
server.onNotFound([](AsyncWebServerRequest *request){
os_printf("NOT_FOUND: ");
if(request->method() == HTTP_GET)
os_printf("GET");
else if(request->method() == HTTP_POST)
os_printf("POST");
else if(request->method() == HTTP_DELETE)
os_printf("DELETE");
else if(request->method() == HTTP_PUT)
os_printf("PUT");
else if(request->method() == HTTP_PATCH)
os_printf("PATCH");
else if(request->method() == HTTP_HEAD)
os_printf("HEAD");
else if(request->method() == HTTP_OPTIONS)
os_printf("OPTIONS");
else
os_printf("UNKNOWN");
os_printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
if(request->contentLength()){
os_printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
os_printf("_CONTENT_LENGTH: %u\n", request->contentLength());
}
int headers = request->headers();
int i;
for(i=0;i<headers;i++){
AsyncWebHeader* h = request->getHeader(i);
os_printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
}
int params = request->params();
for(i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isFile()){
os_printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
} else if(p->isPost()){
os_printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
} else {
os_printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(404);
});
// Start Server
server.begin();
}
void loop() {
if (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
ws.textAll(inputString);
inputString = "";
}
}
ArduinoOTA.handle();
}