简体   繁体   English

来自 ESP8266 的 Firebase 云功能 POST HTTPS 请求

[英]Firebase cloud function POST HTTPS Request from ESP8266

I want to connect to an url of a cloud function but isn´t working.我想连接到云功能的 URL,但无法正常工作。 someone can see the error?有人可以看到错误吗? I´m working with an ESP8266 from arduino IDE.我正在使用来自 arduino IDE 的 ESP8266。 I try sending a POST request by postman and it function perfectly, I don´t understan what is the problem with the conection, i always try to connect with out the https and only with http and again in the esp8266 didn´t wotk but in postman does works fine, I do not understant why is this problem我尝试通过邮递员发送 POST 请求并且它运行完美,我不明白连接有什么问题,我总是尝试连接 https 并且只连接 http 并且在 esp8266 中再次没有 wotk 但在邮递员工作正常,我不明白为什么会出现这个问题

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>


const char* ssid     = "My wifi";
const char* password = "My Password";

const char* host = "https://us-central1-sensores4bad6.cloudfunctions.net/actualizar";

void setup() {
  Serial.begin(9600);
  delay(10);
  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());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  WiFiClientSecure client;
  const int httpPort = 443;
  if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
     return;
  }



 client.print(String("POST ") + host + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

Try removing the s in https .尝试删除https中的s So:所以:

const char* host = "http://us-central1-sensores4bad6.cloudfunctions.net/actualizar";

instead of代替

const char* host = "https://us-central1-sensores4bad6.cloudfunctions.net/actualizar";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM