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: 599
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:
  1. #include <Arduino.h>
  2. #include <WiFi.h>
  3. #include <Adafruit_Sensor.h>
  4. #include <DHT.h>
  5. #include <DHT_U.h>
  6.  
  7. // DHT sensor configuration
  8. #define DHTPIN 16     // Digital pin connected to the DHT sensor
  9. #define DHTTYPE    DHT22     // DHT 22 (AM2302)
  10.  
  11. #define PRJ_DEFAULT_STACK_SIZE 8192
  12.  
  13. // wifi credentials
  14. const char* ssid = "xxxxxxxxxxx";
  15. const char* password = "yyyyyyyyyyyyyyyy";
  16.  
  17.  
  18. void initWiFi() {
  19.   WiFi.mode(WIFI_STA);
  20.   WiFi.begin(ssid, password);
  21.   Serial.print("Connecting to WiFi ..");
  22.   while (WiFi.status() != WL_CONNECTED) {
  23.     Serial.print('.');
  24.     delay(1000);
  25.   }
  26.   Serial.println(WiFi.localIP());
  27. }
  28.  
  29. void TaskInternetManager ( void * pvParameters ){
  30.  
  31.   Serial.print("Task TaskInternetManager running on core ");
  32.   Serial.println(xPortGetCoreID());
  33.  
  34.   initWiFi();
  35.  
  36.   for(;;){
  37.     Serial.println(WiFi.localIP());
  38.     delay(10000);
  39.   }
  40. }
  41.  
  42.  
  43.  
  44.  
  45. DHT_Unified dht(DHTPIN, DHTTYPE);
  46. uint32_t delayMS;
  47.  
  48. void initSensorsManager() {
  49.     dht.begin();
  50.     // Print temperature sensor details.
  51.     sensor_t sensor;
  52.     dht.temperature().getSensor(&sensor);
  53.     Serial.println(F("------------------------------------"));
  54.     Serial.println(F("Temperature Sensor"));
  55.     Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  56.     Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  57.     Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  58.     Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  59.     Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  60.     Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  61.     Serial.println(F("------------------------------------"));
  62.     // Print humidity sensor details.
  63.     dht.humidity().getSensor(&sensor);
  64.     Serial.println(F("Humidity Sensor"));
  65.     Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  66.     Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  67.     Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  68.     Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  69.     Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  70.     Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  71.     Serial.println(F("------------------------------------"));
  72.     // Set delay between sensor readings based on sensor details.
  73.     delayMS = sensor.min_delay / 1000;
  74. }
  75.  
  76.  
  77. void sensorLoop() {
  78.   // Delay between measurements.
  79.   delay(delayMS);
  80.   // Get temperature event and print its value.
  81.   sensors_event_t event;
  82.   dht.temperature().getEvent(&event);
  83.   if (isnan(event.temperature)) {
  84.     Serial.println(F("Error reading temperature!"));
  85.   }
  86.   else {
  87.     Serial.print(F("Temperature: "));
  88.     Serial.print(event.temperature);
  89.     Serial.println(F("°C"));
  90.   }
  91.   // Get humidity event and print its value.
  92.   dht.humidity().getEvent(&event);
  93.   if (isnan(event.relative_humidity)) {
  94.     Serial.println(F("Error reading humidity!"));
  95.   }
  96.   else {
  97.     Serial.print(F("Humidity: "));
  98.     Serial.print(event.relative_humidity);
  99.     Serial.println(F("%"));
  100.   }
  101. }
  102.  
  103. void TaskSensorsManager ( void * pvParameters ){
  104.   Serial.print("Task TaskSensorsManager running on core ");
  105.   Serial.println(xPortGetCoreID());
  106.   initSensorsManager();
  107.  
  108.   for(;;){
  109.     sensorLoop();
  110.   }
  111. }
  112.  
  113.  
  114.  
  115.  
  116. TaskHandle_t TaskInternetManagerHandle;
  117. TaskHandle_t TaskSensorsManagerHandle;
  118.  
  119.  
  120. void setup() {
  121.   Serial.begin(115200);
  122.  
  123.   // Initialize device.
  124.   Serial.println(F("HELLO WORLD"));
  125.  
  126.   xTaskCreatePinnedToCore( TaskInternetManager, "TaskInternetManager", PRJ_DEFAULT_STACK_SIZE, NULL, 1, &TaskInternetManagerHandle, 0);          /* pin task to core 0 */
  127.   xTaskCreatePinnedToCore( TaskSensorsManager, "TaskSensorsManager", PRJ_DEFAULT_STACK_SIZE, NULL, 5, &TaskSensorsManagerHandle, 0);
  128.  
  129.   vTaskDelete(NULL);
  130. }
  131.  
  132. void loop() {
  133. }
  134.  
  135.  
  136.  
[/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 :!:

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_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: No registered users and 58 guests