Issue running in two different tasks Wifi and I2C device

explorian
Posts: 4
Joined: Wed Jun 16, 2021 9:52 pm

Issue running in two different tasks Wifi and I2C device

Postby explorian » Wed Jun 16, 2021 10:12 pm

Hi,

I don't know if it is a bug, however I have this situation on ESP32-WROOM-32D.

In the setup function I start 2 separated tasks. One task manages the connection to WiFi network with the "Wifi.h" library, after the Wifi connection completed the task stay IDLE with a infinite loop of delays.
The other task manages the I2C sensor DHT22; it essentially init the sensor and query it in a infinite loop and print the temperatures in the Serial terminal. The libraries used are "#Adafruit_Sensor.h, DHT.h, DHT_U.h".

The problem in this configuration is I get a sensor reading failure after some random number of successful readings (meanly 8-10).

This problem doesn't occur if I perform the same Wifi connection and sensor reading in the same task. I also tried to bind the two tasks on different cores, but I have the same problem.

Anyone may help me to figure out what can be wrong?

Regards

mikemoy
Posts: 688
Joined: Fri Jan 12, 2018 9:10 pm

Re: Issue running in two different tasks Wifi and I2C device

Postby mikemoy » Fri Jun 18, 2021 9:00 am

Anyone may help me to figure out what can be wrong?
without posting your code, probably not.

explorian
Posts: 4
Joined: Wed Jun 16, 2021 9:52 pm

Re: Issue running in two different tasks Wifi and I2C device

Postby explorian » Fri Jun 18, 2021 3:14 pm

Sure:

Code: Untitled.cpp Select all


#include <Arduino.h>
#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

// DHT sensor configuration
#define DHTPIN 16 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)

#define PRJ_DEFAULT_STACK_SIZE 8192

// wifi credentials
const char* ssid = "xxxxxxxxxxx";
const char* password = "yyyyyyyyyyyyyyyy";


void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}

void TaskInternetManager ( void * pvParameters ){

Serial.print("Task TaskInternetManager running on core ");
Serial.println(xPortGetCoreID());

initWiFi();

for(;;){
Serial.println(WiFi.localIP());
delay(10000);
}
}




DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;

void initSensorsManager() {
dht.begin();
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
Serial.println(F("------------------------------------"));
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
}


void sensorLoop() {
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
}

void TaskSensorsManager ( void * pvParameters ){
Serial.print("Task TaskSensorsManager running on core ");
Serial.println(xPortGetCoreID());
initSensorsManager();

for(;;){
sensorLoop();
}
}




TaskHandle_t TaskInternetManagerHandle;
TaskHandle_t TaskSensorsManagerHandle;


void setup() {
Serial.begin(115200);

// Initialize device.
Serial.println(F("HELLO WORLD"));

xTaskCreatePinnedToCore( TaskInternetManager, "TaskInternetManager", PRJ_DEFAULT_STACK_SIZE, NULL, 1, &TaskInternetManagerHandle, 0); /* pin task to core 0 */
xTaskCreatePinnedToCore( TaskSensorsManager, "TaskSensorsManager", PRJ_DEFAULT_STACK_SIZE, NULL, 5, &TaskSensorsManagerHandle, 0);

vTaskDelete(NULL);
}

void loop() {
}


[/code]

explorian
Posts: 4
Joined: Wed Jun 16, 2021 9:52 pm

Re: Issue running in two different tasks Wifi and I2C device

Postby explorian » Thu Jul 15, 2021 10:56 am

Up :!:

explorian
Posts: 4
Joined: Wed Jun 16, 2021 9:52 pm

Re: Issue running in two different tasks Wifi and I2C device

Postby explorian » Thu Aug 18, 2022 6:25 pm

up :!:

Sprite
Espressif staff
Espressif staff
Posts: 10599
Joined: Thu Nov 26, 2015 4:08 am

Re: Issue running in two different tasks Wifi and I2C device

Postby Sprite » Fri Aug 19, 2022 1:14 pm

Just posting here to say that I don't see any obvious interference between the two... perhaps it's a problem in the dht driver not bein built for another task intercepting it? Possibly you could try running that task on a different core?

Who is online

Users browsing this forum: ChatGPT-User and 2 guests