如何正确地使用ESP8266向IFTTT Webhook服务发出POST请求?

2024-06-16 12:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python和ESP8266(类似于Arduino)模块完全陌生。当模块打开并且成功连接到wifi时,我试图发出IFTTT webhook POST请求。但似乎我做错了什么,根本没用,我可能完全不知道,有什么我可以做的,也许可以学到新的东西?在

下面的代码是通过不断的复制和粘贴来完成的,因为我还没有使用Python的经验(只是现在?)。恕我直言,如果它看起来非常可怕和不切实际,但这正是我现在所拥有的。在

#include <IFTTTWebhook.h>
#include <ESP8266WiFi.h>

#define led LED_BUILTIN
#define ssid "ssid name"
#define password "pass"
#define IFTTT_API_KEY "key"
#define IFTTT_EVENT_NAME "your event"
//these were filled with the required data, I changed it for privacy reasons.

void setup() {
  Serial.begin(74880);

  connectToWifi();

  //just connected to Wi-Fi
  IFTTTWebhook hook(IFTTT_API_KEY, IFTTT_EVENT_NAME);
  hook.trigger();
  Serial.print("hook triggered");

  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);   
  delay(200);              
  digitalWrite(led, LOW); 
  //now sending board to sleep

  ESP.deepSleep(wakePin); 
}
void loop(){
  //if deep sleep is working, this code will never run.
  Serial.println("This shouldn't get printed");
}

void connectToWifi() {
  Serial.print("Connecting to: "); //uncomment next line to show SSID name
  Serial.print(ssid); 
  WiFi.begin(ssid, password);  
  Serial.println(" ");// print an empty line
  Serial.print("Attempting to connect: ");

  //try to connect for 10 seconds
  int i = 10;
  while(WiFi.status() != WL_CONNECTED && i >=0) {
    delay(1000);
    Serial.print(i);
    Serial.print(", ");
    i--;
  }
  Serial.println(" ");// print an empty line

  //print connection result
  if(WiFi.status() == WL_CONNECTED){
    Serial.print("Connected."); 
    Serial.println(" ");// print an empty line
    Serial.print("NodeMCU ip address: "); 
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed - check your credentials or connection");
  }
}

如果模块已经成功连接到wifi,它会自动发送webhook POST请求,我也会在手机上收到一个通知,但最终,它什么都没有。在

谢谢你抽出时间。在


Tags: 模块toanledlineserialhookwifi
1条回答
网友
1楼 · 发布于 2024-06-16 12:33:19

如果这是我的ifttwebhook库,我要收回库。别用它。我没时间保养了。我不想出版一个不安全地与IFTTT通信的库。处理HTTPS一直是一个移动的目标(有很好的理由),所以我要撤销这个库。很抱歉浪费你的时间。我建议您仔细阅读iftttapi,并直接使用ESP8266HTTPClient与它通信;这是一个相当简单的练习。在

相关问题 更多 >