Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help needed: #890

Open
NielsvanDijk opened this issue Jul 16, 2024 · 1 comment
Open

Help needed: #890

NielsvanDijk opened this issue Jul 16, 2024 · 1 comment

Comments

@NielsvanDijk
Copy link

i get this error message:

WiFi status: 3
[IOc] Disconnected!

I don't know any more what to do, can someone help?

This is the code
expressJS:

const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const path = require('path');

const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
  cors: {
    origin: "http://192.168.1.*"
  }
});

const port = process.env.PORT || 9800;

// Serve static files (index.html, CSS, JS, etc.) from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));

// Handle root endpoint - serve index.html
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

let currentState = 'off';

// Socket.IO setup
io.on('connection', (socket) => {
  console.log('A client connected');

  // Send current state to newly connected client
  socket.emit('currentState', currentState);

  // Handle toggle command from client
  socket.on('toggle', () => {
    // Toggle the state
    currentState = currentState === 'on' ? 'off' : 'on';
    console.log(`Switched to ${currentState}`);

    // Broadcast new state to all connected clients
    io.emit('currentState', currentState);
  });

  socket.on('disconnect', () => {
    console.log('Client disconnected');
  });
});

// Start server
server.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});
#include <Arduino.h>
#include <WiFi.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>
#include <credentials.h>

SocketIOclient socketIO;

const char* ssid = WIFI_SSID;
const char* password = WIFI_PASS;

void socketIOEvent(socketIOmessageType_t type, uint8_t* payload, size_t length) {
  switch (type) {
    case sIOtype_DISCONNECT:
      Serial.println("[IOc] Disconnected!");
      break;
    case sIOtype_CONNECT:
      Serial.println("[IOc] Connected to server!");
      break;
    case sIOtype_EVENT:
      Serial.printf("[IOc] got event: %s\n", payload);
      break;
    case sIOtype_ACK:
      Serial.printf("[IOc] got ack: %s\n", payload);
      break;
    case sIOtype_ERROR:
      Serial.printf("[IOc] got error: %s\n", payload);
      break;
    case sIOtype_BINARY_EVENT:
      Serial.println("[IOc] got binary event");
      break;
    case sIOtype_BINARY_ACK:
      Serial.println("[IOc] got binary ack");
      break;
  }
}

void wifiConnect() {
  Serial.println("");
  Serial.println("[======================]");
  Serial.println("Connecting to Wi-Fi");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("[======================]");
  Serial.println("");
  Serial.flush();
}

void setup() {
  Serial.begin(115200);
  Serial.println("");
  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  wifiConnect();

  // Connect to Socket.IO server
  socketIO.begin("192.168.1.252", 9800, "/socket.io/?EIO=4&transport=websocket");

  // Set event handler
  socketIO.onEvent(socketIOEvent);

  // Optional: Set reconnect interval
  socketIO.setReconnectInterval(5000);
}

void loop() {
  socketIO.loop();

  // Check and print WiFi status periodically
  static unsigned long lastStatusCheck = 0;
  if (millis() - lastStatusCheck > 10000) { // Check every 10 seconds
    lastStatusCheck = millis();
    Serial.print("WiFi status: ");
    Serial.println(WiFi.status());

    if (WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi disconnected, reconnecting...");
      wifiConnect();
    }
  }
}
@NielsvanDijk
Copy link
Author

NielsvanDijk commented Jul 16, 2024

Okay this helped me! : #641

#include <ArduinoJson.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <credentials.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>

#ifdef DEBUG_ESP_PORT
#define DEBUG_MSG(...) DEBUG_ESP_PORT( __VA_ARGS__)
#else 
#define DEBUG_MSG(...)
#endif
#define DEBUG_ESP_PORT Serial

const char* ssid = WIFI_SSID;
const char* password = WIFI_PASS;


WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
SocketIOclient socketIO;
//WebSocketsClient socketIO;
#define USE_SERIAL Serial

void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
	const uint8_t* src = (const uint8_t*) mem;
	USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
	for(uint32_t i = 0; i < len; i++) {
		if(i % cols == 0) {
			USE_SERIAL.printf("\n[0x%0hola8X] 0x%08X: ", (ptrdiff_t)src, i);
		}
		USE_SERIAL.printf("%02X ", *src);
		src++;
	}
	USE_SERIAL.printf("\n");
}

void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
  switch(type) {
    case sIOtype_DISCONNECT:
      Serial.printf("[IOc] Disconnected!\n");
      break;
    case sIOtype_CONNECT:
      Serial.printf("[IOc] Connected to url: %s\n", payload);
      // join default namespace (no auto join in Socket.IO V3)
      socketIO.send(sIOtype_CONNECT, "/");
      // socketIO.send("hiiii");
      break;
    case sIOtype_EVENT:
      Serial.printf("[IOc] get event: %s\n", payload);
      break;
    case sIOtype_ACK:
      Serial.printf("[IOc] get ack: %u\n", length);
      hexdump(payload, length);
      break;
    case sIOtype_ERROR:
      Serial.printf("[IOc] get error: %u\n", length);
      hexdump(payload, length);
      break;
    case sIOtype_BINARY_EVENT:
      Serial.printf("[IOc] get binary: %u\n", length);
      hexdump(payload, length);
      break;
    case sIOtype_BINARY_ACK:
      Serial.printf("[IOc] get binary ack: %u\n", length);
      hexdump(payload, length);
      break;
  }
}


void setup() {

	USE_SERIAL.begin(115200);
  delay(10);
  
  USE_SERIAL.print("hola//////////////////////");
	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, password);

	//WiFi.disconnect();
	while(WiFiMulti.run() != WL_CONNECTED) {
    Serial.println("reconectando...");
		delay(100);
	}

  socketIO.setExtraHeaders("Authorization: 1234567890");
  socketIO.begin("192.168.1.252", 9800, "/socket.io/?EIO=4");


	// event handler
  socketIO.onEvent(socketIOEvent);

	// try ever 5000 again if connection has failed
	//webSocket.setReconnectInterval(3000);

}
unsigned long messageTimestamp = 0;

void loop() {
  socketIO.loop();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant