The value not change in iot cloud

I uploaded the code but the value in the dashboard not changed, can any one help me please:


I checked it was connected to my wifi
Here is the code

#include "thingProperties.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define VREF 5.0      // analog reference voltage(Volt) of the ADC
#define SCOUNT  30           // sum of sample point
int analogBuffer[SCOUNT];    // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;
float averageVoltage = 0;
// Pin definitions
#define PhSensorPin A0
#define TdsSensorPin A1
#define TurbiditySensorPin A2
#define SIG_PIN A3  // DS18B20 sensor pin
#define Offset 0.00            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLength  40    //times of collection
int pHArray[ArrayLength];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;
int turbidityArray[ArrayLength];   // Store Turbidity sensor value
OneWire oneWire(SIG_PIN);

DallasTemperature ds(&oneWire);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  ds.begin();
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // 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();
  // Your code here


}


/*
  Since PHValue is READ_WRITE variable, onPHValueChange() is
  executed every time a new value is received from IoT Cloud.
*/
int getMedianNum(int bArray[], int iFilterLen)
{
  int bTab[iFilterLen];
  for (byte i = 0; i < iFilterLen; i++)
    bTab[i] = bArray[i];
  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++)
  {
    for (i = 0; i < iFilterLen - j - 1; i++)
    {
      if (bTab[i] > bTab[i + 1])
      {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }
  if ((iFilterLen & 1) > 0)
    bTemp = bTab[(iFilterLen - 1) / 2];
  else
    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  return bTemp;
}
double avergearray(int* arr, int number) {
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0) {
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number < 5) { //less than 5, calculated directly statistics
    for (i = 0; i < number; i++) {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  } else {
    if (arr[0] < arr[1]) {
      min = arr[0]; max = arr[1];
    }
    else {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++) {
      if (arr[i] < min) {
        amount += min;      //arr<min
        min = arr[i];
      } else {
        if (arr[i] > max) {
          amount += max;  //arr>max
          max = arr[i];
        } else {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

float readAverageVoltage(int pin) {
  int sum = 0;
  for (int i = 0; i < 10; i++) {
    sum += analogRead(pin);
    delay(10);
  }
  return (float)sum / 10.0 * (5.0 / 1024.0);
}

void onPHValueChange()  {
  // Add your code here to act upon PHValue change
  // Read pH Sensor
  int pHArray[ArrayLength];   //Store the average value of the sensor feedback
  int pHArrayIndex = 0;
  static float pHValue, voltage;
  pHArray[pHArrayIndex++] = analogRead(PhSensorPin);
  if (pHArrayIndex == ArrayLength)pHArrayIndex = 0;
  voltage = avergearray(pHArray, ArrayLength) * 5.0 / 1024;
  pHValue = 3.5 * voltage + Offset;
}

/*
  Since TdsValue is READ_WRITE variable, onTdsValueChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTdsValueChange()  {
  float temperature = ds.getTempCByIndex(0);
  for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
    analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
  averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
  float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
  float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
  tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value


}

/*
  Since AverageTurbidityVoltage is READ_WRITE variable, onAverageTurbidityVoltageChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAverageTurbidityVoltageChange()  {
  // Add your code here to act upon AverageTurbidityVoltage change
  // Read Turbidity Sensor
  float averageTurbidityVoltage = readAverageVoltage(TurbiditySensorPin);

}

/*
  Since Temperature is READ_WRITE variable, onTemperatureChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange()  {
  ds.requestTemperatures();
  float temperature = ds.getTempCByIndex(0);
}

I am sorry for Vietnamese comments on code snippet

Hi @nhpcbn0608. There are a couple of problems with your code.

Shadowing of Cloud Variables

If you look in the thingProperties.h tab of the sketch, you would see declarations of the Cloud Variables. This means that you can use those variables in the .ino file of the sketch without declaring them.

For example, if you wanted the sketch program to set the value of the averageTurbidityVoltage Cloud Variable to 1.23, you would do this:

averageTurbidityVoltage = 1.23;

What you are doing instead in the .ino file is this:

float averageTurbidityVoltage = readAverageVoltage(TurbiditySensorPin);

This declares a new variable and sets the value of that variable rather than the value of the variable of the same name that was declared at global scope. That means a line like this in your sketch will never affect the Cloud Variable.

This is known as "variable shadowing" and is the source of many a confusing bug.

Incorrect Use of Callback Functions

Even after you fix the variable shadowing bug, you will find that your code still doesn't behave as you expect. The reason is that the only place in the sketch that you change the values of the Cloud Variables is in the callback functions.

For example:

/*
  Since AverageTurbidityVoltage is READ_WRITE variable, onAverageTurbidityVoltageChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAverageTurbidityVoltageChange()  {
  // Add your code here to act upon AverageTurbidityVoltage change
  // Read Turbidity Sensor
  float averageTurbidityVoltage = readAverageVoltage(TurbiditySensorPin);

}

As the comment explains, the onAverageTurbidityVoltageChange is only called when you change the value of the averageTurbidityVoltage cloud variable from the Arduino Cloud dashboard. That function is never called otherwise.

If you want your sketch to update the values of the Cloud Variables, then you need to put the code that updates the variables in a part of the program that will be executed under normal conditions. That would be inside the loop function.


My recommendation is for you to put this complex project aside for now and start fresh with a more simple sketch so you can learn the fundamentals of how Arduino Cloud works.

A great way to get a simple Arduino Cloud Thing up and running very quickly with a minimum of difficulty is to use the "Cloud Blink" template:

https://app.arduino.cc/templates/cloud-blink

(If the above link doesn't work, make sure you are signed in to your arduino.cc account)

This will create a dashboard that allows you to control the LED on the Arduino board. You can study the code of the Thing sketch that was generated by the "Cloud Blink" template to understand how it works.