当我包含Esp32 Google Cloud IoT库功能时,CO2传感器值会发生变化

如何解决当我包含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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-