Omar Almatov
Published © GPL3+

MeteoStation

This is a simple weather station that can measure PM 1.0, PM 2.5, PM 10.0, CO2, TVOC, humidity, pressure, and temperature.

IntermediateShowcase (no instructions)763
MeteoStation

Things used in this project

Hardware components

Seeed HM3301
×1
Adafruit BMP280
×1
SparkFun Air Quality Breakout - CCS811
SparkFun Air Quality Breakout - CCS811
×1
Gravity:SHT1x Humidity and Temperature Sensor
DFRobot Gravity:SHT1x Humidity and Temperature Sensor
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Code

meteo.cpp

C/C++
This program collects data from all four sensors and saves it to an SQLite database.
#include <getopt.h>
#include <sstream>
#include <ctime>
#include <sqlite3.h>

#include "bmp280.h"
#include "ccs811.h"
#include "hm3301.h"
#include "sht20.h"

using namespace std;

const char*     DB_FILE( "/var/lib/sqlite3/meteo.db" );

static struct option LONGOPTIONS_[] = 
{
    { "reset",          no_argument,    nullptr, 'r' },
    { "environment",    no_argument,    nullptr, 'e' },
    { "help",           no_argument,    nullptr, 'h' },
    { nullptr,          0,              nullptr,  0  }
};

int main( int argc, char* argv[] )
{
    Bmp280      bmp280( "/dev/i2c-1", 0x77 );
    Ccs811      ccs811( "/dev/i2c-1", 0x5A );
    Hm3301      hm3301( "/dev/i2c-0", 0x40 );
    Sht20       sht20( "/dev/i2c-1", 0x40 );
    string      progname( argv[0] );
    int         opt;

    bmp280.readData();
    ccs811.readData();
    hm3301.readData();
    sht20.readData();

    while ( (opt = getopt_long(argc,argv,"reh",LONGOPTIONS_,nullptr)) != -1 )
    {
        switch ( opt )
        {
            case 'r':

                ccs811.reset();

            break;

            case 'e':

                ccs811.setEnvironmentalData( sht20.humidity(), sht20.temperature() );

            break;
            
            case 'h':
            default:

            cout << 
            "Usage: " << progname << " [options]\n"
            "\t-r --reset\t\tReset CCS811\n"
            "\t-e --humidity\t\tSet environmental data\n"
            "\t-h --help \t\tPrint help message\n";

            return EXIT_SUCCESS;
        }
    }

    sqlite3*    db;

    if ( sqlite3_open(DB_FILE, &db) )
    {
        cerr << "Cannot open database" << endl;
    }

    stringstream    sql;

    sql << "INSERT INTO meteo VALUES ("; 
    sql << time(nullptr) << ", "; 
    sql << dec << bmp280.temperature() << ", "; 
    sql << sht20.humidity() << ", "; 
    sql << dec << bmp280.pressure() << ", ";
    sql << ccs811.co2() << ", ";
    sql << ccs811.tvoc() << ", ";
    sql << hm3301.spm010() << ", ";
    sql << hm3301.spm025() << ", ";
    sql << hm3301.spm100() << ");";

    cout << sql.str() << endl;


    sqlite3_exec( db, sql.str().c_str(), nullptr, nullptr, nullptr );

    /*cout << "Temperature: " << dec << bmp280.temperature() << " C" << endl;
    cout << "Pressure: " << dec << bmp280.pressure() << endl; 
    cout << "CO2: " << ccs811.co2() << endl;
    cout << "TVOC: " << ccs811.tvoc() << endl;
    cout << "Humidity: " << sht20.humidity() << "%" << endl;
    cout << "Standard PM1.0\t" << hm3301.spm010() << endl;
    cout << "Standard PM2.5\t" << hm3301.spm025() << endl;
    cout << "Standard PM10\t" << hm3301.spm100() << endl;*/
}

BMP280

HM3301

CCS811

SHT20

Credits

Omar Almatov

Omar Almatov

0 projects • 0 followers

Comments

Add projectSign up / Login