esp01s作为wed服务器实时接收arduino r3用软串口发送的json型数据mySerial.println(jsonString);的详细实施,分别列出esp01s的代码和r3的代码
时间: 2023-06-16 18:05:03 浏览: 50
以下是ESP01S的代码,使用SoftSerial库实现软串口接收来自Arduino R3的JSON数据,并且解析和处理它:
```C++
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial mySerial(2, 3); // RX, TX pins for ESP01S
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
// Connect to WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
mySerial.begin(9600); // Set baud rate for soft serial
}
void loop() {
if (mySerial.available()) { // If data available on soft serial
StaticJsonDocument<200> doc; // Create a JSON document
DeserializationError error = deserializeJson(doc, mySerial); // Parse the JSON data
if (error) {
Serial.println("JSON parsing failed.");
return;
}
const char* name = doc["name"]; // Retrieve data from JSON
int age = doc["age"];
Serial.print("Name: ");
Serial.println(name);
Serial.print("Age: ");
Serial.println(age);
}
}
```
以下是Arduino R3的代码,使用SoftwareSerial库实现软串口发送JSON数据到ESP01S:
```C++
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
SoftwareSerial mySerial(2, 3); // RX, TX pins for Arduino R3
void setup() {
Serial.begin(9600);
mySerial.begin(9600); // Set baud rate for soft serial
}
void loop() {
StaticJsonDocument<200> doc; // Create a JSON document
doc["name"] = "John";
doc["age"] = 30;
char jsonString[200];
serializeJson(doc, jsonString); // Serialize the JSON data
mySerial.println(jsonString); // Send the JSON data to ESP01S
delay(1000);
}
```