ESP8266WebServer server(80);esp01s作为wed服务器实时接收arduino r3用软串口发送的json型数据mySerial.println(jsonString);的详细实施,分别列出esp01s的代码和r3的代码
时间: 2023-06-16 08:05:08 浏览: 108
esp8266web.rar_esp8266 visual_esp8266 服务器_esp8266 webserver_es
ESP01S代码:
```c++
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h> //需要此库解析json数据
const char* ssid = "Your-SSID";
const char* password = "Your-Password";
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
server.on("/data", HTTP_POST, [](){
StaticJsonDocument<200> doc;
deserializeJson(doc, server.arg("plain"));
int value1 = doc["key1"];
int value2 = doc["key2"];
Serial.print("Value 1: ");
Serial.println(value1);
Serial.print("Value 2: ");
Serial.println(value2);
server.send(200, "text/plain", "Data received");
});
server.begin();
Serial.println("Server started");
}
void loop() {
server.handleClient();
}
```
R3代码:
```c++
#include <SoftwareSerial.h>
#include <ArduinoJson.h> //需要此库生成json数据
SoftwareSerial mySerial(2, 3); //使用软串口连接R3和ESP01S
StaticJsonDocument<200> doc;
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
while (!Serial) {
; //等待串口连接
}
}
void loop() {
doc["key1"] = random(1, 101); //设置要发送的数据
doc["key2"] = random(1, 101);
String jsonString;
serializeJson(doc, jsonString); //将数据转化为json格式
mySerial.println(jsonString); //通过软串口发送数据
delay(1000);
}
```
以上代码仅供参考,具体根据需求进行修改。
阅读全文