Multiple DS18B20 Raspberry Pi

Hello
Does anyone know why this is?
So I have 8 DS18B20 sensors and use a Raspberry Pi which uses the python programming language, and here I have connected it to Ubidots, but it only reads 1 sensor, in which part of my coding is wrong?
Sorry in advance, I’m not very familiar with coding, please help

import os
import time
import requests

os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)

device_files = []
device_files.append(’/sys/bus/w1/devices/28-000000147e7d/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-000000150f73/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-3c01f0962836/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-00000014ebab/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-00000016eceb/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-3c01f09664ab/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-00000014f22c/w1_slave’)
device_files.append(’/sys/bus/w1/devices/28-3c01f0960e4c/w1_slave’)

def temp_raw(device_file):
f = open(device_file, ‘r’)
lines = f.readlines()
f.close()
return lines

def read_temp(device_file):
lines = temp_raw(device_file)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw(device_file)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘Temperatur’: temp_c}
return payload

#Create a list to store our results. List will be the same length as the number of devices we have
temperature_results = [0]*len(device_files)

while True:
for device_num, device_file in enumerate(device_files):
temperature_results[device_num] = read_temp(device_file)
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/raspberry/?token=tokencencoredhehe’, data=read_temp(device_file))
print(‘Posting temperatures in Ubidots’)
print(“1. T.Lingkungan =”,temperature_results[0], “C”)
print(“2. T.Keluar Evaporator =”,temperature_results[1], “C”)
print(“3. T.Suction Kompresor =”,temperature_results[2], “C”)
print(“4. T.Discharge Kompresor =”,temperature_results[3], “C”)
print(“5. T.Masuk Kondensor =”,temperature_results[4], “C”)
print(“6. T.Keluar Kondensor =”,temperature_results[5], “C”)
print(“7. T.Masuk Ekspansi =”,temperature_results[6], “C”)
print(“8. T.Keluar Ekspansi/Masuk Evaporator =”,temperature_results[7], “C”)
time.sleep(3)

Greetings @Kamalzkim, hope you are doing great!

It seems to me that you are sending all of the data to the same device and to the same variable, and so, the data is overwriting.

Id’ suggest you to send each device’s data to a single device on Ubidots, in that way, you’d need 8 devices on Ubidots.
An alternative could be to send all the data to the same device, but, changing the variable name to something like “temperature1”, “temperature2”,…,“temperature8” in order to avoid overwriting the variable each time.

Feel free to reach back to us.
Best,
-Juan David-

Hello, thank you very much for the answer :smiley:
I’ve tried to fix this and the results are better than before, but not perfect

The problem is like this, on the lxterminal on the raspberry pi it already shows all measurement points (totaling 8), but when checked on ubidots it reads only 3 measurement points


Coding:
import os
import time
import requests

os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)

temp_sensor1 = ‘/sys/bus/w1/devices/28-000000147e7d/w1_slave’
temp_sensor2 = ‘/sys/bus/w1/devices/28-000000150f73/w1_slave’
temp_sensor3 = ‘/sys/bus/w1/devices/28-3c01f0962836/w1_slave’
temp_sensor4 = ‘/sys/bus/w1/devices/28-00000014ebab/w1_slave’
temp_sensor5 = ‘/sys/bus/w1/devices/28-00000016eceb/w1_slave’
temp_sensor6 = ‘/sys/bus/w1/devices/28-3c01f09664ab/w1_slave’
temp_sensor7 = ‘/sys/bus/w1/devices/28-00000014f22c/w1_slave’
temp_sensor8 = ‘/sys/bus/w1/devices/28-3c01f0960e4c/w1_slave’

def temp_raw1(temp_sensor1):
f = open(temp_sensor1, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw2(temp_sensor2):
f = open(temp_sensor2, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw3(temp_sensor3):
f = open(temp_sensor3, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw4(temp_sensor4):
f = open(temp_sensor4, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw5(temp_sensor5):
f = open(temp_sensor5, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw6(temp_sensor6):
f = open(temp_sensor6, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw7(temp_sensor7):
f = open(temp_sensor7, ‘r’)
lines = f.readlines()
f.close()
return lines

def temp_raw8(temp_sensor8):
f = open(temp_sensor8, ‘r’)
lines = f.readlines()
f.close()
return lines

def read_temp1(temp_sensor1):
lines = temp_raw1(temp_sensor1)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw1(temp_sensor1)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘Kabin’: temp_c}
return payload

def read_temp2(temp_sensor2):
lines = temp_raw2(temp_sensor2)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw2(temp_sensor2)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘Out Evap’: temp_c}
return payload

def read_temp3(temp_sensor3):
lines = temp_raw3(temp_sensor3)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw3(temp_sensor3)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘Suction’: temp_c}
return payload

def read_temp4(temp_sensor4):
lines = temp_raw4(temp_sensor4)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw4(temp_sensor4)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘Discharge’: temp_c}
return payload

def read_temp5(temp_sensor5):
lines = temp_raw5(temp_sensor5)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw5(temp_sensor5)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘In Kondensor’: temp_c}
return payload

def read_temp6(temp_sensor6):
lines = temp_raw6(temp_sensor6)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw6(temp_sensor6)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘Out Kondensor’: temp_c}
return payload

