burim:python:temp-sesnor
Table of Contents
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
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:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
- for just one senssor
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
#device_file = device_folder + '/w1_slave'
device_file = '/sys/bus/w1/devices/28-02079177659c/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
# f = open(/sys/bus/w1/devices/28-02079177659c/w1_slave, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
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:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
LCD temperatrue
#!/usr/bin/python
import os
import glob
import time
import lcddriver
import subprocess, platform
lcd = lcddriver.lcd()
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
#device_file = device_folder + '/w1_slave'
device_file = '/sys/bus/w1/devices/28-020e91771447/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
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:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c
def read_temp_zlla():
temp_zlla_c_temp = subprocess.Popen('ssh -i /home/pi/.ssh/id_rsa pi@10.11.12.110 "python3 /home/pi/scripts/temp-sensor1.zlla.py"', shell=True, stdout=subprocess.PIPE)
temp_zlla_c = float(temp_zlla_c_temp.stdout.read())
return temp_zlla_c
while True:
lcd.lcd_clear()
lcd.lcd_display_string("Temp1: " + str(read_temp()),1)
lcd.lcd_display_string("Temp2: " + str(read_temp_zlla()),2)
time.sleep(30)
LCD temperature with connection check
#!/usr/bin/python
import os
import glob
import time
import lcddriver
import subprocess, platform
lcd = lcddriver.lcd()
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
dest_check = 'ssh -i /home/pi/.ssh/id_rsa -o ConnectTimeout=1 -o ConnectionAttempts=1 -o StrictHostKeyChecking=no pi@10.11.12.110 exit; echo $?'
dest_temp = 'ssh -i /home/pi/.ssh/id_rsa pi@10.11.12.110 "python3 /home/pi/scripts/temp-sensor1.zlla.py"'
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
#device_file = device_folder + '/w1_slave'
device_file = '/sys/bus/w1/devices/28-020e91771447/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
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:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c
def read_temp_zlla():
conn_check_temp = subprocess.Popen(dest_check, shell=True, stdout=subprocess.PIPE)
conn_check = float(conn_check_temp.stdout.read())
if conn_check == 0:
temp_zlla_c_temp = subprocess.Popen(dest_temp, shell=True, stdout=subprocess.PIPE)
temp_zlla_c = float(temp_zlla_c_temp.stdout.read())
return temp_zlla_c
else:
return "DOWN"
while True:
print(read_temp_zlla())
lcd.lcd_clear()
lcd.lcd_display_string("Temp1: " + str(read_temp()),1)
lcd.lcd_display_string("Temp2: " + str(read_temp_zlla()),2)
time.sleep(30)
lcd temperature cron scheduler + logger
#!/usr/bin/python
import os
import glob
import time
import lcddriver
import subprocess, platform
lcd = lcddriver.lcd()
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
dest_check = 'ssh -i /home/pi/.ssh/id_rsa -o ConnectTimeout=1 -o ConnectionAttempts=2 -o StrictHostKeyChecking=no pi@10.11.12.110 exit; echo $?'
dest_temp = 'ssh -i /home/pi/.ssh/id_rsa pi@10.11.12.110 "python3 /home/pi/scripts/temp-sensor1.zlla.py"'
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
#device_file = device_folder + '/w1_slave'
device_file = '/sys/bus/w1/devices/28-020e91771447/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
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:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c
def read_temp_zlla():
conn_check_temp = subprocess.Popen(dest_check, shell=True, stdout=subprocess.PIPE)
conn_check = float(conn_check_temp.stdout.read())
if conn_check == 0:
temp_zlla_c_temp = subprocess.Popen(dest_temp, shell=True, stdout=subprocess.PIPE)
temp_zlla_c = float(temp_zlla_c_temp.stdout.read())
return temp_zlla_c
else:
return "DOWN"
lcd.lcd_clear()
temp1 = "Temp1: " + str(read_temp())
lcd.lcd_display_string(str(temp1),1)
temp2 = "Temp2: " + str(read_temp_zlla())
lcd.lcd_display_string(str(temp2),2)
loggertemp1 = "/usr/bin/logger temp executed - " + temp1
loggertemp2 = "/usr/bin/logger temp executed - " + temp2
#print(temp1)
#print(temp2)
subprocess.Popen(loggertemp1, shell=True, stdout=subprocess.PIPE)
subprocess.Popen(loggertemp2, shell=True, stdout=subprocess.PIPE)
burim/python/temp-sesnor.txt · Last modified: 2019/07/07 18:10 by admin
