首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Arduino连接到Heroku网站连接到Django数据库

问将Arduino连接到Heroku网站连接到Django数据库
EN

Stack Overflow用户
提问于 2020-05-02 22:59:35
回答 1查看 438关注 0票数 0

为了描述我的问题,,,我试图将我的Arduino UNO连接到由我在Heroku中创建的网站。

的主要目的是在中调用rest 函数,连接到Internet并获得json数据。

我的Arduino密码:

代码语言:javascript
复制
#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端口终端中得到响应时,程序取消了错误:

代码语言:javascript
复制
Unexpected response: HTTP/1.1 400 Bad Request

我检查了我的heroku日志,但是没有列出来自Arduino的任何请求。(为了确保我试图从web浏览器调用API,它可以工作)

你能帮我一下哪里有问题吗?我在想这可能是因为安全的HTTPS。你觉得呢?

谢谢您的帮助:)

EN

回答 1

Stack Overflow用户

发布于 2020-05-03 13:52:38

首先,将client.connect("https://salty-cliffs-06856.herokuapp.com", 80)改为

代码语言:javascript
复制
`https` 

至

代码语言:javascript
复制
`http` 

由于端口80不是https端口,所以以太网屏蔽不支持SSL。

其次,Host的http头错误。HTTP1.1要求只使用没有协议使用的域名(即http://)前缀)。因此,改变行文:

代码语言:javascript
复制
client.println(F("Host: https://salty-cliffs-06856.herokuapp.com"));

至:

代码语言:javascript
复制
client.println(F("Host: salty-cliffs-06856.herokuapp.com"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61567425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档