当我包含Esp32谷歌云物联网库函数时,CO2传感器值会发生变化

当我包含Esp32谷歌云物联网库函数时,CO2传感器值会发生变化,c,arduino,esp32,C,Arduino,Esp32,我最近做了一个代码,输出你的温度和嗡嗡声(通过I2C接口)和二氧化碳(模拟接口)传感器。我还使用了一个ESP32羽毛微控制器和一个ESP32羽毛显示器,我使用Arduino IDE将我的代码上传到Microcontroller。由于温度和湿度值已经起作用,CO2值是我唯一关心的问题。每当我使用第一个代码时,CO2传感器值都是正确的,但是当我使用第二个代码时(因为我想上传到GCP),温度和湿度都很好,但是CO2读取的原始值太高,因此,ppm值太高 我尝试了一些方法,以下是我的观察结果: mqtt

我最近做了一个代码,输出你的温度和嗡嗡声(通过I2C接口)和二氧化碳(模拟接口)传感器。我还使用了一个ESP32羽毛微控制器和一个ESP32羽毛显示器,我使用Arduino IDE将我的代码上传到Microcontroller。由于温度和湿度值已经起作用,CO2值是我唯一关心的问题。每当我使用第一个代码时,CO2传感器值都是正确的,但是当我使用第二个代码时(因为我想上传到GCP),温度和湿度都很好,但是CO2读取的原始值太高,因此,ppm值太高

我尝试了一些方法,以下是我的观察结果:

  • mqtt->loop()和connect()函数本身已经导致CO2读数最大化
  • 如果我删除readCO2()函数并将CO2值初始化为5或任何其他值,则显示的CO2读数分别为5或任何其他值(使用GCP函数)
  • 注释掉“mqtt->loop”,将代码连接到GCP,但给出了错误的值
  • 注释掉“connect()”,将停止代码连接到GCP,但会显示值
我有两个代码:

读取并显示传感器值

''//CO2不含GCP

#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>

#ifdef ESP32
  #define STMPE_CS  32
  #define TFT_CS    15
  #define TFT_DC    33
  #define SD_CS     14
#endif

#define window_size 50


//AQMS VALUES:
// temp & hum:
float t = 0;
float h = 0; 

float c = 0; // CO2
//DATA SMOOTHING VARIABLES:
int indices = 0;
int sensorValue = 0;
int sum = 0;
int readings[window_size];
int averaged = 0;
byte sensorIn = A0;


//WCS VALUES: (no readings)
float p = 0; //ph
float e = 0; //ec
float w = 0; //watertemp


String sys1 = "AQMS0111";
String devID1 = "espDDSTM";
int b = 7;
String tant1 = "IBC";
String sit1 = "Mabolo";


//Intervals:

//CO2 SENSOR:
unsigned long co2MillisSet = 0;
unsigned long co2MillisInterval = 1000; //1s

//DISPLAY:
unsigned long printMillisSet = 0;
unsigned long printMillisInterval = 1000; //2s

unsigned long temphumMillisSet = 0;
unsigned long temphumMillisInterval = 1000;
//INTERNET RECONNECTION:
unsigned long check_wifi = 30000; // 30s

uint8_t rotation = 1;

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup(){
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(rotation);
  
  if (!sht31.begin(0x44)) {
    Serial.println("Error reading SHT31");
  }
  
  tft.fillScreen(ILI9341_GREEN);
  tft.setCursor(0,50);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);
  tft.print("NXTLVL AQMS0111");
  delay(3000);

  // Read diagnostics (optional but can help debug problems)
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);

  
}


void loop(){
  unsigned long universalMillis = millis();

  //READ FROM SENSORS:
  if(universalMillis - temphumMillisSet > temphumMillisInterval){
  t = sht31.readTemperature(); //returns temperature
  h = sht31.readHumidity(); // returns humidity
  }
  readCO2(); // returns CO2 concentration

  //DISPLAY READINGS:
  printValues(t, h, c);

}


void readCO2(){
//Read voltage
  if(millis() - co2MillisSet > co2MillisInterval){
  co2MillisSet = millis();
  sensorValue = ReadVoltage(sensorIn) * 1000;
  sum = sum - readings[indices];
  readings[indices] = sensorValue;
  sum = sum + sensorValue;
  indices = (indices + 1) % window_size;

  averaged = sum / window_size;
  
  // The analog signal is converted to a voltage
  float voltage = averaged*(3300/4096.0);
  if(voltage == 0)
  {
    Serial.println("Fault");
  }
  else if(voltage < 400)
  {
    Serial.println("preheating");
  }
  else
  {
    int voltage_diference=voltage-300;
    c = voltage_diference*50.0/16.0;
    // Print Voltage
    Serial.print("Raw Analog Values: ");
    Serial.println(sensorValue);
    Serial.print("Average: ");
    Serial.println(averaged);
    Serial.print("voltage: ");
    Serial.print(voltage);
    Serial.println("mv");
    //Print CO2 concentration
    Serial.print(c);
    Serial.println("ppm");

  }
 }
}

