1

I write a script python that access to Mysql Database and switch on/off some led.

I want it to start at boot of raspberry but not work. If i delete the mysql access the script (with cron) work. Why?

It is the script:

import RPi.GPIO as gpio
import _mysql

gpio.setmode(gpio.BCM)
gpio.setwarnings(False)

rele_luci=17;
fotoresistenza=4;

gpio.setup(fotoresistenza,gpio.IN) #fotoresistenza
gpio.setup(rele_luci,gpio.OUT) #rele-luci-giardino

connessione = _mysql.connect("localhost","residente","pinkrabbits","domotica")
comando = "SELECT attivo FROM casa WHERE id_stanza = 1"


while 1==1:
        query = connessione.query(comando)
        risultato = connessione.store_result()
        attivo = int(risultato.fetch_row()[0][0])

        if (attivo == 0):

                valore=gpio.input(fotoresistenza)
                if valore == 0:
                        gpio.output(rele_luci,1)
                        comando2 = "UPDATE casa SET luci=1 WHERE id_stanza=1"
                else:
                        gpio.output(rele_luci,0)
                        comando2 = "UPDATE casa SET luci=0 WHERE id_stanza=1"
                query = connessione.query(comando2)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Please post your crontab. – S. Adam Nissley May 07 '15 at 13:12
  • when you say "If i delete the mysql access the script (with cron) work", it means that you remove the SQL connection in this script ? You should first test it before adding it to the CRON (which, btw, is not the best place to be sure that it is launched at start: CRON is for launching job at regular times, not at start time, for which you should use /etc/init.d/ as adivsed http://stackoverflow.com/questions/12973777/how-to-run-a-shell-script-at-startup But first launch your script alone, you'll probably get an error – Pixou May 07 '15 at 16:11

1 Answers1

0

If you run it at startup, it's possible that it attempts to run before the mysqld starts.

Anyway, just wrap the connection to mysql into try .. except block and log whatever error it returns

MacHala
  • 2,159
  • 1
  • 15
  • 18