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.
baseURL = "https://api.thingspeak.com/update?api_key=(MY KEY)&field1=&field2="
# 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'
def read_temp_raw(): # gets the temps one by one
f = open(device_file, 'r')
lines = f.readlines()
def read_temp(): # checks the temp recieved for errors
lines = read_temp_raw()
while lines.strip()[-3:] != 'YES':
lines = read_temp_raw()
equals_pos = lines.find('t=')
if equals_pos != -1:
temp_string = lines[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)
# 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()
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))
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
(Since the forum seems to be especially unkind to python code)
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.
Apologies, it appears my cut and paste added spurious characters to the end of the URL
Try this link
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()))
Most Users Ever Online: 114
Currently Browsing this Page:
Guest Posters: 1
Administrators: Hans, lee