I have a MKR Wifi 1010, RS485 plug-in module, a 4x20 LCD, and a temp/humidity sensor. Right now I am just testing the setup and making sure it will connect reliably. I am trying to make my setup as error-resistant as possible. One thing I have noticed is that very occasionally the IoT connection will be dropped. Sometimes it doesn't ever connect at initial boot, other times I will come back later (maybe several days) and it will be disconnected. I have the code basically debugging everything to the LCD or serial, that's how I know IoT has disconnected.
So, what I would like to do is, maybe after a certain number of seconds of it being in "disconnected" mode, it will try to reestablish the connection. That's the part I need help with. Is there a best practice for restarting the connection? watchdog? reboot?
111// RTClib - Version: Latest
#include <RTClib.h>
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/d875dd3b-76af-476b-adee-147df5b8d463
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudElectricPotential voltageIn;
CloudTemperatureSensor controlRoomTemp;
CloudPercentage controlRoomHumidity;
bool sendTrigger;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <ArduinoModbus.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
float temperature;
float humidity;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 4000; //the value is a number of milliseconds
#define ANALOG_INPUT_PIN A0 //Specify analog pin
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Booting...");
// Initialize serial and wait for port to open:
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Serial Begin...");
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
// for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 1000);
delay(1000);
// Defined in thingProperties.h
initProperties();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Init Properties...");
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false);
/*
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(4);
ArduinoCloud.printDebugInfo();
// while (!Serial);
Serial.println("Modbus Temperature Humidity Sensor");
// start the Modbus RTU client
ModbusRTUClient.begin(9600);
delay(1000);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);
setDebugMessageLevel(DBG_INFO);
ArduinoCloud.printDebugInfo();
startMillis = millis();
}
void loop() {
ArduinoCloud.update();
currentMillis = millis();
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
switch (WiFi.status()) {
case 0:
// statements
Serial.println("Wifi Not Connected");
// lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" "); // 20 spaces
lcd.setCursor(0, 0);
lcd.print("Wifi: Not Connected");
break;
case 3:
// statements
// Serial.print("Wifi Connected ");
// Serial.println(WiFi.SSID());
// lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" "); // 20 spaces
lcd.setCursor(0, 0);
lcd.print("Wifi: ");
lcd.print(WiFi.SSID());
break;
default:
// statements
break;
}
// Your code here
// send a Holding registers read request to (slave) id 1, for 2 registers
if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) {
Serial.print("failed to read registers! ");
Serial.println(ModbusRTUClient.lastError());
} else {
// If the request succeeds, the sensor sends the readings, that are
// stored in the holding registers. The read() method can be used to
// get the raw temperature and the humidity values.
short rawtemperature = ModbusRTUClient.read();
short rawhumidity = ModbusRTUClient.read();
// To get the temperature in Celsius and the humidity reading as
// a percentage, divide the raw value by 10.0.
temperature = rawtemperature / 100.0;
humidity = rawhumidity / 100.0;
Serial.print("Temp: ");
Serial.print((temperature*1.8)+32);
Serial.println("F");
Serial.print("Humd: ");
Serial.print(humidity);
Serial.println("%");
lcd.setCursor(0,2);
lcd.print("Temp: ");
lcd.print((temperature*1.8)+32);
lcd.print("F");
lcd.setCursor(0,3);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
controlRoomTemp = (temperature*1.8)+32;
controlRoomHumidity = humidity;
// read the input on analog pin:
int sensorValue = analogRead(ANALOG_INPUT_PIN);
// print out the value you read:
Serial.print(": Analog reading task, value is: ");
Serial.println(sensorValue);
lcd.setCursor(14,2);
lcd.print("V=");
lcd.print(sensorValue);
voltageIn = sensorValue;
Serial.println(ArduinoCloud.getLocalTime());
DateTime now = (ArduinoCloud.getLocalTime());
char buf1[] = "hh:mm";
Serial.println(now.toString(buf1));
char buf2[] = "YYMMDD-hh:mm:ss";
Serial.println(now.toString(buf2));
char buf3[] = "Today is DDD, MMM DD YYYY";
Serial.println(now.toString(buf3));
char buf4[] = "MM-DD-YYYY";
Serial.println(now.toString(buf4));
}
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}
void doThisOnConnect(){
/* add your custom code here */
Serial.println("Board successfully connected to Arduino IoT Cloud");
lcd.setCursor(0, 1);
lcd.print("IoT: Connect no sync");
}
void doThisOnSync(){
/* add your custom code here */
Serial.println("Board Sync'd IoT");
lcd.setCursor(0, 1);
lcd.print("IoT: Data Sync'd ");
}
void doThisOnDisconnect(){
/* add your custom code here */
Serial.println("Board disconnected from Arduino IoT Cloud");
lcd.setCursor(0, 1);
lcd.print("IoT: Disconnected ");
}