roby1chenoby
Posts: 5
Joined: Tue Dec 10, 2013 3:08 pm
Location: Italy

DS18B20 reading 85°C

Wed Dec 11, 2013 1:35 pm

Hi all,
after some time spent configuring and adding some funcionality at my new Raspberry i've decided to create a temperature logger using python, sqlite and the Dallas DS18B20 sensor. Once the database and script were ready i placed an order and bought me 2 sensors and the 4,7Kohm resistors. In the first test i've followed the schema you can find in this page:
http://www.devicehive.com/samples/pytho ... ure-sensor
As a result i've my sensor at the bottom of a 3mt cat5 network cable with the other side attached at the GPIO (pin 2, 6 and 7). When i firs loaded the modules (w1_gpo and w1_therm) and looked in /sys/bus/w1/devices/ i was happy 'cause i found my device BUT when i've tried to do a cat at the w1_slave file the temperature reading is costantly 85°C
Searching for other information i've found that the pin2 in the GPIO have 5v but almost any schemas that use the DS18B20 connect the vcc in pin1 (3v3). So i took the other sensor from the static bag and connect it using a short cable and another resistor. Same result :(
What can i check? Anybody can help me?
Roberto

jets
Posts: 58
Joined: Mon May 06, 2013 9:40 am

Re: DS18B20 reading 85°C

Wed Dec 11, 2013 1:52 pm

Well, I had similar problem. The 1820 gives temperature 85 in case there is some sort of communication failure.

How long is your cable? Check that you have connected correct pins correctly etc.

I use 1-wire master though.

I had the 85 degree problem with poor cables and poor connections while testing. I sometimes do still get 85 degrees.

See this about my configuration:
http://www.raspberrypi.org/phpBB3/viewt ... 3&p=454645

And google for "1820 85 degrees" :)

roby1chenoby
Posts: 5
Joined: Tue Dec 10, 2013 3:08 pm
Location: Italy

Re: DS18B20 reading 85°C

Wed Dec 11, 2013 2:22 pm

Thank you for the prompt reply! Inthe firs test my sensor was connected via 3mt long cat5 network cable and the other one (for testing pourposes) now is "working" with 15cm thin cable. In both cases i use a 4k7 resistor but i've read that is not needed in some configurations. I'm not an expert...could it be convenient if i try without the resistor or there is any risk of damage?

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Wed Dec 11, 2013 7:24 pm

Hi

A working configuration for the Pi is to use 3.3v

The Vdd connection on the sensor is connected to pin 1 (3.3volts)
The Ground connection is to pin 6 (ground)
and the Data connection (DQ pin) to pin 7 (gpio 4)

A single 4.7k resistor goes between data and 3.3v.
This resistor is placed at the Pi end of the cable and only one resistor is required even for multiple temperature sensors.
(Under some circumstances a different value resistor helps, but for a simple 1-wire network like yours, use the recommended 4.7k)

Using cat 5 cable is best and will make it easier to maintain a working 1-wire network when you expand and add more sensors.
The data and ground should use one twisted pair, for example blue/blue-white.
A single wire from another pair is used for the 3.3 volt supply.

