Temperaturlogger RPi+DS18B20

Hei. Jeg jeg har lenge jobbet med en temperaturlogger til gjæringsskapet mitt. Den skal ha to temp.prober (en til vørteren og en til skapet) og vise temeraturene online via tingspeak.com. Nå har jeg koblet opp alt. To DS18B20 prober på en GPIOpin. Jeg har fått Raspberry Pien til å lese begge probene. Problemet er at jeg bare får frem en teperatur på thingspeak. Vedlag ligger pythonskripte jeg bruker. Det er laget for en temp.avleser, men jeg skulle veldig gjerne få til å bruke to. Noen som kan hjelpe meg?

#!/usr/bin/python

import time, httplib, urllib, os, glob, socket, gmaileralert
from datetime import datetime


os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

"""
This script streams data to ThingSpeak:
https://www.thingspeak.com
"""

def readsensor(input):
raw = open(input, "r").read()
temperature = float(raw.split("t=")[-1])/1000
return round(temperature, 1)

def doit(input):
params = urllib.urlencode(input)
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
# Uncomment the print statement to check connection to TS
# print response.status, response.reason
data = response.read()
conn.close()

def log_temperature(input):
with open("log_temperature.txt", "a") as f:
f.write(str(datetime.now()))
for sensor, temperature in sorted(input.items()):
f.write("\t")
f.write(str(temperature))
f.write("\n")

if __name__ == "__main__":
# dictionary for each sensor and device location
w1= { 'field1': '/sys/bus/w1/devices/28-00000521ca90/w1_slave' }

toemail = "From: My <email@gmail.com>\n"
fromemail = "To: You <email@mac.com>\n"
subject = "Subject: RPI Sensor Error \n"
msg1 = "There is a problem with the temperture sensor."
msg2 = "The program quit, SystemExit error."
mymsg = toemail + fromemail + subject

print "starting temperature readings on: " + str(datetime.now())
while True:
try:
dict = { }
for key, value in w1.iteritems():
dict[key] = readsensor(value)

# Log the data to a file
# log_temperature(dict)

# add Things Speak special API key pair
dict['key'] = 'ThingSpeak Key'

doit(dict) # send the data to Things Speak

#sleep for 16 seconds (api limit of 15 secs)
time.sleep(16)

except socket.gaierror, e:
print "%s There was a socket.gaierror: %s" % (e, str(datetime.now()))
gmaileralert.blue_alert(mymsg+msg1)
time.sleep(15)
except socket.error, e:
print "%s There was a socket.error: %s" % (e, str(datetime.now()))
gmaileralert.blue_alert(mymsg+msg1)
time.sleep(15)
except (SystemExit):
gmaileralert.blue_alert(mymsg+msg2)
raise
 
Ikke akkurat forumet for kode, github el lignende hadde vært bedre. Er ikke phython mester men slik jeg ser så logger den begge sensorene til log_temperature.txt. Kan du bekrefte at den inneholder data fra begge sensorene?
 
# dictionary for each sensor and device location
w1= { 'field1': '/sys/bus/w1/devices/28-00000521ca90/w1_slave' }

Burde ikke denne inneholdt begge sensorene og ikke bare den ene?

EDIT: Testet selv og
Kode:
w1= { 'field1': '/sys/bus/w1/devices/28-000005b4e756/w1_slave','field2': '/sys/bus/w1/devices/28-000005be369e/w1_slave'}
virket fint for meg med 2 sensorer.
 
Sist redigert:
# dictionary for each sensor and device location
w1= { 'field1': '/sys/bus/w1/devices/28-00000521ca90/w1_slave' }

Burde ikke denne inneholdt begge sensorene og ikke bare den ene?

EDIT: Testet selv og
Kode:
w1= { 'field1': '/sys/bus/w1/devices/28-000005b4e756/w1_slave','field2': '/sys/bus/w1/devices/28-000005be369e/w1_slave'}
virket fint for meg med 2 sensorer.

1000 takk. Tipset ditt fungerte het perfekt
 
Tilbake
Topp