【问题标题】:ESP32 Multitask Parameter pass through SpreadsheetESP32 多任务参数通过电子表格
【发布时间】:2022-01-24 17:26:30
【问题描述】:

我正在为土壤湿度和压力传感器工作,在 ESP32 中使用相同的波特率 115200。另外,我使用 2 核的多任务 ESP32 执行这两个传感器。两个程序的核心 1 和核心 2。

参数可以通过串口监视器查看,但不能通过电子表格传递。我正在使用 IFTTT 平台将 ESP32 连接到电子表格。

这是我的代码:

#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);
}

【问题讨论】:

  • 请缩进您的代码。适当的缩进和空白使代码更易于理解,并有助于快速识别许多问题。如果你自己做不到,有在线代码美化和格式化。

标签: spreadsheet esp32 multitasking arduino-c++ arduino-esp32


【解决方案1】:

您需要将WiFiClient 对象传递给HTTPClient 参数。

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();
}

【讨论】:

  • 非常感谢先生,我会努力的。我发现的另一个问题是在没有任务值参数的 ESP32 的 Taskcore1 和 2 中。那为什么它无法发送到电子表格。但是,我尝试编辑但发现错误。哈哈
  • @sya “没有任务值的参数”是什么意思?
  • 在 xTaskCreatePinnedToCore( ) 中,task 的参数被声明为“NULL”。我不知道如何修改NULL。
  • @sya 你不需要。因为您的变量是全局变量,所以您可以在任何地方访问该变量。
  • 好的,先生,我会试试的。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 1970-01-01
  • 2017-01-23
相关资源
最近更新 更多