BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Getting error when trying to run python script in Crontab

Hi,

I am new to Blynk, just spend a couple of evenings trying get it up and running. To be able to see some home stuff data so fancy/fast on your mobile phone, really cool! :partying_face:

I am using a Raspbery Pi 3, connecting to it via SSH. When i run my test script manually it is working fine and this is the output (i also see it on my phone so its working) :

   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ for Python v0.2.0 (Linux)

1 sec elapsed, sending data to the server...
1 sec elapsed, sending data to the server...

However when i try to start that script automatically using crontab (sudo crontab -e), i can see in the log file that i am getting the following error:

   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ for Python v0.2.0 (Linux)

Traceback (most recent call last):
  File "/home/pi/blynk-library-python-master/examples/testnew3.py", line 20, in <module>
    blynk = BlynkLib.Blynk(BLYNK_AUTH)
  File "/usr/local/lib/python2.7/dist-packages/BlynkLib.py", line 224, in __init__
    BlynkProtocol.__init__(self, auth, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/BlynkLib.py", line 59, in __init__
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/BlynkLib.py", line 236, in connect
    raise ValueError('Connection with the Blynk server %s:%d failed' % (self.server, self.port))
ValueError: Connection with the Blynk server blynk-cloud.com:80 failed

This is my crontab code:

@reboot sudo /bin/sleep 5 ; sudo /usr/bin/python /home/pi/blynk-library-python-master/examples/testnew3.py > /home/pi/blynk-library-python-master/examples/log.out 2>&1

And this is my python script itself:

import BlynkLib
import time

BLYNK_AUTH = 'mytoken'

# initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
temp = 15
tmr_start_time = time.time()
#print("while true starting up...")
while True:
    blynk.run()

    t = time.time()
    if t - tmr_start_time > 1:
        temp = temp + 1
        if temp > 25:
                temp = 15
        print("1 sec elapsed, sending data to the server...")
        now = time.ctime(int(time.time()))
#        blynk.virtual_write(0, "time:" + str(t))
        blynk.virtual_write(0, str(now))
        blynk.virtual_write(4, temp)
        tmr_start_time += 1

Anyone can help me out here?

Thx!

I found the problem myself. Apparently the 5s delay after booting was not enough. Changing it to 15 seconds fixed the problem.

@reboot sudo /bin/sleep 15 ; sudo /usr/bin/python /home/pi/blynk-library-python-master/examples/testnew3.py > /home/pi/blynk-library-python-master/examples/log.out 2>&1