Skip to content

Help needed:  #890

Open
Open
@NielsvanDijk

Description

@NielsvanDijk

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();
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions