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:

Code: main.cpp Select all


#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:

Code: wifi-Code.cpp Select all


 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);
}
Console Output:

Code: Output.txt Select all


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

lbernstone
Posts: 1131
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:

Code: main.cpp Select all


[[noreturn]] void reader(void *pvParameters) {
dht.begin(); //Changed location of the begin statement
while (true) {
if (WiFiClass::status() == WL_CONNECTED) {
int hum = dht.readTemperature();
int temp = dht.readTemperature();
Serial.printf("Task 1 Reads %d\n", hum);
vTaskDelay(pdMS_TO_TICKS(1000)); //should be rougly 1 second - Enough time
}
}
}

// . . .

lbernstone
Posts: 1131
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: 1131
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: 1131
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 2 guests