-
Notifications
You must be signed in to change notification settings - Fork 31
/
WebSocketSerialMonitor.ino
136 lines (105 loc) · 3.18 KB
/
WebSocketSerialMonitor.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
/*
WebSocketServer.ino
Created on: 22.05.2015
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WebSocketsServer.h> //https://github.com/Links2004/arduinoWebSockets/tree/async
#include <Hash.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
WebSocketsServer webSocket = WebSocketsServer(81);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %s url: %s\n", num, ip.toString().c_str(), payload);
// send message to client
webSocket.sendTXT(num, "Connected to Serial on " + WiFi.localIP().toString() + "\n");
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload);
// send message to client
// webSocket.sendTXT(num, "message here");
// send data to all connected clients
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
Serial.printf("[%u] get binary lenght: %u\n", num, lenght);
hexdump(payload, lenght);
// send message to client
// webSocket.sendBIN(num, payload, lenght);
break;
}
}
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// Serial.begin(921600);
Serial.begin(115200);
//Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//tries to connect to last known settings
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP" with password "password"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.reset();
delay(5000);
}
webSocket.begin();
webSocket.onEvent(webSocketEvent);
Serial.println("START MIRRORING SERIAL");
Serial.println(WiFi.localIP());
// Serial.setTimeout(500);
inputString.reserve(256);
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n') {
stringComplete = true;
return;
} else {
inputString += inChar;
}
}
}
void loop() {
serialEvent();
if (stringComplete) {
String line = inputString;
// clear the string:
inputString = "";
stringComplete = false;
//line += '\n';
webSocket.broadcastTXT(line);
Serial.println(line);
}
webSocket.loop();
/*
String line = Serial.readStringUntil('\n');
if (line.length() > 0) {
// add back line ending
line += '\n';
webSocket.broadcastTXT(line);
Serial.print(line);
}
webSocket.loop();
*/
}