Skip to content

Commit 6a6c0f0

Browse files
committed
Updated esp8266_WebMenu example to work with ESP32
1 parent 0b3cc28 commit 6a6c0f0

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

examples/esp8266_WebMenu/WebMenu/WebMenu.ino

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@ so don't forget to change it.
4343
#endif
4444
//#include <menuIO/jsFmt.h>//to send javascript thru web socket (live update)
4545
#include <FS.h>
46+
#ifdef ESP8266
4647
#include <Hash.h>
4748
extern "C" {
4849
#include "user_interface.h"
4950
}
51+
#elif defined(ESP32)
52+
#include <SPIFFS.h>
53+
54+
// Use external analogWrite library for ESP32 (optional)
55+
// for compatibility with older versions of espressif/arduino-esp32
56+
// https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
57+
#include <analogWrite.h>
58+
#endif
5059

5160
using namespace Menu;
5261

@@ -105,7 +114,11 @@ const char* serverName="192.168.1.79";
105114
#define HTTP_PORT 80
106115
#define WS_PORT 81
107116
#define USE_SERIAL Serial
117+
#ifdef ESP8266
108118
ESP8266WebServer server(80);
119+
#elif defined(ESP32)
120+
WebServer server(80);
121+
#endif
109122
WebSocketsServer webSocket(81);
110123

111124
#define MAX_DEPTH 2
@@ -245,60 +258,60 @@ navRoot wsNav(mainMenu, ws_cursors, MAX_DEPTH, none, ws_out);
245258

246259
//config myOptions('*','-',defaultNavCodes,false);
247260

248-
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
249-
switch(type) {
250-
case WStype_DISCONNECTED:
251-
//USE_SERIAL.printf("[%u] Disconnected!\n", num);
252-
break;
253-
case WStype_CONNECTED: {
254-
IPAddress ip = webSocket.remoteIP(num);
255-
//USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
256-
webSocket.sendTXT(num, "console.log('ArduinoMenu Connected')");
257-
}
258-
break;
259-
case WStype_TEXT: {
260-
//USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
261-
// nav.async((const char*)payload);//this is slow!!!!!!!!
262-
__trace(Serial.printf("[%u] get Text: %s\n", num, payload));
263-
char*s=(char*)payload;
264-
_trace(Serial<<"serve websocket menu"<<endl);
265-
wsOut.response.remove(0);
266-
wsOut<<"{\"output\":\"";
267-
wsNav.async((const char*)payload);
268-
wsOut<<"\",\n\"menu\":";
269-
wsNav.doOutput();
270-
wsOut<<"\n}";
271-
webSocket.sendTXT(num,wsOut.response);
272-
// wsOut.response.remove(0);
273-
// jsonEnd();
274-
} break;
275-
case WStype_BIN: {
276-
USE_SERIAL<<"[WSc] get binary length:"<<length<<"[";
277-
for(int c=0;c<length;c++) {
278-
USE_SERIAL.print(*(char*)(payload+c),HEX);
279-
USE_SERIAL.write(',');
280-
}
281-
USE_SERIAL<<"]"<<endl;
282-
uint16_t id=*(uint16_t*) payload++;
283-
idx_t len=*((idx_t*)++payload);
284-
idx_t* pathBin=(idx_t*)++payload;
285-
const char* inp=(const char*)(payload+len);
286-
//Serial<<"id:"<<id<<endl;
287-
if (id==nav.active().hash()) {
288-
//Serial<<"id ok."<<endl;Serial.flush();
289-
//Serial<<"input:"<<inp<<endl;
290-
//StringStream inStr(inp);
291-
//while(inStr.available())
292-
nav.doInput(inp);
293-
webSocket.sendTXT(num, "binBusy=false;");//send javascript to unlock the state
294-
} //else Serial<<"id not ok!"<<endl;
295-
//Serial<<endl;
296-
}
297-
break;
298-
default:break;
299-
}
300-
}
301-
261+
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
262+
switch(type) {
263+
case WStype_DISCONNECTED:
264+
//USE_SERIAL.printf("[%u] Disconnected!\n", num);
265+
break;
266+
case WStype_CONNECTED: {
267+
IPAddress ip = webSocket.remoteIP(num);
268+
//USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
269+
webSocket.sendTXT(num, "console.log('ArduinoMenu Connected')");
270+
}
271+
break;
272+
case WStype_TEXT: {
273+
//USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
274+
// nav.async((const char*)payload);//this is slow!!!!!!!!
275+
__trace(Serial.printf("[%u] get Text: %s\n", num, payload));
276+
char*s=(char*)payload;
277+
_trace(Serial<<"serve websocket menu"<<endl);
278+
wsOut.response.remove(0);
279+
wsOut<<"{\"output\":\"";
280+
wsNav.async((const char*)payload);
281+
wsOut<<"\",\n\"menu\":";
282+
wsNav.doOutput();
283+
wsOut<<"\n}";
284+
webSocket.sendTXT(num,wsOut.response);
285+
// wsOut.response.remove(0);
286+
// jsonEnd();
287+
} break;
288+
case WStype_BIN: {
289+
USE_SERIAL<<"[WSc] get binary length:"<<length<<"[";
290+
for(int c=0;c<length;c++) {
291+
USE_SERIAL.print(*(char*)(payload+c),HEX);
292+
USE_SERIAL.write(',');
293+
}
294+
USE_SERIAL<<"]"<<endl;
295+
uint16_t id=*(uint16_t*) payload++;
296+
idx_t len=*((idx_t*)++payload);
297+
idx_t* pathBin=(idx_t*)++payload;
298+
const char* inp=(const char*)(payload+len);
299+
//Serial<<"id:"<<id<<endl;
300+
if (id==nav.active().hash()) {
301+
//Serial<<"id ok."<<endl;Serial.flush();
302+
//Serial<<"input:"<<inp<<endl;
303+
//StringStream inStr(inp);
304+
//while(inStr.available())
305+
nav.doInput(inp);
306+
webSocket.sendTXT(num, "binBusy=false;");//send javascript to unlock the state
307+
} //else Serial<<"id not ok!"<<endl;
308+
//Serial<<endl;
309+
}
310+
break;
311+
default:break;
312+
}
313+
}
314+
302315
void pageStart() {
303316
_trace(Serial<<"pasgeStart!"<<endl);
304317
serverOut<<"HTTP/1.1 200 OK\r\n"

0 commit comments

Comments
 (0)