#define DEBBUG 1
#if DEBBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
//#include <Ethernet.h>
//#include "secrets.h"
//#include "ThingSpeak.h"
//#define THINGER_SERIAL_DEBUG
//#include <ThingerEthernet.h>
//#include "arduino_secrets.h"
#include <SPI.h>
#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
//Set time and date for RTC Clock
//#define rtcTime "22:47:00"
//#define rtcDate "Dec 15 2022"
unsigned long currTime = 0;
unsigned long prevTime[] = {};
//DS1302 rtc(7, 9, 8);
DS1307 rtc; // running via I2C A4 - SDA, A5 - SCL
// RTC_Millis rtc; //SOft RTC
//byte mac[] = {0x00, 0x08, 0xDC, 0x09, 0xDB, 0x7D};
//IPAddress ip(192, 168, 1, 245);
//IPAddress dns(192, 168, 1, 1);
//IPAddress gw(192, 168, 1, 1);
//EthernetClient client;
//ThingerEthernet thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
//unsigned long chNo = SECRET_CH_ID;
//const char * apiKeyW = SECRET_WRITE_APIKEY;
bool initLCD = true;
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
int tTresh = 26;
int hTresh = 60;
float pTemp = 0;
float pHumi = 0;
float iotTemp = 0;
float iotHumi = 0;
int hLed = 0;
int tLed = 0;
int initHT = 0;
int lcdUpdIcon = 0;
int lcdRoffset = 1;
int lcdLoffset = 0;
int lcdBright = 127;
//byte lcdCharLines[8] = {0b00100,0b00100,0b00100,0b00100,0b00100,0b00100,0b00100,0b00100,};
//byte lcdCharTime[8] = {0b00100,0b01010,0b10101,0b10111,0b10001,0b01010,0b00100,0b00000,};
//byte lcdCharCal[8] = {0b11111,0b00000,0b11111,0b11111,0b11001,0b11001,0b11111,0b00000,};
//byte lcdCharUpd[8] = {0b00100,0b01010,0b00100,0b01010,0b00000,0b00000,0b00000,0b00000,};
void setup() {
Serial.begin(9600);
if(clockSET()) {
debug("Sahat je podesen: ");
debugln(clockSET());
}
Wire.begin();
dht.begin();
//rtc.begin(DateTime(rtcDate, rtcTime)); //Soft RTC
//rtc.begin(DateTime(__DATE__, __TIME__)); //Soft RTC
// Thing.io setup
// thing["temp"] << iotTemp;
// thing["humid"] << iotHumi;
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
lcd.init();
lcd.backlight();
// Ethernet.begin(mac, ip, dns, gw);
// debug("My IP address: ");
// debugln(Ethernet.localIP());
delay(2000);
}
void loop() {
//Serial.println(__TIME__);
//Serial.println(__DATE__);
currTime = millis();
setLCDicons();
if(execDelay(1,3000) || initHT != 1) {
printHTval();
initHT = 1;
}
if(execDelay(2,1000)) {
printDateTime();
}
// if(execDelay(3,20000)) {
// thing.handle();
// }
if(execDelay(5,5000) == false){
if(lcdUpdIcon == 1) {
lcd.setCursor(7 + lcdLoffset, 0);
lcd.print("^");
//lcd.createChar(3, lcdCharUpd);
//lcd.write((byte)3);
lcd.setCursor(7 + lcdLoffset, 1);
lcd.print("^");
//lcd.write((byte)3);
}
} else {
lcd.setCursor(7 + lcdLoffset, 0);
lcd.print(" ");
lcd.setCursor(7 + lcdLoffset, 1);
lcd.print(" ");
lcdUpdIcon = 0;
}
}
void setLCDicons(){
if(initLCD) {
lcd.setCursor(0 + lcdLoffset, 0);
lcd.print("H:");
lcd.setCursor(0 + lcdLoffset, 1);
lcd.print("T:");
//lcd.createChar(0, lcdCharLines);
lcd.setCursor(8, 0);
lcd.print("|");
//lcd.write((byte)0);
lcd.setCursor(8, 1);
lcd.print("|");
//lcd.write((byte)0);
//lcd.createChar(1, lcdCharTime);
//lcd.setCursor(15, 0);
//lcd.write((byte)1);
//lcd.createChar(2, lcdCharCal);
//lcd.setCursor(15, 1);
//lcd.write((byte)2);
initLCD = false;
}
}
void printHTval() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humi) || isnan(tempC)) {
debugln(F("Failed to read from DHT sensor!"));
} else {
if(pHumi != humi || pTemp != tempC) {
pHumi = humi;
pTemp = tempC;
lcd.setCursor(2, 0);
lcd.print(pHumi);
lcd.setCursor(2, 1);
lcd.print(pTemp);
}
if(humi >= hTresh && hLed != 1) {
analogWrite(5, HIGH);
hLed = 1;
} else if(tempC >= tTresh && tLed != 1) {
analogWrite(6, HIGH);
tLed = 1;
} else if(humi < hTresh && hLed != 0) {
analogWrite(5, LOW);
hLed = 0;
} else if(tempC < tTresh && tLed != 0) {
analogWrite(6, LOW);
tLed = 0;
}
}
}
void printDateTime() {
DateTime now = rtc.now();
char timeBuf[100];
strncpy(timeBuf, "hh:mm", 100);
char dateBuf[100];
strncpy(dateBuf, "DD/MM", 100);
lcd.setCursor(9 + lcdRoffset, 0);
lcd.print(now.format(timeBuf));
lcd.setCursor(9 + lcdRoffset, 1);
lcd.print(now.format(dateBuf));
}
bool clockSET() {
//char timeChkInit[20];
//char timeChkUpd[20];
//String nesta;
rtc.begin();
//DateTime now = rtc.now();
//if(! clockSET()) {
// nesta = now.tostr(timeChkInit);
//}
// rtc.adjust(DateTime("Dec 15 2022", "22:55:00"));
// return true;
if (! rtc.isrunning()) {
debugln(F("RTC is NOT running, let's set the time!"));
rtc.adjust(DateTime(__DATE__, __TIME__));
// nesta = now.tostr(timeChkUpd);
// debugln(now.tostr(timeChkUpd));
// if(timeChkInit == timeChkUpd) {
// return false;
// } else {
return true;
// }
}// else if((Serial)) {
// rtc.adjust(DateTime(__DATE__, __TIME__));
// return true;
// }
}
bool execDelay(int instance, unsigned long v) {
if(currTime - prevTime[instance] >= v) {
debugln("Instance :" + String(instance, DEC) + " - " + (currTime - prevTime[instance]));
prevTime[instance] = currTime;
return true;
} else {
return false;
}
}