qiu776
Published © MIT

Room CO2 monitor

A device that monitors the CO2 concentration in the room with Wio-terminal and Grove VOC and eCO2 Gas Sensor.

BeginnerShowcase (no instructions)1 hour333
Room CO2 monitor

Things used in this project

Story

Read more

Schematics

2_E5QCe8SKFo.png

_20221203170043_4qBeNrcUdK.png

Code

Seeedstudio_wio-terminal display&Develop with Ubidots

Arduino
#include <Arduino.h>
#include "sensirion_common.h"
#include "Wire.h"
#include "sgp30.h"
#include"TFT_eSPI.h"
TFT_eSPI tft;
TFT_eSprite spr = TFT_eSprite(&tft);

#include <PubSubClient.h>
#include <rpcWiFi.h>
 
#define WIFISSID "<YOUR-WIFISSD>" // Put your WifiSSID here
#define PASSWORD "<YOUR-WIFI-PASSWORD>" // Put your wifi password here
#define TOKEN "<YOUR-UBIDOTS-TOKEN>" // Put your Ubidots' TOKEN
#define VARIABLE_LABEL1 "voc" // Assign the variable label
#define VARIABLE_LABEL2 "co2"
#define DEVICE_LABEL "wio-terminal" // Assign the device label
#define MQTT_CLIENT_NAME "6420496143" // MQTT client Name
 
const long interval = 100;
unsigned long previousMillis = 0;
 
char mqttBroker[] = "industrial.api.ubidots.com";
 
WiFiClient wifiClient;
PubSubClient client(wifiClient);
 
static unsigned short int VOC = 0;
static unsigned short int CO2 = 0;
 
// Space to store values to send
char str_voc[6];
char str_co2[6];
char payload[700];
char topic[150];
 
void callback(char* topic, byte* payload, unsigned int length){
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
}
 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
 
  // Attempt to connect
  if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) {
    Serial.println("connected");
  }
  else {
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" try again in 2 seconds");
    // Wait 2 seconds before retrying
    delay(2000);
    }
  }
}
 
void read_sgp30()
{
  s16 err = 0;
  sgp_measure_iaq_blocking_read(&VOC, &CO2);
  if (err == STATUS_OK) {
      Serial.print("tVOC  Concentration:");
      Serial.print(VOC);
      Serial.println("ppb");
 
      Serial.print("CO2eq Concentration:");
      Serial.print(CO2);
      Serial.println("ppm");
  } else {
      Serial.println("error reading IAQ values\n");
  }
}
 
void send_data()
{
  dtostrf(VOC, 4, 0, str_voc);
  dtostrf(CO2, 4, 0, str_co2);
 
  if (!client.connected()) {
    reconnect();
  }
 
  // Builds the topic
  sprintf(topic, "%s", ""); // Cleans the topic content
  sprintf(topic, "%s%s", "/v2.0/devices/", DEVICE_LABEL);
 
  //Builds the payload
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
  sprintf(payload, "%s%s", payload, str_voc); // Adds the value
  sprintf(payload, "%s}", payload); // Closes the dictionary brackets
  client.publish(topic, payload);
  Serial.println(payload);
  delay(500);
 
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label
  sprintf(payload, "%s%s", payload, str_co2); // Adds the value
  sprintf(payload, "%s}", payload); // Closes the dictionary brackets
  client.publish(topic, payload);
  Serial.println(payload);
  delay(500);
 
  client.loop();
}
 
void setup() {
  Serial.begin(115200);
  while (sgp_probe() != STATUS_OK) {
      Serial.println("SGP failed");
  }
  sgp_set_absolute_humidity(13000);
  sgp_iaq_init();
 
    tft.init();
    tft.begin();            
    tft.setRotation(3);     
    digitalWrite(LCD_BACKLIGHT, HIGH); 
    tft.fillScreen(tft.color565(130,244,207));
    Wire.begin();      
    Serial.begin(115200);  
    Serial.println("serial start!!"); 
    while (sgp_probe() != STATUS_OK) 
    { 
     Serial.println("SGP failed");
    while (1);
    }
 
//  while(!Serial);
 
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
 
  tft.drawString("Connecting to WiFi...",20,120);
  WiFi.begin(WIFISSID, PASSWORD);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    WiFi.begin(WIFISSID, PASSWORD);
  }
  delay(1000);
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);
}
 
void loop() {
  read_sgp30();    //Reading sgp30 sensor values
  send_data();     //Sending data to Ubidots
  delay(5000);
  u16 tvoc_ppb, co2_eq_ppm;  
    s16 err = 0;
    err = sgp_measure_iaq_blocking_read(&tvoc_ppb, &co2_eq_ppm);
    if (err == STATUS_OK) 
    {
      Serial.print("tVOC  Concentration:");
      Serial.println("ppb");
      Serial.print("CO2eq Concentration:");
      Serial.print(co2_eq_ppm);
      Serial.println("ppm");
    } else {
      Serial.println("error\n");
    }  
     tft.fillScreen(tft.color565(130,244,207));        
     tft.setTextSize(4);
     tft.setTextColor(TFT_BLACK);
     tft.drawString("VOC: ", 40, 70);
     tft.drawNumber(tvoc_ppb, 40, 140, 1);
     tft.drawString("CO2: ", 200, 70); 
     tft.drawNumber(co2_eq_ppm, 200, 140, 1);
     spr.pushSprite(0, 0);
     spr.deleteSprite();
     delay(1500); 
}

Credits

qiu776

qiu776

1 project • 1 follower

Comments