0

Hhow do I set the data format in a text block
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Hhow do I set the data format in a text block

by RedSafety on Tue Jan 22, 2019 2:13 pm

Hi all,

I am trying to construct a dashboard to display weather temperature data in centigrade . The problem I have is that by default 6 decimal places are been displayed, this is even though the data from my ESP32 via the Arduino serial monitor shows for example 22.00. But the dashboard via the text block shows 22.000000. So my question is how do I set the data format so only data is displayed to two decimal places?

RedSafety
 
Posts: 13
Joined: Mon Jan 14, 2019 4:26 pm

Re: Hhow do I set the data format in a text block

by franklin97355 on Tue Jan 22, 2019 2:20 pm

What are you sending to the block? Can you post the code fragment that sends the data?

franklin97355
 
Posts: 20270
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Hhow do I set the data format in a text block

by RedSafety on Tue Jan 22, 2019 3:18 pm

Hi franklin 97355, as requested
void SensorCode(){
Serial.println("Reading Sensors...");
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.println("DONE");

Serial.print("Sensor 1: ");
Serial.print(sensors.getTempC(sensor1));
Serial.println("(C) ");

S1 = sensors.getTempC(sensor1);
}

void feedWrite(){

AdafruitIO_Feed *sensor1Feed = io.feed("S1");

Serial.println("sending value to feed");

// send data to feed
sensor1Feed->save(S1);

// write data to AIO
io.run();
}

RedSafety
 
Posts: 13
Joined: Mon Jan 14, 2019 4:26 pm

Re: Hhow do I set the data format in a text block

by brubell on Wed Jan 23, 2019 4:56 pm

By default, Adafruit IO Arduino saves to six precision points. you can change this to show only two decimal places by modifying the call from:

Code: Select all | TOGGLE FULL SIZE
sensor1Feed->save(S1);

to:
Code: Select all | TOGGLE FULL SIZE
sensor1Feed->save(S1, 0, 0, 0, 2);

brubell
 
Posts: 336
Joined: Fri Jul 17, 2015 10:33 pm

Re: Hhow do I set the data format in a text block

by RedSafety on Thu Jan 24, 2019 2:47 pm

Hi brubell.

Sorry to say, that updating sensor1Feed->save(S1); to sensor1Feed->save(S1, 0, 0, 0, 2); still displays six precision points.

Best regards

RedSafety
 
Posts: 13
Joined: Mon Jan 14, 2019 4:26 pm

Re: Hhow do I set the data format in a text block

by brubell on Thu Jan 24, 2019 6:01 pm

RedSafety wrote:Hi brubell.

Sorry to say, that updating sensor1Feed->save(S1); to sensor1Feed->save(S1, 0, 0, 0, 2); still displays six precision points.

Best regards


Try:
sensor1Feed->save(S1 0, 0, 0, precision = 2);

Alternatively, you can cast the S1 value to an integer (int(S1)) and send that.

brubell
 
Posts: 336
Joined: Fri Jul 17, 2015 10:33 pm

Re: Hhow do I set the data format in a text block

by RedSafety on Fri Jan 25, 2019 1:13 pm

Hi
I have tried sensor1Feed->save(S1, 0, 0, 0, precision = 2); however this flags precision was not declared in this scope , thus could you advice what additional code has to be added.

Your suggestion regarding integer would result in a whole number i require precision of two places.

Why have you not built built-in formatting within the desktop environment?

Regards

RedSafety
 
Posts: 13
Joined: Mon Jan 14, 2019 4:26 pm

Re: Hhow do I set the data format in a text block

by brubell on Fri Jan 25, 2019 4:26 pm

RedSafety wrote:Hi
I have tried sensor1Feed->save(S1, 0, 0, 0, precision = 2); however this flags precision was not declared in this scope , thus could you advice what additional code has to be added.

Why have you not built built-in formatting within the desktop environment?


