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

Sensor read data doesn't change #882

Open
sostenesapollo opened this issue May 22, 2024 · 0 comments
Open

Sensor read data doesn't change #882

sostenesapollo opened this issue May 22, 2024 · 0 comments

Comments

@sostenesapollo
Copy link

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

@sostenesapollo sostenesapollo changed the title Sensor read data doesn't changes Sensor read data doesn't change May 22, 2024
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