fw_mega/fw_mega.ino

506 lines
14 KiB
Arduino
Raw Normal View History

2020-02-14 14:19:51 +01:00
#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>
2020-04-10 14:01:56 +02:00
#include<Fonts/TomThumb.h>
2020-02-14 14:19:51 +01:00
//rtc includes
#include <MD_DS3231.h>
//#include <BME280I2C.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
2020-04-10 14:01:56 +02:00
#include<bc_co2_module_arduino.h>
2020-02-14 14:19:51 +01:00
//BME280I2C bme;
Adafruit_BME280 bme;
2020-02-15 22:03:22 +01:00
/**************************** Serial setup ************************************/
#define BAUDRATE 115200
2020-02-14 14:19:51 +01:00
2020-02-15 22:03:22 +01:00
/**************************** I2C setup **************************************/
2020-02-14 14:19:51 +01:00
2020-02-15 22:03:22 +01:00
#define BME280_ADDR 0x76
#define BME280_ID 0x60
2020-02-14 14:19:51 +01:00
2020-02-15 22:03:22 +01:00
/**************************** Screen setup ***********************************/
2020-02-14 14:19:51 +01:00
2020-02-15 22:03:22 +01:00
#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 *********************************/
2020-02-14 14:19:51 +01:00
struct data {
2020-02-14 15:21:43 +01:00
int yr;
char mo;
char dy;
char hr;
char mn;
char s;
float temp;
float hum;
int rhum;
float pres;
unsigned int rpres;
2020-04-10 14:01:56 +02:00
int16_t co2;
2020-02-14 14:19:51 +01:00
};
volatile static struct data dataset;
static bool reqTimeSync = false;
//
2020-02-15 22:03:22 +01:00
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(BAUDRATE);
Serial2.begin(BAUDRATE);
Wire.begin();
2020-04-10 14:01:56 +02:00
Serial.print(F("\r\n***Enviro data monitor***\r\n"));
//start co2 module
init_module();
2020-02-15 22:03:22 +01:00
2020-04-10 14:01:56 +02:00
while(!bme.begin(BME280_ADDR)) { //for adafruit driver
// while(!bme.begin()) { //for bme280i2c driver
Serial.println(F("Could not find BME280 sensor"));
2020-02-15 22:03:22 +01:00
delay(1000);
}
2020-04-10 14:01:56 +02:00
//for adafruit driver
2020-02-15 22:03:22 +01:00
if(bme.sensorID() == BME280_ID){
2020-04-10 14:01:56 +02:00
Serial.println(F("Found BME280 sensor"));
2020-02-15 22:03:22 +01:00
}
else {
2020-04-10 14:01:56 +02:00
Serial.println(F("Found unknown sensor"));
2020-02-15 22:03:22 +01:00
}
2020-04-10 14:01:56 +02:00
//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."));
// }
2020-02-15 22:03:22 +01:00
delay(100);
2020-04-10 14:01:56 +02:00
display.init(); //init display
2020-02-15 22:03:22 +01:00
delay(100);
makeGrid(); //draw divider grid
delay(7000); //wait for esp to finish booting, this usually takes around 5s
//make sure we're always in 24hr mode because of db timestamps
if(RTC.status(DS3231_12H)){
RTC.control(DS3231_12H, DS3231_OFF);
switch (RTC.status(DS3231_12H) ){
2020-04-10 14:01:56 +02:00
case 0: Serial.println(F("24h mode"));
case 1: Serial.println(F("12h mode")); //should not occur
2020-02-15 22:03:22 +01:00
}
}
while(!syncTime()){ //sync time with network
delay(75);
}
printRtimeToScreen();
printRtDateToScreen();
2020-04-10 14:01:56 +02:00
Serial.println(F("Setup complete"));
2020-02-14 14:19:51 +01:00
};
void loop() {
2020-02-14 15:21:43 +01:00
static unsigned char oldmins;
2020-02-15 22:03:22 +01:00
2020-02-14 15:21:43 +01:00
RTC.readTime();
if(RTC.m != oldmins){
printRtimeToScreen();
doStuff(); //measure data
printTempToScreen();
sendDataToServer();
oldmins = RTC.m;
}
2020-04-10 14:01:56 +02:00
else if(RTC.m == 0 && RTC.h == 0 && RTC.s == 5){
2020-02-14 15:21:43 +01:00
printRtDateToScreen();
2020-02-15 22:03:22 +01:00
delay(1000);
2020-02-14 15:21:43 +01:00
}
2020-04-10 14:01:56 +02:00
// if (RTC.h == 6 || RTC.h == 22 && RTC.m == 00 && RTC.s == 30 && reqTimeSync ==true){
// reqTimeSync = false;
// while(!syncTime()){
// delay(75);
// }
// }
2020-02-14 15:21:43 +01:00
};
2020-02-14 14:19:51 +01:00
2020-02-15 22:03:22 +01:00
void doStuff() {
2020-04-10 14:01:56 +02:00
RTC.readTime();
2020-02-15 22:03:22 +01:00
dataset.temp = measureTemp();
dataset.hum = measureHum();
dataset.rhum = round(dataset.hum);
dataset.pres = measurePres();
dataset.rpres = round(dataset.pres);
2020-04-10 14:01:56 +02:00
dataset.co2 = measureCO2();
2020-02-15 22:03:22 +01:00
dataset.yr = RTC.yyyy;
dataset.mo = RTC.mm;
dataset.dy = RTC.dd;
dataset.hr = RTC.h;
dataset.mn = RTC.m;
dataset.s = RTC.s;
};
bool syncTime() {
2020-02-14 15:21:43 +01:00
char tbuf;
static bool trecvd = false;
static bool trecvn = false;
char tarry[21];
2020-02-15 22:03:22 +01:00
// int yyyy;
// int mm;
// int dd;
// int h;
// int m;
// int s;
2020-02-14 15:21:43 +01:00
static char j = 0;
static char l = 0;
2020-02-14 14:19:51 +01:00
2020-04-10 14:01:56 +02:00
Serial.print(F("Syncing time...\n"));
2020-02-14 15:21:43 +01:00
delay(50);
Serial2.print("?ntp\n");
2020-02-15 22:03:22 +01:00
delay(25);
2020-02-14 15:21:43 +01:00
while(Serial2.available() > 0 && trecvd == false){
tbuf = Serial2.read();
2020-02-15 22:03:22 +01:00
if(trecvn == true) {
2020-02-14 15:21:43 +01:00
if(tbuf != '\n'){
tarry[j] = tbuf;
j++;
2020-02-15 22:03:22 +01:00
}
else {
2020-02-14 15:21:43 +01:00
tarry[j] = '\0';
trecvn = false;
j = 0;
trecvd = true;
}
2020-02-15 22:03:22 +01:00
}
else if (tbuf == '>'){
2020-02-14 15:21:43 +01:00
trecvn = true;
}
2020-02-14 14:19:51 +01:00
}
2020-02-14 15:21:43 +01:00
if(trecvd == true){
//Serial output for debugging
2020-02-15 22:03:22 +01:00
Serial.println(tarry);
2020-02-14 15:21:43 +01:00
// Serial.println("Parsed time");
2020-02-15 22:03:22 +01:00
char* token = strtok(tarry, "/");
2020-02-14 15:21:43 +01:00
while(token != NULL){
if(l == 0){
2020-02-15 22:03:22 +01:00
// yyyy = atoi(token);
RTC.yyyy = atoi(token);
2020-02-14 15:21:43 +01:00
};
if(l == 1) {
2020-02-15 22:03:22 +01:00
// mm = atoi(token);
RTC.mm = atoi(token);
2020-02-14 15:21:43 +01:00
};
if(l == 2) {
2020-02-15 22:03:22 +01:00
// dd = atoi(token);
RTC.dd = atoi(token);
2020-02-14 15:21:43 +01:00
};
if(l == 3) {
2020-02-15 22:03:22 +01:00
// h = atoi(token);
RTC.h = atoi(token);
2020-02-14 15:21:43 +01:00
};
if(l == 4){
2020-02-15 22:03:22 +01:00
// m = atoi(token);
RTC.m = atoi(token);
2020-02-14 15:21:43 +01:00
};
if(l == 5){
2020-02-15 22:03:22 +01:00
// s = atoi(token);
RTC.s = atoi(token);
2020-02-14 15:21:43 +01:00
};
2020-02-15 22:03:22 +01:00
token = strtok(NULL, "/");
2020-02-14 15:21:43 +01:00
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);
2020-02-15 22:03:22 +01:00
// RTC.yyyy = yyyy;
// RTC.mm = mm;
// RTC.dd = dd;
// RTC.h = h;
// RTC.m = m;
// RTC.s = s;
2020-04-10 14:01:56 +02:00
Serial.println(F("NTP sync complete"));
2020-02-14 15:21:43 +01:00
RTC.writeTime();
trecvd = false;
reqTimeSync = true;
2020-02-15 22:03:22 +01:00
return(true);
2020-02-14 14:19:51 +01:00
}
}
void makeGrid() {
2020-04-10 14:01:56 +02:00
display.setRotation(0); //portrait, connector at the top
2020-02-14 15:21:43 +01:00
display.setFullWindow();
2020-04-10 14:01:56 +02:00
display.setFont(&TomThumb);
display.setTextColor(GxEPD_BLACK);
2020-02-14 15:21:43 +01:00
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
2020-04-10 14:01:56 +02:00
// display.drawFastVLine(64, 192, display.height(), GxEPD_BLACK);
// display.drawFastHLine(0, 64, display.width(), GxEPD_BLACK);
display.setCursor(8, 69);
// display.drawFastHLine(0,69, display.width(), GxEPD_BLACK);
display.print(F("Temperature:"));
display.setCursor(8, (69 + 40));
// display.drawFastHLine(0,69+40, display.width(), GxEPD_BLACK);
display.print(F("Humidity:"));
display.setCursor(8, (69 + 80));
// display.drawFastHLine(0,69+80, display.width(), GxEPD_BLACK);
display.print(F("Pressure:"));
display.setCursor(8, (69 + 120));
// display.drawFastHLine(0,69+120, display.width(), GxEPD_BLACK);
display.print(F("CO2 concentration:"));
// display.drawFastHLine(0, 128, display.width(), GxEPD_BLACK);
// display.drawFastHLine(0, 192, display.width(), GxEPD_BLACK);
// display.drawFastHLine(0, 256, display.width(), GxEPD_BLACK);
2020-02-14 15:21:43 +01:00
}
while (display.nextPage());
2020-02-14 14:19:51 +01:00
}
void printRtimeToScreen() {
2020-02-14 15:21:43 +01:00
char delim[] = ":";
RTC.readTime();
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);
2020-04-10 14:01:56 +02:00
uint16_t y = ((32 - tbh) / 2) - (tby - 4);
2020-02-14 15:21:43 +01:00
display.getTextBounds("00", 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x2 = (display.width() / 2) - 8 - tbw ;
2020-04-10 14:01:56 +02:00
uint16_t y2 = ((32 - tbh) / 2) - ( tby - 4);
2020-02-14 15:21:43 +01:00
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);
2020-04-10 14:01:56 +02:00
display.setCursor(x2, y2);
2020-02-14 15:21:43 +01:00
if(RTC.h < 10) {
display.print("0");
display.print(RTC.h);
2020-02-15 22:03:22 +01:00
}
else {
2020-02-14 15:21:43 +01:00
display.print(RTC.h);
}
2020-04-10 14:01:56 +02:00
display.setCursor(x3, y2);
2020-02-14 15:21:43 +01:00
if(RTC.m < 10) {
display.print("0");
display.print(RTC.m);
2020-02-15 22:03:22 +01:00
}
else {
2020-02-14 15:21:43 +01:00
display.print(RTC.m);
}
2020-02-14 14:19:51 +01:00
}
2020-02-14 15:21:43 +01:00
while(display.nextPage());
2020-02-14 14:19:51 +01:00
}
2020-02-14 15:21:43 +01:00
void printRtDateToScreen() {
RTC.readTime();
display.setFont(&FreeSansBold9pt7b);
display.setTextColor(GxEPD_BLACK);
int16_t tbx, tby; uint16_t tbw, tbh;
2020-02-15 22:03:22 +01:00
display.getTextBounds("00 - 00 - 0000", 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x = (display.width() / 2) - (tbw / 2) ;
2020-02-14 15:21:43 +01:00
uint16_t y = 48 + (tbh / 2);
display.setPartialWindow(0, 33, display.width(), 31);
display.firstPage();
do {
//display.fillScreen(GxEPD_WHITE);
display.setCursor(x, y);
display.print(RTC.dd);
display.print(" - ");
2020-02-15 22:03:22 +01:00
if(RTC.mm < 10){
display.print("0");
display.print(RTC.mm);
}
else {
display.print(RTC.mm);
}
2020-02-14 15:21:43 +01:00
display.print(" - ");
display.print(RTC.yyyy);
}
while(display.nextPage());
2020-02-14 14:19:51 +01:00
}
void printTempToScreen() {
2020-02-14 15:21:43 +01:00
struct data *dtsp = &dataset;
display.setFont(&FreeSansBold9pt7b);
display.setTextColor(GxEPD_BLACK);
int16_t tbx, tby; uint16_t tbw, tbh;
2020-04-10 14:01:56 +02:00
display.getTextBounds("00.0 C", 0, 0, &tbx, &tby, &tbw, &tbh);
// uint16_t x = ((64 - tbw) / 2 ) - tbx;
uint16_t x = ((display.width() - tbw) / 2) - tbx;
// uint16_t y = ((96 - tbh) /*/ 2 */) - tby;
uint16_t y = (92 - tbh) - tby;
display.getTextBounds("00 %", 0, 0, &tbx, &tby, &tbw, &tbh);
// uint16_t x2 = (96 - tbw);
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;
2020-02-14 15:21:43 +01:00
uint16_t x3 = ((display.width() - tbw) / 2) - tbx;
2020-04-10 14:01:56 +02:00
uint16_t y3 = ( 176 - tbh) - tby;
display.getTextBounds("1488 ppm", 0, 0, &tbx, &tby, &tbw, &tbh);
// uint16_t x4 = ((display.width() - tbw) / 2) - tbx;
uint16_t y4 = ( 216 - tbh) - tby;
2020-02-14 15:21:43 +01:00
2020-04-10 14:01:56 +02:00
display.setPartialWindow(0, 70, display.width(), 33);
2020-02-14 15:21:43 +01:00
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.setCursor(x, y);
display.print(dtsp -> temp, 1);
2020-04-10 14:01:56 +02:00
display.print(F(" C"));
2020-02-14 15:21:43 +01:00
}
while (display.nextPage());
2020-02-14 14:19:51 +01:00
2020-04-10 14:01:56 +02:00
display.setPartialWindow(0, 110, display.width(), 33);
2020-02-14 15:21:43 +01:00
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
2020-04-10 14:01:56 +02:00
display.setCursor(x2, y2);
2020-02-14 15:21:43 +01:00
display.print(dtsp -> rhum);
2020-04-10 14:01:56 +02:00
display.print(F(" %"));
// display.drawFastVLine(64, 64, 64, GxEPD_BLACK);
2020-02-14 15:21:43 +01:00
}
while(display.nextPage());
2020-04-10 14:01:56 +02:00
display.setPartialWindow(0, 150 ,display.width(), 33);
2020-02-14 15:21:43 +01:00
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.setCursor(x3, y3);
display.print(dtsp -> rpres);
2020-04-10 14:01:56 +02:00
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"));
2020-02-14 15:21:43 +01:00
}
while(display.nextPage());
2020-02-14 14:19:51 +01:00
}
2020-02-14 15:21:43 +01:00
float measureTemp() {
// BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
int temps[6];
//light led while measuring
digitalWrite(13, HIGH);
2020-04-10 14:01:56 +02:00
Serial.print(F("Measuring temperature..."));
2020-02-14 15:21:43 +01:00
for(char i = 0; i <= 5; i++){
temps[i] = bme.readTemperature() * 100;
delay(1000);
}
2020-02-14 14:19:51 +01:00
2020-02-14 15:21:43 +01:00
float temp;
temp = (temps[1] + temps[2] + temps[3] + temps[4] + temps[5]) / 5;
2020-04-10 14:01:56 +02:00
Serial.print(F("done: "));
2020-02-14 15:21:43 +01:00
Serial.println(temp / 100);
digitalWrite(13, LOW);
return temp / 100;
2020-02-14 14:19:51 +01:00
}
float measureHum() {
2020-02-14 15:21:43 +01:00
int hums[6];
//light led while measuring
digitalWrite(13, HIGH);
2020-04-10 14:01:56 +02:00
Serial.print(F("Measuring humidity..."));
2020-02-14 15:21:43 +01:00
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;
2020-04-10 14:01:56 +02:00
Serial.print(F("done: "));
2020-02-14 15:21:43 +01:00
Serial.println(hum / 100);
digitalWrite(13, LOW);
return hum / 100;
2020-02-14 14:19:51 +01:00
}
float measurePres() {
// BME280::PresUnit presUnit(BME280::PresUnit_hPa);
2020-02-14 15:21:43 +01:00
float presrs[6];
//light led while measuring
digitalWrite(13, HIGH);
2020-04-10 14:01:56 +02:00
Serial.print(F("Measuring pressure..."));
2020-02-14 15:21:43 +01:00
for(char i = 0; i <= 5; i++){
// presrs[i] = bme.pres();
2020-02-15 22:03:22 +01:00
presrs[i] = bme.readPressure() / 100.0F; //returns in Pa, deivide by 100 to get value in hPa
2020-02-14 15:21:43 +01:00
delay(1000);
}
float pres;
pres = (presrs[1] + presrs[2] + presrs[3] + presrs[4] + presrs[5]) / 5;
2020-04-10 14:01:56 +02:00
Serial.print(F("done: "));
2020-02-14 15:21:43 +01:00
Serial.println(pres);
digitalWrite(13, LOW);
return pres;
2020-02-14 14:19:51 +01:00
}
2020-04-10 14:01:56 +02:00
int16_t measureCO2(){
Serial.print(F("Measuring CO2..."));
int16_t co2conc = get_concentration();
Serial.print(F("done:"));
Serial.println(co2conc);
return(co2conc);
}
2020-02-15 22:03:22 +01:00
bool sendDataToServer(){
2020-02-14 15:21:43 +01:00
char payload[40];
char stemp[8];
char shum[8];
dtostrf(dataset.temp, 5, 2, stemp);
dtostrf(dataset.hum, 5, 2, shum); //convert floats to strings
sprintf(payload, "%d-%d-%d %d:%d:%d,%s,%s,%d,%d", dataset.yr, dataset.mo, dataset.dy, dataset.hr, dataset.mn, dataset.s, stemp, shum, dataset.rpres, dataset.co2);
Serial2.print(payload); //send over serial 2 to esp
Serial2.print('\n'); //terminate transmission
delay(1000); //give time to process comm
2020-04-10 14:01:56 +02:00
Serial.print(F("Data sent\r\n"));
2020-02-15 22:03:22 +01:00
return(true);
2020-02-14 14:19:51 +01:00
}