Good evening. I'm developing a web application of python with django, the goal is to turn on and off devices using relays and arduino, display the power consumption and cost generated. I have the python and arduino code ready, but I'm having trouble finding a way to run the code below automatically and independently, without worrying about user interaction and the rest of the program.
def armazenamentoDados():
conexao = serial.Serial('com3', 9600); #cria conex�o com o arduino
con = MySQLdb.connect('localhost','root','12345') #cria a conex�o com o banco
con.select_db('sys') #seleciona o schema
c = con.cursor() #posiciona o cursor
while (1):
if(estadoLam == 1):
lampada = 7 #potencia do disposito em watts
convlam = (((lampada/60))/1000) #conversao da ((potencia do dispositivo / h(em minutos))/ conversao kwh)
c.execute("INSERT INTO energia (dispositivo,kwh) VALUES ('LAM', '%s')"%(convlam)) #salva dados da l�mpada no banco
con.commit() #commit
c.close() #encerra conexao
con.close()
conexao.close()
This code needs to be looped during program execution waiting for the status control variable (LAM status) to receive the information that the device is connected (equal to 1), so it must add in the database every minute of the device connected to its consumption in kwh and the cost and from the moment the device is turned off (other than 1), the loop only waits again to change to status 1, ie you should check every minute the changes in the variable of control.