Open
Description
Hi there,
I have the WebSocketClientSocketIOack.ino example running on my ESP32. I have set up a wireless access point using a Raspberry Pi 3B with a DHCP Server running.
A Node.js Socket IO Server is running on my Raspberry Pi and I am hosting an index.html. The setup is not connected to the internet. I can access the website locally using my phone (https://[RaspberryPi_IP]:[PORT]) and the connection is verified by the server. So I assume the Server is working just fine.
I did not alter the ESP example code, except, I did not set a URL as a path as I want to connect locally.
ESP32 Code
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>
WiFiMulti WiFiMulti;
SocketIOclient socketIO;
#define USE_SERIAL Serial
void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
switch(type) {
case sIOtype_DISCONNECT:
USE_SERIAL.printf("[IOc] Disconnected!\n");
break;
case sIOtype_CONNECT:
USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload);
// join default namespace (no auto join in Socket.IO V3)
socketIO.send(sIOtype_CONNECT, "/");
break;
case sIOtype_EVENT:
{
char * sptr = NULL;
int id = strtol((char *)payload, &sptr, 10);
USE_SERIAL.printf("[IOc] get event: %s id: %d\n", payload, id);
if(id) {
payload = (uint8_t *)sptr;
}
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload, length);
if(error) {
USE_SERIAL.print(F("deserializeJson() failed: "));
USE_SERIAL.println(error.c_str());
return;
}
String eventName = doc[0];
USE_SERIAL.printf("[IOc] event name: %s\n", eventName.c_str());
// Message Includes a ID for a ACK (callback)
if(id) {
// creat JSON message for Socket.IO (ack)
DynamicJsonDocument docOut(1024);
JsonArray array = docOut.to<JsonArray>();
// add payload (parameters) for the ack (callback function)
JsonObject param1 = array.createNestedObject();
param1["now"] = millis();
// JSON to String (serializion)
String output;
output += id;
serializeJson(docOut, output);
// Send event
socketIO.send(sIOtype_ACK, output);
}
}
break;
case sIOtype_ACK:
USE_SERIAL.printf("[IOc] get ack: %u\n", length);
break;
case sIOtype_ERROR:
USE_SERIAL.printf("[IOc] get error: %u\n", length);
break;
case sIOtype_BINARY_EVENT:
USE_SERIAL.printf("[IOc] get binary: %u\n", length);
break;
case sIOtype_BINARY_ACK:
USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
break;
}
}
void setup() {
//USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);
//Serial.setDebugOutput(true);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("SSID", "passpasspass");
//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
String ip = WiFi.localIP().toString();
USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
// server address, port and URL
socketIO.begin("RaspberryIP", myPortNumber);
// event handler
socketIO.onEvent(socketIOEvent);
}
unsigned long messageTimestamp = 0;
void loop() {
socketIO.loop();
uint64_t now = millis();
if(now - messageTimestamp > 2000) {
messageTimestamp = now;
// creat JSON message for Socket.IO (event)
DynamicJsonDocument doc(1024);
JsonArray array = doc.to<JsonArray>();
// add evnet name
// Hint: socket.on('event_name', ....
array.add("event_name");
// add payload (parameters) for the event
JsonObject param1 = array.createNestedObject();
param1["now"] = (uint32_t) now;
// JSON to String (serializion)
String output;
serializeJson(doc, output);
// Send event
socketIO.sendEVENT(output);
// Print JSON for debugging
USE_SERIAL.println(output);
}
}
node.js server code
const express = require('express'); //web server
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io').listen(server); //web socket server
var values = 0; //static variable to hold the current values
server.listen(PORT, () => {
console.log("server launched on port ...")
});
app.use(express.static('public')); //tell the server that ./public/ contains the static webpages
io.sockets.on('connection', (socket) => { //gets called whenever a client connects
socket.emit('led', {value: values}); //send the new client the current brightness
socket.on('led', (data) => { //makes the socket react to 'led' packets by calling this function
values = data.value; //updates brightness from the data object
console.log(values);
io.sockets.emit('led', {value: brightness}); //sends the updated brightness to all connected clients
});
});
Arduino IDE Output
[SETUP] BOOT WAIT 4...
[SETUP] BOOT WAIT 3...
[SETUP] BOOT WAIT 2...
[SETUP] BOOT WAIT 1...
[SETUP] WiFi Connected XXXXXXX
["event_name",{"now":8971}]
["event_name",{"now":10972}]
["event_name",{"now":12973}]
[IOc] Disconnected!
["event_name",{"now":14974}]
["event_name",{"now":16975}]
["event_name",{"now":18976}]
[IOc] Disconnected!
["event_name",{"now":20977}]
["event_name",{"now":22978}]
["event_name",{"now":24979}]
[IOc] Disconnected!
Server output
server launched on port ...
Metadata
Metadata
Assignees
Labels
No labels