Issue with integrating sensor + WIFI

AggelosK
Posts: 5
Joined: Wed Dec 27, 2023 4:24 pm

Issue with integrating sensor + WIFI

Postby AggelosK » 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:
  1. #include <Arduino.h>
  2. #include <freertos/FreeRTOS.h>
  3. #include <freertos/task.h>
  4. #include <dht.h>
  5. #include <HTTPClient.h>
  6. #include <WiFi.h>
  7. #include <WiFiMulti.h>
  8. #include <WiFiClientSecure.h>
  9. #include <WebSocketsClient.h>
  10. #include <ArduinoJson.h>
  11.  
  12. HTTPClient http;
  13.  
  14.  
  15. WiFiMulti WiFiMulti;
  16.  
  17. WebSocketsClient webSocket;
  18. TaskHandle_t readerHandle;
  19.  
  20. namespace JSON {
  21.     class serializer {
  22.     public:
  23.         static String serializeRequestData(const char* key1, int val1, const char* key2, int val2);
  24.     };
  25.     class deserializer {
  26.     public:
  27.         static DynamicJsonDocument deserializeData(const char* input);
  28.     };
  29. }
  30.  
  31. DynamicJsonDocument JSON::deserializer::deserializeData(const char* input) {
  32.     DynamicJsonDocument doc(256);
  33.     DeserializationError error = deserializeJson(doc, input);
  34.     if (error) {
  35.         Serial.print("deserializeJson() failed: ");
  36.         Serial.println(error.c_str());
  37.     }
  38.     return doc;
  39. };
  40.  
  41. String JSON::serializer::serializeRequestData(const char* key1, int val1, const char* key2, int val2) {
  42.     DynamicJsonDocument doc(256);
  43.     String JSONData;
  44.     if(key1 && val1) {
  45.         doc[key1] = val1;
  46.     }
  47.  
  48.     if(key2 && val2) {
  49.         doc[key2] = val2;
  50.     }
  51.     serializeJson(doc, JSONData);
  52.     return JSONData;
  53. }
  54.  
  55. using namespace JSON;
  56.  
  57. DHT dht(26, DHT11);
  58.  
  59.  
  60. [[noreturn]] void reader(void *pvParameters) {
  61.     while (true) {
  62.         if (WiFiClass::status() == WL_CONNECTED) {
  63.             int hum = dht.readTemperature();
  64.             int temp = dht.readTemperature();
  65.             Serial.printf("Task 1 Reads %d\n", hum);
  66.         }
  67.     }
  68. }
  69.  
  70.  
  71. void setup() {
  72.     xTaskCreate(reader, "reader", 4048, nullptr, 1, &readerHandle);
  73.     Serial.begin(9600);
  74.     pinMode(2, OUTPUT);
  75.     pinMode(26, OUTPUT); // DHT 11
  76.     dht.begin();
  77.     WiFi.mode(WIFI_AP_STA);
  78.     WiFi.enableLongRange(true);
  79.     WiFi.softAP("ESP-32-Server", "12345678910");
  80.     WiFi.begin("ssid", "pass");
  81.  
  82.     while (WiFiClass::status() != WL_CONNECTED) {
  83.         Serial.println("Connecting..");
  84.         delay(500);
  85.     }
  86.     Serial.printf("[WS]: Connected to %s\n", WiFi.SSID().c_str());
  87.  
  88. }
  89.  
  90. void loop() {
  91. }
  92.  

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:
  1.  WiFi.mode(WIFI_AP_STA);
  2.     WiFi.enableLongRange(true);
  3.     WiFi.softAP("ESP-32-Server", "12345678910");
  4.     WiFi.begin("ssid", "pass");
  5.  
  6.     while (WiFiClass::status() != WL_CONNECTED) {
  7.         Serial.println("Connecting..");
  8.         delay(500);
  9.     }
Console Output:
  1. Task 1 Reads 25
  2. Task 1 Reads 2147483647
  3. Task 1 Reads 2147483647
  4. Task 1 Reads 2147483647
  5. Task 1 Reads 2147483647
  6. Task 1 Reads 25
  7. Task 1 Reads 25
  8. Task 1 Reads 25
  9. Task 1 Reads 25

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Issue with integrating sensor + WIFI

