Issue with integrating sensor + WIFI
Posted: Wed Dec 27, 2023 4:37 pm
Hello. I've came across an issue whiel trying to connect my sensor with my esp32s board.
Code:
First of all, the issue im facing is that the sensor occasionally reads the maximum value for a signed 32-bit integer which indicates an error. Ive tried altering the delay - Even to 5000 but that doesnt seem to be the case. Nevertheless, when i removed the WiFi connecting code (CHECK BELOW), it worked without any issues - even in 500s read rate. I acknowledge that i am allocating enough memory for the task and etc but i dont seem to understand this behaviour. I would love for some help.
Regards,
Aggelos.
Wifi Code:
Console Output:
Code:
- #include <Arduino.h>
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- #include <dht.h>
- #include <HTTPClient.h>
- #include <WiFi.h>
- #include <WiFiMulti.h>
- #include <WiFiClientSecure.h>
- #include <WebSocketsClient.h>
- #include <ArduinoJson.h>
- HTTPClient http;
- WiFiMulti WiFiMulti;
- WebSocketsClient webSocket;
- TaskHandle_t readerHandle;
- namespace JSON {
- class serializer {
- public:
- static String serializeRequestData(const char* key1, int val1, const char* key2, int val2);
- };
- class deserializer {
- public:
- static DynamicJsonDocument deserializeData(const char* input);
- };
- }
- DynamicJsonDocument JSON::deserializer::deserializeData(const char* input) {
- DynamicJsonDocument doc(256);
- DeserializationError error = deserializeJson(doc, input);
- if (error) {
- Serial.print("deserializeJson() failed: ");
- Serial.println(error.c_str());
- }
- return doc;
- };
- String JSON::serializer::serializeRequestData(const char* key1, int val1, const char* key2, int val2) {
- DynamicJsonDocument doc(256);
- String JSONData;
- if(key1 && val1) {
- doc[key1] = val1;
- }
- if(key2 && val2) {
- doc[key2] = val2;
- }
- serializeJson(doc, JSONData);
- return JSONData;
- }
- using namespace JSON;
- DHT dht(26, DHT11);
- [[noreturn]] void reader(void *pvParameters) {
- while (true) {
- if (WiFiClass::status() == WL_CONNECTED) {
- int hum = dht.readTemperature();
- int temp = dht.readTemperature();
- Serial.printf("Task 1 Reads %d\n", hum);
- }
- }
- }
- void setup() {
- xTaskCreate(reader, "reader", 4048, nullptr, 1, &readerHandle);
- Serial.begin(9600);
- pinMode(2, OUTPUT);
- pinMode(26, OUTPUT); // DHT 11
- dht.begin();
- WiFi.mode(WIFI_AP_STA);
- WiFi.enableLongRange(true);
- WiFi.softAP("ESP-32-Server", "12345678910");
- WiFi.begin("ssid", "pass");
- while (WiFiClass::status() != WL_CONNECTED) {
- Serial.println("Connecting..");
- delay(500);
- }
- Serial.printf("[WS]: Connected to %s\n", WiFi.SSID().c_str());
- }
- void loop() {
- }
First of all, the issue im facing is that the sensor occasionally reads the maximum value for a signed 32-bit integer which indicates an error. Ive tried altering the delay - Even to 5000 but that doesnt seem to be the case. Nevertheless, when i removed the WiFi connecting code (CHECK BELOW), it worked without any issues - even in 500s read rate. I acknowledge that i am allocating enough memory for the task and etc but i dont seem to understand this behaviour. I would love for some help.
Regards,
Aggelos.
Wifi Code:
- WiFi.mode(WIFI_AP_STA);
- WiFi.enableLongRange(true);
- WiFi.softAP("ESP-32-Server", "12345678910");
- WiFi.begin("ssid", "pass");
- while (WiFiClass::status() != WL_CONNECTED) {
- Serial.println("Connecting..");
- delay(500);
- }
- Task 1 Reads 25
- Task 1 Reads 2147483647
- Task 1 Reads 2147483647
- Task 1 Reads 2147483647
- Task 1 Reads 2147483647
- Task 1 Reads 25
- Task 1 Reads 25
- Task 1 Reads 25
- Task 1 Reads 25