【得捷电子Follow me第2期】空气质量传感器和网络控制led显示展示
[复制链接]
本帖最后由 Lucheni 于 2023-11-14 16:43 编辑
本帖子为Follow me第二期最终的作业提交展示,共完成了两个项目
①空气质量传感器
②基于板载按键和网页按键的rgb led控制系统
具体项目展示可参考视频:https://training.eeworld.com.cn/video/38390?md5__2280=eqRxy7GQeiu49DBqDTClYOi%3Dq0Qweq%2Bex&alichlgref=http%3A%2F%2Ftraining.eeworld.com.cn%2Fcourse%2F68227
空气质量传感器项目介绍及关键代码说明:
此项目使用Adafruit ESP32-S3 TFT Feather板卡连接了SGP40和SCD41传感器,这两颗传感器可以分别测量voc、温度、湿度以及真实二氧化碳数值,开发板板载有iic接口,同时还有一块TFT彩屏,非常适合用来进行传感器的链接以及数据显示,所以本项目基于这三个部分搭建了空气质量传感器的demo
代码采用arduino编写,各个模块初始化、数据读取以及显示刷新都被我封装为函数,在setup及loop函数中直接进行调用,个人认为可阅读性还是非常高的
以下是传感器数据读取函数,其中要注意的是sgp40内部计算voc时需要将scd41获取的温湿度数据传入
void ScdRead(uint16_t &co2, float &temperature, float &humidity){
bool isDataReady = false;
uint16_t error;
error = scd4x.getDataReadyFlag(isDataReady);
if (isDataReady) {
delay(10);
error = scd4x.readMeasurement(co2, temperature, humidity);
if (error) {
Serial.print("Error trying to execute readMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else if (co2 == 0) {
Serial.println("Invalid sample detected, skipping.");
}
}
}
void SgpRead(uint16_t &sraw, int32_t &voc_index, float &temperature, float &humidity){
sraw = sgp.measureRaw(temperature, humidity);
voc_index = sgp.measureVocIndex(temperature, humidity);
}
以下为tft刷新函数,每次仅局部刷新数据部分显示块
void TftRefresh() {
// tft print function!
tft.fillRect(60, 0, 60, 135, ST77XX_BLACK);
tft.setCursor(60, 10);
tft.setTextColor(ST77XX_RED);
tft.setTextSize(2);
tft.println(temperature);
tft.setCursor(60, 35);
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(2);
tft.println(humidity);
tft.setCursor(60, 60);
tft.setTextColor(ST77XX_GREEN);
tft.setTextSize(2);
tft.println("null");
tft.setCursor(60, 85);
tft.setTextColor(ST77XX_BLUE);
tft.setTextSize(2);
tft.println(co2);
tft.setCursor(60, 110);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.println(voc_index);
}
tft端的数据采用中文显示,本代码将中文字转为位图,调用drawbitmap函数进行处理(处理方法可以看之前发的速通教程https://bbs.eeworld.com.cn/thread-1254565-1-1.html):
const uint8_t PROGMEM str1[]= {0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00};/*"温",1*/
const uint8_t PROGMEM str2[]= {0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E};/*"度",1*/
const uint8_t PROGMEM str3[]= {0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00};/*"湿",0*/
const uint8_t PROGMEM str4[]= {0x10,0x00,0x10,0x00,0x3F,0xFC,0x20,0x00,0x4F,0xF0,0x80,0x00,0x3F,0xF0,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x0A,0x00,0x0A,0x00,0x06,0x00,0x02};/*"气",0*/
const uint8_t PROGMEM str5[]= {0x00,0x00,0x3F,0xFE,0x20,0x00,0x20,0x80,0x20,0x80,0x20,0x80,0x20,0x80,0x2F,0xFC,0x20,0x80,0x20,0x80,0x20,0x90,0x20,0x88,0x20,0x88,0x40,0x80,0x5F,0xFE,0x80,0x00};/*"压",1*/
本项目实际完成了任务一在屏幕上显示中文的要求
基于板载按键和网页按键的rgb led控制系统介绍及关键代码说明:
本项目实现了板载按键控制板载led颜色变化,网页按键控制外接led阵列显示内容的效果。
loop函数轮询按键是否被按下,若按下则板载led开始变色(这里可以优化为按键中断,不过目前项目轮询也可以就没有修改)
if (!digitalRead(0)) {
TB.setColor(TB.Wheel(j++));
}
同时loop函数轮询esp32生成网页页面中的按键是否被按下,若按下则修改标志位
WiFiClient client = server.available();
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to move LED.<br>");
client.print("Click <a href=\"/L\">here</a> to stop LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
go=1; // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
go=0; // GET /L turns the LED off
}
}
最后根据标志位和当前时间判断是否对外接led阵列上显示内容进行滑动
if ((((t=millis()%65536)-ledprevtime)>SHIFTDELAY)&&(go==1)){
matrix.fillScreen(0);
matrix.setCursor(x, 5);
for (byte i = 0; i < strlen(adafruit); i++) {
// set the color
matrix.setTextColor(adaColors[i]);
// write the letter
matrix.print(adafruit[i]);
}
if (--x < -50) {
x = matrix.width();
}
matrix.show();
ledprevtime = millis()%65536;
}
本项目实际完成了任务二、任务三以及任务四的分任务二,实现了wifi生成网页和控制,板载按键的使用,板载rgb led使用,外接6*12led阵列的使用四个小项目
以上项目代码可以在这里找到:https://download.eeworld.com.cn/detail/Lucheni/629865
总结:
本次活动研究了基于esp32s3的wifi控制,led控制,传感器数据读取及中文显示,收获还是很多的,当然我个人最喜欢的还是这次活动的开发板,在小巧的feather版型上放下了tft彩屏,还板载电量计以及预留了温湿度传感器的焊接接口,双面布局,对于开发板而言是非常精致了,期待以后的活动能玩到更多更好的板子!
|