Don't double up wires on the assumption that this lowers resistance and is a 'good thing', it actually alters the impedance of the network and makes it less reliable.
All unused wires in the cat 5 cable should be left unconnected (don't connect them to ground).

The 85 degree reading is the power-up state of the DS18B20 & DS1822 sensors see datasheet page 7 http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf. This means that the temperature sensor has either not received an instruction to do a temperature conversion, or it has not had the time or the power to do a conversion.

In my experience, poor network cabling has been the major cause of failed reads. I recently replaced the non-cat 5 cable supplying three sensors on a branch of the network with cat 5 cable, and immediately stopped seeing errors.

Many articles state that a single cable with sensors attached along it, is better than a star or branching configuration.
Despite that I have found that having a few branches works OK - I am running 11 sensors on a 1-wire network with 5 branches. One of the sensors on one branch does fail a few times a day - I just have the software detect a failure and do a re-read.

If you are planning a fairly large network with some branches, it might be helpful to have your scripts log errors (crc errors or temperature conversion errors). That way you can identify where some cabling changes might help.

Regards

anita2R

roby1chenoby
Posts: 5
Joined: Tue Dec 10, 2013 3:08 pm
Location: Italy

Re: DS18B20 reading 85°C

Thu Dec 12, 2013 10:49 am

Many thanks Anita,
i'll try as soon i've some time to do a complete cable check and re-wiring using the tip you've mentioned (data and gnd using a twisted pair).
Another question: using the Raspberry the only way to read the temperature is to do a "cat" of the w1_slave file? are there any other alternatives?
Thanks in advance,
Roberto

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Thu Dec 12, 2013 3:44 pm

Hi,

If you are using the Raspberry Pi's inbuilt 1-wire modules (wire, w1-gpio & w1-therm) then as far as I know, the only way to read the temperatures is with the cat command.

There are other ways of running a 1-wire network from the Pi. These use small hardware devices that manage the 1-wire timing and communication, and the Pi does simple communication with the device. The upside of these devices is that the timing of the signals on the 1-wire network is mainly hardware controlled and is highly accurate, compared to the Pi's system where the timing is software generated and more susceptible to errors.

The downside is that the hardware devices cost money! For small networks, particularly if you have some software checking of results with automatic re-reads, the Pi's system will be OK.

There are USB dongles that provide 1-wire connection and management, although I have not used one, and there is software to manage the communication between the Pi and the USB connected device (you won't be using 'cat').

The other device (that I have used) is the HA7s from Embedded Systems http://www.embeddeddatasystems.com/HA7S ... _p_23.html
This device communicates with the Pi using the UART - a simple serial communication on pins 8 and 10.
They claim that up to 100 devices can be connected and it will work on up to 1000 feet of cat 5 cable.
It's other advantage is that you are not limited to temperature measurement - other 1-wire devices can be used. Also you can interact with the temperature sensors, for example to change measurement sensitivity and to use the alarm features of the sensors. To use the HA7s with the Pi, all you need is some simple serial communication using the UART (/dev/ttyAMA0).

Using the Pi's inbuilt system on pin 7, you can do all the work using a bash script. Here is the script I used:

Code: Select all

#!/bin/bash
# a script to read and save temperature readings from all the group 28 1-wire temperature sensors
#
# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
# iterate through all the devices
for file in $FILES
    do
      # get the 2 lines of the response from the device
      GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
      GETDATA1=`echo "$GETDATA" | grep crc`
      GETDATA2=`echo "$GETDATA" | grep t=`
      # get temperature
      TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
      #
        # test if crc is 'YES' and temperature is not -62 or +85
        if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000"  ]
           then
               # crc is 'YES' and temperature is not -62 or +85 - so save result
               echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> /var/1w_files/$file
           else
               # there was an error (crc not 'yes' or invalid temperature)
               # try again after waiting 1 second
               sleep 1
               # get the 2 lines of the response from the device again
               GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
               GETDATA1=`echo "$GETDATA" | grep crc`
               GETDATA2=`echo "$GETDATA" | grep t=`
               # get the temperature from the new response
               TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
                  if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000" ]
                      then
                      # save result if crc is 'YES' and temperature is not -62 or +85 - if not, just miss it and move on
                      echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> /var/1w_files/$file
                  fi
               # this is a retry so log the failure - record date/time & device ID
               echo `date +"%d-%m-%Y %H:%M:%S"`" - ""$file" >> /var/1w_files/err.log
           fi
    done
exit 0
# before using this script you need to make a folder for the data files
# sudo mkdir /var/1w_files
# then create files for each device e.g. /var/1w_files/28-0000018a4be8
# 28-0000018a4be8 is one of your 1-wire devices and will be the filename for its data
# and create a file for the error log:
# /var/1w_files/err.log
# The bash script will need to be made executable for you
# sudo chown <username> <script name>
# sudo chmod 0700 <script name>
The script can then be automatically called at regular intervals using crontab
(add the script path/filename and read frequency to crontab by entering crontab -e in a terminal)
Now you have a nice log file with date & time stamp and temperature readings for every temperature sensor
Then you can create charts and statistics

Regards

anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Sat Feb 22, 2014 11:36 am

@anita2r

thank you for yours excellent scripts.

this last script you put here, if I use 4 sensors ds18b20, it´s possible to print every info in the same line in Cº ?
like this ( 22-02-2014 11:25:56 17,125 34,234 22,443)

thank you

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Sat Feb 22, 2014 3:39 pm

patalhetas wrote:@anita2r

thank you for yours excellent scripts.

this last script you put here, if I use 4 sensors ds18b20, it´s possible to print every info in the same line in Cº ?
like this ( 22-02-2014 11:25:56 17,125 34,234 22,443)

thank you
Yes it's certainly possible.

First a couple of questions for clarification:
1. do you want the one line output to go to a file - and if so how do you want to name the file
a will it be one file - always the same name
or
b. will it be new file every day or every month - perhaps named by the start date
2. what do you want to happen if a reading is missing - a gap or perhaps an asterix '*' or something like 'N/R'
like this: 22-02-2014 11:25:56 17,125 * 22,443

With that information I will try a re-write for you.

Regards

anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Sat Feb 22, 2014 5:52 pm

anita2r wrote:
patalhetas wrote:@anita2r

thank you for yours excellent scripts.

this last script you put here, if I use 4 sensors ds18b20, it´s possible to print every info in the same line in Cº ?
like this ( 22-02-2014 11:25:56 17,125 34,234 22,443)

thank you
Yes it's certainly possible.

First a couple of questions for clarification:
1. do you want the one line output to go to a file - and if so how do you want to name the file
a will it be one file - always the same name
or
b. will it be new file every day or every month - perhaps named by the start date
2. what do you want to happen if a reading is missing - a gap or perhaps an asterix '*' or something like 'N/R'
like this: 22-02-2014 11:25:56 17,125 * 22,443

With that information I will try a re-write for you.

Regards

anita2R
Thank you for your replay.
One picture it´s beater then 1000 words :)

Code: Select all

17.312 17.250 17.187 
17.312 17.250 17.250 
17.375 17.250 17.250 
17.375 17.250 17.187 
I don´t need the date/hour only the value form ds18b20, every minute (one line per minute) in loop (24h/day)
If some sensor give error, yes you can put "*" .
Now I have a sensors working in arduino but I need change to a raspberry pi !

it´s possible ?
Last edited by patalhetas on Sun Feb 23, 2014 1:30 pm, edited 1 time in total.

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Sun Feb 23, 2014 7:46 pm

Hi patalhetas,

I have modified the script so that there is a daily file - named by date in the format: 23-02-2014.log
If you want to always use the same file, just change the second of these two lines:
# create name for daily log file
DLOG=`date +"%d-%m-%Y.log"`
As daily files contain the date, each line of data starts with the time - no repetition of the date.
If you want the date at the start of every line, just change the date format string in the second of these two lines:
# put timestamp at start of each line of daily log file followed by a space (040 is octal for decimal 32)
echo -ne `date +"%H:%M:%S"`"\040" >> $TPATH$DLOG
for example: "%d-%m-%Y %H:%M:%S"

I left in-place code to also save the data in files named for each sensor, as in the original script.
If you don't want this just comment-out or remove the line of code (occurs in two places in the script):
# save to file named by sensor ID - comment out next line if this is not required
echo `date +"%d-%m-%Y %H:%M:%S "`"$TEMP" >> $TPATH$file
I have not had a chance to test this script over different days and I haven't been able to force read errors to test how the script copes with errors - including putting a '*' in place of a missing temperature.
If it does not work, let me know what doesn't work and what output you get and I will see if I can correct it.

You did not say what character you wanted to use for missing data. You can change the character or characters in the second of these two lines:
# character or characters to use as place marker for missing value (no spaces allowed)
PMARK="*"
In your earlier post you used ',' as the thousands delimiter, but used '.' in the later post.
The math calculation done using the 'bc' program called by the script uses '.'. I am not aware of a way to change that within the 'bc' program, however you could add a line to do a substitution using 'sed', before saving the temperature to the file.

The 'bc' math program has a parameter for the number of decimal places used in the temperature division, which you can edit in the second of these two lines (occurs in two places in the script):
# convert temperature result (divide by 1000) - use 'bc' for floating point math
TEMP=$(echo "scale=3; $TEMP/1000" | bc)
If I recall correctly you can also add a parameter to control leading zeros if necessary.

Hopefully this will give you enough information to get up and running.

Here is a sample of the output - I only had two sensors attached to the Pi for this test. The script should cope with any number of sensors - putting all temperature results on one line for each time-point.
  • 14:02:25 22.375 22.562
    14:04:58 22.375 22.562
    14:41:53 22.000 22.187
Regards

anita2R

Code: Select all

#!/bin/bash
# a script to read and save temperature readings from all the group 28 1-wire temperature sensors
#  and output all readings from each time-point on a single line of a daily file
#
# setup
# path to temerature log files
TPATH="/var/1w_files/"
# character or characters to use as place marker for missing value (no spaces allowed)
PMARK="*"
# name of error log
ERRLOG="err.log"
# create name for daily log file
DLOG=`date +"%d-%m-%Y.log"`
#
# put timestamp at start of each line of daily log file followed by a space (040 is octal for decimal 32)
echo -ne `date +"%H:%M:%S"`"\040" >> $TPATH$DLOG
#
# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
#
# iterate through all the devices
for file in $FILES
        do
        # get the 2 lines of the response from the device
        GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
        GETDATA1=`echo "$GETDATA" | grep crc`
        GETDATA2=`echo "$GETDATA" | grep t=`
        # get temperature
        TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
        # test if crc is 'YES' and temperature is not -62 or +85
        if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000"  ]
                then
                # crc is 'YES' and temperature is not -62 or +85 - so save result
                # save to file named by sensor ID - comment out next line if this is not required
                echo `date +"%d-%m-%Y %H:%M:%S "`"$TEMP" >> $TPATH$file
                # convert temperature result (divide by 1000) - use 'bc' for floating point math
                TEMP=$(echo "scale=3; $TEMP/1000" | bc)
                # save to daily file (just append to same line followed by space but no trailing new line character)
                echo -ne "$TEMP""\040">> $TPATH$DLOG
                else
                # there was an error (crc not 'yes' or invalid temperature)
                # try again after waiting 1 second
                sleep 1
                # get the 2 lines of the response from the device again
                GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
                GETDATA1=`echo "$GETDATA" | grep crc`
                GETDATA2=`echo "$GETDATA" | grep t=`
                # get the temperature from the new response
                TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
                if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000" ]
                        then
                        # save result if crc is 'YES' and temperature is not -62 or +85 - if not, just miss it and move on
                        # save to file named by sensor ID - comment out next line if this is not required
                        echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> $TPATH$file
                        # convert temperature result (divide by 1000) - use 'bc' for floating point math
                        TEMP=$(echo "scale=3; $TEMP/1000" | bc)
                        # save to daily file (just append to same line followed by space but no trailing new line character)
                        echo -ne "$TEMP""\040">> $TPATH$DLOG
                        else
                        # put a place marker in the daily file to show missing value
                        echo -ne "$PMARK""\040" >> $TPATH$DLOG
                fi
                # this is a retry so log the failure - record date/time & device ID
                echo `date +"%d-%m-%Y %H:%M:%S"`" . ""$file" >> $TPATH$ERRLOG
        fi
done
#
# put a new line character into daily file - ready for next set of data
echo >> $TPATH$DLOG
#
exit 0

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Sun Feb 23, 2014 10:10 pm

Thank you,
work like a charm :)

