I am using esp32 to send data to my nodejs socketio server. Esp32 is getting these data from arduino uno via serial communication. It works fine. But after sending data for about 10 second, the esp32 gets disconnected from socket server. It is connected to wifi, but disconnected from the socketio server and not gets reconnected. If I restart the esp32 then it gets connected and works fine for around 10 seconds and after that again gets disconnected.
Can anyone help me how can I solve this.
Nobody here because you're hiding your code. As you're the only one seeing the code you're the only one who might solve this. Did you actually read the sticky post ("How to get the best out of this forum") at the top of the forum?
ohh, here is the code:
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>
#define RXp2 16
#define TXp2 17
WiFiMulti WiFiMulti;
SocketIOclient socketIO;
#define USE_SERIAL Serial
#define RXp2 16
#define TXp2 17
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);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
//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);
}
// this timeout is the interval of serial data read
Serial2.setTimeout(100);
WiFiMulti.addAP("Nord N100", "123456789");
//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("192.168.220.98", 8080, "/socket.io/?EIO=4");
// event handler
socketIO.onEvent(socketIOEvent);
}
unsigned long messageTimestamp = 0;
void loop() {
socketIO.loop();
while (socketIO.isConnected()) {
// int val = analogRead(34);
// USE_SERIAL.println(val);
DynamicJsonDocument doc(1024);
JsonArray array = doc.to<JsonArray>();
// // add evnet name
// // Hint: socket.on('event_name', ....
array.add("new-data-to-server");
// // add payload (parameters) for the event
// JsonObject param1 = array.createNestedObject();
// param1["ldr"] = "val";
// // socketIO.sendEVENT(output);
// USE_SERIAL.println(output);
String data = Serial2.readString();
array.add(data);
String output;
serializeJson(doc, output);
socketIO.sendEVENT(output);
USE_SERIAL.println(data);
USE_SERIAL.println(WiFi.isConnected());
// delay(1000);
}
USE_SERIAL.println(WiFi.isConnected());
}
Change the while to an if in this line and try again.
yeah it works now, thank you very much, thank you so much.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.