ESP32多传感器数据上传ThingSpeak时fireLevel与gasLevel显示0的问题
问题解决:引入ThingSpeak库后ESP32模拟传感器读数变为0
问题描述
使用ESP32 NodeMCU开发板,搭配DHT11、KY-026火焰传感器、MQ-135空气质量传感器。不引入ThingSpeak库时,fireLevel(KY-026)和gasLevel(MQ-135)的analogRead值正常(非0);但引入ThingSpeak库并连接WiFi上传数据后,这两个值始终显示为0。
核心原因
ESP32的WiFi模块会独占ADC2通道进行射频校准,当WiFi处于连接状态时,所有ADC2对应的GPIO引脚无法正常读取模拟值。你的代码中:
AIRQ_SENSOR定义为GPIO2,属于ADC2_CH2FIRE_SENSOR_ANALOG定义为GPIO14,属于ADC2_CH6
这两个引脚在WiFi启动后被占用,导致analogRead返回0。
解决方案
将模拟传感器的接线切换到ESP32的ADC1引脚,ADC1的引脚不受WiFi影响,可在WiFi连接时正常读取模拟值。ESP32的ADC1引脚包括:GPIO32、GPIO33、GPIO34、GPIO35、GPIO36、GPIO39。
修改步骤
- 硬件接线调整:
- 将MQ-135的模拟输出引脚接到GPIO34
- 将KY-026的模拟输出引脚接到GPIO35
- 代码引脚定义修改:
#define AIRQ_SENSOR 34 // 替换为ADC1引脚GPIO34 #define FIRE_SENSOR_ANALOG 35 // 替换为ADC1引脚GPIO35
额外优化建议
- 将WiFi连接逻辑移到
setup()函数中,避免每次循环重复执行连接操作,减少资源浪费 - 修正代码中
Unhealty的拼写错误为Unhealthy - 在
fireSensor函数中,将全屏清屏改为局部清除位图区域,避免误删其他显示内容
修改后的完整代码
#include <WiFi.h> #include "ThingSpeak.h" #include <SPI.h> #include <Wire.h> #include <DHT.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> const char* ssid = "SECRET_SSID"; const char* password = "SECRET_PASSWORD"; WiFiClient client; unsigned long channelNum = X; const char* APIKey = "SECRET_KEY"; // 替换为ADC1引脚 #define AIRQ_SENSOR 34 // ESP32 pin GPIO34 connected to mq135 sensor #define DHT_SENSOR_PIN 13 // ESP32 pin GIOP13 connected to DHT11 sensor #define DHT_SENSOR_TYPE DHT11 #define FIRE_SENSOR_ANALOG 35 // ESP32 pin GPIO35 connected to Fire sensor #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE); float humi, tempC, gasLevel, fireLevel; String quality = ""; const uint8_t bitmap19[] = {0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x3f, 0x40, 0x00, 0x7f, 0x60, 0x00, 0xff, 0xe0, 0x00, 0xff, 0xf0, 0x06, 0xff, 0xf0, 0x07, 0xff, 0xf8, 0x07, 0xff, 0xf8, 0x0f, 0xf7, 0xf8, 0x0f, 0xe7, 0xf8, 0x1f, 0xe3, 0xfc, 0x1f, 0xc1, 0xfc, 0x3f, 0x80, 0xfc, 0x3f, 0x80, 0xf8, 0x1f, 0x00, 0xf8, 0x1f, 0x00, 0xf0, 0x0f, 0x00, 0xe0, 0x07, 0x80, 0xc0, 0x01, 0x81, 0x00}; void setup() { Serial.begin(9600); dht_sensor.begin(); // initialize the DHT sensor if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); } display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(2); display.setCursor(50, 0); display.println("Air"); display.setTextSize(1); display.setCursor(23, 20); display.println("Quality Monitor"); display.display(); delay(1200); display.clearDisplay(); // WiFi连接移到setup执行一次 WiFi.mode(WIFI_STA); Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); while(WiFi.status() != WL_CONNECTED){ WiFi.begin(ssid, password); Serial.print("."); delay(5000); } Serial.println("\nConnected."); ThingSpeak.begin(client); } void fireSensor() { fireLevel = analogRead(FIRE_SENSOR_ANALOG); Serial.println("fire level: " + String(fireLevel)); if(fireLevel < 2000) { display.drawBitmap(90, 35, bitmap19, 24, 24, 1); } else { // 仅清除位图区域,保留其他显示内容 display.fillRect(90, 35, 24, 24, BLACK); } } void temphSensor() { // read humidity humi = dht_sensor.readHumidity(); // read temperature in Celsius tempC = dht_sensor.readTemperature(); // check whether the reading is successful or not if ( isnan(tempC) || isnan(humi)) { Serial.println("Failed to read from DHT sensor!"); } else { display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(0, 35); display.println("Tmp:"); display.setCursor(30, 35); display.println(tempC); display.setCursor(64, 35); display.println("C"); display.setCursor(0, 50); display.println("RH:"); display.setCursor(30, 50); display.println(humi); display.setCursor(64, 50); display.println("%"); } } void airqSensor() { gasLevel = analogRead(AIRQ_SENSOR); Serial.println("gas Level: " + String(gasLevel)); if(gasLevel < 500) { quality = "Good"; } else if(gasLevel < 750) { // avg 750 quality = "Moderate"; } else if(gasLevel < 1500) { // avg 1500 quality = "Unhealthy"; // 修正拼写错误 } else { // 2000 > quality = "Hazardous"; } display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(30, 5); display.println("Air Quality"); display.setCursor(35, 20); display.println(quality); } void writeThingSpeak() { ThingSpeak.setField(1, tempC); ThingSpeak.setField(2, humi); ThingSpeak.setField(3, gasLevel); ThingSpeak.setField(4, fireLevel); int x = ThingSpeak.writeFields(channelNum, APIKey); if(x == 200) { Serial.println("Channel update successful."); } else { Serial.println("Problem updating channel. HTTP error code " + String(x)); } delay(15000); } void loop() { display.clearDisplay(); fireSensor(); airqSensor(); temphSensor(); display.display(); writeThingSpeak(); }
内容的提问来源于stack exchange,提问作者dizzyme09
相关产品推荐
相关产品推荐

