为了描述我的问题,,,我试图将我的Arduino UNO连接到由我在Heroku中创建的网站。
的主要目的是在中调用rest 函数,连接到Internet并获得json数据。
我的Arduino密码:
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
void setup() {
// Initialize Serial port
Serial.begin(9600);
while (!Serial) continue;
// Initialize Ethernet library
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
Ethernet.init(8); // use pin 53 for Ethernet CS
if (!Ethernet.begin(mac)) {
Serial.println(F("Failed to configure Ethernet"));
return;
}
delay(1000);
Serial.println(F("Connecting..."));
// Connect to HTTP server
EthernetClient client;
client.setTimeout(10000);
if (!client.connect("https://salty-cliffs-06856.herokuapp.com", 80)) {
Serial.println(F("Connection failed"));
return;
}
Serial.println(F("Connected!"));
// Send HTTP request
client.println(F("GET /api/command/ HTTP/1.1"));
client.println(F("Host: https://salty-cliffs-06856.herokuapp.com"));
client.println(F("Connection: close"));
Serial.println(F("Done"));
if (client.println() == 0) {
Serial.println(F("Failed to send request"));
return;
}
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
Serial.println(status);
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
return;
}
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(client);
if (!root.success()) {
Serial.println(F("Parsing failed!"));
return;
}
// Extract values
Serial.println(F("Response:"));
Serial.println(root["command"].as<char*>());
// Disconnect
client.stop();当我尝试将非安全的HTTP地址放在那里时,这段代码一切正常。在把我的网络,由Heroku驱动,由HTTPS 隔离后,我总是得到错误。
当我正在检查HTTP 和在我的Arduino端口终端中得到响应时,程序取消了错误:
Unexpected response: HTTP/1.1 400 Bad Request我检查了我的heroku日志,但是没有列出来自Arduino的任何请求。(为了确保我试图从web浏览器调用API,它可以工作)
你能帮我一下哪里有问题吗?我在想这可能是因为安全的HTTPS。你觉得呢?
谢谢您的帮助:)
发布于 2020-05-03 13:52:38
首先,将client.connect("https://salty-cliffs-06856.herokuapp.com", 80)改为
`https` 至
`http` 由于端口80不是https端口,所以以太网屏蔽不支持SSL。
其次,Host的http头错误。HTTP1.1要求只使用没有协议使用的域名(即http://)前缀)。因此,改变行文:
client.println(F("Host: https://salty-cliffs-06856.herokuapp.com"));至:
client.println(F("Host: salty-cliffs-06856.herokuapp.com"));https://stackoverflow.com/questions/61567425
复制相似问题