void printValues(float temp, float hum,float carbondioxide){
if(millis() - printMillisSet > printMillisInterval){
 tft.fillScreen(ILI9341_BLACK);
 tft.setCursor(0,50);
 tft.setTextColor(ILI9341_WHITE);
 tft.setTextSize(3);

 if (!isnan(temp)){
    tft.print("Temp : "); tft.println(temp);
    Serial.print("airTemp : "); Serial.println(temp);
  }
  else {
    tft.print("Temp : "); tft.println("ERROR");
    Serial.println("Error reading SHT31-temperature");
  }
  if (!isnan(hum)) {
    tft.print("Hum : "); tft.println(hum);
    Serial.print("airHum : "); Serial.println(hum);
  }
  else {
    tft.print("Hum : "); tft.println("ERROR");
    Serial.print("Error reading SHT31-humidity");
  }
  tft.print("CO2 : "); tft.println(carbondioxide);

 printMillisSet = millis();
 }
}

double ReadVoltage(byte pin){
  double reading = analogRead(pin); // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if(reading < 1 || reading > 4095) return 0;
  //return -0.000000000009824 * pow(reading,3) + 0.000000016557283 * pow(reading,2) + 0.000854596860691 * reading + 0.065440348345433;
   return -0.000000000000016 * pow(reading,4) + 0.000000000118171 * pow(reading,3)- 0.000000301211691 * pow(reading,2)+ 0.001109019271794 * reading + 0.034143524634089;
} // Added an improved polynomial, use either, comment out as required '''
#包括
#包括“Adafruit_SHT31.h”
#包括
#包括