U are 5*
23-02-2014 22:18:02 9.56 14.18 48.06 10.31
23-02-2014 22:20:02 9.56 14.18 48.00 10.37
23-02-2014 22:22:01 9.56 14.18 48.00 10.37
23-02-2014 22:24:01 9.62 14.18 47.93 10.43
23-02-2014 22:26:01 9.62 14.12 47.93 10.43

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Mon Feb 24, 2014 12:03 am

patalhetas wrote:Thank you,
work like a charm :)
You are very welcome ... lets hope it works OK if there are read errors :(

Regards

anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Mon Feb 24, 2014 12:09 am

anita2r wrote:
patalhetas wrote:Thank you,
work like a charm :)
You are very welcome ... lets hope it works OK if there are read errors :(

Regards

anita2R
the errors I change the value 1 to 2 seconds (sleep 2) after that i don´t have errors´s any more. we gone a see :)

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Thu Mar 13, 2014 12:24 am

hi @anita2R
I need you help again :)

if you remember, you create a bash script to get a output like this :

Code: Select all

13-03-2014 00:48:01 5.75 15.06 49.12 9.12

I need the same log with date hour and cat from usb, like this "cat /dev/ttyUSB0" and the output change to temperatures !
it´s possible ?

thank you in advance

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: DS18B20 reading 85°C

