我尝试使用 Sachin Bhangale 的 IFTTT 和 esp8266 对物联网开门通知进行编码。但是当我上传代码时出现错误。我已经尝试过 (Error: call to 'HTTPClient::begin' declared with attribute error: obsolete API, use ::begin(WiFiClient, url)) 但我仍然遇到同样的错误。希望你能帮助我。
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define REED_SWITCH 5 //D1
int status = WL_IDLE_STATUS; //not required.
const char* ssid = "AndroidAP3DEC";
const char* password = "12345678";
int doorClosed = 1;
void setup() {
pinMode(REED_SWITCH, INPUT_PULLUP);
Serial.begin(115200);
setupWifi();
//get_http();
}
void setupWifi()
{
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.mode(WIFI_STA);
status = WiFi.begin(ssid, password);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to wifi");
}
void loop() {
if (WiFi.status() != WL_CONNECTED)
{
setupWifi();
}
// put your main code here, to run repeatedly:
if ((digitalRead(REED_SWITCH) == HIGH) && (doorClosed == 1))
{
Serial.println("DOOR OPEN!!");
while (get_http(String("DOOR_OPEN_")) != 0);
doorClosed = 0;
}
else if ((digitalRead(REED_SWITCH) == LOW) && (doorClosed == 0))
{
Serial.println("DOOR CLOSED!!");
while (get_http(String("DOOR_CLOSED_")) != 0);
doorClosed = 1;
}
delay(10);
}
int get_http(String state)
{
HTTPClient http;
int ret = 0;
Serial.print("[HTTP] begin...\n");
// configure ifttt server and url should be HTTP only..not https!!! (http://)
http.begin("http://maker.ifttt.com/trigger/door/json/with/key/"); //HTTP
//If you have enabled value1 from iftt settings then uncomment below line to send value and comment out above line
//http.begin("http://maker.ifttt.com/trigger/door/with/key/your_key_from_Iftt/?value1="+state); //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);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
ret = -1;
Serial.printf("[HTTP] GET failed, error: %s\n", http.errorToString(httpCode).c_str());
delay(500); // wait for half sec before retry again
}
http.end();
return ret;
}
C:\Users\User\AppData\Local\Temp.arduinoIDE-unsaved2023328-9108-1ek0ww1.q0a7\sketch_apr28a\sketch_apr28a.ino: 在函数 'int get_http(String)' 中: C:\Users\User\AppData\Local\Temp.arduinoIDE-unsaved2023328-9108-1ek0ww1.q0a7\sketch_apr28a\sketch_apr28a.ino:69:15: 错误:调用'HTTPClient::begin' 声明为属性错误:过时的 API , 使用 ::begin(WiFiClient, url) 69 | http.begin("http://maker.ifttt.com/trigger/door/json/with/key/"); //HTTP协议 | ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
退出状态 1
编译错误:调用'HTTPClient::begin'声明为属性错误:过时的API,使用::begin(WiFiClient, url)
编译错误:调用'HTTPClient::begin'声明为属性错误:过时的API,使用::begin(WiFiClient, url)