Raspberry pi and ds18b20

Hi everyone , i am very new to this forum , i have a thermal store that i wish to monitor the temperature at the top and bottom of the store , i have a raspberry pi and standard expansion board to which i have connected 2 x cable ds18b20 sensors , i have a python script that queries the sensors and sends the data to google spreadsheet , this works ok


what i would like to know is just using the hardware i have can i send the data to emoncms so i can view the data in a nice format? , if so can someone point me to any guides on how to do this ?       Many many thanks in advance ......

fcattaneo's picture

Re: Raspberry pi and ds18b20


I did a software bridge in python, for send 1-wire temp data to emoncms , using the raspberry 1-wire bus managed by kernel on GPIO4.

This script use mysql db to store the unique 64bit (8 byte) address and for convert this value

in human readable format ( ex.  'room temperature' instead of '10-00080082fd88' )

The name of DB is homeaut  where there is a table called  'sensors'.

create this table using s sql command :

-- Database: `homeaut`

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `enabled` tinyint(1) unsigned NOT NULL,
  `name` varchar(50) NOT NULL,
  `address` varchar(15) DEFAULT NULL,
  PRIMARY KEY (`id`)

Edit the script 1wireTempRead.py and :

-In line 51 set your correct emoncms url

-in line 54 set your user and pasword for mysql user

-in line 76 set the apikey of your emoncms installation


Save the script in this path and  with this name :


Set the correct permission:

[email protected] ~ $ sudo chmod 755  /home/pi/python/1wireTempRead.py


This script must scheduled every minute by cron :

[email protected] ~ $ sudo crontab -e

insert this line :

* * * * * python /home/pi/python/1wireTempRead.py

after this you will see in emoncms input folder, a new line for each 1-wire device connected on raspberry gpio4.

If the unique 64bit (8 byte) address is not set in db, you will see the unique 64bit (8 byte) address as input name .

I suggest to insert in mysql db, the address and the uman readable format for each device.... in this mode in case of 1-wire device failure, you will change the reference of new device without changing anything in emoncms.

Sorry for my english





argofanatic's picture

Re: Raspberry pi and ds18b20

Here's a python script I've been working on this morning. I'm a noob here but I managed to get this going my pi with emoncms running on another machine.

Very similar to the previous post.


rodw's picture

Re: Raspberry pi and ds18b20

Here is some C code for the Raspberry PI if it helps. I borrowed this from the web somewhere and redid it to break it up into an Initialise function and a call to read the temperature. It supports both Celcius and Farenheit in the call to read the temp.


DS18B20 temperature sensor for raspberry PI
See: http://bradsrpi.blogspot.com.au/2013/12/c-program-to-read-temperature-fr...

sudo modprobe w1-gpio
sudo modprobe w1-therm

Compile: gcc -Wall -std=gnu99 w1.c -o w1

Connections (looking at flat side):
DS18B20 Pin   Raspberry Pi (Rev. B)
1 Left          6 GND
2 Center        7 GPIO4 (w/ 4.7k pull-up resistor to pin 1 - 3V3)
3 Right         1 3V3

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

int  tempScale = 'C'; // Scale in degrees  C or F
char dev[16];       // Dev ID for DS18B20 thermometer
char devPath[128];  // Path to device

int init_thermometer(char *dpath, char *tdev);
float read_thermometer(char *dpath, int scale);

// Initialise
int init_thermometer(char *dpath, char *tdev)

DIR *dir;
struct dirent *dirent;
char path[] = "/sys/bus/w1/devices";

dir = opendir (path);
if (dir != NULL){
  while ((dirent = readdir (dir)))
  // 1-wire devices are links beginning with 28-
  if (dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL) {
   strcpy(tdev, dirent->d_name);
  (void) closedir (dir);
  printf("Couldn't open the w1 devices directory");
  return 1;
// Assemble path to OneWire device
sprintf(dpath, "%s/%s/w1_slave", path, tdev);
return 0;

float read_thermometer(char *dpath, int scale)
char buf[256];     // Data from device
char tmpData[6];   // Temp C * 1000 reported by device
float tempC;
ssize_t numRead;

int fd = open(dpath, O_RDONLY);
if(fd == -1){
  printf("Couldn't open the w1 device: %s\n.", dpath);
     return 1;  
while((numRead = read(fd, buf, 256)) > 0) {
  strncpy(tmpData, strstr(buf, "t=") + 2, 5);
  tempC = strtof(tmpData, NULL);
   case 'C':          //Convert to Celcius
    tempC = tempC / 1000;
    //printf("Temp: %.3f C  ", tempC);
   case 'F':          // Convert to Farenheit
    tempC =  ((tempC / 1000) * (9 / 5)) + 32;
    //printf("%.3f F\n\n", tempC);
    printf("Invalid Scale requested: %c\n",scale);
return tempC;

int main (void) {
int retval;
float tTemp;

retval = init_thermometer(devPath, dev);
  printf("\nDevice: %s\n", dev);
  // Read temp continuously
  // Opening the device's file triggers new reading
  while(1) {
   tTemp = read_thermometer(devPath, tempScale);
   printf("Temperature =  %.3f %c\n",tTemp, tempScale);
  /* return 0; --never called due to loop */



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.