Hi all,
I have been doing simple things with Arduino for a while but this is the first time using the the Arduino UNO R4 Wifi and the IoT cloud.
I am implementing a code that already works on a normal Arduino UNO R3 on the Arduino UNO R4. The code is for controlling a diesel heater that warms up the hot water deposit of my boat and at the same time warms up the radiators of my central heating. The temperatures are taken with two sensors and a relay controls the heater. As I said, I've been using this set up with Arduino UNO R3 or a long time with no issues, using a LCD screen, an encoder, a push button and a couple of switches for the control. I am trying now to implement this in the R4 Wifi so I can switch my central heating or heat up my water remotely when I am away.
The code works pretty well for a while when using the phisical buttons or the dashboard on my phone. The problem is that after a few hours the arduino goes offline and won't reconnect again. It will only reconnect if I reset the Arduino.
As I said I am not an expert and I am only using the pre-set code that IoT cloud provide for the wifi and remote connection. The rest of the code is the same one I had in my Arduino UNO R3 (some new variables have been created to interface with IoT).
Please find below the code. Is there anything that can be implemented so the Arduino can reconnect itself to the wifi when disconnected?
/*Icluding libraries*/
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <DHT_U.h>
#include "thingProperties.h"
/*Declaring the buttons, switches, econder and sensors pins*/
const byte ENC_BUTTON = 2; //Enconder button - interruption.
const byte ENC_INPUT_CLK = 3; //Encoder CLK wire - interruption.
const byte ENC_INPUT_DT = 4; //Encoder DT wire.
const byte HEAT_UP = 5; //Push button to heat up the water.
const byte HEATING_AUTO = 6; //Automatic central heating switch.
const byte SENSOR_ROOM = 7; //Temperature sensor in the room.
const byte SENSOR_WATER = 8; //Temperature sensor of the hot water deposit.
const byte HEATER_RELAY = 9; //Relay controlling the heater.
const byte SENSOR_BATTERY = A0; //Battery voltage sensor.
/*Declaring the variables to used when operatir the encoder*/
int cState;
int pState;
float encoder_count;
float temp_cabina; //variable donde se almacena el valor e la temperatura de la cabina.
float temp_agua; //variable donde se almacena la temperatura del agua caliente.
/*Declaring data variables*/
//float set_water_temp = 50; //Desired adjustable hot water temperature.
//float set_room_temp = 21; //Desired adjustable room temperature.
float water_temp; //Hot water temperature.
float room_temp; //Room temperature.
float room_humidity; //Room humidity.
float battery_voltage; //Baterry voltage.
float battery_soc; //Battery SOC (%).
/*Declaring other variables*/
byte count = 0; //Counts everytime the enconder button is pressed.
bool hot_water_auto_on = false; //variable true when heating the water automatically is on, false in oter case.
bool central_heating_temp = false; //variable true when the temperature of the room activate the automatic mode.
bool roomif = true; //variable to help when to gain temperature data when adjusting.
/*Creating an object room_sensor from DHT class for the room sensor*/
DHT room_sensor(SENSOR_ROOM, DHT22);
/*Creating objects from class OneWire and DallasTemperature for the hot water cylinder sensor*/
OneWire oneWire(SENSOR_WATER);
DallasTemperature hot_water_sensor(&oneWire);
/*Creating an object lcd from LiquidCrystal_I2C class*/
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
/*Initialising the temperature sensors*/
room_sensor.begin();
hot_water_sensor.begin();
/*Initialising the lcd display*/
lcd.init();
lcd.backlight();
lcd.clear();
/*Declaring inputs*/
pinMode(ENC_BUTTON, INPUT);
pinMode(ENC_INPUT_CLK, INPUT);
pinMode(ENC_INPUT_DT, INPUT);
pinMode(HEAT_UP, INPUT);
pinMode(HEATING_AUTO, INPUT);
pinMode(SENSOR_ROOM, INPUT_PULLUP);
pinMode(SENSOR_WATER, INPUT);
pinMode(SENSOR_BATTERY, INPUT);
/*Declaring outputs*/
pinMode(HEATER_RELAY, OUTPUT);
/*Interruption for the enconder reading*/
attachInterrupt(digitalPinToInterrupt(ENC_INPUT_CLK), tempAdjust, CHANGE);
/*Interruption for the enconder push button*/
//attachInterrupt(digitalPinToInterrupt(ENC_BUTTON), countButton, LOW);
/*Reading the initial state of the encoder*/
pState = digitalRead(ENC_INPUT_CLK);
set_room_temp = 21;
set_water_temp = 50;
encoder_count = set_water_temp;
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
Serial.begin(9600);
}
void loop() {
ArduinoCloud.update();
/*Obtaining the room temperature value*/
room_temp = room_sensor.readTemperature();
/*Obtaining the hot water deposit temperature value*/
hot_water_sensor.requestTemperatures();
water_temp = hot_water_sensor.getTempCByIndex(0);
/*Obtaining the battery voltage*/
battery_voltage = analogRead(SENSOR_BATTERY)*25/1023;
/*Reseting the count variable*/
if (count >= 3)
{
count = 0;
roomif = true;
encoder_count = set_water_temp;
}
/*Normal loop when no temperature is adjusted phisically and it can be done from the dashboard*/
if (count == 0)
{
if (digitalRead(ENC_BUTTON) == LOW)
{
count = 1;
delay (1000);
}
}
/*Secondary loop when the hot water temperature is being adjusted*/
if (count == 1)
{
set_water_temp = encoder_count;
if (digitalRead(ENC_BUTTON) == LOW)
{
count = 2;
delay (1000);
}
}
/*Secondary loop when the room temperature is being adjusted*/
if (count == 2)
{
if (roomif)
{
encoder_count = set_room_temp;
roomif = false;
}
set_room_temp = encoder_count;
if (digitalRead(ENC_BUTTON) == LOW)
{
count = 3;
delay (1000);
}
}
//Showing room temperature in the LCD.
lcd.setCursor(0,0);
lcd.print("Room temp: ");
lcd.setCursor(14,0);
lcd.print(room_temp);
lcd.setCursor(18,0);
lcd.print((char)223);
lcd.setCursor(19,0);
lcd.print("C");
//Showing room temperature set in the LCD.
lcd.setCursor(1,1);
lcd.print("Set: ");
lcd.setCursor(6,1);
lcd.print(set_room_temp);
lcd.setCursor(8,1);
lcd.print((char)223);
lcd.setCursor(9,1);
lcd.print("C ");
//Showing hot water temperature in the LCD.
lcd.setCursor(0,2);
lcd.print("Water temp: ");
lcd.setCursor(14,2);
lcd.print(water_temp);
lcd.setCursor(18,2);
lcd.print((char)223);
lcd.setCursor(19,2);
lcd.print("C");
//Showing hot water temperature set in the LCD.
lcd.setCursor(1,3);
lcd.print("Set: ");
lcd.setCursor(6,3);
lcd.print(set_water_temp);
lcd.setCursor(8,3);
lcd.print((char)223);
lcd.setCursor(9,3);
lcd.print("C ");
//Showing the central heating state in the LCD.
lcd.setCursor(11,1);
lcd.print("Auto: ");
lcd.setCursor(17,1);
if (digitalRead(HEATING_AUTO) == HIGH)
{
lcd.print("ON!");
}
else if (remote_central_heating)
{
lcd.print("RMT");
}
else
{
lcd.print("OFF");
}
//Showing the state of the water heating state on LCD.
lcd.setCursor(11,3);
lcd.print("Auto:");
lcd.setCursor(17,3);
if (hot_water_auto_on)
{
lcd.print("ON!");
}
else
{
lcd.print("OFF");
}
/*Switching off the heater relay*/
if (((digitalRead(HEATING_AUTO) == LOW && !remote_central_heating) || !central_heating_temp) && !hot_water_auto_on)
{
digitalWrite(HEATER_RELAY, LOW);
heater_state = false;
}
/*Switching on the heater relay*/
if (((digitalRead(HEATING_AUTO) == HIGH || remote_central_heating) && central_heating_temp) || hot_water_auto_on)
{
digitalWrite(HEATER_RELAY, HIGH);
heater_state = true;
}
/*Heating up the hot water*/
if ((digitalRead(HEAT_UP) == HIGH || remote_heat_water) && water_temp <= set_water_temp - 2)
{
hot_water_auto_on = true;
}
/*Reaching hot water set temperature*/
if (water_temp >= set_water_temp + 2)
{
hot_water_auto_on = false;
}
/*Comparing room temperature*/
if (room_temp <= set_room_temp - 1)
{
central_heating_temp = true;
}
if (room_temp >= set_room_temp + 1)
{
central_heating_temp = false;
}
/*Variables to be shown in the dashboard*/
remote_water_temp = round(water_temp*10/10.0);
remote_room_temp = round(room_temp*10/10.0);
remote_battery_voltage = battery_voltage;
}
/*Function to read the enconder when adjusting temperature*/
void tempAdjust ()
{
cState = digitalRead(ENC_INPUT_CLK);
if (cState != pState)
{
if (digitalRead(ENC_INPUT_DT) != cState)
{
encoder_count = encoder_count + 0.5;
}
else
{
encoder_count = encoder_count - 0.5;
}
}
pState = cState;
}
/*Function to detect the enconder push button*/
//void countButton ()
//{
// count ++;
// delay (1000);
//}
/*
Since AguaWifi is READ_WRITE variable, onAguaWifiChange() is
executed every time a new value is received from IoT Cloud.
*/
//void onAguaWifiChange() {
// Add your code here to act upon AguaWifi change
//}
/*
Since RemoteCentralHeating is READ_WRITE variable, onRemoteCentralHeatingChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRemoteCentralHeatingChange() {
// Add your code here to act upon RemoteCentralHeating change
}
/*
Since RemoteHeatWater is READ_WRITE variable, onRemoteHeatWaterChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRemoteHeatWaterChange() {
// Add your code here to act upon RemoteHeatWater change
}
/*
Since HeaterState is READ_WRITE variable, onHeaterStateChange() is
executed every time a new value is received from IoT Cloud.
*/
void onHeaterStateChange() {
// Add your code here to act upon HeaterState change
}
/*
Since RemoteSetWaterTemp is READ_WRITE variable, onRemoteSetWaterTempChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRemoteSetWaterTempChange() {
// Add your code here to act upon RemoteSetWaterTemp change
}
/*
Since RemoteSetRoomTemp is READ_WRITE variable, onRemoteSetRoomTempChange() is
executed every time a new value is received from IoT Cloud.
*/
void onRemoteSetRoomTempChange() {
// Add your code here to act upon RemoteSetRoomTemp change
}
/*
Since SetRoomTemp is READ_WRITE variable, onSetRoomTempChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSetRoomTempChange() {
// Add your code here to act upon SetRoomTemp change
}
/*
Since SetWaterTemp is READ_WRITE variable, onSetWaterTempChange() is
executed every time a new value is received from IoT Cloud.
*/
void onSetWaterTempChange() {
// Add your code here to act upon SetWaterTemp change
}