我需要将json格式的数据从android应用程序发送到esp32 wifi模块(“arduino”)。
android 应用程序成功以正确的 json 格式发送数据...esp32 模块知道它接收到了一些东西,但它总是作为空输入到达,并因此给我一个错误。
另一方面,当我从 Flask 服务器向 esp32 模块发送/接收数据时,它工作正常。与从 Flask 服务器向 Android 应用程序发送/接收数据相同...有什么建议或者我在两者之间做错了什么吗?
这是课堂“大项目”的一部分,已经花费了两个多月的时间,所以我真的需要这个来在这里工作。谢谢!
我尝试发送简单的字符串,如“hey”,其中内容类型设置为“plain”,但它仍然以空字符串形式到达 esp32...
我在esp32模块中有以下内容:
#include <ArduinoJson.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <HTTPClient.h>
#include "AsyncJson.h"
const char* ssid = "wifi_ssid"; //changed these for the post
const char* password = "wifi_password"; //changed these for the post
const unsigned long connectionTimeout = 10000;
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
Serial.print("Connecting to Wifi...");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());
//other code stuff...
// Start server
//other server.on() stuff...
server.on("/brew_coffee", HTTP_POST, handleBrewCoffee);
server.begin();
}
void handleBrewCoffee(AsyncWebServerRequest *request) {
if (request != nullptr) {
String clientIPAddress = request->client()->remoteIP().toString();
// Here I receive the data and print it
String recipeReceived = request->arg("application/json");
Serial.print("\n" + clientIPAddress);
Serial.print(" sent string: ");
Serial.println(recipeReceived);
DynamicJsonDocument jsonDoc(16386);
DeserializationError error = deserializeJson(jsonDoc, recipeReceived);
//This is where I get the EmptyInput error
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
recipeReceived = "not: " + clientIPAddress;
} else{
//Extracting json data
const char* username = jsonDoc["username"];
const char* user_ip = jsonDoc["user_ip"];
const char* user_id = jsonDoc["user_id"];
const char* name = jsonDoc["name"];
//I do rest of the data likewise
// Print the coffee information
Serial.println("\nCoffee Information:");
Serial.print("username: "); Serial.println(username);
Serial.print("user_id: "); Serial.println(user_id);
Serial.print("Coffee Name: "); Serial.println(name);
//I do rest of the data likewise
recipeReceived = "Coffee Received";
}
}
}
这是我的android应用程序(来自android studio)中发送数据的函数:
public String makeServiceCallPostJSON(String reqUrl, String username, String user_ip, int user_id, String name, more params...) {
String response = null;
try {
//This url is the same as the esp32 with its proper endpoint
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
JSONObject jsonParams = new JSONObject();
jsonParams.put("username", username);
jsonParams.put("user_ip", user_ip);
jsonParams.put("user_id", user_id);
jsonParams.put("name", name);
//I do rest likewise
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
System.out.println(jsonParams.toString());
conn.getOutputStream().write(jsonParams.toString().getBytes("UTF-8"));
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
// Convert the InputStream in a String
response = convertStreamToString(in);
System.out.println("This is what i got... " + response);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
这是我在 esp32 模块中得到的输出
###.###.#.###(客户端地址)发送字符串: 无法解析 JSON:EmptyInput
这些是我在android studio的Logcat中得到的打印结果
{"username":"guest","user_ip":"###.###.#.###","user_id":1,"name":"咖啡B", ...} 这就是我得到的...不是
user_ip 与 esp32 输出中打印的客户端地址相同。这进一步证明了它“接收”了一些东西,即使它是空的......
出于某种原因,当我尝试从移动应用程序向 esp32 发送某些内容时,以下库总是给我一个空字符串: ESP8266网络服务器 AsyncWebServer_ESP32_W5500 ESPAsyncWeb服务器 ESPAsyncWebSrv
注意:这些在与 python 中的 Flask 服务器等其他东西通信时起作用......我仍然不知道移动应用程序有什么问题。
这个与移动应用程序和 Flask 服务器完美配合...除了以下库的细微语法更改之外,我并没有真正改变我正在做的任何事情: 网络服务器
它的外观如下:
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);
void setup(){
Serial.print("Connecting to Wifi...");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
server.on("/brew_coffee", HTTP_POST, handleBrewCoffee);
server.begin();
}
void handleBrewCoffee() {
if (server.hasArg("plain")) {
String recipeReceived = server.arg("plain");
Serial.println("Received JSON data:");
Serial.println(recipeReceived);
DynamicJsonDocument jsonDoc(1024);
DeserializationError error = deserializeJson(jsonDoc, recipeReceived);
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
recipeReceived = "not";
} else{
const char* username = jsonDoc["username"];
//same for the rest
Serial.println("Coffee Information:");
Serial.print("username: "); Serial.println(username);
//same for the rest
recipeReceived = "Coffee Received";
}
server.send(200, "text/plain", recipeReceived);
}
}