def read_temp7(temp_sensor7):
lines = temp_raw7(temp_sensor7)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw7(temp_sensor7)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘In Ekspansi’: temp_c}
return payload

def read_temp8(temp_sensor8):
lines = temp_raw8(temp_sensor8)
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = temp_raw8(temp_sensor8)
temp_output = lines[1].find(‘t=’)
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
payload = {‘In Evap’: temp_c}
return payload

while True:
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/Kabin/?token=TOKEN’, data=read_temp1(temp_sensor1))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/OutEvap/?token=TOKEN’, data=read_temp2(temp_sensor2))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/Suction/?token=TOKEN’, data=read_temp3(temp_sensor3))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/Discharge/?token=TOKEN’, data=read_temp4(temp_sensor4))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/InKondensor/?token=TOKEN’, data=read_temp5(temp_sensor5))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/OutKondensor/?token=TOKEN’, data=read_temp6(temp_sensor6))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/InEkspansi/?token=TOKEN’, data=read_temp7(temp_sensor7))
r = requests.post(‘http://things.ubidots.com/api/v1.6/devices/InEvap/?token=TOKEN’, data=read_temp8(temp_sensor8))
print(‘Posting temperatures in Ubidots’)
print(read_temp1(temp_sensor1))
print(read_temp2(temp_sensor2))
print(read_temp3(temp_sensor3))
print(read_temp4(temp_sensor4))
print(read_temp5(temp_sensor5))
print(read_temp6(temp_sensor6))
print(read_temp7(temp_sensor7))
print(read_temp8(temp_sensor8))
time.sleep(3)

So, is there still something wrong with my coding results?

Thank you :slight_smile:

I 'm working on similar project, in your case you have used multiple devices, I just wanted to be multiple variables. Can you help to do it ?

Hi, Currently working on a similar project, I got stuck in between since my sensor are no giving temp values

Here’s the code for the same kindly help me figure whats the problem

import os

import time

import requests

os.system(‘modprobe w1-gpio’)

os.system(‘modprobe w1-therm’)

overall_payload = {}

temp_sensor1 = ‘/sys/bus/w1/devices/28-3cb1f6494e99/w1_slave’

temp_sensor2 = ‘/sys/bus/w1/devices/28-3c8af6493df8/w1_slave’

temp_sensor3 = ‘/sys/bus/w1/devices/28-3c01f0968a61/w1_slave’

temp_sensor4 = ‘/sys/bus/w1/devices/28-3c01f09691a1/w1_slave’

def temp_raw():

f = open(temp_sensor1, 'r')

lines = f.readlines()

f.close()

return lines

def read_temp():

lines = temp_raw()

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

    time.sleep(0.2)

    lines = temp_raw()

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

if temp_output != -1:

    temp_string = lines[1].strip()[temp_output+2:]

    temp_c = float(temp_string) / 1000.0

    #temp_f = temp_c * 9.0 / 5.0 + 32.0

    payload1 = temp_c

    return payload1

def temp_raw1():

f = open(temp_sensor2, 'r')

lines = f.readlines()

f.close()

return lines

def read_temp1():

lines = temp_raw1()

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

    time.sleep(0.2)

    lines = temp_raw1()

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

if temp_output != -1:

    temp_string = lines[1].strip()[temp_output+2:]

    temp_c = float(temp_string) / 1000.0

    #temp_f = temp_c * 9.0 / 5.0 + 32.0

    payload2 = temp_c

    return payload2

def temp_raw2():

f = open(temp_sensor3, 'r')

lines = f.readlines()

f.close()

return lines

def read_temp2():

lines = temp_raw2()

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

    time.sleep(0.2)

    lines = temp_raw2()

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

if temp_output != -1:

    temp_string = lines[1].strip()[temp_output+2:]

    temp_c = float(temp_string) / 1000.0

    #temp_f = temp_c * 9.0 / 5.0 + 32.0

    payload2 = temp_c

    return payload2

def temp_raw3():

f = open(temp_sensor4, 'r')

lines = f.readlines()

f.close()

return lines

def read_temp3():

lines = temp_raw3()

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

    time.sleep(0.2)

    lines = temp_raw3()

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

if temp_output != -1:

    temp_string = lines[1].strip()[temp_output+2:]

    temp_c = float(temp_string) / 1000.0

    #temp_f = temp_c * 9.0 / 5.0 + 32.0

    payload4 = temp_c

    return payload4

while True:

    try:

        overall_payload = {'Temp1':read_temp, 'Temp2':read_temp1, 'Temp3': read_temp2, 'Temp4': read_temp3}

        r = requests.post('http://industrial.api.ubidots.com/api/v1.6/devices/raspberry/?token=', data=overall_payload)

        print('Posting temperatures in Ubidots')

        print(overall_payload)

    except:

        pass          

    time.sleep(10)

These are the values getting displayed

{‘Temp1’: <function read_temp at 0xb51f8c48>, ‘Temp2’: <function read_temp1 at 0xb50d4540>, ‘Temp3’: <function read_temp2 at 0xb50d45d0>, ‘Temp4’: <function read_temp3 at 0xb50d4660>}