IoT Dashboard Value not updating

Hello,

I am trying to use IoT Remote App to display a value and use a button. This button ('reset'), works perfectly well and does its task. However, I am not able to receive any value. The 'value' widget just shows a constant "0".

Please let me know if you see anything obvious and why the dashboard is not updating.

I have attached my code below:

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/55e6fad5-b3e3-428e-9cf8-175c2c04550f

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudLight reset_button;
  CloudTime total_Time_Elapsed;

  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 "Arduino_GigaDisplay_GFX.h"
#include "Arduino_H7_Video.h"
#include <lvgl.h>
#include <ArduinoGraphics.h>

GigaDisplay_GFX display;

#define BLACK 0x0000

const int analogPin = A2;
unsigned long timer = 0;
unsigned long maxTimer = 0;
unsigned long interval = 1; // 1 millisecond
int timerMode = 0;
int timeMode = 0;
unsigned long startTime = 0;
unsigned long previousMillis = 0;
float threshold = 1.0; // Volts
unsigned long MaxTimer = 0;

bool printedFinalTimer = false;

void resetDevice() {
  display.fillScreen(BLACK);
  display.setCursor(0, 0);
  display.setTextSize(3);
  display.println("JHR");
  display.println("Timer Starting");
  display.println("Time Elapsed:");

  timer = 0;
  maxTimer = 0;
  MaxTimer = 0;
  timerMode = 0;
  startTime = 0;
  previousMillis = 0;
  timeMode = 0;
  printedFinalTimer = false;
}

void setup() {
  Serial.begin(9600);
  display.begin();
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  resetDevice();  // Call the reset function during setup
}

void loop() {
  ArduinoCloud.update();

  unsigned long currentMillis = millis();

  if (((analogRead(analogPin)) > 200) && ((currentMillis - previousMillis) > interval)) {

    int sensorValue = analogRead(analogPin);
    float volt = sensorValue * (5.0 / 1023.0);

    // Start the Timer when pressure signal is greater than 0.5 V

    if ((volt > threshold) && (timerMode == 0) && (timeMode == 0)) {
      startTime = millis();
      timerMode = 1;
      Serial.println("Recording Time");
    }

    if ((volt > 2.0) && (timerMode == 1)) {
      timeMode = 1;
    }

    // If the pressure signal goes below 0.5V; the program will reset the timerMode = 0

    if ((volt < threshold) && (timerMode == 1) && (timeMode == 1)) {
      unsigned long currentTimer = millis() - startTime;
      if (currentTimer > maxTimer) {
        maxTimer = currentTimer;
      }
      startTime = 0;
      timerMode = 0;
      timeMode = 0;
    }

    Serial.println(sensorValue);
    Serial.print(volt);
    Serial.println(" V");
    Serial.print("Timer Mode: ");
    Serial.println(timerMode);
    Serial.println(millis());
    Serial.print("Start Time: ");
    Serial.println(startTime);
    Serial.println(maxTimer);
    Serial.println(timeMode);
    Serial.println("--");
  }

  // Check if the condition is no longer true and the timer has been recorded

  if (((analogRead(analogPin)) < 215) && (maxTimer > 0) && (!printedFinalTimer) && (timeMode == 0)) {
    printFinalTimerValue();
    printedFinalTimer = true;
    timerMode = 0;
  }
}

void printFinalTimerValue() {
    ArduinoCloud.update();
  if ((maxTimer > 0) && (timerMode == 0)) {
    MaxTimer = maxTimer;
    display.fillScreen(BLACK);
    display.setCursor(0, 0);
    display.print("Final Time: ");
    display.print(MaxTimer);
    display.print(" ms");
    Serial.print("Max Time: ");
    Serial.print(MaxTimer);
    Serial.println(" ms");
    CloudCounter total_Time_Elapsed = MaxTimer;
    total_Time_Elapsed = MaxTimer;
    Serial.print("Total Time: ");
    Serial.println(total_Time_Elapsed);
    ArduinoCloud.update();
  }
}

void onResetButtonChange() {
  if (reset_button == true) {
    display.fillScreen(BLACK);
    display.setCursor(0, 0);
    display.print("Resetting Device");
    delay (1500);
    resetDevice();  // Call the reset function to properly reset the device
    reset_button = false;
  }
}

image

Hi @amjhr, I think the problem can be the line above.
You are redeclaring the variable total_Time_Elapsed which, as a consequence, become a local variable, which actually override the value coming from the Cloud.

You should remove this line, and just keep the next:

    total_Time_Elapsed = MaxTimer;

Just to clarify, your code should look like this:

    [...]
    Serial.println(" ms");
    total_Time_Elapsed = MaxTimer;
    Serial.print("Total Time: ");
    [...]

Moreover I think you should remove all other calls to ArduinoCloud.update(); inside printFinalTimerValue functions, since you're calling it at the top of the loop function.

Let us know how it goes.

Greetings,
Christian.

Hello @c_sarnataro_arduino ,

I tried that way and it did not work for me. However, I converted the value to a string and that worked immediately. I also saw that there was an update to the the cloud to fix bugs, I am yet to try if that fixed it.

Thank you for your help!

  • AT

I am very new at this so can you elaborate on how you converted the value to a string?
I am trying to display temperature on a widget and it just remains zero.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.