文章目录
- 前言
- 先上效果图
- platformio.ini
- wokwi.toml
- diagram.json
- 源代码
- 编译
- 编译成功
- 上传云端
- 完结撒花⭐⭐⭐⭐⭐
前言
阅读此篇前建议先看 此片熟悉下wokwi
https://blog.csdn.net/qq_20330595/article/details/144289986
先上效果图
Column 1 | Column 2 |
---|---|
platformio.ini
wokwi.toml
diagram.json
注意这里代码是自动生成的
源代码
// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0const int ledPin = 27; // 定义27号引脚为LED引脚void sendHttpRequest()
{if (WiFi.status() == WL_CONNECTED){WiFiClientSecure client;HTTPClient http;client.setInsecure(); // 如果是自签名证书可以使用此行http.begin(client, "https://wanandroid.com/harmony/index/json");int httpCode = http.GET();/**/LCD.clear();LCD.setCursor(0, 0);LCD.print("Sending Http");LCD.setCursor(0, 1);LCD.print("Request..... ");if (httpCode > 0){String payload = http.getString();Serial.println("HTTP Response code: " + String(httpCode));// Serial.println("Response payload: " + payload);// 在LCD的第二行显示HTTP返回值的长度LCD.clear();LCD.setCursor(0, 0);LCD.print("httpCode: ");LCD.print(String(httpCode));LCD.setCursor(0, 1);LCD.print("Len: ");LCD.print(payload.length());}else{Serial.print("Error on HTTP request: "); // 输出错误信息Serial.println(http.errorToString(httpCode)); // 转换错误代码为可读格式}http.end(); // 结束请求}else{Serial.println("WiFi Disconnected");}
}void spinner()
{static int8_t counter = 0;const char *glyphs = "\xa1\xa5\xdb";LCD.setCursor(15, 1);LCD.print(glyphs[counter++]);if (counter == strlen(glyphs)){counter = 0;}
}void printLocalTime()
{struct tm timeinfo;// 如果获取本地时间失败,则打印错误信息并返回if (!getLocalTime(&timeinfo)){// 在LCD的第二行显示连接错误信息LCD.setCursor(0, 1);LCD.println("Connection Err");return;}// 在LCD的第一行第9列开始显示当前时间LCD.setCursor(8, 0);LCD.println(&timeinfo, "%H:%M:%S");// 在LCD的第二行显示当前日期和时区LCD.setCursor(0, 1);LCD.println(&timeinfo, "%d/%m/%Y %Z");
}/*** @brief 初始化设置函数** 该函数用于初始化串口通信、LCD显示屏,并连接到WiFi网络。* 它会在设备启动时执行一次。*/
void setup()
{// 设置27号引脚为输出模式pinMode(ledPin, OUTPUT);// 初始化串口通信,波特率为115200Serial.begin(115200);// 初始化LCD显示屏LCD.init();// 打开LCD背光LCD.backlight();// 在LCD的第一行显示连接信息LCD.setCursor(0, 0);LCD.print("Connecting to ");// 在LCD的第二行显示WiFi信息LCD.setCursor(0, 1);LCD.print("WiFi ");// 开始连接到WiFi网络,SSID为"Wokwi-GUEST",密码为空,超时时间为6秒WiFi.begin("Wokwi-GUEST", "", 6);// 等待WiFi连接成功while (WiFi.status() != WL_CONNECTED){// 延迟250毫秒delay(250);// 显示连接状态的旋转图标spinner();}Serial.println("");Serial.println("WiFi connected");Serial.print("IP address: ");Serial.println(WiFi.localIP());LCD.clear();LCD.setCursor(0, 0);LCD.println("Online");LCD.setCursor(0, 1);LCD.println("Updating time...");configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}void loop()
{// printLocalTime();// delay(250);// delay(4000);// sendHttpRequest();// 循环输出高低电平digitalWrite(ledPin, HIGH);LCD.clear();LCD.setCursor(0, 0);LCD.println("HIGH");delay(1000);digitalWrite(ledPin, LOW);LCD.clear();LCD.setCursor(0, 0);LCD.println("LOW");delay(1000);
}
编译
Ctrl+Alt+B
编译成功
注意这里输出的固件路径对应前面wokwi.toml的内容
上传云端
Ctrl+Shift+P