也许谷歌的图书馆正在重新配置ADC?也许它会改变参考电压?感谢用户253751的回复!这是可能的,因为CO2传感器使用ESP32的模拟引脚读取传感器值,而温度和湿度传感器使用I2C引脚。我会看看谷歌的图书馆。这对我来说很困难,因为我需要谷歌云功能来处理数据,而二氧化碳传感器只能从模拟引脚读取模拟信号。尝试使用另一个传感器并从其模拟接口读取数据,结果相同。我几乎确信它应该从I2C接口读取,因为SetupIoTCloud()函数允许温度和湿度传感器,但不允许仅从模拟接口读取CO2传感器。>也许谷歌的库正在重新配置ADC?也许它改变了参考电压?我贴到了他们的个人Github上。一旦我找到原因,我会发布更新。也许谷歌的库正在重新配置ADC?也许它会改变参考电压?感谢用户253751的回复!这是可能的,因为CO2传感器使用ESP32的模拟引脚读取传感器值,而温度和湿度传感器使用I2C引脚。我会看看谷歌的图书馆。这对我来说很困难,因为我需要谷歌云功能来处理数据,而二氧化碳传感器只能从模拟引脚读取模拟信号。尝试使用另一个传感器并从其模拟接口读取数据,结果相同。我几乎确信它应该从I2C接口读取,因为SetupIoTCloud()函数允许温度和湿度传感器,但不允许仅从模拟接口读取CO2传感器。>也许谷歌的库正在重新配置ADC?也许它改变了参考电压?我贴到了他们的个人Github上。一旦我找到原因,我将发布更新。
''' //CO2 TEMP HUM GCP TEST 

#include "esp32-mqtt.h"
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>

#ifdef ESP32
  #define STMPE_CS  32
  #define TFT_CS    15
  #define TFT_DC    33
  #define SD_CS     14
#endif

#define window_size 50

//AQMS VALUES:
// temp & hum:
float t = 0;
float h = 0; 

float c = 0; // CO2
//DATA SMOOTHING VARIABLES:
int indices = 0;
int sensorValue = 0;
int sum = 0;
int readings[window_size];
int averaged = 0;
byte sensorIn = A0;


//WCS VALUES: (no readings)
float p = 0; //ph
float e = 0; //ec
float w = 0; //watertemp

String payload;

String sys1 = "AQMS0111";
String devID1 = "espDDSTM";
int b = 7;
String tant1 = "IBC";
String sit1 = "Mabolo";

int voltage_diference;
float voltage;

//Intervals:

//CO2 SENSOR:
unsigned long co2MillisSet = 0;
unsigned long co2MillisInterval = 1000; //1s

//DISPLAY:
unsigned long printMillisSet = 0;
unsigned long printMillisInterval = 150000; //2.5min

unsigned long temphumMillisSet = 0;
unsigned long temphumMillisInterval = 1000;


//INTERNET RECONNECTION:
unsigned long check_wifi = 30000; // 30s

uint8_t rotation = 1;

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup(){
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(rotation);
  
  if (!sht31.begin(0x44)) {
    Serial.println("Error reading SHT31");
  }
 
  tft.fillScreen(ILI9341_GREEN);
  tft.setCursor(0,50);
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);
  tft.print("NXTLVL AQMS0111");
  delay(3000);

  // Read diagnostics (optional but can help debug problems)
  uint8_t x = tft.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  x = tft.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);

  setupCloudIoT();
  
}


void loop(){
  unsigned long universalMillis = millis();
  
  mqtt->loop();
  delay(10);

  if(!mqttClient->connected()){
  connect();  
  }

  if(universalMillis - temphumMillisSet > temphumMillisInterval){
  t = sht31.readTemperature();
  h = sht31.readHumidity();
  }
  readCO2(); // Returns c (carbon dioxide)

  printValues(t, h, c); 
  
 if(millis() - uploadMillis > uploadMillisInterval){
 payload   =  String("{\"System\":") + ("\"") + sys1  + ("\"") +   //string
                     String(",\"deviceID\":") + ("\"") + devID1 + ("\"") + //string
                     String(",\"Box\":") + ("\"") + b + ("\"") + //integer
                     String(",\"tank\":") + ("\"") + tant1 + ("\"") + //string
                     String(",\"site\":") + ("\"") + sit1 + ("\"") + //string
                     String(",\"timecollected\":") + ("\"") + time(nullptr) + ("\"") + //timestamp
                     String(",\"pH\":") + ("\"") + p + ("\"") + //float
                     String(",\"EC\":") + ("\"") + e + ("\"") + //float
                     String(",\"waterTemp\":") + ("\"") + w + ("\"") + //float
                     String(",\"airTemp\":") + ("\"") + t + ("\"") + //float
                     String(",\"airHum\":") + ("\"") + h + ("\"") + //float
                     String(",\"CO2\":") + ("\"") + c + ("\"") + //float
                     String("}"); 
 
 Serial.print(payload); 
 publishTelemetry(payload); 

 uploadMillis = millis();
 }
 
}


void readCO2(){
//Read voltage
  if(millis() - co2MillisSet > co2MillisInterval){
  co2MillisSet = millis();
  sensorValue = ReadVoltage(sensorIn) * 1000;
  sum = sum - readings[indices];
  readings[indices] = sensorValue;
  sum = sum + sensorValue;
  indices = (indices + 1) % window_size;

  averaged = sum / window_size;
  
  // The analog signal is converted to a voltage
  voltage = averaged*(3300/4096.0);
  if(voltage == 0)
  {
    Serial.println("Fault");
  }
  else if(voltage < 400)
  {
    Serial.println("preheating");
  }
  else
  {
    voltage_diference=voltage-300;
    c = voltage_diference*50.0/16.0;
    // Print Voltage
    Serial.print("Raw Analog Values: ");
    Serial.println(sensorValue);
    Serial.print("Average: ");
    Serial.println(averaged);
    Serial.print("voltage: ");
    Serial.print(voltage);
    Serial.println("mv");
    //Print CO2 concentration
    Serial.print(c);
    Serial.println("ppm");

  }
 }
}


void printValues(float temp, float hum,float carbondioxide){
if(millis() - printMillisSet > printMillisInterval){
 tft.fillScreen(ILI9341_BLACK);
 tft.setCursor(0,50);
 tft.setTextColor(ILI9341_WHITE);
 tft.setTextSize(3);

 if (!isnan(temp)){
    tft.print("Temp : "); tft.println(temp);
    Serial.print("airTemp : "); Serial.println(temp);
  }
  else {
    tft.print("Temp : "); tft.println("ERROR");
    Serial.println("Error reading SHT31-temperature");
  }
  if (!isnan(hum)) {
    tft.print("Hum : "); tft.println(hum);
    Serial.print("airHum : "); Serial.println(hum);
  }
  else {
    tft.print("Hum : "); tft.println("ERROR");
    Serial.print("Error reading SHT31-humidity");
  }
  tft.print("CO2 : "); tft.println(carbondioxide);

 printMillisSet = millis();
 }
}



double ReadVoltage(byte pin){
  double reading = analogRead(pin); // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if(reading < 1 || reading > 4095) return 0;
  //return -0.000000000009824 * pow(reading,3) + 0.000000016557283 * pow(reading,2) + 0.000854596860691 * reading + 0.065440348345433;
   return -0.000000000000016 * pow(reading,4) + 0.000000000118171 * pow(reading,3)- 0.000000301211691 * pow(reading,2)+ 0.001109019271794 * reading + 0.034143524634089;
} // Added an improved polynomial, use either, comment out as required '''