• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


[Fully Supported] 1-Wire DS18B20 and DS18S20
#1
As reuqested by CurlyMo some info about the DS18B20 (digital 1-wire temp sensor).
Once installed, you can find the sensor in /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave
When you take a look at the Adafruit site, they tell you to run theses 2 command's :

sudo modprobe w1-gpio
sudo modprobe w1-therm

After that, grab the contents of the device file. It should be like this:

b8 01 4b 46 7f ff 08 10 8a : crc=8a YES
b8 01 4b 46 7f ff 08 10 8a t=27500

The last number at the second line (t=) should give the current temperature.
(sometime the output can have NO at the first line, don't know when that will happen).
The DS18B20 is a 3 pin sensor. Left and right for power, middel pin for data.
Schematics for connecting:

[Image: breadboard-ic.png?1359394493]

Changing the + and - will break the sensor Tongue

When confirguring a protocol for this sensor in pilight, you should work with the unique number the device has : xxxxxxxxxxx (mine has 000004bc87a8).
You can hook up as many temp sensors parallel as you want. All sensors should share the same pins, but you only need one 4.7K resistor for all of them.
 
Reply
#2
Nice info, thanks very much.

JurnD
 
Reply
#3
Can you test the following script w1temp.c:
Code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

int main(void) {
    struct dirent *dir;
    struct dirent *file;
    char path[] = "/sys/bus/w1/devices/";
    DIR *d = NULL;
    DIR *f = NULL;
    FILE *fp;
    char *content;
    size_t bytes;
    struct stat st;

    char *w1nr = malloc(3);
    char *w1id = malloc(13);
    char *w1dir = NULL;
    char *w1slave = NULL;
    int w1valid = 0;
    int w1temp = 0;
    memset(w1nr, '\0', 3);
    memset(w1id, '\0', 13);
    
    if((d = opendir(path))) {
        while((dir = readdir(d)) != NULL) {
            if(dir->d_type == DT_DIR) {
                if(strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
                    sprintf(w1nr, "%c%c", dir->d_name[0], dir->d_name[1]);
                    if(atoi(w1nr) == 28) {
                        memcpy(&w1id[0], &dir->d_name[3], 12);
                        w1dir = malloc(strlen(dir->d_name)+strlen(path)+1);
                        sprintf(w1dir, "%s%s/", path, dir->d_name);

                        if((f = opendir(w1dir))) {
                            w1valid = 0;
                            while((file = readdir(f)) != NULL) {
                                if(file->d_type == DT_REG) {
                                    if(strcmp(file->d_name, "w1slave") == 0) {
                                        w1slave = malloc(strlen(w1dir)+8);
                                        sprintf(w1slave, "%sw1slave", w1dir);

                                        if(!(fp = fopen(w1slave, "rb"))) {
                                            // logprintf(LOG_ERR, "cannot read w1 file: %s", configfile);
                                            return EXIT_FAILURE;
                                        }

                                        fstat(fileno(fp), &st);
                                        bytes = (size_t)st.st_size;

                                        if(!(content = calloc(bytes+1, sizeof(char)))) {
                                            //logprintf(LOG_ERR, "out of memory");
                                            return EXIT_FAILURE;
                                        }

                                        if(fread(content, sizeof(char), bytes, fp) == -1) {
                                            //logprintf(LOG_ERR, "cannot read config file: %s", w1slave);
                                        }

                                        char *pch = strtok(content, "\n=: ");
                                        int x = 0;
                                        while(pch) {
                                            if(strlen(pch) > 2) {
                                                if(x == 1 && strstr(pch, "YES")) {
                                                    w1valid = 1;
                                                }
                                                if(x == 2) {
                                                    w1temp = atoi(pch);
                                                }
                                                x++;
                                            }
                                            pch = strtok(NULL, "\n=: ");
                                        }
                                        free(w1slave);
                                        break;
                                    }
                                }
                            }
                            printf("ID: %s\nValid: %d\nTemp: %d\n", dir->d_name, w1valid, w1temp);
                            free(w1dir);
                        }
                    }
                }
            }
        }
        closedir(d);
    }
    free(w1nr);
    free(w1id);

    return(0);
}

Compilation:
Code:
cc -o w1temp w1temp.c
./w1temp
 
Reply
#4
I assume it should print the current temp on the screen?
Right now it's not doing anything?
It doesn't give any info or error. It runs and immediately returns to the prompt without any info.
 
Reply
#5
Can you try and debug on which step it fails by adding echo's like this:
Code:
printf("check\n");

Also, if you want to check the value of one of the variables, you can do:

char *:
Code:
printf("%s\n", variable);

int:
Code:
printf("%d\n", variable);

I've bought this sensor myself so i hope i can add support for it soon.
 
Reply
#6
Done
 
Reply
#7
hi there.. is it poossible to add ds18s20?
It seems, the only difference seems to be that th ID starts not with 28- but with 10-.

so.. what about supporting both devices by deleting the hardcoded 28- infront of device id?

i would jtest it for myself.. but im not into compiling things.. i just installed pilight by deb package..

if you are able to compile such a lib.. i would copy it to my installation and test it..

thx a lot
 
Reply
#8
Please do compile and test if it works with the 10- as well.
http://www.pilight.org/getting-started/installation/
 
Reply
#9
after playing around i got my DS18S20 Sensors to work.. i had to edit
line 84 of https://github.com/pilight/pilight/blob/.../ds18b20.c

i changed "%s28-%s/" to "%s10-%s/".. now my sensors are shown and working.. and temperature is showing correct..


i also tried to change "%s28-%s/" to "%s%s/".. but pilight failed to start with that..

so.. this is a workaround for me.. but maybe you can change the code for other people? or make a new ds18s20.c with "%s10-%s/"?
i don´t have any knowledge about those things.. i just played.. and played.. and played.. around for hours!
 
Reply
#10
Added the ds18s20 protocol. And good work in trying Cool
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Partially Supported] TFA / Conrad Weather Yves 203 49,022 03-27-2020, 01:21 PM
Last Post: Ascenion
  [Fully Supported] KlikAanKlikUit Motion Sensor koen01 37 21,020 03-26-2020, 02:46 PM
Last Post: Gisto
Lightbulb [Fully Supported] Kaku Door sensor (AMST-606) geerttttt 53 35,651 10-19-2019, 06:26 PM
Last Post: curlymo
  [Fully Supported] DHT22 IcedEarth 86 47,431 11-18-2018, 09:33 AM
Last Post: curlymo
  [Fully Supported] Arctech Old Martin 111 55,960 03-27-2018, 04:43 PM
Last Post: genfersee
  [Fully Supported] Clarus Switches Marcin 69 38,986 01-30-2018, 07:10 PM
Last Post: Niek
  [Fully Supported] Remote Control Socket (RC101-U/RC201) Sean 18 15,928 01-04-2018, 06:18 AM
Last Post: ettman8
  [Fully Supported] LM75 and LM76 temperature sensor horst_dieter 64 36,695 11-19-2017, 08:54 PM
Last Post: edepi
  [Fully Supported] No-brand temp/humidity sensor (alecto_ws1700) meloen 57 48,506 12-25-2016, 09:53 PM
Last Post: creamers
  [Fully Supported] Impuls/SelectRemote Bram 113 68,858 05-28-2016, 02:53 PM
Last Post: Puuu

Forum Jump:


Browsing: 1 Guest(s)