【Arduino 动手做】紧凑且低成本的ESP32物联网空气质量监测器(AP模式)

发布时间:2025-08-07 10:45

物联网技术在环境监测中,如空气质量、水质监测。 #生活知识# #科技生活# #科技改变生活# #物联网设备#

#include <AHTxx.h>#include <Arduino.h>// #include <BluetoothSerial.h> // Inclure la bibliothque BluetoothSerial#include <PNGdec.h>#include <TFT_eSPI.h> // Bibliothque graphique#include <WebServer.h>#include <WiFi.h>#include <Wire.h>#include <image.h> #include "SPI.h"#include "ScioSense_ENS160.h" const char *ssid = "ESP8266_Mesure";const char *password = "12345678"; // Configuration de l'IP fixeIPAddress local_IP(192, 168, 4, 22); // Adresse IP fixeIPAddress gateway(192, 168, 4, 1);IPAddress subnet(255, 255, 255, 0);WebServer server(80); // Dfinition des constantes#define MAX_IMAGE_WIDTH 240 // Dfinition des variablesint16_t xpos = 0;int16_t ypos = 0;float temperature, humidity, eCO2, TVOC;int AQI = 0;int AQI_precedent = 0;uint16_t compteur = 0; // Dfinition des variables pour les bibliothquesTFT_eSPI tft = TFT_eSPI(); // Dclaration de l'instance de l'cranScioSense_ENS160 ens160(ENS160_I2CADDR_1);AHTxx aht20(AHTXX_ADDRESS_X38, AHT2x_SENSOR); // Adresse du capteur, type du capteurPNG png; // Instance du dcodeur PNG// BluetoothSerial SerialBT; // Instance de la bibliothque BluetoothSerial /// FONCTIONS // Page principalevoid handleRoot() { server.sendHeader("Cache-Control", "no-store"); String html = "<!DOCTYPE html>\ <html>\ <head>\ <meta charset='UTF-8'>\ <title>ESP32 Mesures</title>\ <style>\ body { font-family: Arial, sans-serif; text-align: center; background-color: #f4f4f9; color: #333; margin: 0; padding: 0; }\ h1 { font-size: 28px; margin-top: 20px; }\ ul { list-style: none; padding: 0; font-size: 20px; }\ li { margin: 15px 0; }\ .aqi-bar { width: 80%; height: 25px; margin: 20px auto; background: linear-gradient(to right, green, yellow, orange, red); position: relative; border-radius: 5px; overflow: hidden; }\ .aqi-fill { height: 100%; background: white; width: " + String((1 - (AQI / 5.0)) * 100) + "%; position: absolute; right: 0; top: 0; }\ footer { margin-top: 20px; font-size: 14px; color: #666; }\ </style>\ <script>\ setTimeout(function() { location.reload(true); }, 1000); // Force le rechargement toutes les secondes\ </script>\ </head>\ <body>\ <h1>ESP32 - Mesures de Qualit de l'Air</h1>\ <ul>\ <li><strong>Temprature :</strong> " + String(temperature, 2) + " C</li>\ <li><strong>Humidit :</strong> " + String(humidity, 2) + " %</li>\ <li><strong>eCO2 :</strong> " + String(eCO2, 0) + " ppm</li>\ <li><strong>TVOC :</strong> " + String(TVOC, 0) + " ppb</li>\ <li><strong>AQI :</strong> " + String(AQI) + "</li>\ </ul>\ <div class='aqi-bar'>\ <div class='aqi-fill'></div>\ </div>\ <footer>Page mise jour automatiquement toutes les secondes</footer>\ </body>\ </html>"; server.send(200, "text/html", html);} // Fonction de rappel pour dessiner des pixels sur l'cranvoid pngDraw(PNGDRAW *pDraw) { uint16_t lineBuffer[MAX_IMAGE_WIDTH]; png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff); tft.pushImage(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer);} void fond_ecran() { tft.fillScreen(TFT_BLACK); // Efface l'cran tft.setCursor(20, 10); tft.setTextColor(TFT_GREEN); tft.printf("Temp: ", temperature); tft.setCursor(20, 30); tft.printf("Hum.: ", humidity); tft.setCursor(20, 55); tft.printf("eCO2: ", eCO2); tft.setCursor(20, 75); tft.printf("TVOC: ", TVOC);} void miseajour_ecran() { tft.fillRect(105, 0, 120, 94, TFT_BLACK); tft.setTextSize(2); tft.setTextColor(TFT_WHITE); tft.setCursor(105, 10); tft.printf("%.2f C\n", temperature); tft.setCursor(105, 30); tft.printf("%.2f %%\n", humidity); tft.setCursor(105, 55); tft.printf("%.0f ppm \n", eCO2); tft.setCursor(105, 75); tft.printf("%.0f ppb \n", TVOC); if (AQI_precedent != AQI) { tft.fillRect(17, 100, 3, 25, TFT_BLACK); tft.fillRect(220, 100, 3, 25, TFT_BLACK); tft.fillRectHGradient(20, 100, 200, 25, TFT_GREEN, TFT_RED); tft.setTextSize(3); tft.setTextColor(TFT_BLACK); tft.setCursor(95, 101); tft.printf("AQI"); float position = 18 + 200 * (25 * (AQI - 1) / 100.00); tft.fillRect((int)position, 100, 4, 25, TFT_YELLOW); }} SETUP function initiate sensor --------------------------------------------------------------------------*/void setup() { // Initialisation cran tft.init(); tft.setRotation(1); // Rotation de l'cran, ajustez selon vos besoins tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextSize(2); tft.setCursor(0, 0); int16_t rc = png.openFLASH((uint8_t *)fond_reduit, sizeof(fond_reduit), pngDraw); if (rc == PNG_SUCCESS) { Serial.println("Fichier PNG ouvert avec succs"); tft.startWrite(); uint32_t dt = millis(); rc = png.decode(NULL, 0); Serial.print(millis() - dt); Serial.println("ms"); tft.endWrite(); } // Initialiser le bus I2C avec les pins spcifies Wire.begin(21, 22); Serial.begin(9600); // // Initialisation Bluetooth // SerialBT.begin("ESP32_AirQuality"); // Nom du priphrique Bluetooth // Serial.println("Le priphrique est prt jumeler!"); // Configuration de l'IP fixe WiFi.config(local_IP, gateway, subnet); WiFi.softAP(ssid, password); IPAddress IP = WiFi.softAPIP(); Serial.print("Point d'accs IP address: "); Serial.println(IP); server.on("/", handleRoot); server.begin(); Serial.println("Serveur web dmarr"); while (!Serial) { } delay(500); Serial.print("ENS160..."); ens160.begin(); Serial.println(ens160.available() ? "done." : "failed!"); if (ens160.available()) { // Print ENS160 versions Serial.print("\tRev: "); Serial.print(ens160.getMajorRev()); Serial.print("."); Serial.print(ens160.getMinorRev()); Serial.print("."); Serial.println(ens160.getBuild()); ens160.setMode(ENS160_OPMODE_RESET); delay(100); ens160.setMode(ENS160_OPMODE_STD); // Initialisation AHT20 while (aht20.begin() != true) { Serial.println(F("AHT2x non connect ou chec du chargement des coefficients de calibration")); delay(5000); } Serial.println(F("AHT20 OK")); } fond_ecran();} MAIN LOOP FUNCTION Cycle every 1000ms and perform measurement --------------------------------------------------------------------------*/ void loop() { if (ens160.available()) { temperature = aht20.readTemperature(); // read 6-bytes via I2C, takes 80 milliseconds humidity = aht20.readHumidity(); ens160.set_envdata(temperature, humidity); ens160.measure(true); ens160.measureRaw(true); AQI_precedent = AQI; AQI = ens160.getAQI(); TVOC = ens160.getTVOC(); eCO2 = ens160.geteCO2(); // Envoyer les donnes par Bluetooth // SerialBT.printf("Temp: %.2f C, Hum: %.2f %%, eCO2: %.0f ppm, TVOC: %.0f ppb, AQI: %d\n", temperature, humidity, eCO2, TVOC, AQI); } miseajour_ecran(); server.handleClient(); delay(1000); compteur++; if (compteur > 300) { compteur = 0; ens160.setMode(ENS160_OPMODE_RESET); delay(250); ens160.setMode(ENS160_OPMODE_STD); }}

网址:【Arduino 动手做】紧凑且低成本的ESP32物联网空气质量监测器(AP模式) https://www.yuejiaxmz.com/news/view/1224074

相关内容

实时监测室内空气质量: ESP32
基于开源硬件的在线空气质量监测系统设计
利用Arduino ESP32
随时关注空气质量:工程师自制Arduino空气检测器
空气质量监测器(室内空气质量监测器)
ESP32智能设备:蓝牙音箱、AI语音助手、环境监测与调节以及智能控制,基于BLE与MQTT技术(代码详解)
物联网传感器在空气质量监测中的作用
AQM 65紧凑型空气质量监测站
室内空气质量监测
基于ESP32的智能家庭健康系统

随便看看