We have built in formatting for specifying how many precision points of a float to send (see: https://github.com/adafruit/Adafruit_IO ... d.cpp#L107)

The function call for to feed->save for a float would look like:

Code: Select all | TOGGLE FULL SIZE
sensor1Feed->save(float value, double lat, double lon, double ele, int precision)

brubell
 
Posts: 336
Joined: Fri Jul 17, 2015 10:33 pm

Re: Hhow do I set the data format in a text block

by brubell on Fri Jan 25, 2019 4:33 pm

I skimmed the code too quickly, here are some issues I noticed that you might want to correct (if you can provide your entire code, I can compile it and test from here, you only provided two functions.

Code: Select all | TOGGLE FULL SIZE
void  SensorCode(){
    Serial.println("Reading Sensors...");
 //Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  //Serial.println("DONE");
 
  Serial.print("Sensor 1: ");
  // get the value of S1 and save it to a float
  float S1 = sensors.getTempC(sensor1);
  Serial.print(S1);
  Serial.println("(C) ");
}

void feedWrite(float sensorValue){

AdafruitIO_Feed *sensor1Feed = io.feed("S1");
 
  Serial.println("sending value to feed");
 
  // send data to feed w/2 prec. points
  sensor1Feed->save(sensorValue, 0, 0, 0, 2);
 
  // write data to AIO
  io.run();

brubell
 
Posts: 336
Joined: Fri Jul 17, 2015 10:33 pm

Re: Hhow do I set the data format in a text block

by RedSafety on Sat Jan 26, 2019 6:32 am

Hi brubell.
Thank you for the assistance you are providing .
For the record:-
I did not originally send the entire code as franklin97355 requested a code fragment.
The entire code below using your example sensor1Feed->save(S1, 0, 0, 0, 2) does compile but does not display to two decimal places.
Using your 2nd suggestion of sensor1Feed->save(S1, 0, 0, 0, precision = 2) produces the error.

// Adafruit IO Environmental Data Logger
// Tutorial Link: https://learn.adafruit.com/adafruit-io- ... ty-monitor
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Brent Rubell for Adafruit Industries
// Copyright (c) 2018 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Adafruit IO Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"

/**************************** Sensor Configuration ***************************************/
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) */

// Data wire is connected to GPIO15
#define ONE_WIRE_BUS 15
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0x80, 0x7, 0x43, 0x98, 0x4, 0x0, 0xFB };


/**************************** ***************************************/
// Delay between sensor reads, in seconds
#define READ_DELAY 10

float S1;

/**************************** Setup ***************************************/
void setup() {
// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while (!Serial);

Serial.println("Adafruit IO Environmental Logger");
sensors.begin();
sensors.setResolution(sensor1, 11);

/**************************** Calls***************************************/

// connect to the Adafruit IO Library
connectAIO();

//DS18B20 Sensor code
SensorCode();

// set up and write to deepsleep feed
feedWrite();


/**************************** DS18B20 Sensor***************************************/

// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
//io.run();

/*
First we configure the wake up source
We set our ESP32 to wake up every 60 seconds
*/
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Going to sleep now");
delay(1000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}

/****************************LOOP NOT USED ***************************************/
void loop() {

}

/**************************** Subs 1 ***************************************/

void connectAIO() {
Serial.println("Connecting to Adafruit IO...");
io.connect();

// wait for a connection
while (io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}

// we are connected
Serial.println();
Serial.println(io.statusText());
}
/**************************** Subs 2 ***************************************/
void feedWrite(){


AdafruitIO_Feed *sensor1Feed = io.feed("S1");

Serial.println("sending value to feed");

// send data to feed
sensor1Feed->save(S1, 0, 0, 0, 2);

// write data to AIO
io.run();
}
/**************************** Sub***************************************/
void SensorCode(){
Serial.println("Reading Sensors...");
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.println("DONE");

Serial.print("Sensor 1: ");
Serial.print(sensors.getTempC(sensor1));
Serial.println("(C) ");

S1 = sensors.getTempC(sensor1);

}

RedSafety
 
Posts: 13
Joined: Mon Jan 14, 2019 4:26 pm

Re: Hhow do I set the data format in a text block

by brubell on Mon Jan 28, 2019 12:47 pm

I updated the library (and tested with an ESP32) for sending floats of variable precision on ESP32/SAMD/AVR hardware in the format:
Code: Select all | TOGGLE FULL SIZE
feedName->save(floatValue, 0, 0, 0, 2);


Download the latest library version (https://github.com/adafruit/Adafruit_IO ... tag/2.7.23) and let me know how it goes.

brubell
 
Posts: 336
Joined: Fri Jul 17, 2015 10:33 pm

Re: Hhow do I set the data format in a text block

by RedSafety on Tue Jan 29, 2019 1:56 pm

Hi brubell.

Thank you all works Ok now, pleased my code was OK.

Best regards

RedSafety
 
Posts: 13
Joined: Mon Jan 14, 2019 4:26 pm

Please be positive and constructive with your questions and comments.