Hello, guys.. my I have tested some sensors all together just to make sure everithing working than I would like to start put everithing to objects etc.. but now as far I got sometimes temperature sensors part stop working to be exact no values on the Nextion display. Sometimes it start with no rtc time change on display. But the LED 13 stil blinking so looks like arduino it self is running.
Looking to it second day and no luck find out.
#include <RTClib.h>
#include <Wire.h>
#include <Nextion.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
int systemFlowCheckInt = 1;
int systemFlowCheckLastInt = 0;
int actualTimeInterval = 0;
int time_will_read_at = 0;
int time_read_interval = 1;
RTC_DS3231 rtc;
DateTime now;
int actual_day = 1;
int actual_month = 1;
int actual_year = 2022;
int actual_hour = 0;
int actual_minute = 0;
int actual_second = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float hum = 0;
float temp = 0;
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
//Electric heater
int el_heat_dayli=0;//0 night 1 day 2 away
int el_heat_on_level_day = 20; //degree celsius
int el_heat_on_level_night = 16; //degree celsius
int el_heater_check_interval = 30; //minutes
int el_heater_temperature_will_check_at = 0; //minutes
#define el_heater 25
//Temperature sensors
String stringOne = "";
int temperature_will_check_at = 4;
int temperature_check_interval = 3;
DallasTemperature sensors(&oneWire);
int numberOfDevices = 0;
DeviceAddress tempDeviceAddress;
float actual_inside_temp = 0;
float actual_outside_temp = 0;
float actual_temp_switch_board = 0;
bool wasTempInside = false;
bool wasTempOutside = false;
bool wasTempSwitchBoard = false;
/* TEMPERATURE END*/
#define ledPin 13
#define mainLIghtPin 23
#define boilerPin 25
#define D_MAX_PROGRESS_VALUE 100
NexDSButton btnMainLIght = NexDSButton(4, 1, "bt0");
NexDSButton btnBoiler = NexDSButton(3, 5, "bt6");
NexProgressBar boilerTimerProgressBar1 = NexProgressBar(3, 15, "j0");
NexProgressBar boilerTimerProgressBar2 = NexProgressBar(2, 18, "j0");
NexDSButton btnNight = NexDSButton(2, 8, "bt8");
NexDSButton btnDay = NexDSButton(2, 58, "bt5");
NexNumber boilerTimerValue1 = NexNumber(3, 20, "n0");
NexNumber boilerTimerValue2 = NexNumber(2, 34, "n0");
int prescaler_timeconstantvalue = 49900; //ONE SECOND 49900
NexTouch *nex_listen_list[] = {
&btnMainLIght,
&btnBoiler,
&btnNight,
&btnDay,
NULL
};
void btnMainLIghtPushCallback(void *ptr)
{
uint32_t dual_state = 0;
btnMainLIght.getValue(&dual_state);
if (dual_state == 1)
{
//Serial2.print("Světla zapnuta");
digitalWrite(mainLIghtPin, LOW);
}
else if (dual_state == 0)
{
//Serial2.print("Světla vypnuta");
digitalWrite(mainLIghtPin, HIGH);
}
}
void btnNightPushCallback(void *ptr)
{
Serial.print("Night");
el_heat_dayli = 0;
}
void btnDayPushCallback(void *ptr)
{
Serial.print("Day");
el_heat_dayli=1;
}
/*
void btnMainLIghtPopCallback(void *ptr)
{
//digitalWrite(LED_BUILTIN, LOW);
}
*/
void btnBoilerPushCallback(void *ptr)
{
/*
uint32_t dual_state = 0;
btnBoiler.getValue(&dual_state);
if (dual_state == 1)
{
WATER_HEATER_ON = true;
Serial2.print("Water heater manual on");
digitalWrite(boilerPin, LOW);
WATER_HEATER_BTN_CHANGED = true;
//read timer on value and save it to
//WATER_HEATER_TIMER_ON
WATER_HEATER_TIMER_ON = true;
if (WATER_HEATER_TIMER_ON) {
WATER_HEATER_TIMER_VALUE = WATER_HEATER_INTERVAL;
WATER_BOILER_PROGRESS_VALUE = D_MAX_PROGRESS_VALUE;
boilerTimerProgressBar1.setValue(100);
// boilerTimerProgressBar2.setValue(100);
}
}
if (dual_state == 0)
{
WATER_HEATER_ON = false;
WATER_HEATER_TIMER_ON = false;
WATER_HEATER_BTN_CHANGED = false;
Serial2.print("Water heater manual off");
digitalWrite(boilerPin, HIGH);
boilerTimerValue1.setValue(0);
//boilerTimerValue2.setValue(0);
boilerTimerProgressBar1.setValue(0);
//boilerTimerProgressBar2.setValue(0);
}
*/
}
/*
void btnBoilerPopCallback(void *ptr)
{
//digitalWrite(LED_BUILTIN, LOW);
}
*/
void setup(void) {
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = prescaler_timeconstantvalue; // preload timer
TCCR1B |= (1 << CS10) | (1 << CS12); // 1024 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt ISR
interrupts(); // enable all interrupts
Serial2.begin(115200);
delay(500);
Serial2.print("Baud=9600");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.end();
Serial2.begin(9600);
//Serial2.begin(115200);
//Serial.begin(9600);
//Serial.print("Baud=115200");
nexInit();
dht.begin();
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(2022, 9, 22, 23, 29, 0));
delay(1000);
btnMainLIght.attachPush(btnMainLIghtPushCallback);
//btnMainLIght.attachPop(btnMainLIghtPopCallback);
btnBoiler.attachPush(btnBoilerPushCallback);
//btnBoiler.attachPop(btnBoilerPopCallback);
btnNight.attachPush(btnNightPushCallback);
btnDay.attachPush(btnDayPushCallback);
pinMode(ledPin, OUTPUT);
pinMode(mainLIghtPin, OUTPUT);
digitalWrite(mainLIghtPin, HIGH);
//pinMode(mainLIght, INPUT_PULLUP);
pinMode(el_heater, OUTPUT);
digitalWrite(el_heater, HIGH);
pinMode(boilerPin, OUTPUT);
digitalWrite(boilerPin, HIGH);
/* TEMP */
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
/*
Serial2x.print("Locating temperature sensors...");
Serial2x.print("Found ");
Serial2x.print(numberOfDevices, DEC);
Serial2x.println(" sensors.");
*/
for (int i = 0; i < numberOfDevices; i++) {
if (sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Nalezen teplotní sensor ");
Serial.print(1, DEC);
Serial.println();
} else {
Serial.print("Nalezen teplotní sensor bez adresy");
Serial.print(i, DEC);
Serial.println();
}
}
/* TEMP END*/
}
//void (*resetFunc)(void)=0;
ISR(TIMER1_OVF_vect) // interrupt service routine for overflow
{
TCNT1 = prescaler_timeconstantvalue; // preload timer
digitalWrite(ledPin, digitalRead(ledPin) ^ 1); //Turns LED ON and OFF
/*
actualTimeInterval++;
if (actualTimeInterval > 59) {
actualTimeInterval = 0;
}
*/
/*
if (WATER_HEATER_TIMER_ON) {
WATER_HEATER_TIMER_PREVIOUS_VALUE = WATER_HEATER_TIMER_VALUE;
WATER_HEATER_TIMER_VALUE = WATER_HEATER_TIMER_VALUE - 1;
WATER_BOILER_PROGRESS_VALUE = WATER_BOILER_PROGRESS_VALUE - WATER_BOILER_PROGRESS_STEP;
WATER_HEATER_TIMER_ONE_CYCLE_SEMAPHOR = true;
if (WATER_HEATER_TIMER_VALUE <= 0) {
WATER_HEATER_ON = false;
WATER_HEATER_TIMER_VALUE = 0;
WATER_HEATER_TIMER_ON = false;
digitalWrite(boilerPin, HIGH);//HIGH
Serial2.println("Water heater end on timer off");
}
}
*/
/*
Serial.print("time_last_readed> ");
Serial.print(time_last_readed);
Serial.print("interval> ");
Serial.println(actualTimeInterval);
*/
}
void getSensors(void) {
//--------------------- TEMPERATURE
/*
if ((temperature_will_check_at < actual_second)) {
temperature_will_check_at = actual_second + temperature_check_interval;
if (temperature_will_check_at >= 60)temperature_will_check_at = temperature_will_check_at - 60;
*/
Serial.println("Načítání teplot:");
numberOfDevices = sensors.getDeviceCount();
sensors.requestTemperatures();
for (int i = 0; i <= numberOfDevices; i++) {
// Search the wire for address
if (sensors.getAddress(tempDeviceAddress, i)) {
if (i == 0) {
actual_inside_temp = sensors.getTempC(tempDeviceAddress);
//Serial.print("Teplota uvnitř: ");Serial.println(actual_inside_temp);
//Serial2x.print(tempC);
stringOne = "t3.txt=\"";
stringOne.concat(String(actual_inside_temp, 1));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(stringOne);
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
wasTempInside = true;
//Serial2.println("...");
}
if (i == 1) {
actual_outside_temp = sensors.getTempC(tempDeviceAddress);
//Serial.print("Teplota rozvaděč: ");Serial.println(actual_temp_switch_board);
//Serial2x.print(tempC);
stringOne = "t0.txt=\"";
stringOne.concat(String(actual_outside_temp, 1));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(stringOne);
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
wasTempOutside = true;
//Serial2.println("...");
}
if (i == 2) {
actual_temp_switch_board = sensors.getTempC(tempDeviceAddress);
//Serial.print("Teplota venku: ");Serial.println(actual_outside_temp);
//Serial2x.print(tempC);
stringOne = "t8.txt=\"";
stringOne.concat(String(actual_temp_switch_board, 1));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(stringOne);
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
wasTempSwitchBoard = true;
//Serial2.println("...");
}
if (!wasTempInside) {
//Serial2.print("Teplota uvnitř neodpovídá... ");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t3.txt=\"ERR"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
}
}//For end
if (!wasTempOutside) {
//Serial2.print("Teplota venku neodpovídá... ");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t0.txt=\"ERR"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
if (!wasTempSwitchBoard) {
//Serial2.print("Teplota v rozvaděči neodpovídá... ");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t8.txt=\"ERR"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
wasTempInside = false;
wasTempOutside = false;
wasTempSwitchBoard = false;
/* HUMIDITY */
hum = dht.readHumidity();
//temp = dht.readTemperature();
//Serial.print("Vlhkost: ");
//Serial.print(hum);
//Serial.print(" %, Teplota: ");
//Serial.print(temp);
//Serial.println(" Celsius");
/* HUMIDITY */
stringOne = "t17.txt=\"";
stringOne.concat(String(hum, 1));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(stringOne);
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
//}//interval check
//Serial.print(stringOne);
}
void loop() {
nexLoop(nex_listen_list);
//Get time
/*
if ((time_will_read_at <= actualTimeInterval)) {
time_will_read_at = actualTimeInterval + time_read_interval;
if (time_will_read_at >= 60)time_will_read_at = time_will_read_at - 60;
*/
Serial.println("Time read");
now = rtc.now();
actual_day = now.day();
actual_month = now.month();
actual_year = now.year();
actual_hour = now.hour();
actual_minute = now.minute();
actual_second = now.second();
//sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
String stringOne = "t2.txt=\"22";
stringOne.concat(String(actual_hour, 1));
stringOne.concat(":");
stringOne.concat(actual_minute);
stringOne.concat(":");
stringOne.concat(actual_second);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(stringOne);
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
//Serial.print(stringOne);
stringOne = "t1.txt=\"22";
stringOne.concat(String(actual_day, 1));
stringOne.concat(".");
stringOne.concat(actual_month);
stringOne.concat(".");
stringOne.concat(actual_year);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(stringOne);
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
//}
getSensors();
if((el_heater_temperature_will_check_at<=actual_second)){
el_heater_temperature_will_check_at = actual_second+el_heater_check_interval;
if (el_heater_temperature_will_check_at>=60)el_heater_temperature_will_check_at=el_heater_temperature_will_check_at-60;
Serial.println("Heater check");
if (el_heat_dayli==0){
if(actual_inside_temp<el_heat_on_level_night){
digitalWrite(el_heater, LOW);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t24.txt=\"N-Topí"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}else{
digitalWrite(el_heater, HIGH);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t24.txt=\"N-Netopí"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
}
if (el_heat_dayli==1){
if(actual_inside_temp<el_heat_on_level_day){
digitalWrite(el_heater, LOW);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t24.txt=\"D-Topí"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}else{
digitalWrite(el_heater, HIGH);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.print(F("t24.txt=\"D-Netopí"));
Serial2.print(F("\""));
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
}
}
}
}