Skip to content

Sensor read data doesn't change #882

Open
@sostenesapollo

Description

@sostenesapollo

Hello, I'm connecting to a websockets server locally and trying to read a sensor, but the data keeps the same always:

[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223
[LOOP] Sending sensor reading to server... reading,4,0,223

But when I run it without the wss stuff it is working fine

#include <WiFi.h>
#include <WebSocketsClient.h>

#define USE_SERIAL Serial

const char* ssid = "TIM"; // Replace with your WiFi SSID
const char* password = "password"; // Replace with your WiFi Password
const char* websocket_server = "192.168.1.118"; // Replace with your server's IP address
const uint16_t websocket_port = 3000; // Replace with your server's port

WebSocketsClient webSocket;

unsigned long lastSensorReadTime = 0;
const unsigned long sensorReadInterval = 50; // Minimum interval for sensor reads

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
    switch(type) {
        case WStype_DISCONNECTED:
            USE_SERIAL.printf("[WSc] Disconnected!\n");
            break;
        case WStype_CONNECTED:
            USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
            webSocket.sendTXT("ping");
            break;
        case WStype_TEXT:
            USE_SERIAL.printf("[WSc] Received text: %s\n", payload);

            // Split the received text into action and pin
            char *action = strtok((char *)payload, ",");
            char *pin = strtok(NULL, ",");

            if (action != NULL && pin != NULL) {
                USE_SERIAL.printf("Action: [%s], Pin: [%s]\n", action, pin);

                // Perform the action based on the received values
                int pinNumber = atoi(pin);

                // Validate pin number (GPIO 0 to GPIO 39 are valid on most ESP32 boards)
                if (pinNumber >= 0 && pinNumber <= 39) {
                    pinMode(pinNumber, OUTPUT);

                    if (strcmp(action, "on") == 0) {
                        digitalWrite(pinNumber, HIGH);
                    } else if (strcmp(action, "off") == 0) {
                        digitalWrite(pinNumber, LOW);
                    } else {
                        USE_SERIAL.println("Invalid action");
                    }
                } else {
                    USE_SERIAL.println("Invalid pin number");
                }
            } else {
                USE_SERIAL.println("Invalid message format");
            }
            break;
    }
}

void setup() {
    USE_SERIAL.begin(115200); // Initialize serial communication
    USE_SERIAL.println("[SETUP] Booting...");

    // Initialize WebSocket client
    webSocket.begin(websocket_server, websocket_port, "/");
    webSocket.onEvent(webSocketEvent);

    USE_SERIAL.println("[SETUP] WebSocket connecting...");

    // Connect to WiFi
    WiFi.begin(ssid, password);
    USE_SERIAL.print("[SETUP] Connecting to WiFi");

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

    USE_SERIAL.println();
    USE_SERIAL.println("[SETUP] WiFi connected");
    USE_SERIAL.print("[SETUP] IP Address: ");
    USE_SERIAL.println(WiFi.localIP());
}

int countPing = 0;
int countSensor1 = 0;

void loop() {
    webSocket.loop();

    countPing++;
    if (countPing == 180000) { // Adjust this value as needed
      countPing = 0;

      USE_SERIAL.println("[LOOP] Sending ping message to server...");
      webSocket.sendTXT("ping");
    }

    unsigned long currentMillis = millis();
    if (currentMillis - lastSensorReadTime >= sensorReadInterval) {
        lastSensorReadTime = currentMillis;
        
        int sensorValue = analogRead(4);
        int percentage = map(sensorValue, 352, 637, 100, 0);
        char message[50];
        sprintf(message, "reading,4,%d,%d", sensorValue, percentage);
        
        char logMessage[100]; // Tamanho da string depende da quantidade de caracteres que você espera
        sprintf(logMessage, "[LOOP] Sending sensor reading to server... %s", message);

        USE_SERIAL.println(logMessage);
        webSocket.sendTXT(message);
    }
}

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