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
}