
基于立创·ESP32S3R8N8开发板的桌面气象站
简介
基于立创·ESP32S3R8N8开发板的桌面气象站
简介:基于立创·ESP32S3R8N8开发板的桌面气象站开源协议
:GPL 3.0
(未经作者授权,禁止转载)描述
系统简介
本项目是基于立创·ESP32S3R8N8开发板的桌面气象站,我手上有好几块ESP32的开发板,但是我敢说立创·ESP32S3R8N8开发板是颜值最高,速度最快的,没有之一。这个桌面气象站项目,功能包含实时时间的获取、网络天气预报获取、本地气压温度湿度获取、彩色LED灯、按键功能等
PCB设计说明
提示:原理图设计了两种屏幕接口的,间距0.5mm的适合使用插接,间距0.8mm的适合使用焊接,FPC转接板是针对0.8mm的屏幕做延长使用的。
软件说明
提示:部分软件代码
代码块:
// 1、RGBLED控制
#include
// 初始化灯珠控制实例
Adafruit_NeoPixel pixels(4, 11, NEO_GRB + NEO_KHZ800);
int16_t idx = 0; // 当前灯珠指向
unsigned int bright = 0; // 定义亮度变量
//点亮RgbLed任务
void TaskRgbLed(void *parameter)
{
while(1)
{
esp_random();
pixels.setPixelColor(0,pixels.Color(random(255),random(255),random(255)));
pixels.setPixelColor(1,pixels.Color(random(255),random(255),random(255)));
pixels.setPixelColor(2,pixels.Color(random(255),random(255),random(255)));
pixels.setPixelColor(3,pixels.Color(random(255),random(255),random(255)));
// bright++;
// bright%=256;
bright=20;
pixels.setBrightness(bright);
delay(1000);//延时,改变速度
pixels.show(); // 显示
}
}
xTaskCreate(
TaskRgbLed,
"TaskRgbLed", // 任务名
10000, // 任务栈的大小,用于存储任务运行时的上下文信息。简单来说,就是最多存这么多信息
NULL, /* Parameter passed as input of the task */
1, // 任务优先级, with 1 (configMAX_PRIORITIES - 1) 是最高的,0是最低的.
NULL); /* Task handle. */
// 2、I2C读取压力值
#include "Wire.h"
#define I2C_SDA 15
#define I2C_SCL 16
#define I2C_DEV_ADDR 0x60
float P=0.00; //压力
float T=0.00; //温度
//读取气压值
void getCps121(){
float buff[5]={0}; //保存温度和压力原始数据:前三位是压力,后两位是温度
uint8_t bytesReceived=0;
Serial.println("正在测量大气压力和温度");
//发送测试命令MR,唤醒器件并进行一次测量
Wire.beginTransmission(0x6C);
Wire.write(0x30);
Wire.write(0xA);
Wire.endTransmission(true);
delay(100);
//发送读取命令,读取数据
Wire.beginTransmission(0x6C);
Wire.write(0x06);
Wire.endTransmission(true);
bytesReceived=Wire.requestFrom(0x6C,5,true);
if(bytesReceived==5)
{
buff[0] = Wire.read();//读取第一个数据,压力
buff[1] = Wire.read();//读取第二个数据,压力
buff[2] = Wire.read();//读取第三个数据,压力
buff[3] = Wire.read();//读取第四个数据,温度
buff[4] = Wire.read();//读取第五个数据,温度
Serial.println(buff[0]);
Serial.println(buff[1]);
Serial.println(buff[2]);
Serial.println(buff[3]);
Serial.println(buff[4]);
//Pressure [kPa] = (Pressure 3rd Byte [23:16] x 65536+Pressure 2nd Byte [15:8] x 256 + Pressure1st Byte [7:0]) / 2^6/1000
P = ((buff[0]*65536+buff[1]*256 + buff[2])/64)/1000;
Serial.printf("压力 = %.2f kPa\r\n",P);
//Positive Temperature [°C] = (Temperature High Byte [15:8] x 256 + Temperature Low Byte [7:0]) / 2^8Negative Temperature [°C] = (Temperature High Byte [15:8] x 256 + Temperature Low Byte [7:0]-65536) / 2^8
T = (buff[3]*256 + buff[4])/256;
Serial.printf("温度 = %.2f ℃\r\n",T);
}
}
// 3、点亮WorkLed任务
void TaskWorkLed(void *parameter)
{
while(1)
{
// 设置48引脚 输出低电平
digitalWrite(48,LOW);
digitalWrite(10,LOW);
//digitalWrite(9,LOW);
// 延时100ms
delay(500);
// 设置48引脚 输出高电平
digitalWrite(48,HIGH);
digitalWrite(10,HIGH);
//digitalWrite(9,HIGH);
// 延时100ms
delay(500);
// 刷新屏幕放到同一个线程中,否则会崩溃
// 刷新时间
digitalClockDisplay();
delay(100);
// 刷新天气
drawWeather();
}
//vTaskDelete(NULL);
}
4、屏幕部分
#include
#include
#include
#include
#include
//屏幕引脚定义
#define TFT_CS 48
#define TFT_RST 21
#define TFT_DC 40
#define TFT_MOSI 39 // SDA
#define TFT_SCLK 38 // SCL
// 创建TFT对象
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
// Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
U8G2_FOR_ADAFRUIT_GFX u8g2; //设置字体,支持中文
// 屏幕主题、颜色
int Theme=1;
int backColor = BACK_WHITE; // BACK_BLACK;
uint16_t backFillColor = 0x0000;
int penColor = 0x0000;
// 初始化tft
void tftInit(){
tft.initR(INITR_BLACKTAB); // 初始化 ST7735S 128x60
// tft.init(240, 320); // 初始化 ST7789 320x240
tft.invertDisplay(false); // 不启用反色功能
tft.setSPISpeed(40000000); // 设置SPI_DEFAULT_FREQ
tft.setRotation(1); // 旋转显示屏方向 0,1
//设定主题颜色
if(Theme == 1){ // 原先为黑色主题,改为白色
backColor = WHITE;
backFillColor = 0xFFFF;
penColor = 0x0000;
}else{
backColor = BLACK;
backFillColor = 0x0000;
penColor = 0xFFFF;
}
tft.fillScreen(backColor);// 设置屏幕背景颜色
u8g2.begin(tft); //将u8g2与Adafruit_ST77xx建立连接,很重要,不写会出错
}
// 按背景颜色刷新整个屏幕
void reflashTFT(){
tft.fillScreen(backColor);
}
// 屏幕反色显示
void invertDisplayTFT(){
tft.invertDisplay(true);
delay(500);
tft.invertDisplay(false);
delay(500);
}
// 在指定位置显示文字
void drawtext(uint16_t x,uint16_t y,String text, uint16_t color) {
// 1、使用u8g2输出,支持中文和字符
u8g2.setFont(FONT); // 中文字体 u8g2_font_wqy12_t_gb2312字体多(210000左右) u8g2_font_wqy14_t_chinese1字体少(14000左右)
u8g2.setFontMode(0); // 使用 u8g2 透明模式(默认)
u8g2.setFontDirection(0);
u8g2.setForegroundColor(color); // 前景色
u8g2.setBackgroundColor(WHITE); // 背景色
u8g2.setCursor(x, y); // 文字位置
u8g2.print(text);
}
// 在屏幕上绘制图标
void drawicon()
{
u8g2.setFont(u8g2_font_siji_t_6x10); // icon字体
u8g2.setFontMode(1); // 使用 u8g2 透明模式(默认)
u8g2.setForegroundColor(BLACK); // 前景色
u8g2.setBackgroundColor(WHITE); // 背景色
u8g2.drawGlyph(5, 12, 0x0e1ff); // 电源图标 0x0e200
u8g2.drawGlyph(160-16-5, 12, 0x0e21a); // WiFi图标
u8g2.drawGlyph(10, 90, 0x0e015);// 时间图标
u8g2.drawGlyph(10, 40, 0x0e0cc); // 温度图标
u8g2.drawGlyph(10, 55, 0x0e0cf); // 湿度图标
u8g2.drawGlyph(80, 40, 0x0e022); // 天气图标
u8g2.drawGlyph(80, 55, 0x0e083); // 风力图标
}
// 绘制标题
void drawTitle(){
drawtext(35, 11, "ESP32桌面气象站", BLACK);
}
// 绘制矩形
void drawRectsBackground(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint16_t color) {
//tft.fillScreen(WHITE);
tft.drawRect(x, y,width,height, color);
}
// 绘制圆角矩形
void drawRoundRectsBackground(uint16_t x,uint16_t y,uint16_t width,uint16_t height,int16_t radius,uint16_t color) {
//tft.fillScreen(WHITE);
tft.drawRoundRect(x, y,width,height,radius,color);
}
// 绘制直线
void drawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color) {
tft.drawLine(x1, y1, x2, y2, color);
}
实物展示
注意事项
提示:如果你是跟我一样的新手,购买屏幕的时候,一定要备注好要**“插接”**,千万不要焊接,否则等你焊坏一个屏幕的时候,你就后悔莫及了,别问我是怎么知道的,全是眼泪。 如果不小心买成了焊接的,最好做个FPC转接线,卖家给我发的焊接的,18P,间距0.8,这个间距想买现成的FPC转接线都没有,我只好自己设计了一个,用了一下嘉立创的FPC打板,拿到手以后,感觉还真不错。
设计图

BOM


评论