esp32 multicore programing problem: http send only 10 times

youjunjer
Posts: 1
Joined: Sun Mar 22, 2020 6:22 am

esp32 multicore programing problem: http send only 10 times

Postby youjunjer » Sun Mar 22, 2020 7:18 am

Hi all,
I wrote some codes about parallel processing which one core is reading dht11 data and another core is sending data to thingspeak.
I use arduino IDE and using function: xTaskCreatePinnedToCore to do this job.
But the httpclient always send 10 times succeeded and get error continually.
When I use traditionally perform it in the loop, it runs well.

Code: Select all

#include <WiFi.h>
#include <HTTPClient.h>
#include <SimpleDHT.h>

char ssid[] = "ssid";
char password[] = "pws";
//
String url = "http://api.thingspeak.com/update?api_key=yourAPIKey";
int pinDHT11 = 14;

SimpleDHT11 dht11(pinDHT11);

byte temperature = 0;
byte humidity = 0;
//Task1
TaskHandle_t Task1;
int count = 1;
void senddata(void * pvParameters ) {
  //Sending to thingspeak
  for ( ;; ) {
    Serial.print("Sending via core:");
    Serial.println(xPortGetCoreID());
    Serial.println("Start sending");
    HTTPClient http;
    
    String url1 = url + "&field1=" + (int)temperature + "&field2=" + (int)humidity;
    Serial.println(url1);
    Serial.println(WiFi.status());
    //http client
    http.begin(url1);
    int httpCode = http.GET();
    Serial.println(httpCode);
    if (httpCode == HTTP_CODE_OK) {
      //payload
      String payload = http.getString();
      Serial.print("payload=");
      Serial.println(payload);
      Serial.println("count=" + String(count++));
    } else {
      //Fail
      Serial.println("Send fail");
    }
    //http.end();
    Serial.println("deleting");
    vTaskDelete(Task1);
    Serial.println("deleted");
  }

}

void setup()
{
  Serial.begin(115200);
  Serial.print("connecting SSID:");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("connected");
}

void loop()
{
  Serial.print("reading dht11 via core:");
  Serial.println(xPortGetCoreID());  
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read error="); Serial.println(err); delay(1000);
    return;
  }
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" H");

  xTaskCreatePinnedToCore(
    senddata,  /*Function*/
    "Task1",   /**/
    100000,     /**/
    NULL,      /**/
    0,         /**/
    &Task1,    /**/
    0);        /**/

  delay(20000);//20s
}

未命名 - 4.png
未命名 - 4.png (60.98 KiB) Viewed 2211 times

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

Re: esp32 multicore programing problem: http send only 10 times

Postby ESP_Sprite » Sun Mar 22, 2020 11:29 am

In your loop function, you're reading the sensor, starting up the senddata task, then waiting 20 seconds. However, the senddata task does not exit (the for loop does not have an exit condition). On the other hand, 20 seconds later *another* senddata task is started, and 20 seconds later *another* one and so forth. At some point, resources are exhausted and the thing grinds to a halt.

What you should do is start up the senddata task at the beginning (in the setup() loop) and use FreeRTOS multithreading things like queues or semaphores to communicate between the two threads. You could also remove the for (;;) loop in senddata so the thread exits, but if for some reason the senddata() code lasts longer than 20 seconds, you'll still run the risk of multiple threads 'heaping up'.

Edit: Never mind, I see that you try to delete the task after the push is succesfull... but this is a very risky setup, as Task1 only contains the *last* thread you started; in case multiple are running, the scheme will still fail.

Who is online

Users browsing this forum: No registered users and 150 guests