如何解决来自 ESP8266 的 Firebase Cloud Function Post 请求
我正在尝试从 ESP8266 调用 firebase 云函数,它从邮递员那里工作正常,但我在 esp 中收到了 http 响应代码 -1。下面是代码,谁能帮我找出我的错误。谢谢
void loop() {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
Serial.println("Still Connected !");
WiFiClientSecure client;
HTTPClient http;
http.begin(client,"https://us-central1-firedetectionapi.cloudfunctions.net/status"); //Specify destination for HTTP request
http.addHeader("Content-Type","application/json"); //Specify content-type header
int httpResponseCode = http.POST("{\"F\":\"T\"}"); //Send the actual POST request
if(httpResponseCode>0){
Serial.println("API Called"); //Print return code
}else{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); //Free resources
}else{
Serial.println("Error in WiFi connection");
}
解决方法
Arduino HTTP 客户端在使用 HTTPS 协议时需要服务器证书的指纹/哈希。
这可以通过以下方式设置:
client.setFingerprint("81:dc:88:59:f1:fd:3b:f2:4a:27:c6:ba:39:44:3c:1c:16:4f:9c:ae");
http.begin(client,"https://us-central1-firedetectionapi.cloudfunctions.net/status");
,
我也遇到了同样的问题,但我想我已经解决了。
在您的云函数 URL 中,去掉 /status(我们稍后会重新添加)。
另外,在 http.begin() 行之前设置这个
client.setInsecure();
现在,它将连接。你还没有完成。在 http.begin() 之后,这样写:
http.setURL("/status");
现在你应该很好。乐于助人
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。