ESP32 Arduino IDE library for managing OneWire DS18B20 temperature sensors.
Get easy temperature readings between -55° and +85° and log these to FFat.
Just set the number of desired sensors and a GPIO pin number and you are good to go.
logger.startSensors( 3, 5 )
Starts max 3 sensors on GPIO 5 that are scanned and updated every 750ms.logger.startSensors( 3, 5, 1 )
Starts max 3 sensors on GPIO 5 that are scanned and updated every 750ms.
Use ESP32 HW timer 1. If no timer number is given, timer 0 will be used.logger.sensorCount()
Get the number of sensors actually connected.logger.getSensorTemp( 0 )
Get a temperature reading from the first sensor.
sensor.startTempLogging( 180 )
Starts temperature logging to FFat every 180 seconds.
The interval and log state are saved in NVS.
If after a rebootstartSensors()
is called logging will resume with the specified interval.sensor.stopTempLogging()
Stops temperature logging to FFat.sensor.isTempLogging()
Gives the current temperature logging state.
Temperature logging writes to a csv file formatted as 1970-01-01.log
if no system time is set before. Logging is based on UTC. A typical log entry looks like:
1564356928,26.69,23.00,18.44
- The number
1564356928
is the Unix timeMon Jul 29 2019 01:35:28 GMT+0200
when used in JS as:new Date( 1564356928 * 1000 )
.
Because JSDate
is the number of milliseconds since 01-01-1970 while the UNIX time is the number of seconds since then.
So to process these time values with JavaScript just take the UNIX time value and multiply it with 1000 to get a valid JSDate
object. (with a one second resolution ofcourse) 26.69,23.00,18.44
are the logged sensor temperatures at that time.- A start marker is written to the log file every time sensors are started.
A start marker looks like#1572769710,FFatSensor start
where the number is a UNIX timestamp.
sensor.startErrorLogging()
Starts sensor error logging to FFat.sensor.stopErrorLogging()
Stops sensor error logging to FFat.sensor.isErrorLogging()
Gives the current error logging state.
Error logging writes to sensor_error.txt
.
Error logging is based on calender time.
FFatSensor runs fine without FFat partition mounted, but then you have no logging ofcourse.
- ESP32 FFat library. (only needed to log to file)
- ESP32 OneWire library by Chuck Todd. Use this library instead of the standard Arduino version which will not work for ESP32 MCUs.
- ESP32 Task by Neil Kolban.
Download and install FFatSensor
, OneWire
and Task
in the esp32 libraries folder.
#include <OneWire.h>
#include <Task.h>
#include <FFat.h>
#include <FFatSensor.h> // 1. Include the libraries.
#define FORMAT_FS false /*WILL FORMAT FFAT! set to true to format FFat.*/
/* Should be compiled with a FFat partition in Tools>Partition scheme */
FFatSensor sensor; // 2. Make an object.
void setup() {
Serial.begin( 115200 );
Serial.println();
Serial.println();
Serial.println("FFatSensor example sketch. Starting sensors.");
if ( FORMAT_FS ) FFat.format();
if ( !FFat.begin() ) Serial.println( "Could not mount FFat.");
sensor.startSensors( 10, 5 ); // 3. Start max 10 DS18B20 sensors on GPIO 5.
Serial.print("Waiting for sensors"); // 4. Wait for sensors to become available.
while ( !sensor.sensorCount() ) {
Serial.print(".");
delay( 100 );
}
Serial.println();
Serial.printf( "%i sensors found.\n", sensor.sensorCount()); // 5. Check how many sensors are found.
Serial.print( "First sensor value: "); // 6. Get a sensor reading.
Serial.println( sensor.sensorTemp( 0 ) );
// how to get a name and ID
sensorId_t id; // Make a id variable.
sensorName_t name; // Make a name variable.
for ( uint8_t num = 0; num < sensor.sensorCount(); num++ ) {
// Get the id and print it in one go.
Serial.printf( "sensor %i id: %s\n", num, sensor.getSensorId( num, id ) );
// Get the name and print it in one go.
Serial.printf( "sensor %i name: %s\n", num, sensor.getSensorName( num, name ) );
}
Serial.println();
//setting a sensor name
if ( !sensor.setSensorName( id, "thisnameistoolong" ) ) // Rename a sensor. The new name will be stored in NVS and be available after a reboot.
sensor.setSensorName( id, "FFatSensor" ); // Will return true or false depending on the result of the operation.
Serial.printf( "name from ID: %s is ", id);
Serial.println( sensor.getSensorName( id, name ) );
Serial.print( "Name from first sensor is " );
Serial.println( sensor.getSensorName( 0, name ) );
if ( FFat.totalBytes() ) {
if ( !sensor.isErrorLogging() ) sensor.startErrorLogging(); // Log sensor errors to FFat.
if ( sensor.isTempLogging() ) Serial.println( "Logging already on." ); // You can check the current log state.
if ( !sensor.startTempLogging() ) Serial.println( "Logging already on. (again)" ); // If FFat is mounted sensor values will be logged every 180 seconds.
}
else
Serial.println( "FFat not mounted so no logging enabled." );
Serial.println( "Done with setup, waiting 5 seconds before starting loop..." );
delay( 5000 );
}
void loop() {
// How to use time stamps
timeStampBuffer_t tsb;
Serial.printf( "Human timestamp: %s\n", sensor.timeStamp( HUMAN_TIME, tsb ) );
Serial.printf( "Unix timestamp: %s\n", sensor.timeStamp( UNIX_TIME, tsb ) );
Serial.printf( "Millis timestamp: %s\n", sensor.timeStamp( MILLIS_TIME, tsb ) );
Serial.printf( "%i sensors found.\n", sensor.sensorCount() );
sensorId_t id;
sensorName_t name;
for ( uint8_t num = 0; num < sensor.sensorCount(); num++ ) {
if ( !sensor.sensorError( num ) )
Serial.printf( "Sensor %i: %.4f '%15s' id: '%s'\n",
num,
sensor.sensorTemp( num ),
sensor.getSensorName( num, name ),
sensor.getSensorId( num, id ) );
else
Serial.printf( "Sensor %i: reports an error\n", num );
}
Serial.println("Waiting 500ms...");
Serial.println();
delay(500);
}