Hello Everyone,
I made script to record temperature readout into mysql database. If I use simple command:
"Python3 Temperatura.py" everything is ok - script finish and I have record in database, but when I try to use:
"./Temepratura" or "sudo ./Temperatura" script starts and is still working - I have to use Ctrl-c (script doesn't made any record in database. I thin it is problem connected with header of the script - someone help?
Scirpt code is under:
#! /usr/bin/env python3
import os
import glob
import mysql.connector
nazwaKontroleraTemp = "'RaspXBMC'"
connectionsql = mysql.connector.connect(user='raspberry', password='*********',host='10.0.0.80',database='home_rasp')
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def odczytajtemperature_surowy():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def odczytajtemperature():
lines = odczytajtemperature_surowy()
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
return temp_c
kursor = connectionsql.cursor()
temperatura = odczytajtemperature()
insert_temperatura = ("INSERT INTO Temperatura "
"(Termometr, Data, Odczyt)"
" VALUES (%s , now() , %s )")
dane_temperatura = (nazwaKontroleraTemp,temperatura)
kursor.execute(insert_temperatura,dane_temperatura)
kursor.close()
connectionsql.commit()
connectionsql.close()