Sleeping Room Sensor

Primary tabs

I did build an indoor air quality sensor with this breakout board ccs811 sensor

I did find it very interessting to see the air quality in the sleeping room and compare it to the feeling i did have about my sleeping quality.

link to Co2 levels which harm you  and link to interessting report which show this tabel

Here you see a screendump of my data ... I do use a pinboard from allthingstalk.com

Here you can see the code for the arduino

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include "LowPower.h"
#include <sSense-CCS811.h>
#include "ClosedCube_HDC1080.h"

ClosedCube_HDC1080 hdc1080;

CCS811 ssenseCCS811;

#define ENABLE_DEBUG (0)


// keys from TheThingsNetwork
// I do use ABP so have to put them in manually
// this because of LowPower
// update will come later when found a solution ;o)
static const PROGMEM u1_t NWKSKEY[16] = { 0x67, 0xFA, 0x4A, 0xC4, 0xCF, 0x6C, 0x1D, 0xB6, 0x0C, 0x01, 0xDF, 0x17, 0xAD, 0xDC, 0xB5, 0x00 };
static const u1_t PROGMEM APPSKEY[16] = { 0xD4, 0x59, 0x9A, 0xFF, 0xBC, 0x71, 0x4B, 0xA2, 0x9E, 0x3E, 0x9E, 0xAB, 0xEA, 0xA1, 0x81, 0x00 };
static const u4_t DEVADDR = 0x260xxxx ;

const lmic_pinmap lmic_pins = {
    .nss = 6,                      // Digital pin connected to SS
    .rxtx = LMIC_UNUSED_PIN,       // we do not use these
    .rst = 5,                      // Digital pin connected to RST
    .dio = {2, 3, 4},              // Digital pin connected to 00,01,02 
};

// Our array to put data into
// The Things Network suggest max 10 byte

static uint8_t mydata[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void do_getsensor_values(){
  
  int temp = hdc1080.readTemperature()*100;
  temp = temp-400;
  mydata[0]= highByte(temp);
  mydata[1]= lowByte(temp);
  delay(10);
  int hum = hdc1080.readHumidity()*100;
  mydata[2]= highByte(hum);
  mydata[3]= lowByte(hum);
  delay(10);
  ssenseCCS811.setEnvironmentalData(hdc1080.readTemperature(), hdc1080.readHumidity()); 
  if (ssenseCCS811.checkDataAndUpdate())
  {
    int co2 = ssenseCCS811.getCO2();
    mydata[4]= highByte(co2);
    mydata[5]= lowByte(co2);

    int tvoc = ssenseCCS811.gettVOC();
    mydata[6]= highByte(tvoc);
    mydata[7]= lowByte(tvoc);
  }
}

void do_sleep(){
  for (int i=0; i<=8; i++) {
    // Here we go to sleep ...
    // watchdog can go max 8 sec
    // so we make a loop 8 times 10 equal times 2 160 sec between ( times 2 because of 8 mhz clock )
    // each time we do connetc to gateway
    // think this only work for ABP

   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
   
  }
}

////////////////////////////////////////////////////////////////////////////
///////////////// DO NOT CHANGE BELOW THIS LINE  ///////////////////////////
////////////////////////////////////////////////////////////////////////////

static osjob_t sendjob;
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }

void onEvent (ev_t ev) {
    //Serial.print(os_getTime());
    //Serial.print(": ");
    switch(ev) {
        case EV_SCAN_TIMEOUT:
            //Serial.println(F("EV_SCAN_TIMEOUT"));
            break;
        case EV_BEACON_FOUND:
            //Serial.println(F("EV_BEACON_FOUND"));
            break;
        case EV_BEACON_MISSED:
            //Serial.println(F("EV_BEACON_MISSED"));
            break;
        case EV_BEACON_TRACKED:
            //Serial.println(F("EV_BEACON_TRACKED"));
            break;
        case EV_JOINING:
            //Serial.println(F("EV_JOINING"));
            break;
        case EV_JOINED:
            //Serial.println(F("EV_JOINED"));
            break;
        case EV_RFU1:
            //Serial.println(F("EV_RFU1"));
            break;
        case EV_JOIN_FAILED:
            //Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            //Serial.println(F("EV_REJOIN_FAILED"));
            break;
            break;
        case EV_TXCOMPLETE:
            //Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            if (LMIC.txrxFlags & TXRX_ACK)
                //Serial.println(F("Received ack"));
            if(LMIC.dataLen) {
                // data received in rx slot after tx
                //Serial.print(F("Data Received: "));
                //Serial.write(LMIC.frame+LMIC.dataBeg, LMIC.dataLen);
                //Serial.println();
            }
            delay(100);
            do_sleep();
            delay(100);
            do_getsensor_values();
            do_send(&sendjob);
            break;
        case EV_LOST_TSYNC:
            //Serial.println(F("EV_LOST_TSYNC"));
            break;
        case EV_RESET:
            //Serial.println(F("EV_RESET"));
            break;
        case EV_RXCOMPLETE:
            // data received in ping slot
            //Serial.println(F("EV_RXCOMPLETE"));
            break;
        case EV_LINK_DEAD:
            //Serial.println(F("EV_LINK_DEAD"));
            break;
        case EV_LINK_ALIVE:
            //Serial.println(F("EV_LINK_ALIVE"));
            break;
         default:
            //Serial.println(F("Unknown event"));
            break;
    }
}

void do_send(osjob_t* j){
    // Check if there is not a current TX/RX job running
    if (LMIC.opmode & OP_TXRXPEND) {
        //Serial.println(F("OP_TXRXPEND, not sending"));
    } else {
        LMIC_setTxData2(1, mydata, sizeof(mydata), 0);
        //Serial.println(F("Packet queued"));
    }
}

void setup() {
   // Serial.begin(115200);
   // Serial.println(F("Starting"));
    ssenseCCS811.begin(uint8_t(0x5A), uint8_t(6), driveMode_1sec);
    hdc1080.begin(0x40);
    os_init();
    LMIC_reset();
    #ifdef PROGMEM
      uint8_t appskey[sizeof(APPSKEY)];
      uint8_t nwkskey[sizeof(NWKSKEY)];
      memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
      memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
      LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
    #else
      LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
    #endif
    LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI);      // g-band
    LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
    LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK,  DR_FSK),  BAND_MILLI);      // g2-band
    LMIC_setLinkCheckMode(0);
    LMIC_setDrTxpow(DR_SF7, 14);    // SF7 and max power from module = 14
    #define LMIC_CLOCK_ERROR_PERCENTAGE 3
    LMIC_setClockError(LMIC_CLOCK_ERROR_PERCENTAGE * (MAX_CLOCK_ERROR / 100.0));
    do_getsensor_values();
    do_send(&sendjob);
}

void loop() {  
   os_runloop_once();                             
}