iT邦幫忙

0

使用Arduino ESP8266 向Server傳送POST請求的問題

各位大神好,

我在學習:
Arduino環境下,使用ESP8266向遠端Server傳送POST請求,並上傳一段JSON格式的資料。
當Server接收到資料後,再將接收到的資料回傳ESP8266,並印在COM Port的監視視窗上。

目前遇到的問題是:
1.當我設定ESP8266自動獲取AP分配的IP時,POST請求可以傳送成功,但裡面的JSON資料會不見。
2.當我設定ESP8266使用固定IP時,POST請求可以正常運作,裡面的資料也不會丟失。

這是Arduino主體的程式碼:
ESP8266WiFi.h是ESP8266內建的函式庫,用於WIFI連接。
ArduinoHttpClient.h是Http客戶端的函式庫

#include <ESP8266WiFi.h>
#include <ArduinoHttpClient.h>
#include "setting.h"

//host 是Server
const char *host = "192.168.31.221";
const int httpPort = 3000;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, host, httpPort);

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  connect_to_wifi();
}

void loop()
{
  Serial.println("making POST request");
  String contentType = "application/x-www-form-urlencoded"; //它是post的預設格式,使用js中URLencode轉碼方法。
  String postData = "name=Alice&age=12";

  client.post("/data", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);
}

這是AP自動分配IP Setting.h的程式碼:

#include <ESP8266WiFi.h>

const char *ssid = "ssid";
const char *password = "password";

void connect_to_wifi()
{
    
    Serial.println("\nsetting wifi mode");
    WiFi.mode(WIFI_STA);
    
    WiFi.begin(ssid,password);
    Serial.printf("Connecting to %s",ssid);
    while (WiFi.status() != WL_CONNECTED)  //連接AP
    {
        delay(500);
        Serial.print(".");
    }

    Serial.print("\nConnected,IP address:");
    Serial.println(WiFi.localIP());
}

圖片
Server:https://ithelp.ithome.com.tw/upload/images/20200929/20130097GfQjaAqg5J.png
Com Port:https://ithelp.ithome.com.tw/upload/images/20200929/20130097EolTh49p6X.png

這是靜態IP Setting.h的程式碼:

#include <ESP8266WiFi.h>

//靜態 IP

IPAddress staticIP(192, 168, 31, 211);

IPAddress gateway(192, 168, 31, 1); 

IPAddress subnet(255, 255, 255, 0);

IPAddress dns(8, 8, 8, 8); 

const char *deviceName = "esp8266"; 

const char *ssid = "ssid";
const char *password = "password";


void connect_to_wifi()
{
    WiFi.disconnect(); 
    WiFi.hostname(deviceName);
    WiFi.config(staticIP, gateway, subnet, dns);
    Serial.println("setting wifi mode");
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.printf("Connecting to %s", ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }

    Serial.print("Connected,IP address:");
    Serial.println(WiFi.localIP());
}

圖片
Server:https://ithelp.ithome.com.tw/upload/images/20200929/20130097oR0N7OeugP.png
Com Port:https://ithelp.ithome.com.tw/upload/images/20200929/20130097Yu0k4i4VsL.png

這是Server的程式碼
主程式:app.js

const express = require("express");

const app = express()

//hanld post data
app.use(express.json())

app.post("/data" ,(req, res) => {
	console.log("get request");
	const data = req.body
	console.log(JSON.stringify(data));
	res.json({
		data
	});
})

app.listen(3000, () => console.log("Server has started!"))

package.json

{
	"name": "js-server",
	"version": "1.0.0",
	"main": "app.js",
	"license": "MIT",
	"dependencies": {
		"express": "^4.17.1"
	}
}

使用Node.JS架設的。
不好意思,剛剛才發現沒有貼Server的code。

我想知道為何會有這些差別,請各位大神能不能給我一點思路。
或是Google的關鍵字也可以!

謝謝各位大神!

來自答一下
是Arduino的Bug......

刪掉存檔,重建一個新的就沒有這個差異了.......
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
marlin12
iT邦研究生 5 級 ‧ 2020-09-30 20:50:42
最佳解答

ESP8266 Arduino Core是一個讓ESP8266可以在Arduino環境下開發的項目。雖然官方說可以使用Arduino的函数和程式庫,但畢竟是两個不同的硬件和程式庫。因此,拿Arduino和ESP8266的程式庫混合來用,是很危險的。

你用的WiFi.config函数便是一個好的例子。在舊版本的ESP8266程式庫上,該函数的參數排序,跟Arduino的參數排序,完全不同。可是,ArduinoHttpClient程式庫卻以為是呼叫Arduino的程式庫。

至於你發現,刪掉存檔,重建一個便好了。可能是更新了ESP8266程式庫的版本,問題便改正了。

Arduino開發環境的其中一個問題,是[程式庫的版本,不是跟隨每一個項目],導致程式庫更新後,用舊程式庫的項目 便可能會有相容的問題。

為了讓業餘愛好者容易用,Arduino把很多東西都隱藏起來,造成很多的問題和限制。因此,較專業的人都會轉用platformIO或其他的開發環境。

謝謝您的解答!大概了解原因了!

我要發表回答

立即登入回答