Postby lbernstone » Wed Dec 27, 2023 7:18 pm

Put dht.begin at the start of your reader function. Don't manually set the pinMode on that pin. Put a delay at the end of the while loop in reader.
If it still has problems, then we can troubleshoot.

AggelosK
Posts: 5
Joined: Wed Dec 27, 2023 4:24 pm

Re: Issue with integrating sensor + WIFI

Postby AggelosK » Wed Dec 27, 2023 7:44 pm

Thank you for your reply. However, ive tried your solution and im still getting the same output.

Removed the pinMode setting for the DHT pin,

Code altered:
  1. [[noreturn]] void reader(void *pvParameters) {
  2.     dht.begin(); //Changed location of the begin statement
  3.     while (true) {
  4.         if (WiFiClass::status() == WL_CONNECTED) {
  5.             int hum = dht.readTemperature();
  6.             int temp = dht.readTemperature();
  7.             Serial.printf("Task 1 Reads %d\n", hum);
  8.             vTaskDelay(pdMS_TO_TICKS(1000)); //should be rougly 1 second - Enough time
  9.         }
  10.     }
  11. }
  12.  
  13. // . . .
  14.  

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Issue with integrating sensor + WIFI

Postby lbernstone » Thu Dec 28, 2023 3:49 pm

delay should be at the end of the while loop, not in the if. You need a delay on every pass here or else you are starving other processes (eg WiFi).
Do you actually need this to run continuously? The DHT is a slow device, and if it is unable to come up with a response, it may be giving you those negative numbers as an indication that it is timing out. Run it on a reasonable cycle that matches up with how frequently you will use the data.
I don't know much about the Adafruit library. I use UncleRus' drivers which are esp32 optimized, and allow you to pull both pieces of data in one query of the dht. It will give a clear error if it is unable to get a response from the dht.

AggelosK
Posts: 5
Joined: Wed Dec 27, 2023 4:24 pm

Re: Issue with integrating sensor + WIFI

Postby AggelosK » Sat Dec 30, 2023 7:48 pm

Thank you for your response.

I intended to also add a vTaskDelay(pdMS_TO_TICKS(1000)) in the original code hwoever it still kept eventually showing wrong values. Positioning the taskcreation in the end still did not fix the issue. Nevertheless, i will take a look at the attached link.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Issue with integrating sensor + WIFI

Postby lbernstone » Mon Jan 01, 2024 12:14 am

I will recommend testing on https://wokwi.com . It lets you separate software from hardware issues more easily, and then you can share your example easily when you get a reproducible issue.

AggelosK
Posts: 5
Joined: Wed Dec 27, 2023 4:24 pm

Re: Issue with integrating sensor + WIFI

Postby AggelosK » Tue Jan 02, 2024 11:57 am

Thank you for your response.

I tried it but since it didnt have support for DHT11, i tried DHT22 and it worked perfectly. In the meantime, i tried with a different dht11 sensor however they both malfunctioned and produced the same wrong output. Should i consider that the sensor is way too slow and dht22 isnt?

Project: https://wokwi.com/projects/385810614370936833

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Issue with integrating sensor + WIFI

Postby lbernstone » Fri Jan 12, 2024 8:31 pm

A DHT22 is a thing closer to a USB power adapter than a Intel Core i5. They are made by many companies in China from a reference model, so you don't really know quite what you are getting in the package. Some will work quite perfectly, and may even have a brand name and a lot number on them. Some will be cheap junk, scraped off an old board and resold.

AggelosK
Posts: 5
Joined: Wed Dec 27, 2023 4:24 pm

Re: Issue with integrating sensor + WIFI

Postby AggelosK » Mon Jan 22, 2024 6:34 pm

Yeah, true. Well m not sure what else do i have as a solution.


Pardon for my inactivity, i got a lot of stuff to accomplish.

Who is online

Users browsing this forum: No registered users and 96 guests