Thu Mar 13, 2014 4:33 am

I would strongly recommend getting a bus master like a DS2483 and using owfs. The bus is much higher-performance, offloads processing and reduces errors, and will make your life so much easier. You can get a DS2483 for less than $1 and save your GPIO.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Thu Mar 13, 2014 3:09 pm

patalhetas wrote:hi @anita2R

if you remember, you create a bash script to get a output like this :

Code: Select all

13-03-2014 00:48:01 5.75 15.06 49.12 9.12

I need the same log with date hour and cat from usb, like this "cat /dev/ttyUSB0" and the output change to temperatures !
it´s possible ?
Can you give some more detail.
1. What is the output from "cat /dev/ttyUSB0" and do you want all of the 'cat' output on each line of the log.
2. What do you want changed to temperatures. The existing log is a list of temperatures - does the output of the cat command contain a temperature reading, or do you want the existing temperature results reformatted, for example:
5.75C 15.06C 49.12C 9.12C

Regards

anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Thu Mar 13, 2014 3:45 pm

hello, @anita2R
I have solved the problem :)

thank´s for your attention !

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Sat Mar 15, 2014 8:15 pm

:cry: I have a small (big) problem
this script work 150%, but after I need send the log to a server ...and my problem´s start
I use the rync in remote machine, I try in local machino to, but the result it´s the same, I lost information :(

" rsync -avuz --rsh="ssh -l root" [email protected]:/home/arduino/ /var/www/arduino "

in local machine where I run your script @anita2R I have this :

Code: Select all

15-03-2014 19:56:01 9.68 28.06 60.00 14.31 
15-03-2014 19:57:02 9.68 28.06 60.00 14.25 
15-03-2014 19:58:02 9.62 28.06 60.00 14.31 
15-03-2014 19:59:01 9.68 28.06 59.93 14.37 
15-03-2014 20:00:01 9.62 28.06 59.93 14.12
in remote server, I only have this ! :(

Code: Select all

15-03-2014 19:56:01 9.68 28.06 60.00 14.31 
15-03-2014 19:57:02 9.68 28.06 60.00 14.25 
15-03-2014 19:58:02 9.62 28.06 60.00 14.31 
15-03-2014 19:59:01 9.68 28.06 59.93 14.37 
15-03-2014 20:00:01 9.62 

I lost data ......

I try one million configurations, but the result not change !!!
what can I do more :(

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Sun Mar 16, 2014 4:39 pm

patalhetas wrote:I have a small (big) problem
this script work 150%, but after I need send the log to a server ...and my problem´s start
I use the rync in remote machine, I try in local machino to, but the result it´s the same, I lost information :(

" rsync -avuz --rsh="ssh -l root" [email protected]:/home/arduino/ /var/www/arduino "
Do you always lose the same amount of data from the last line of the file
or do you lose different amounts of data each time rsync runs?

I use rsync, but I don't have any in-depth experience.

You might consider posting this question in a different forum with a title that refers to rsync - it should bring responses from individuals with rsync experience

Regards


anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Sun Mar 16, 2014 6:34 pm

anita2r wrote:
patalhetas wrote:I have a small (big) problem
this script work 150%, but after I need send the log to a server ...and my problem´s start
I use the rync in remote machine, I try in local machino to, but the result it´s the same, I lost information :(

" rsync -avuz --rsh="ssh -l root" [email protected]:/home/arduino/ /var/www/arduino "
Do you always lose the same amount of data from the last line of the file
or do you lose different amounts of data each time rsync runs?

I use rsync, but I don't have any in-depth experience.

You might consider posting this question in a different forum with a title that refers to rsync - it should bring responses from individuals with rsync experience

Regards


anita2R
Hello,
I lost differnt amount at eath run !!
but I do that, I try in another forum !!!

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Sun Apr 27, 2014 1:22 pm

hello,
I return with a question, anitaR2, it´s possible modify your script to establish the order as the sensors are read?
In this moment I read 4 sensors ok,but if you add a new one, this new is not the last value, appears in the middle the others :(
the prefect script was fix the order as they are read !!

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Tue Apr 29, 2014 3:03 pm

Hi,

The order the sensors are read depends on the output of the list command 'ls'

Code: Select all

# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
#
# iterate through all the devices
for file in $FILES
The output of 'ls' is put into a variable called 'FILES' and then the devices are read in the order found in that variable.

I can't change the order created by the list command

If you don't mind having to update the script every time you add or change a sensor, just remove the line that starts FILES= and replace it with something like
FILES=( 28-0000018a67e2 28-0000018a58b4 28-0000018a4c23 )
This creates an array
The following code shows how the array works:

Code: Select all

#!/bin/bash
FILES=( 28-0000018a67e2 28-0000018a58b4 28-0000018a4c23 )
for file in ${FILES[@]}
do
echo $file
done
exit 0
This echo’s the list in the order you set it up. Note that sensor ID's are separated by spaces in the list
To use it in your code the line 'for file in $FILES'
needs to be changed to:
for file in ${FILES[@]}
Note the curly braces { } - not ordinary ones ( )
The rest of the loop 'do ... done' should work as before, but using the sensors in the order you placed them.

Regards

anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Tue Apr 29, 2014 10:00 pm

Hello,
Thank you for your replay.
my "problem" not a problem :) is add one more sensor, without do a new configuration in the files I use now.
Because I do that and the new sensor change everting :(

this is your script :
#!/bin/bash
#
######## script to get 18B20 data ########################################
## this script waits for a correct crc check ##
# - you need to apt-get install bc
# - change the directory of the sensor
# a script to read and save temperature readings from all the group 28 1-wire temperature sensors
# and output all readings from each time-point on a single line of a daily file
# setup
# path to temerature log files
#TPATH="/var/1w_files/"
TPATH="/home/arduino/"
# character or characters to use as place marker for missing value (no spaces allowed)
PMARK="00"
# name of error log
ERRLOG="err.log"
# create name for daily log file
DLOG=`date +"aqs_%Y%m%d.txt"`
#
# put timestamp at start of each line of daily log file followed by a space (040 is octal for decimal 32)
echo -ne `date +"%d-%m-%Y %H:%M:%S"`"\040" >> $TPATH$DLOG
#
# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
#
# iterate through all the devices
for file in $FILES
do
# get the 2 lines of the response from the device
GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
GETDATA1=`echo "$GETDATA" | grep crc`
GETDATA2=`echo "$GETDATA" | grep t=`
# get temperature
TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
# test if crc is 'YES' and temperature is not -62 or +85
if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000" ]
then
# crc is 'YES' and temperature is not -62 or +85 - so save result
# save to file named by sensor ID - comment out next line if this is not required
echo `date +"%d-%m-%Y %H:%M:%S "`"$TEMP" >> $TPATH$file
# convert temperature result (divide by 1000) - use 'bc' for floating point math
TEMP=$(echo "scale=2; $TEMP/1000" | bc)
# save to daily file (just append to same line followed by space but no trailing new line character)
echo -ne "$TEMP""\040">> $TPATH$DLOG
else
# there was an error (crc not 'yes' or invalid temperature)
# try again after waiting 1 second
sleep 2
# get the 2 lines of the response from the device again
GETDATA=`cat /sys/bus/w1/devices/w1_bus_master1/$file/w1_slave`
GETDATA1=`echo "$GETDATA" | grep crc`
GETDATA2=`echo "$GETDATA" | grep t=`
# get the temperature from the new response
TEMP=`echo $GETDATA2 | sed -n 's/.*t=//;p'`
if [ `echo $GETDATA1 | sed 's/^.*\(...\)$/\1/'` == "YES" -a $TEMP != "-62" -a $TEMP != "85000" ]
then
# save result if crc is 'YES' and temperature is not -62 or +85 - if not, just miss it and move on
# save to file named by sensor ID - comment out next line if this is not required
#echo `date +"%d-%m-%Y %H:%M:%S "; echo $GETDATA2 | sed -n 's/.*t=//;p'` >> $TPATH$file
# convert temperature result (divide by 1000) - use 'bc' for floating point math
TEMP=$(echo "scale=2; $TEMP/1000" | bc)
# save to daily file (just append to same line followed by space but no trailing new line character)
echo -ne "$TEMP""\040">> $TPATH$DLOG
else
# put a place marker in the daily file to show missing value
echo -ne "$PMARK""\040" >> $TPATH$DLOG
fi
# this is a retry so log the failure - record date/time & device ID
echo `date +"%d-%m-%Y %H:%M:%S"`" . ""$file" >> $TPATH$ERRLOG
fi
done
#
# put a new line character into daily file - ready for next set of data
echo >> $TPATH$DLOG
#
exit 0
where I change the script ?

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: DS18B20 reading 85°C

Wed Apr 30, 2014 1:22 am

Hi,

Just follow the changes I suggested.

Code: Select all

# get all devices in the '28' family
FILES=`ls /sys/bus/w1/devices/w1_bus_master1/ | grep '^28'`
#
# iterate through all the devices
for file in $FILES
This snippet of the script contains the two lines that need to be changed.

The line starting 'FILES=`ls ...
is changed to a line that creates an array of sensor names:
FILES=( 28-0000018a67e2 28-0000018a58b4 28-0000018a4c23 )
obviously the sensor names are the ones you use and in the order you want.

The line 'for file in $FILES'
is changed to:
'for file in ${FILES[@]}'

These changes should do what you want if you don't mind updating your script whenever you add or change a sensor

Regards

anita2R

patalhetas
Posts: 14
Joined: Tue Feb 18, 2014 10:53 pm

Re: DS18B20 reading 85°C

Wed Apr 30, 2014 5:57 pm

@anita2r
Again....! thank you, it´s work 5* :)

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 8 guests