Multiple DS18B20 on Raspberry Pi | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Multiple DS18B20 on Raspberry Pi
Avatar
steveknott
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
1
January 25, 2018 - 10:43 am
sp_Permalink sp_Print

I have at present 2 (soon to be 3) ds18b20 probes connected to a Pi zero and want to send this data to ThingSpeak using python, I have setup the 2 fields and it will report data to both but both sets of data are the same. How can I get both probes to report the correct information.

Both of the data values report correctly in the terminal on my pi when I run the command.

 

.py below

Many thanks

 

import os

 

import glob

 

import time

 

import sys

 

import datetime

 

import urllib2

 

 

 

baseURL = "https://api.thingspeak.com/update?api_key=(MY KEY)&field1=&field2="

 

 

 

os.system('modprobe w1-gpio')

os.system('modprobe w1-therm')

 

# add more sensor variables here based on your setup

temp_0 = 0

temp_1 = 0

 

for sensors in range (2): # change number of sensors based on your setup

        base_dir = '/sys/bus/w1/devices/'

        device_folder = glob.glob(base_dir + '28*')[sensors]

        device_file = device_folder + '/w1_slave'

        print device_file

        print sensors

 

        def read_temp_raw(): # gets the temps one by one

                f = open(device_file, 'r')

                lines = f.readlines()

                f.close()

                return lines

 

        def read_temp(): # checks the temp recieved for errors

                lines = read_temp_raw()

                while lines[0].strip()[-3:] != 'YES':

                        time.sleep(0.2)

                        lines = read_temp_raw()

                equals_pos = lines[1].find('t=')

                if equals_pos != -1:

                        temp_string = lines[1][equals_pos+2:]

                        # set proper decimal place for C

                        temp = float(temp_string) / 1000.0

                        # Round temp to 2 decimal points

                        temp = round(temp, 1)

                        return temp

 

# ask for temps to be read and store each in a variable

        if sensors == 0:

                temp_0 = read_temp()

               

        if sensors == 1:

                temp_1 = read_temp()

 

 

 

                print temp_0

                print temp_1

 

while True: #infinite loop

 

     temp_0 = read_temp() #get the temp

 

     values = [datetime.datetime.now(), temp_0]

 

     temp_1 = read_temp() #get the temp

 

     values = [datetime.datetime.now(), temp_1]

 

     g = urllib2.urlopen(baseURL + "&field1=%s&field2=%s" % (temp_0, temp_1))

 

     time.sleep(60)

 

Avatar
rw950431

Forum Posts: 257
sp_UserOfflineSmall Offline
2
January 25, 2018 - 10:57 pm
sp_Permalink sp_Print

Maybe posting to the forum messed up the indenting but that code doesnt look right to me.   The way its come out it looks like you are defining functions read_temp_raw() and read_temp() within the "for sensors " loop.  Function definitions are usually done once at the top of the program.  When you start on the infinite loop the value of the variable "sensors" will be constant so you read the same temp repeatedly.

 

There are existing python modules (https://github.com/timofurrer/w1thermsensor looks like the most popular) to do the hard work of actually reading the sensor but if you want to keep rolling your own I've posted some suggestions for your code at 

https://gist.github.com/rw950431/2b1a632f49e89cb0c3ac6b77433f13fa   

 

(Since the forum seems to be especially unkind to python code)

Avatar
cstapels
Moderator
Forum Posts: 158
sp_UserOfflineSmall Offline
3
January 26, 2018 - 4:42 pm
sp_Permalink sp_Print

You may also want to consider removing "&field1=&field2=" from baseURL at definition time, since the statement at the end seems to add it again. 

g = urllib2.urlopen(baseURL + "&field1=%s&field2=%s" % (temp_0, temp_1))

Avatar
steveknott
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
4
January 27, 2018 - 8:32 am
sp_Permalink sp_Print

Hi,

Thank you both for taking the time to reply.

This is my first project with python and I don’t understand which part of the timofurrer code I need, my sensors are working locally and also I have them working with Domoticz so I only need to send this data to thingspeak I have read through all the code by timofurrer but can’t see anything about thingspeak which I could use or modify as I’m new to all of this.

also the link https://gist.github.com/rw950431/2b1a632f49e89cb0c3ac6b77433f13fa  does not work.

Avatar
rw950431

Forum Posts: 257
sp_UserOfflineSmall Offline
5
January 29, 2018 - 12:25 am
sp_Permalink sp_Print

Apologies, it appears my cut and paste added spurious characters to the end of the URL

Try this link

https://gist.github.com/rw950431/2b1a632f49e89cb0c3ac6b77433f13fa

 

In terms of the library you probably want the multiple sensor example: this replaces all the globbing and string parsing you currently do.

from w1thermsensor import W1ThermSensor

for sensor in W1ThermSensor.get_available_sensors():
    print("Sensor %s has temperature %.2f" % (sensor.id, sensor.get_temperature()))
Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
33 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

ibgedubo

aogomaeqaza

ureomug

itixuwidouzuv

eilimir

memozupi

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee