Setting precision on dashboard

Hi All.

New to IOT.
I`ve set up a temperature dashboard. The serial monitor reads the value with 2 decimal places, were the one on the chard reads 3 decimal places.
Is there a way so that the dashboard only displays 1 decimal places?

#include <OneWire.h>
#include <DallasTemperature.h>
#include "thingProperties.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempCelcius = 0;
int analogPin = A3;

void setup() {
  //pinMode(2, INPUT_PULLUP);
  // 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);
  sensors.begin();
  // 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
  analog = analogRead(analogPin);  // read the input pin
  //Serial.println(analog);          // debug value
  sensors.requestTemperatures();
  tempCelcius = sensors.getTempCByIndex(0);
  Serial.print("T = ");
  Serial.print(temp);
  Serial.println("ºC");
  temp = tempCelcius;
}


/*
  Since Analog is READ_WRITE variable, onAnalogChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAnalogChange()  {
  // Add your code here to act upon Analog change
}

Like that?

(I think your assignments are in the wrong order, but that's minor)

Well, this only going to display 1 decimal point printed to serial and dashboard value will be unaffected. I've tried that already

I commented only in the code I could see (which lacks a definition of "temp" - back to you)

Appologies. I have not copied the top description. The Temp is somehow predefined when you create "thing" hence for me to have it displayed on the chart i had to link "Temp" with the TempCelcius so that the data can be forwarded do dashboard. ( I think)

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/a0c6a2b4-63f4-4586-be40-f0e857368a23

  Arduino IoT Cloud Variables description

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

  int analog;
  CloudTemperature temp;

  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 <OneWire.h>
#include <DallasTemperature.h>
#include "thingProperties.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempCelcius = 0;
int analogPin = A3;

void setup() {
  //pinMode(2, INPUT_PULLUP);
  // 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);
  sensors.begin();
  // 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
  analog = analogRead(analogPin);  // read the input pin
  //Serial.println(analog);          // debug value
  sensors.requestTemperatures();
  tempCelcius = sensors.getTempCByIndex(0);
  Serial.print("T = ");
  Serial.print(temp);
  Serial.println("ºC");
  temp = tempCelcius;
}


/*
  Since Analog is READ_WRITE variable, onAnalogChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAnalogChange()  {
  // Add your code here to act upon Analog change
}

Instead of defining the precision in the dashboard you can define it directly in the code writing something like this:

temp = (int) (tempCelcius * 10) / 10.0;
or
temp = int(tempCelcius * 10) / 10.0;

If you want to round the number to the next decimal if the decimal part is greater than 0.5, you can do:

temp = (int) ((tempCelcius + 0.05) * 10) / 10.0;
or
temp = int((tempCelcius + 0.05) * 10) / 10.0;

I didn't test this code but I believe it should work.
Hope this can help.

1 Like

Hiya,

the point is that there is predefined temperature on the IOT which seems not to be made for working with tempeature unless someone need such a precise 3 decimal points for temp.
Otherwise, sending the value as float is far more simpler in this case i think.

By the way, the temp = (int) ((tempCelcius + 0.05) * 10) / 10.0; does the trick.

Thanks

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