[英]Problem sending message with my ESP8266 using IFTTT
我正在嘗試制作一個簡單的項目,其中 esp8266 使用 IFTTT 向我的手機發送短信。 我已經測試了我的 IFTTT 小程序/食譜,它運行良好。 我認為這可能是我的 wifi 的連接問題,我發現這也不是問題。 所以我檢查了與主機的連接問題,這似乎是問題所在。 它根據這段代碼打印連接失敗:
if (.client,connect(host, httpsPort)) {
Serial.println("連接失敗");
返回;
有人可以幫我解決這個問題。 謝謝
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "ssid";
const char* password = "pass";
const char* host = "maker.ifttt.com";
const int httpsPort = 443;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
WiFiClientSecure client;
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/ESP8266/json/with/key/xxxxxxxxxx";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
}
void loop() {
}```
[1]: https://i.stack.imgur.com/RkQqL.png
您必須等待開發板連接到您的 wifi 網絡,然后才能發起 http 呼叫。
以下是連接 wifi 的示例:
#define WIFI_TIMEOUT 5000
#define WIFI_DEBUG true
/*
This function will wait for a wifi to connect.
If the connection takes more then WIFI_TIMEOUT it will return
false, otherwise returns true.
*/
boolean connectWifi(){
WiFi.begin(ssid, password);
long wifiConnStartMS = millis();
#if WIFI_DEBUG
Serial.printf("\nConnecting to: %s...",ssid);
#endif
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
if( millis() - wifiConnStartMS >= WIFI_TIMEOUT ){
#if WIFI_DEBUG
Serial.println("\nTimeout on wifi connection. Wrong credentials?");
#endif
return false;
}
}
#if WIFI_DEBUG
Serial.printf("\nWiFi Connected to: %s\n",ssid);
#endif
return true;
}
void setup(){
Serial.begin(115200);
if( connectWifi() ){
// Begin the call after that...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.