Hi guys. I've finally got my project to a point where the DS3231 RTC prints the time date on the first 2 lines of the 20x4 LCD and the DHT11 prints temp and humidity on the 3rd and 4th and switches a fan via relay.
Something though is conflicting and after about 20 seconds temp and humidity start blinking at 2 second intervals. Any ideas as to what is causing this
Thanks
Chris
#include <DHT.h>
#include <RTClib.h>
#include <Wire.h>
#include "ds3231.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define BUFF_MAX 128 // rtc
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
#define fan 4 // Pin the relay is connectd to
int maxHum = 100; // max humidity
int maxTemp = 31; // max temperature
DHT dht(DHTPIN, DHTTYPE);
uint8_t time[8];
char recv[BUFF_MAX];
unsigned int recv_size = 0;
unsigned long prev, interval = 1000;
void setup()
{
Serial.begin(9600);
pinMode(fan, OUTPUT);
dht.begin();
Wire.begin();
lcd.backlight();
DS3231_init(DS3231_CONTROL_INTCN);
memset(recv, 0, BUFF_MAX);
Serial.println("GET time");
lcd.begin(20, 4);
lcd.clear();
//Serial.println("Setting time");
//parse_cmd("T003521229022020",16);
// TssmmhhWDDMMYYYY
}
void loop() {
float h = dht.readHumidity();
float c = dht.readTemperature();
lcd.setCursor(0, 2);
lcd.print("Temperature: ");
lcd.print(c);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(2, 3);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
//delay(1000);
//lcd.clear();
char in;
char tempF[6];
float temperature;
char buff[BUFF_MAX];
unsigned long now = millis();
struct ts t;
// show time once in a while
if ((now - prev > interval) && (Serial.available() <= 0)) {
DS3231_get(&t); //Get time
parse_cmd("C", 1);
// temperature = DS3231_get_treg(); //Get temperature
//dtostrf(temperature, 5, 1, tempF);
lcd.clear();
lcd.setCursor(2, 0); // First Line
lcd.print(t.mday);
printMonth(t.mon);
lcd.print(t.year);
lcd.setCursor(6, 1); // Second Line
lcd.print(t.hour);
lcd.print(":");
if (t.min < 10)
{
lcd.print("0");
}
lcd.print(t.min);
lcd.print(":");
if (t.sec < 10)
{
lcd.print("0");
}
lcd.print(t.sec);
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (h > maxHum || t > maxTemp) {
digitalWrite(fan, HIGH);
} else {
digitalWrite(fan, LOW);
}
prev = now;
}
if (Serial.available() > 0) {
in = Serial.read();
if ((in == 10 || in == 13) && (recv_size > 0)) {
parse_cmd(recv, recv_size);
recv_size = 0;
recv[0] = 0;
} else if (in < 48 || in > 122) {
; // ignore ~[0-9A-Za-z]
} else if (recv_size > BUFF_MAX - 2) { // drop lines that are too long
// drop
recv_size = 0;
recv[0] = 0;
} else if (recv_size < BUFF_MAX - 2) {
recv[recv_size] = in;
recv[recv_size + 1] = 0;
recv_size += 1;
}
}
}
void parse_cmd(char *cmd, int cmdsize)
{
uint8_t i;
uint8_t reg_val;
char buff[BUFF_MAX];
struct ts t;
//snprintf(buff, BUFF_MAX, "cmd was '%s' %d\n", cmd, cmdsize);
//Serial.print(buff);
// TssmmhhWDDMMYYYY aka set time
if (cmd[0] == 84 && cmdsize == 16) {
//T355720619112011
t.sec = inp2toi(cmd, 1);
t.min = inp2toi(cmd, 3);
t.hour = inp2toi(cmd, 5);
t.wday = inp2toi(cmd, 7);
t.mday = inp2toi(cmd, 8);
t.mon = inp2toi(cmd, 10);
t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
DS3231_set(t);
Serial.println("OK");
} else if (cmd[0] == 49 && cmdsize == 1) { // "1" get alarm 1
DS3231_get_a1(&buff[0], 59);
Serial.println(buff);
} else if (cmd[0] == 50 && cmdsize == 1) { // "2" get alarm 1
DS3231_get_a2(&buff[0], 59);
Serial.println(buff);
} else if (cmd[0] == 51 && cmdsize == 1) { // "3" get aging register
Serial.print("aging reg is ");
Serial.println(DS3231_get_aging(), DEC);
} else if (cmd[0] == 65 && cmdsize == 9) { // "A" set alarm 1
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A1IE);
//ASSMMHHDD
for (i = 0; i < 4; i++) {
time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // ss, mm, hh, dd
}
byte flags[5] = { 0, 0, 0, 0, 0 };
DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
DS3231_get_a1(&buff[0], 59);
Serial.println(buff);
} else if (cmd[0] == 66 && cmdsize == 7) { // "B" Set Alarm 2
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A2IE);
//BMMHHDD
for (i = 0; i < 4; i++) {
time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // mm, hh, dd
}
byte flags[5] = { 0, 0, 0, 0 };
DS3231_set_a2(time[0], time[1], time[2], flags);
DS3231_get_a2(&buff[0], 59);
Serial.println(buff);
} else if (cmd[0] == 67 && cmdsize == 1) { // "C" - get temperature register
Serial.print("temperature reg is ");
Serial.println(DS3231_get_treg(), DEC);
} else if (cmd[0] == 68 && cmdsize == 1) { // "D" - reset status register alarm flags
reg_val = DS3231_get_sreg();
reg_val &= B11111100;
DS3231_set_sreg(reg_val);
} else if (cmd[0] == 70 && cmdsize == 1) { // "F" - custom fct
reg_val = DS3231_get_addr(0x5);
Serial.print("orig ");
Serial.print(reg_val, DEC);
Serial.print("month is ");
Serial.println(bcdtodec(reg_val & 0x1F), DEC);
} else if (cmd[0] == 71 && cmdsize == 1) { // "G" - set aging status register
DS3231_set_aging(0);
} else if (cmd[0] == 83 && cmdsize == 1) { // "S" - get status register
Serial.print("status reg is ");
Serial.println(DS3231_get_sreg(), DEC);
} else {
Serial.print("unknown command prefix ");
Serial.println(cmd[0]);
Serial.println(cmd[0], DEC);
}
}
void printMonth(int month)
{
switch (month)
{
case 1: lcd.print(" January "); break;
case 2: lcd.print(" February "); break;
case 3: lcd.print(" March "); break;
case 4: lcd.print(" April "); break;
case 5: lcd.print(" May "); break;
case 6: lcd.print(" June "); break;
case 7: lcd.print(" July "); break;
case 8: lcd.print(" August "); break;
case 9: lcd.print(" September "); break;
case 10: lcd.print(" October "); break;
case 11: lcd.print(" November "); break;
case 12: lcd.print(" December "); break;
default: lcd.print(" Error "); break;
}
}