Execution of independently executed Python code

Asked

Viewed 111 times

1

good night. I am developing a python web application with Django, the goal is to turn on and off devices using relays and Arduino, display power consumption and generated cost. I have the python and Arduino code ready, but I’m having a hard time 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 loop while running the program waiting for the status control variable (Lam state) to receive the information that the device is on (equal to 1)then it should add in the bank every minute of the connected device its consumption in kwh and the cost and from the time the device is off (other than 1), the loop just waits again for the change to status 1, that is, you should check every minute the changes in the control variable.

  • 1

    Man... Except as a learning exercise, or something very specific, I would not reinvent the wheel, There is a great open source project (in Jango) for this. You can at least draw inspiration from it. See here.

  • 1

    Yeah, it’s a TCC :/

  • 1

    but still I’m sure you’ll find the answer in the [project code] (http://github.com/home-assistant/home-assistant.github.io).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.