-1

I am working for soil moisture and pressure sensor using the same baud-rate 115200 in ESP32. Plus, I execute both sensor using multitask ESP32 with 2 core. Core 1 and Core 2 for both program.

The parameter can be viewed through serial monitor however it can’t be passed through the spreadsheet. I am using IFTTT platform to connect the ESP32 into Spreadsheet.

Here my code :

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

TaskHandle_t Task1;
TaskHandle_t Task2;

const char * ssid = "XXXX";  
const char * password = "XXXX";  

String server = "http://maker.ifttt.com";
String eventName = "soil_pressure";
String IFTTT_Key = "XXXXXXXXXX";
String IFTTTUrl="https://maker.ifttt.com/trigger/soil_pressure/with/key/XXXXX";

int sensorPin = 2;
int sensorValueSoil;
int limit = 300; 
int sensorValuePressure;

int value1; // soil
int value2; // pressure

void setup() 
{

     Serial.begin(115200);
     pinMode(2, OUTPUT);

     WiFi.mode(WIFI_STA);
     WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED)
         {
              delay(500);
              Serial.print(".");
         }

     Serial.println("Internet Connected !!!");

     xTaskCreatePinnedToCore(
                Task1code,   /* Task function. */
                "Task1",     /* name of task. */
                10000,       /* Stack size of task */
                NULL,        /* parameter of the task */
                1,           /* priority of the task */
                &Task1,      /* Task handle to keep track of created task */
                0);          /* pin task to core 0 */                  
      delay(500); 

    
      xTaskCreatePinnedToCore(
                Task2code,   /* Task function. */
                "Task2",     /* name of task. */
                10000,       /* Stack size of task */
                NULL,        /* parameter of the task */
                1,           /* priority of the task */
                &Task2,      /* Task handle to keep track of created task */
                1);          /* pin task to core 1 */
       delay(500);  
}

void Task1code( void * pvParameters )
{
     Serial.print("Task1 running on core ");
     Serial.println(xPortGetCoreID());

     for(;;)
            {
              sensorValueSoil = analogRead(sensorPin);
              Serial.println(sensorValueSoil);

              if (sensorValueSoil<limit) 
              {
                    digitalWrite(2, HIGH); 
              }
              else {
                        digitalWrite(2, LOW); 
                    }

      delay(1000);
            }
}

void Task2code( void * pvParameters )
{
     Serial.print("Task2 running on core ");
     Serial.println(xPortGetCoreID());

     for(;;)
     {
           sensorValuePressure = analogRead(13);
           Serial.println(sensorValuePressure);
           delay(1000);
     }
}

void sendDataToSheet(void)
{
      String url = server + "/trigger/" + eventName + "/with/key/" + IFTTT_Key + "
      value1=" + String((int)value1) + "&value2="+String((int)value2);
      Serial.println(url);
      //Start to send data to IFTTT
      HTTPClient http;
      Serial.print("[HTTP] begin...\n");
      http.begin(url); //HTTP
      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();
      // httpCode will be negative on error

      if(httpCode > 0) 
            {
                 // HTTP header has been send and Server
                 response header has been handled
                 Serial.printf("[HTTP] GET... code: %d\n", httpCode);
                 // file found at server
                 if(httpCode == HTTP_CODE_OK) {
                        String payload = http.getString();
                        Serial.println(payload);
                         }
            } 
      else 
            {
                 Serial.printf("[HTTP] GET... failed, error: %s\n",
                 http.errorToString(httpCode).c_str());
            }

      http.end();
}

void loop() 
{
     value1 =  sensorValueSoil; 
     value2 =  sensorValuePressure;

     Serial.print("Values are ");
     Serial.print(value1);
     Serial.print(' ');
     Serial.print(value2);
     Serial.print(' ');

     sendDataToSheet();
     delay(5000);
}
sya
  • 45
  • 8
  • 1
    Please indent your code. Proper indentation and white space makes code much more comprehensible and will help quickly identity many issues. There are online code beautifies and formatters if you are unable to do it yourself. – romkey Dec 23 '21 at 20:31

1 Answers1

1

You need to pass WiFiClient object into HTTPClient arguments.

HTTPClient http;
WiFiClient client;

void sendDataToSheet(void) {
    String url = server + "/trigger/" + eventName + "/with/key/" + IFTTT_Key + "value1 = " + String((int)value1) + " & value2 = " + String((int)value2);
    Serial.println(url);

    //Start to send data to IFTTT
    Serial.print("[HTTP] begin...\n");
    http.begin(client, url);

    // start connection and send HTTP header
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK) {
            String payload = http.getString();
            Serial.println(payload);
        }
    } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
}
Wachid Susilo
  • 496
  • 4
  • 11
  • Thank you so much sir I will try. Another problem I found is in Taskcore1 and 2 for ESP32 that didn’t have the parameter of the task value. That Why it is unable to send to spreadsheet. However, I try to edit but found error. Haha – sya Dec 26 '21 at 13:14
  • @sya What do you mean by "didn't have the parameter of the task value"? – Wachid Susilo Dec 27 '21 at 02:08
  • At the xTaskCreatePinnedToCore( ) , the parameter of task is declared as “NULL” . I dont know how to modify the NULL. – sya Dec 27 '21 at 02:19
  • @sya You don't need to. Because your variable is global, you can access the variable anywhere. – Wachid Susilo Dec 27 '21 at 02:54
  • Okey Sir, I will try. Thank you so much for your help – sya Dec 27 '21 at 04:01