问题描述
我正在尝试设置 ESP8266 ESP-01 WiFi 模块,以便在发送一些传感器数据时发布推文。目前,我只是在测试 POST > Webhooks > IFTTT > Twitter 工作流程。
当我运行以下代码时,我在串行监视器中得到了 -1(负值)的响应代码。一些注意事项:
- 我正在使用 ESP-01 插入的 USB 编程器来完成所有这些(称为 ESP-01S USB 转 ESP8266 ESP-01S 无线 Wifi 适配器模块 Wi-Fi CH340G 4.5-5.5V,115200 波特率,如果你想看看具体的),所以不要像 arduino uno 或任何东西一样运行 ESP-01,如果这很重要
- 我知道草图上传工作正常,脚本运行良好,因为 ESP-01 报告了它与我的网络的连接,而我的 Wifi 网络报告了与设备的连接
- 我知道触发器 url 有效,因为当我从 IFTTT Webhooks 服务中运行它时以及当我从终端“卷曲”它时它会发布推文
- 我尝试改用 GET,但它有其自身的问题,而且我想发送传感器值,因此希望 POST 工作
- 我尝试了下面的 urlencoded 和 json 版本,结果相同。更喜欢未注释的 json 版本,因为我对此很熟悉
- 我是一个完全的 POST 初学者,所以我可能遗漏了一些愚蠢的东西
- 它说 timerDelay 是 10 秒,但设置为 20。那是因为我认为 POST 可能只是超时了?所以我给了它更多的时间
- 根据某些 Arduino 论坛记录,我还在 Arduino IDE 上回滚了 ESP8266 库。没有帮助。
- 我曾尝试用 NodeMCU 刷入 ESP-01,但什么也没做。但不确定是否必须在每次上传新草图之前完成?
这是我的代码。触发器是真实的,所以请随意输入您自己的 SECRET_KEY 并发布应用程序。在 Twitter 上向 @KreiderPlants 发帖。
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "MY_SSID";
const char* password = "SSID_PW";
const char* serverName = "https://maker.ifttt.com/trigger/tweet_from_esp/with/key/SECRET_KEY";
// const char* serverName = "maker.ifttt.com";
// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application,check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Set timer to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Timer set to 10 seconds (10000)
unsigned long timerDelay = 20000;
void setup()
{
Serial.begin(115200);
delay(100);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable),it will take 10 seconds before publishing the first reading.");
// Random seed is a number used to initialize a pseudorandom number generator
randomSeed(analogRead(0));
}
int value = 0;
void loop() {
//Send an HTTP POST request every 10 seconds
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
// http.addHeader("Content-Type","application/x-www-form-urlencoded");
// Data to send with HTTP POST
// String httpRequestData = "value1=" + String(random(2)) + "&value2=" + String(random(2))+ "&value3=" + String(random(2));
// Send HTTP POST request
// int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: application/json,use the following:
http.addHeader("Content-Type","application/json");
// JSON data to send with HTTP POST
String httpRequestData = "{\"value1\":\"" + String(random(4)) + "\",\"value2\":\"" + String(random(4)) + "\",\"value3\":\"" + String(random(4)) + "\"}";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi disconnected");
}
lastTime = millis();
}
}
解决方法
感谢@hcheung 在评论中帮助我解决了这个问题。我正在使用 http 向 https 站点发送 POST 请求。
我从 http 地址中删除了 's' 并且它起作用了。如果其他人偶然发现并需要真正建立安全连接,请查看评论中的 github 链接。