为了准备2024年TI杯,本团队提前封装一些可能用到的案例
为了应对可能出现的双车通信,采用双ESP8266无线通信方案 一块设为 AT 另一块设为 STA
AP
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define SSID "3302" // AP名
#define PASSWORD "123456" // AP密码
WiFiClient client;
void setup() {
Serial.begin(115200);
// 连接到Wi-Fi AP
WiFi.begin(SSID, PASSWORD);
// 等待连接
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi!");
}
void loop() {
// 连接到服务端
if (!client.connected()) {
Serial.println("Connecting to server...");
if (client.connect("192.168.4.1", 23)) { // 服务端的 IP 和端口(AP模式下,默认 IP 是 192.168.4.1)
Serial.println("Connected to server!");
} else {
Serial.println("Connection failed!");
return;
}
}
// 检测是否有数据可用
if (client.available()) {
// 读取完整的数据包
String jsonStr = "";
while (client.available()) {
jsonStr += char(client.read());
}
// 解析 JSON 数据
StaticJsonDocument<256> jsonDoc;
DeserializationError error = deserializeJson(jsonDoc, jsonStr);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
// 从 JSON 中提取数据
const char* name = jsonDoc["name"];
int randValue = jsonDoc["rand"];
JsonArray dataArray = jsonDoc["data"];
// 输出 JSON 数据
Serial.println("Received JSON data:");
Serial.print("Name: ");
Serial.println(name);
Serial.print("Random: ");
Serial.println(randValue);
Serial.print("Data: ");
for (JsonVariant v : dataArray) {
Serial.print(v.as<double>());
Serial.print(" ");
}
Serial.println();
}
delay(1000); // 限制读取速率
}
STA
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <U8g2lib.h>
#include <Wire.h>
#define MAX_SRV_CLIENTS 10
#define DebugBegin(baud_rate) Serial.begin(baud_rate)
#define DebugPrintln(message) Serial.println(message)
#define DebugPrint(message) Serial.print(message)
#define LED 2
// 创建server,端口号是23
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
long pushtime = 0;
void setup() {
DebugBegin(115200);
u8g2.begin();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
u8g2.setCursor(40, 40);
u8g2.print("Json发送");
u8g2.sendBuffer();
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
// 创建AP模式
WiFi.softAP("3302", "123456");
// 启动server
server.begin();
server.setNoDelay(true);
DebugPrint("Ready! Connect to '3302' with password '123456'");
pushtime = millis();
}
void loop() {
uint8_t i;
// 检测是否有新的 client 请求进来
if (server.hasClient()) {
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
if (!serverClients[i] || !serverClients[i].connected()) {
if (serverClients[i]) {
serverClients[i].stop();
}
serverClients[i] = server.available(); // 分配新的client
DebugPrint("New client: ");
DebugPrint(i);
break;
}
}
// 当达到最大连接数,无法释放无效的 client,需要拒绝连接
if (i == MAX_SRV_CLIENTS) {
WiFiClient serverClient = server.available();
serverClient.stop();
DebugPrintln("Connection rejected ");
}
}
// 检测 client 发过来的数据
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
if (serverClients[i].available()) {
while (serverClients[i].available()) {
Serial.write(serverClients[i].read());
}
}
}
}
if (millis() - pushtime >= 500) {
digitalWrite(LED, !digitalRead(LED)); // LED闪烁
pushtime = millis();
// 使用毫秒作为种子
srand(millis());
int num = rand() % 130;
// 创建一个具有 256 字节大小的 StaticJsonDocument
StaticJsonDocument<256> jsonDoc;
jsonDoc["name"] = "liuzewen";
jsonDoc["rand"] = num;
// 在 json 对象中加入 data 数组
JsonArray data = jsonDoc.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);
// 发送 JSON 到串口
serializeJson(jsonDoc, Serial);
Serial.println();
// OLED 显示 JSON 数据
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_wqy16_t_gb2312);
u8g2.setCursor(40, 16 * 1 - 2);
u8g2.print("Json发送");
u8g2.setCursor(0, 16 * 2 - 2);
u8g2.print("name:");
u8g2.setCursor(40, 16 * 2 - 2);
u8g2.print("3302");
u8g2.setCursor(0, 16 * 3 - 2);
u8g2.print("rand:");
u8g2.setCursor(40, 16 * 3 - 2);
u8g2.print(num);
u8g2.setCursor(0, 16 * 4 - 2);
u8g2.print("data:");
u8g2.setCursor(40, 16 * 4 - 2);
u8g2.print("48.756080");
u8g2.sendBuffer();
// 序列化 JSON 并发送到每个 client
char jsonBuffer[256];
serializeJson(jsonDoc, jsonBuffer, sizeof(jsonBuffer));
for (i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write(jsonBuffer, strlen(jsonBuffer));
delay(1);
}
}
}
}