问题描述
当我在浏览器中键入 serverName 时,它将返回一个json对象。但是在这里,它 Serial.print(payload)返回-1。 我该如何解决?
void loop() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http; //Declare an object of class HTTPClient
http.begin(serverName); //Specify request destination
int httpCode = http.GET();
//Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
}
delay(10000); //Send a request every 30 seconds
}
解决方法
使用http代替https。
如果要使用https,则必须指定服务器证书的SHA1指纹。如何做到这一点很容易谷歌。
某些库将允许使用client->setInsecure();
之类的东西,但是我不会去那里,因为很不安全。