542 lines
15 KiB
C++
542 lines
15 KiB
C++
#include <Wire.h>
|
|
// display mapping suggestion for Arduino MEGA
|
|
// BUSY -> 7, RST -> 9, DC -> 8, CS-> 53, CLK -> 52, DIN -> 51
|
|
#include <GxEPD2_BW.h>
|
|
#include <Fonts/FreeSans18pt7b.h>
|
|
#include <Fonts/FreeSansBold12pt7b.h>
|
|
#include <Fonts/FreeSansBold9pt7b.h>
|
|
#include<Fonts/TomThumb.h>
|
|
//rtc includes
|
|
#include <MD_DS3231.h>
|
|
//#include <BME280I2C.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <Adafruit_BME280.h>
|
|
#include<bc_co2_module_arduino.h>
|
|
|
|
//BME280I2C bme;
|
|
Adafruit_BME280 bme;
|
|
|
|
/**************************** Serial setup ************************************/
|
|
#define BAUDRATE 115200
|
|
|
|
/**************************** I2C setup **************************************/
|
|
|
|
#define BME280_ADDR 0x76
|
|
#define BME280_ID 0x60
|
|
|
|
/**************************** Screen setup ***********************************/
|
|
|
|
#define MAX_DISPAY_BUFFER_SIZE 32 //
|
|
#define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / (EPD::WIDTH / 8))
|
|
GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
|
/*************************** Global variables *********************************/
|
|
|
|
struct data {
|
|
float temp;
|
|
float hum;
|
|
int rhum;
|
|
float pres;
|
|
unsigned int rpres;
|
|
int16_t co2;
|
|
};
|
|
|
|
volatile static struct data dataset;
|
|
//
|
|
|
|
void setup() {
|
|
pinMode(13, OUTPUT);
|
|
digitalWrite(13, LOW); //turn off led
|
|
|
|
Serial.begin(BAUDRATE);
|
|
Serial2.begin(BAUDRATE);
|
|
Wire.begin();
|
|
|
|
Serial.print(F("\r\n***Enviro data monitor***\r\n"));
|
|
//start co2 module
|
|
init_module();
|
|
|
|
while(!bme.begin(BME280_ADDR)) { //for adafruit driver
|
|
// while(!bme.begin()) { //for bme280i2c driver
|
|
Serial.println(F("Could not find BME280 sensor"));
|
|
delay(1000);
|
|
}
|
|
|
|
//for adafruit driver
|
|
if(bme.sensorID() == BME280_ID){
|
|
Serial.println(F("Found BME280 sensor"));
|
|
}
|
|
else {
|
|
Serial.println(F("Found unknown sensor"));
|
|
}
|
|
|
|
//for bme280i2c driver
|
|
// switch(bme.chipModel()) {
|
|
// case BME280::ChipModel_BME280:
|
|
// Serial.println(F("Found BME280 sensor!"));
|
|
// break;
|
|
// case BME280::ChipModel_BMP280:
|
|
// Serial.println(F("Found BMP280 sensor! No Humidity available."));
|
|
// break;
|
|
// default:
|
|
// Serial.println(F("Error, found UNKNOWN sensor."));
|
|
// }
|
|
|
|
display.init(); //init display
|
|
delay(100);
|
|
layoutScreen(); //print static parts of screen
|
|
|
|
delay(7000); //wait for esp to finish booting, this usually takes around 5s, we want to be extra sure so waiting for 7
|
|
|
|
//make sure we're always in 24hr mode
|
|
if(RTC.status(DS3231_12H)){
|
|
RTC.control(DS3231_12H, DS3231_OFF);
|
|
switch (RTC.status(DS3231_12H) ){
|
|
case 0: Serial.println(F("24h mode"));
|
|
case 1: Serial.println(F("12h mode")); //should not occur
|
|
}
|
|
}
|
|
|
|
static byte retries = 0;
|
|
while(!syncTime()){ //sync time with network
|
|
delay(1500); //should take about a second and a half
|
|
retries++;
|
|
if(retries > 4){
|
|
break; //break the loop in case of no connectivity
|
|
}
|
|
}
|
|
|
|
printRtimeToScreen(); //print time to screen
|
|
printRtDateToScreen(); //print date to screen
|
|
|
|
Serial.println(F("Setup complete"));
|
|
};
|
|
|
|
void loop() {
|
|
static byte oldmins;
|
|
|
|
RTC.readTime();
|
|
if(RTC.m != oldmins){ //run this every minute
|
|
printRtimeToScreen(); //update time on screen
|
|
doStuff(); //acquire data
|
|
sendDataToServer(); //send over to esp for publishing
|
|
printTempToScreen(); //update values on screen
|
|
oldmins = RTC.m; //save old value
|
|
}
|
|
else if((RTC.m == 0 || RTC.m == 30) && RTC.s == 55) {
|
|
delay(1100); //delay to prevent firing multiple times
|
|
display.refresh(); //perform a full display refresh every half hour
|
|
}
|
|
else if(RTC.m == 0 && RTC.h == 0 && RTC.s == 40){
|
|
delay(1100); //delay to prevent firing multiple times
|
|
printRtDateToScreen(); //change date on midnight
|
|
}
|
|
|
|
//sync time every morning and evening. only here for safety should the device run for long periods of time without restarting
|
|
if ((RTC.h == 6 || RTC.h == 22) && (RTC.m == 00) && RTC.s == 40){
|
|
delay(1100); //delay to prevent firing multiple times
|
|
static byte retries = 0;
|
|
while(!syncTime()){ //sync time with network
|
|
delay(1500); //should take about a second and a half
|
|
retries++;
|
|
if(retries > 4){
|
|
break; //break the loop in case of no connectivity
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
void doStuff() {
|
|
//function to acquire and save data from sensors
|
|
dataset.temp = measureTemp();
|
|
dataset.hum = measureHum();
|
|
dataset.rhum = round(dataset.hum);
|
|
dataset.pres = measurePres();
|
|
dataset.rpres = round(dataset.pres);
|
|
dataset.co2 = measureCO2();
|
|
};
|
|
|
|
bool syncTime() {
|
|
char tbuf;
|
|
static bool trecvd = false;
|
|
static bool trecvn = false;
|
|
char tarry[21];
|
|
// int yyyy;
|
|
// int mm;
|
|
// int dd;
|
|
// int h;
|
|
// int m;
|
|
// int s;
|
|
static char j = 0;
|
|
static char l = 0;
|
|
|
|
Serial.print(F("Syncing time...\n"));
|
|
delay(50);
|
|
Serial2.print("?ntp");
|
|
Serial2.print("\n");
|
|
delay(25);
|
|
while(Serial2.available() > 0 && trecvd == false){
|
|
tbuf = Serial2.read();
|
|
if(trecvn == true) {
|
|
if(tbuf != '\n'){
|
|
tarry[j] = tbuf;
|
|
j++;
|
|
}
|
|
else {
|
|
tarry[j] = '\0';
|
|
trecvn = false;
|
|
j = 0;
|
|
trecvd = true;
|
|
}
|
|
}
|
|
else if (tbuf == '>'){
|
|
trecvn = true;
|
|
}
|
|
}
|
|
if(trecvd == true){
|
|
//Serial output for debugging
|
|
Serial.println(tarry);
|
|
// Serial.println("Parsed time");
|
|
char* token = strtok(tarry, "/");
|
|
while(token != NULL){
|
|
if(l == 0){
|
|
// yyyy = atoi(token);
|
|
RTC.yyyy = atoi(token);
|
|
};
|
|
if(l == 1) {
|
|
// mm = atoi(token);
|
|
RTC.mm = atoi(token);
|
|
};
|
|
if(l == 2) {
|
|
// dd = atoi(token);
|
|
RTC.dd = atoi(token);
|
|
};
|
|
if(l == 3) {
|
|
// h = atoi(token);
|
|
RTC.h = atoi(token);
|
|
};
|
|
if(l == 4){
|
|
// m = atoi(token);
|
|
RTC.m = atoi(token);
|
|
};
|
|
if(l == 5){
|
|
// s = atoi(token);
|
|
RTC.s = atoi(token);
|
|
};
|
|
token = strtok(NULL, "/");
|
|
l++;
|
|
}
|
|
l = 0;
|
|
//Serial output for debugging
|
|
// Serial.println(yyyy);
|
|
// Serial.println(mm);
|
|
// Serial.println(dd);
|
|
// Serial.println(h);
|
|
// Serial.println(m);
|
|
// Serial.println(s);
|
|
// RTC.yyyy = yyyy;
|
|
// RTC.mm = mm;
|
|
// RTC.dd = dd;
|
|
// RTC.h = h;
|
|
// RTC.m = m;
|
|
// RTC.s = s;
|
|
Serial.println(F("NTP sync complete"));
|
|
RTC.writeTime();
|
|
trecvd = false;
|
|
return(true);
|
|
}
|
|
}
|
|
|
|
void layoutScreen() {
|
|
display.setRotation(0); //portrait, connector at the top
|
|
display.setFullWindow();
|
|
|
|
display.setFont(&TomThumb);
|
|
display.setTextColor(GxEPD_BLACK);
|
|
display.firstPage();
|
|
do {
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(8, 69);
|
|
display.print(F("Temperature:"));
|
|
display.setCursor(8, (69 + 40));
|
|
display.print(F("Humidity:"));
|
|
display.setCursor(8, (69 + 80));
|
|
display.print(F("Pressure:"));
|
|
display.setCursor(8, (69 + 120));
|
|
display.print(F("CO2 concentration:"));
|
|
}
|
|
while (display.nextPage());
|
|
}
|
|
|
|
void printRtimeToScreen() {
|
|
char delim[] = ":";
|
|
byte hrs;
|
|
byte DST;
|
|
|
|
DST = checkDST(); //check whether DST is active and set the var accordingly
|
|
|
|
RTC.readTime();
|
|
hrs = RTC.h;
|
|
if(DST == 1){
|
|
hrs = hrs + 1;
|
|
}
|
|
|
|
display.setFont(&FreeSans18pt7b);
|
|
display.setTextColor(GxEPD_BLACK);
|
|
int16_t tbx, tby; uint16_t tbw, tbh;
|
|
display.getTextBounds(delim, 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x = ((display.width() / 2) - tbw);
|
|
uint16_t y = ((32 - tbh) / 2) - (tby - 4);
|
|
display.getTextBounds("00", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x2 = (display.width() / 2) - 8 - tbw ;
|
|
uint16_t y2 = ((32 - tbh) / 2) - ( tby - 4);
|
|
display.getTextBounds("00", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x3 = (display.width() / 2) + 8;
|
|
|
|
display.setPartialWindow(0, 0, display.width(), (display.height() - (display.height() - 32)));
|
|
|
|
display.firstPage();
|
|
do {
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x, y);
|
|
display.print(delim);
|
|
display.setCursor(x2, y2);
|
|
if(hrs < 10) {
|
|
display.print("0");
|
|
display.print(hrs);
|
|
}
|
|
else {
|
|
display.print(hrs);
|
|
}
|
|
display.setCursor(x3, y2);
|
|
if(RTC.m < 10) {
|
|
display.print("0");
|
|
display.print(RTC.m);
|
|
}
|
|
else {
|
|
display.print(RTC.m);
|
|
}
|
|
}
|
|
while(display.nextPage());
|
|
}
|
|
|
|
void printRtDateToScreen() {
|
|
RTC.readTime();
|
|
display.setFont(&FreeSansBold9pt7b);
|
|
display.setTextColor(GxEPD_BLACK);
|
|
int16_t tbx, tby; uint16_t tbw, tbh;
|
|
display.getTextBounds("00 - 00 - 0000", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x = (display.width() / 2) - (tbw / 2) ;
|
|
uint16_t y = 48 + (tbh / 2);
|
|
|
|
display.setPartialWindow(0, 33, display.width(), 31);
|
|
display.firstPage();
|
|
do {
|
|
//display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x, y);
|
|
if(RTC.dd < 10) {
|
|
display.print("0");
|
|
display.print(RTC.dd);
|
|
}
|
|
else {
|
|
display.print(RTC.dd);
|
|
}
|
|
display.print(" - ");
|
|
if(RTC.mm < 10){
|
|
display.print("0");
|
|
display.print(RTC.mm);
|
|
}
|
|
else {
|
|
display.print(RTC.mm);
|
|
}
|
|
display.print(" - ");
|
|
display.print(RTC.yyyy);
|
|
}
|
|
while(display.nextPage());
|
|
}
|
|
|
|
void printTempToScreen() {
|
|
struct data *dtsp = &dataset;
|
|
display.setFont(&FreeSansBold9pt7b);
|
|
display.setTextColor(GxEPD_BLACK);
|
|
int16_t tbx, tby; uint16_t tbw, tbh;
|
|
display.getTextBounds("00.0 C", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x = ((display.width() - tbw) / 2) - tbx;
|
|
uint16_t y = (92 - tbh) - tby;
|
|
display.getTextBounds("00 %", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x2 = ((display.width() - tbw) / 2) - tbx;
|
|
uint16_t y2 = (132 - tbh) - tby;
|
|
display.getTextBounds("1488 hpa", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t x3 = ((display.width() - tbw) / 2) - tbx;
|
|
uint16_t y3 = ( 176 - tbh) - tby;
|
|
display.getTextBounds("1488 ppm", 0, 0, &tbx, &tby, &tbw, &tbh);
|
|
uint16_t y4 = ( 216 - tbh) - tby;
|
|
|
|
display.setPartialWindow(0, 70, display.width(), 33);
|
|
display.firstPage();
|
|
do {
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x, y);
|
|
display.print(dtsp -> temp, 1);
|
|
display.print(F(" C"));
|
|
}
|
|
while (display.nextPage());
|
|
|
|
display.setPartialWindow(0, 110, display.width(), 33);
|
|
display.firstPage();
|
|
do {
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x2, y2);
|
|
display.print(dtsp -> rhum);
|
|
display.print(F(" %"));
|
|
}
|
|
while(display.nextPage());
|
|
|
|
display.setPartialWindow(0, 150 ,display.width(), 33);
|
|
display.firstPage();
|
|
do {
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x3, y3);
|
|
display.print(dtsp -> rpres);
|
|
display.print(F(" hPa"));
|
|
}
|
|
while(display.nextPage());
|
|
|
|
display.setPartialWindow(0, 190 ,display.width(), 33);
|
|
display.firstPage();
|
|
do {
|
|
display.fillScreen(GxEPD_WHITE);
|
|
display.setCursor(x3, y4);
|
|
display.print(dtsp -> co2);
|
|
display.print(F(" ppm"));
|
|
}
|
|
while(display.nextPage());
|
|
}
|
|
|
|
float measureTemp() {
|
|
// BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
|
|
int temps[6];
|
|
|
|
//light led while measuring
|
|
digitalWrite(13, HIGH);
|
|
|
|
Serial.print(F("Measuring temperature..."));
|
|
|
|
for(char i = 0; i <= 5; i++){
|
|
temps[i] = bme.readTemperature() * 100;
|
|
delay(1000);
|
|
}
|
|
|
|
float temp;
|
|
|
|
temp = (temps[1] + temps[2] + temps[3] + temps[4] + temps[5]) / 5;
|
|
|
|
Serial.print(F("done: "));
|
|
Serial.println(temp / 100);
|
|
digitalWrite(13, LOW);
|
|
return temp / 100;
|
|
}
|
|
|
|
float measureHum() {
|
|
int hums[6];
|
|
|
|
//light led while measuring
|
|
digitalWrite(13, HIGH);
|
|
|
|
Serial.print(F("Measuring humidity..."));
|
|
|
|
for(char i = 0; i <= 5; i++){
|
|
hums[i] = bme.readHumidity() * 100;
|
|
delay(1000);
|
|
}
|
|
|
|
float hum;
|
|
|
|
hum = (hums[1] + hums[2] + hums[3] + hums[4] + hums[5]) / 5;
|
|
|
|
Serial.print(F("done: "));
|
|
Serial.println(hum / 100);
|
|
|
|
digitalWrite(13, LOW);
|
|
return hum / 100;
|
|
}
|
|
|
|
float measurePres() {
|
|
// BME280::PresUnit presUnit(BME280::PresUnit_hPa);
|
|
float presrs[6];
|
|
|
|
//light led while measuring
|
|
digitalWrite(13, HIGH);
|
|
|
|
Serial.print(F("Measuring pressure..."));
|
|
|
|
for(char i = 0; i <= 5; i++){
|
|
// presrs[i] = bme.pres();
|
|
presrs[i] = bme.readPressure() / 100.0F; //returns in Pa, divide by 100 to get value in hPa
|
|
delay(1000);
|
|
}
|
|
|
|
float pres;
|
|
|
|
pres = (presrs[1] + presrs[2] + presrs[3] + presrs[4] + presrs[5]) / 5;
|
|
|
|
Serial.print(F("done: "));
|
|
Serial.println(pres);
|
|
|
|
digitalWrite(13, LOW);
|
|
return pres;
|
|
}
|
|
|
|
int16_t measureCO2(){
|
|
digitalWrite(13, HIGH);
|
|
Serial.print(F("Measuring CO2..."));
|
|
|
|
int16_t co2conc = get_concentration();
|
|
|
|
Serial.print(F("done:"));
|
|
Serial.println(co2conc);
|
|
|
|
digitalWrite(13, LOW);
|
|
return(co2conc);
|
|
}
|
|
|
|
bool sendDataToServer(){
|
|
char payload[30];
|
|
char stemp[8];
|
|
char shum[8];
|
|
dtostrf(dataset.temp, 5, 2, stemp);
|
|
dtostrf(dataset.hum, 5, 2, shum); //convert floats to strings
|
|
sprintf(payload, "%s,%s,%d,%d", stemp, shum, dataset.rpres, dataset.co2); //convert everything to a string
|
|
Serial2.print(payload); //send over serial 2 to esp
|
|
Serial2.print('\n'); //terminate transmission
|
|
delay(1000); //give time to process comm
|
|
Serial.print(F("Data sent\r\n"));
|
|
return(true);
|
|
}
|
|
|
|
byte checkDST() {
|
|
byte DST;
|
|
RTC.readTime();
|
|
// ********************* Calculate offset for Sunday *********************
|
|
int y = RTC.yyyy - 2000; //take only last two digits of year (while not universal, this will work for the next 80 years so good enough for me)
|
|
int x = (y + y/4 + 2) % 7; // remainder will identify which day of month
|
|
// is Sunday by subtracting x from the one
|
|
// or two week window. First two weeks for March
|
|
// and first week for November
|
|
// *********** BEGINS on 2nd Sunday of March @ 2:00 AM *********
|
|
if(RTC.mm == 3 && RTC.dd == (14 - x) && RTC.h >= 2) {
|
|
DST = 1;
|
|
}
|
|
if((RTC.mm == 3 && RTC.dd > (14 - x)) || RTC.mm > 3) {
|
|
DST = 1;
|
|
}
|
|
// ************* ENDS on 1st Sunday of Nov @ 2:00 AM ************
|
|
if(RTC.mm == 11 && RTC.dd == (7 - x) && RTC.h >= 2) {
|
|
DST = 0;
|
|
}
|
|
if((RTC.mm == 11 && RTC.dd > (7 - x)) || RTC.mm > 11 || RTC.mm < 3) {
|
|
DST = 0;
|
|
}
|
|
return(DST);
|
|
}
|