如何使用 ESP8266 向 Heroku API 发送获取或发布请求?

时间:2021-06-03 20:02:43

标签: node.js heroku arduino-esp8266

目前我已经使用 nodejs 构建了一个简单的 api 并将其部署在 heroku 中。 Heroku 是一个安全的网站,所以我在我的 nodemcu 程序中使用了 WiFiClientSecure.h 来连接到 api 。但它返回这样的错误。

connecting to WIFI
....
WiFi connected
IP address: 
xxxxxxxxxxx
connecting to api71.herokuapp.com


 CUT HERE FOR EXCEPTION DECODER ---------------

Exception (9):
epc1=0x40207acc epc2=0x00000000 epc3=0x00000000 excvaddr=0xfeefeffe depc=0x00000000
>>>stack>>>

ctx: cont
sp: 3ffffc80 end: 3fffffc0 offset: 0190
3ffffe10:  3ffe87d5 3fff11bc 3ffffea0 40203b53  
3ffffe20:  00000124 3ffee784 00000001 40205b14  
3ffffe30:  00000000 00000000 00000000 4020610e  
3ffffe40:  00000000 3ffffea0 3ffef52c 4020281c  
3ffffe50:  3ffee680 00000d50 3ffe86fc 3ffe862b  
3ffffe60:  000001bb 3ffffea0 3ffe87d5 3ffe862b  
3ffffe70:  000001bb 3ffffea0 3ffe87d5 40203d29  
3ffffe80:  40208078 54cac522 40208078 54cac522  
3ffffe90:  3ffe84cc 3ffe84cc 3ffee6a0 40201150  
3ffffea0:  40207f18 00000000 00003a98 feefeffe  
3ffffeb0:  00000000 3ffef52c 3fff11bc 3ffef66c  
3ffffec0:  00000000 00000000 3fff11bc 3fff62c4  
3ffffed0:  3ffef75c 00000000 00000000 00000000  
3ffffee0:  00000000 3fff1f14 3ffef57c 3fff6064  
3ffffef0:  3ffef594 00000000 00000000 00000000  
3fffff00:  00004145 00000255 feef0000 00000000  
3fffff10:  00000000 00000000 00000000 00000000  
3fffff20:  00000000 00000000 feef0000 feefeffe  
3fffff30:  feefeffe feefeffe feefeffe fe00effe  
3fffff40:  00000000 feefeffe 00000000 00000000  
3fffff50:  feefef00 00000000 00000000 feefeffe  
3fffff60:  feefeffe feefeffe feefeffe feefeffe  
3fffff70:  feefeffe feefeffe feefeffe feefeffe  
3fffff80:  feefeffe feefeffe feefeffe feefeffe  
3fffff90:  feefeffe feefeffe feefeffe 3ffee728  
3fffffa0:  3fffdad0 00000000 3ffee6e8 40205c24  
3fffffb0:  feefeffe feefeffe 3ffe84f0 40100b85  
<<<stack<<<

我已经在 localhost 中检查了我的 api,它工作正常。与安全网站交互存在问题。 Heroku 使用 sha256RSA 签名算法和 sha256 作为签名哈希。 这是我部署的代码

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

const char * ssid = "YOURNETWORK"; 
const char * password = "YOURPASSWORD"; 
const char * host = "api71.herokuapp.com"; 
String path = "/api/courses"; // Path of Server
const int httpsPort = 443; // HTTPS PORT (default: 443)
int refreshtime = 15; // Make new HTTPS request after x seconds


String datarx; // Received data as string
long crontimer;

void setup () {
  delay (1000);
  Serial.begin (115200);
  WiFi.mode (WIFI_OFF);
  delay (1000);
  WiFi.mode (WIFI_STA);
  WiFi.begin (ssid, password);
  Serial.print ("Connecting");
  while (WiFi.status ()! = WL_CONNECTED) {
    delay (500);
    Serial.print (".");
  }
  Serial.print ("Connected:");
  Serial.println (ssid);
}

void loop () {
  if (crontimer <millis () / 1000) {
    crontimer = (millis () / 1000) + refreshtime;
    callhttps (); //
  }
}

void callhttps () {
  WiFiClientSecure httpsClient;
  httpsClient.setTimeout (15000);
  delay (1000);
  int retry = 0;
  while ((! httpsClient.connect (host, httpsPort)) && (retry <15)) {
    delay (100);
    Serial.print (".");
    retry ++;
  }
  if (retry == 15) {
    Serial.println ("Connection failed");
  }
  else {
    Serial.println ("Connected to Server");
  }
  httpsClient.print (String ("GET") + path + 
                    "HTTP / 1.1 \ r \ n" +
                    "Host:" + host +
                    "\ r \ n" + "Connection: close \ r \ n \ r \ n");
  while (httpsClient.connected ()) {
    String line = httpsClient.readStringUntil ('\ n');
    if (line == "\ r") {
      break;
    }
  }
  while (httpsClient.available ()) {
    datarx + = httpsClient.readStringUntil ('\ n');
  }
  Serial.println (datarx);
  datarx = "";
}

为什么要连接到这个网站?请帮帮我。

1 个答案:

答案 0 :(得分:0)

我使用 http 解决了这个问题。 http://domain.herokuapp.com/api

另一点是... ESP8266 发送“开始”数据进行发布。 funcition.POST() 只是发送在 begin http 函数中声明的值。

使用“std::unique_ptrBearSSL::WiFiClientSecureclient(new BearSSL::WiFiClientSecure);”你失去了连接,因为这个库使用了与 heroku 不同的 RSHA 密钥。

在开始之前发送数据,您收到此消息是因为 Arduino 在内存中发送了对象,而不是连接 url。

“切这里用于异常解码器 ---------------”。

如果您的服务有同样的错误,您会收到消息:连接失败。

HttpClient 和 WifiClient 循环声明,导致内存泄漏和崩溃。

我得出的结论是。

#include //ESP8266 核心 WiFi 库
#include

查看部分代码:

HTTPClient https;    //Declare object of class HTTPClient
WiFiClient client;   // Declare out of loop like a global variable


https.begin(client, "http://domain.herokuapp.com/api?id=xxxx&temp=xx");      
//Specify request destination
    
    https.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
      
    int httpCode = https.POST(""); //Send the request    
    
    String payload = https.getString(); //Get the response payload

    Serial.print("[HTTP] POST...\n");
    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] POST... code: %d\n", httpCode);
      
      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        const String& payload = https.getString();
        Serial.println("received payload:\n<<");
        Serial.println(payload);
        Serial.println(">>");
        https.end();  //Close connection
      }
    } else {
      Serial.print (payload);
      Serial.print (httpCode);
      Serial.println(WiFi.localIP());
      
      Serial.printf("[HTTP] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
      https.end();  //Close connection
    }