如何解决当我包含Esp32 Google Cloud IoT库功能时,CO2传感器值会发生变化
我最近编写了一个代码,用于输出温度和嗡嗡声(均通过I2C接口)和CO2(模拟接口)传感器。我还使用了ESP32 Feather微控制器和ESP32 Featherwing显示器,并且使用Arduino IDE将代码上传到微控制器。在温度和湿度值已经起作用的情况下,CO2值是我唯一关心的问题。每当我使用第一个代码时,CO2传感器的值都是正确的,但是当我使用第二个代码时(因为我想上传到GCP),温度和湿度都很好,但是CO2读取的原始值过高,并且结果,ppm值太高。
我尝试了一些操作,这是我的观察结果:
- mqtt-> loop()和connect()函数本身已经使二氧化碳读数最大化。
- 如果我删除readCO2()函数并将CO2值初始化为5或任何其他值,则显示的CO2读数分别为5或任何其他值(使用GCP函数)
- 注释“ mqtt-> loop”,将代码连接到GCP,但给出错误的值
- 注释“ connect()”,停止代码连接到GCP,但显示值
我有两个代码:
读取和显示传感器值
'''//无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 '''
读取并显示传感器值,然后传输到Google Cloud Platform
''' //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,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,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,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,comment out as required '''
输出值:
-
第一个代码:
- 23:49:23.695->原始模拟值:510
- 23:49:23.695->平均:525
- 23:49:23.695->电压:456.72mv
- 23:49:23.695-> 417.26ppm
-
第二个代码:
- 23:49:23.695->原始模拟值:3140
- 23:49:23.695->平均值:3140
- 23:49:23.695->电压:2529.79mv
- 23:49:23.695-> 6965.63ppm
注意:第一个和第二个代码的唯一区别是第二个代码包含Google Cloud IoT核心版ESP32库中的功能。来源:https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。