fw_mega/fw_mega.ino
2020-02-14 16:02:18 +01:00

405 lines
10 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>
//rtc includes
#include <MD_DS3231.h>
//bme280
//#include <BME280I2C.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
//BME280I2C bme;
Adafruit_BME280 bme;
#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));
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(115200);
Serial2.begin(115200);
Wire.begin();
while(!bme.begin(0x76))
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
if(bme.sensorID() == 0x60){
Serial.println("Found BME280 sensor");
}
else {
Serial.println("Found unknown sensor");
}
Serial.println("***Enviro data monitor***");
Serial.println();
delay(100);
display.init(115200); //init display
delay(100);
makeGrid();
printRtimeToScreen();
printRtDateToScreen();
Serial.println("Setup complete, waiting for esp");
delay(5000); //wait for esp to finish booting
};
struct data {
int yr;
char mo;
char dy;
char hr;
char mn;
char s;
float temp;
float hum;
int rhum;
float pres;
unsigned int rpres;
int co2;
};
volatile static struct data dataset;
static bool reqTimeSync = false;
//
void doStuff() {
dataset.temp = measureTemp();
dataset.hum = measureHum();
dataset.rhum = round(dataset.hum);
dataset.pres = measurePres();
dataset.rpres = round(dataset.pres);
dataset.co2 = 0;
RTC.readTime();
dataset.yr = RTC.yyyy;
dataset.mo = RTC.mm;
dataset.dy = RTC.dd;
dataset.hr = RTC.h;
dataset.mn = RTC.m;
dataset.s = RTC.s;
};
void loop() {
static unsigned char oldmins;
static bool reqTimeSync = false;
RTC.readTime();
if(RTC.m != oldmins){
printRtimeToScreen();
doStuff(); //measure data
printTempToScreen();
sendDataToServer();
oldmins = RTC.m;
}
else if(RTC.m == 0 && RTC.h == 0 && RTC.s == 30){
printRtDateToScreen();
}
if (RTC.h == 6 || RTC.h == 20 && RTC.m == 00 && RTC.s == 30 && reqTimeSync ==true){
reqTimeSync = false;
syncTime();
}
};
int 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("Syncing time...\n");
delay(50);
Serial2.print("?ntp\n");
delay(5);
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);
};
if(l == 1) {
mm = atoi(token);
};
if(l == 2) {
dd = atoi(token);
};
if(l == 3) {
h = atoi(token);
};
if(l == 4){
m = atoi(token);
};
if(l == 5){
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;
RTC.writeTime();
trecvd = false;
reqTimeSync = true;
return(0);
}
}
void makeGrid() {
display.setRotation(2); //portrait, connector at the bottom
display.setFullWindow();
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.drawFastVLine(64, 192, display.height(), GxEPD_BLACK);
display.drawFastHLine(0, 64, display.width(), GxEPD_BLACK);
display.drawFastHLine(0, 128, display.width(), GxEPD_BLACK);
display.drawFastHLine(0, 192, display.width(), GxEPD_BLACK);
display.drawFastHLine(0, 256, display.width(), GxEPD_BLACK);
}
while (display.nextPage());
}
void printRtimeToScreen() {
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);
uint16_t y = ((32 - tbh) / 2) - tby;
display.getTextBounds("00", 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x2 = (display.width() / 2) - 8 - tbw ;
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, y);
if(RTC.h < 10) {
display.print("0");
display.print(RTC.h);
} else {
display.print(RTC.h);
}
display.setCursor(x3, y);
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) ; //orig 8
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(" - ");
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.00", 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x = ((64 - tbw) / 2 ) - tbx;
uint16_t y = ((96 - tbh) /*/ 2 */) - tby;
display.getTextBounds("00", 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x2 = (96 - tbw);
display.getTextBounds("1488", 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x3 = ((display.width() - tbw) / 2) - tbx;
uint16_t y3 = ((160 - tbh) /*/ 2*/) - tby;
display.setPartialWindow(0, 65, 62, 63);
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.setCursor(x, y);
display.print(dtsp -> temp, 1);
display.print("C");
}
while (display.nextPage());
display.setPartialWindow(66, 65, 62, 63);
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.setCursor(x2, y);
display.print(dtsp -> rhum);
display.print(" %");
display.drawFastVLine(64, 64, 64, GxEPD_BLACK);
}
while(display.nextPage());
display.setPartialWindow(0, 129 ,display.width(), 63);
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.setCursor(x3, y3);
display.print(dtsp -> rpres);
display.print(" hPa");
}
while(display.nextPage());
}
float measureTemp() {
// BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
int temps[6];
//light led while measuring
digitalWrite(13, HIGH);
Serial.print("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("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("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("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("Measuring pressure...");
for(char i = 0; i <= 5; i++){
// presrs[i] = bme.pres();
presrs[i] = bme.readPressure() / 100.0F;
delay(1000);
}
float pres;
pres = (presrs[1] + presrs[2] + presrs[3] + presrs[4] + presrs[5]) / 5;
Serial.print("done: ");
Serial.println(pres);
digitalWrite(13, LOW);
return pres;
}
int sendDataToServer(){
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
return(0);
}