I am rather new here. What I am trying to do is to create a datalogging board that updates values to Arduino IoT dashboard. Very simple thing with bunch of analogreads and if-statements used as timer for the reading and bunch of variables.
I have got all the major bugs in the program fixed and it works well most of the time.
Initially I was using way too high update interval of 1s, this is now changed to 60 s for less data use.
For some reason the IoT cloud Dashboard shows zero readings randomly, like once every hour or six hours. All the updated variables show up as zero. I do not understand how this could happen other than if the board crashes and reboots?
Initially with 1s update interval the board remained online for couple of days. Now it has been online for a week, but then it dropped out of the cloud service. It still remains offline after two days.
I think the behaviour is strange, because the IoT cloud should have some sort of watchdog functionality? In which state of the program is the freezing possible? The internet connection is still good, board receives power, etc.
The board is in quite a remote location so troubleshooting is not super easy, and these problems did not occur when I started testing the software but seem to require days or weeks to see.
Any ideas for improved stability? What should I do with the built in IoT-functions to fix these issues, because I do not have any ideas what else to improve in my program.
It would be helpful if you could share your sketch so that we can check if there is any potential problem there.
You are also mentioning that the board is in a remote location (I don't know if it's outdoors or exposed to somewhat extreme weather conditions). I was just wondering if the issue could be related to power supply or WIFI unstability.
The board is located outdoors and is protected in two layers of casing so moisture should not be a problem. That being said, temperatures are rather low, around 0 degrees C. It is powered by a lot of lead acid battery (400Ah, 12V), and the voltage is monitored and has stayed above 12 V. Wifi is still up and running and I can get data from other sensors connected to the same wifi.
Anyways, I will take a look on the board today and hopefully get some fault messages read from the serial monitor.
Sketch is below,
BR,
Jaakko
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/a648adf2-347e-4aa7-b5d7-3b3b1458d3fd
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudElectricCurrent current_mes_avg;
CloudElectricPotential voltage_bat_avg;
CloudElectricPotential voltage_mes_avg;
CloudPower power_avg;
CloudPower power_max;
CloudTemperature temperature;
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.
*/
int temperature_0;
// This part is measurement / resistor / sensor and board specific. Calibrate for each application
const int current_range = 92; // Not a real range of the current measurement, but a calibration factor...
const float voltage_bat_range = 18.56;
const float voltage_mes_range = 68.5;
const float voltage_mes_corr = -0.45;
const float current_corr = - 0.41;
float current_mes_i = 0;
float voltage_bat_i = 0;
float voltage_mes_i = 0;
float current_mes_sum = 0;
float voltage_bat_sum = 0;
float voltage_mes_sum = 0;
float current_mes = 0;
float voltage_bat = 0;
float voltage_mes = 0;
float current_mes_n = 0;
float voltage_bat_n = 0;
float voltage_mes_n = 0;
float power_n = 0;
float power = 0;
float power_max_n = 0;
unsigned long currentMillis = 0;
int analog1;
int analog2;
int analog3;
float voltage1;
float voltage2;
float voltage3;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time data was read
// constants won't change:
const long interval = 100; // interval at which to read data
int i = 0;
int n = 0; //This is for the longer interval value calculation...
#include "thingProperties.h"
#include <Arduino_LSM6DSOX.h>
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
// 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();
}
void loop() {
ArduinoCloud.update();
currentMillis = millis();
// The code inside the "if" will be run on predefined interval
if (currentMillis - previousMillis >= interval) {
// save the last time data was read
previousMillis = currentMillis;
analog_read();
average_calc();
}
}
void analog_read() {
if (IMU.temperatureAvailable())
{
temperature_0;
IMU.readTemperature(temperature_0);
temperature = float(temperature_0);
}
analog1 = analogRead(A3); //trick to try to improve accuracy
delay(3);
analog1 = analogRead(A3);
voltage1 = analog1 * (3.3 /1023.0);
current_mes_i = (voltage1 + current_corr ) * (current_range / 3.3);
//Serial.print(analog1);
//Serial.print(" ");
//Serial.println(current_mes);
analog2 = analogRead(A2); //trick to try to improve accuracy
delay(3);
analog2 = analogRead(A2);
//Serial.print(analog2);
//Serial.print(" ");
voltage2 = analog2 * (3.3 /1023.0);
voltage_bat_i = voltage2 * (voltage_bat_range / 3.3);
analog3 = analogRead(A1); //trick to try to improve accuracy
delay(3);
//Serial.println(voltage_bat);
analog3 = analogRead(A1);
voltage3 = analog3 * (3.3 /1023.0);
voltage_mes_i = voltage3 * (voltage_mes_range / 3.3) + voltage_mes_corr;
//Serial.print(analog3);
//Serial.print(" ");
//Serial.println(voltage_mes);
}
void average_calc() {
// Average reading calculation from 1 s values
if (i <= 9) {
current_mes_sum = current_mes_sum + current_mes_i;
voltage_bat_sum = voltage_bat_sum + voltage_bat_i;
voltage_mes_sum = voltage_mes_sum + voltage_mes_i;
i = i + 1;
}
// average reading calculation and resetting calculator from 1s values
// This part is the end of sampling round, for 100 ms interval and i 0 - 9, happens every 1 s
if (i > 9) {
i = 0;
current_mes = current_mes_sum / 10 ;
voltage_bat = voltage_bat_sum / 10 ;
voltage_mes = voltage_mes_sum / 10 ;
current_mes_sum = 0 ;
voltage_bat_sum = 0 ;
voltage_mes_sum = 0 ;
power = voltage_bat * current_mes;
// Average reading calculation for 1 minute values
if (n <= 59) {
current_mes_n = current_mes_n + current_mes;
voltage_bat_n = voltage_bat_n + voltage_bat;
voltage_mes_n = voltage_mes_n + voltage_mes;
power_n = power_n + power;
n = n + 1;
if (power > power_max_n) { //This stores the maximum power over the averaging period
power_max_n = power;
}
}
if (n > 59) {
current_mes_avg = current_mes_n / 60.0 ;
voltage_bat_avg = voltage_bat_n / 60.0 ;
voltage_mes_avg = voltage_mes_n / 60.0 ;
power_avg = power_n / 60.0 ;
current_mes_n = 0 ;
voltage_bat_n = 0 ;
voltage_mes_n = 0 ;
power_n = 0;
power_max = power_max_n ;
power_max_n = 0;
n = 0;
}
}
}
Seems that the board was totally jammed. When I connected the USB-cable, for a while computer told that connection was established, and after that the board started blinking 4 long 4 short and no serial communication was established. I did try to reset the board by pressing the button on board, but with no luck (?) Finally I disconnected and reconnected the power supply, after this the board restarted and went instantly back online.
Hardware/firmware issue?
IoT cloud Dashboard still keeps showing zero readings randomly every now and then after the reboot.
let me hand this to our FW team.
The first thing that comes to mind is that reading the temperature from the IMU might be the issue there.
It wouldn't be the first time I bump into a sensor hogging the program.
I'll ask someone to reply to this thread, but I'm happy you found a workaround for now