我以这篇文章为例:
我也根据我的环境更改了设置。以下是该草图的代码:
/*
* HTTP Client POST Request
* Copyright (c) 2018, circuits4you.com
* All rights reserved.
* https://circuits4you.com
* Connects to WiFi HotSpot. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include "DHT.h" //https://github.com/adafruit/DHT-sensor-library
/* Set these to your desired credentials. */
const char *ssid = "myssid"; //ENTER YOUR WIFI SETTINGS
const char *password = "mypassword";
//Web/Server address to read/write from
const char *host = "https://mysite.herokuapp.com/reading"; //https://circuits4you.com website or IP address of server
// Create the DHT temperature and humidity sensor object
DHT dht1(D4, DHT11);
DHT dht2(D7, DHT11);
//=======================================================================
// Power on setup
//=======================================================================
void setup() {
delay(1000);
Serial.begin(9600);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
dht1.begin();
dht2.begin();
}
//=======================================================================
// Main Program Loop
//=======================================================================
void loop() {
// Read DHT temperature and humidity values
float DHT11_t = dht1.readTemperature();
float DHT11_h = dht1.readHumidity();
float DHT21_t = dht2.readTemperature();
float DHT21_h = dht2.readHumidity();
//POST data routine starts here
HTTPClient http; //Declare object of class HTTPClient
String postData;
// int adcvalue=analogRead(A0); //Read Analog value of LDR
// ADCData = String(adcvalue); //String to interger conversion
// station = "A";
//Post Data
postData = "InttempC=" + String(DHT11_t) +"&InttempF=" + String(DHT11_t) + "&Inthum=" + String(DHT11_h) + "&ExttempC=" + String(DHT21_t) +"&ExttempF=" + String(DHT21_t) + "&Exthum=" + String(DHT21_h);
http.begin("https://mysite.herokuapp.com/reading"); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
delay(60000); //Post Data at every 60 seconds
}
//=======================================================================
我还在Node / Express中写了一个发布请求,它工作正常。我已经用Postman进行了测试,当我从Postman发送上述六个值时,它们将被存储。以下是Postman请求标签中的代码:
POST /reading HTTP/1.1
Host: mysite.herokuapp.com
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: 2aaba4bc-54bd-4e0a-aeb1-d51d35445164
InttempC=99999InttempF=555Inthum=4ExttempC=545454ExttempF=55Exthum=77
发布请求的路线如下所示(从邮递员访问时,它实际上运行良好):
// Process Form
app.post('/reading', (req, res) => {
const newReading = {
InttempC: req.body.InttempC,
InttempF: req.body.InttempF,
Inthum: req.body.Inthum,
ExttempC: req.body.ExttempC,
ExttempF: req.body.ExttempF,
Exthum: req.body.Exthum
}
new Reading(newReading)
.save();
});
我很困惑,就像在串行监视器中看到的那样,我的NodeMCU已连接,被分配了IP,然后在发出发布请求时它返回响应代码-1,我无法弄清问题出在哪里。>