burim:python:temp-sensor-things-speak
v2 with cpu measurement
#!/usr/bin/env python3
import sqlite3
import os
import time
import glob
from urllib import request
from re import findall
from time import sleep
from subprocess import check_output
myAPI = 'G8QE22SD7OB49PRT'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
# global variables
speriod=(15*60)-1
# get temerature
# returns None on error, or the temperature as a float
def get_temp(devicefile):
try:
fileobj = open(devicefile,'r')
lines = fileobj.readlines()
fileobj.close()
except:
return None
# get the status from the end of line 1
status = lines[0][-4:-1]
# is the status is ok, get the temperature from line 2
if status=="YES":
print(status)
tempstr= lines[1][-6:-1]
tempvalue=float(tempstr)/1000
# print(tempvalue)
return tempvalue
else:
print("There was an error.")
return None
#get cpu temperatute
def get_temp_cpu():
temp_cpu = check_output(["vcgencmd","measure_temp"]).decode()
temp_cpu = float(findall('\d+\.\d+', temp_cpu)[0])
return(temp_cpu)
# main function
# This is where the program starts
def main():
#log cpu temperature
temp_cpu = get_temp_cpu()
conn = request.urlopen(baseURL + '&field2=%s' % (temp_cpu))
print(str(temp_cpu))
conn.close()
# enable kernel modules
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')
# search for a device file that starts with 28
devicelist = glob.glob('/sys/bus/w1/devices/28*')
if devicelist=='':
return None
else:
# append /w1slave to the device file
w1devicefile = devicelist[0] + '/w1_slave'
# while True:
# get the temperature from the device file
temperature = get_temp(w1devicefile)
if temperature != None:
print("temperature="+str(temperature))
conn = request.urlopen(baseURL + '&field1=%s' % (temperature))
print(str(temperature))
conn.close()
sleep(1)
else:
# Sometimes reads fail on the first attempt
# so we need to retry
temperature = get_temp(w1devicefile)
print("temperature="+str(temperature))
conn = request.urlopen(baseURL + '&field1=%s' % (temperature))
print(str(temperature))
conn.close()
sleep(1)
if __name__=="__main__":
main()
v1 without cpu measurement
#!/usr/bin/env python3
import sqlite3
import os
import time
import glob
from urllib import request
from re import findall
from time import sleep
from subprocess import check_output
myAPI = 'G8QE22SD7OB49PRT'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
# global variables
speriod=(15*60)-1
# get temerature
# returns None on error, or the temperature as a float
def get_temp(devicefile):
try:
fileobj = open(devicefile,'r')
lines = fileobj.readlines()
fileobj.close()
except:
return None
# get the status from the end of line 1
status = lines[0][-4:-1]
# is the status is ok, get the temperature from line 2
if status=="YES":
print(status)
tempstr= lines[1][-6:-1]
tempvalue=float(tempstr)/1000
# print(tempvalue)
return tempvalue
else:
print("There was an error.")
return None
# main function
# This is where the program starts
def main():
# enable kernel modules
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')
# search for a device file that starts with 28
devicelist = glob.glob('/sys/bus/w1/devices/28*')
if devicelist=='':
return None
else:
# append /w1slave to the device file
w1devicefile = devicelist[0] + '/w1_slave'
# while True:
# get the temperature from the device file
temperature = get_temp(w1devicefile)
if temperature != None:
print("temperature="+str(temperature))
conn = request.urlopen(baseURL + '&field1=%s' % (temperature))
print(str(temperature))
conn.close()
sleep(1)
else:
# Sometimes reads fail on the first attempt
# so we need to retry
temperature = get_temp(w1devicefile)
print("temperature="+str(temperature))
conn = request.urlopen(baseURL + '&field1=%s' % (temperature))
print(str(temperature))
conn.close()
sleep(1)
if __name__=="__main__":
main()
burim/python/temp-sensor-things-speak.txt · Last modified: 2020/01/25 09:29 by burim
