We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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); } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Hello, I'm connecting to a websockets server locally and trying to read a sensor, but the data keeps the same always:
But when I run it without the wss stuff it is working fine
The text was updated successfully, but these errors were encountered: