0

I wrote a code in Python to change Bluetooth Low Energy information. If I write for example python beacon.py 12 12 major of BLE advertised data will be 12 and minor will be 12. When I run it using terminal is fine however in the crontab -e doesn't work. I tried running another python program (file creation) and it ran. So I am confusing with the problem anybody has any ideas?

import os
import sys

value1 = int(sys.argv[1])
value2 = int(sys.argv[2])

if (( 0 <= value1 <= 65535)and(0 <= value2 <= 65535)):
    def int_to_hex(n):
        str = "0x{:04x}".format(n)
        str = str[-4:]
        [str[i:i+2] for i in range(0, len(str), 2)]
        str = ' '.join([str[i:i+2] for i in range(0, len(str), 2)])
        str = str + ' '
        return str

    Major = int_to_hex(value1)
    Minor = int_to_hex(value2)

    print 'Major (in hex): ' + Major
    print 'Minor (in hex):' + Minor

    sudoPassword = 'raspberry'

    command = 'sudo hciconfig hci0 up'
    p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

    command = 'sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 75 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 ' + Major + Minor +'C8 00'
    p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

    command = 'sudo hciconfig hci0 leadv 3' 
    p = os.system('echo %s|sudo -S %s' % (sudoPassword, command))

    #apple: 76 (d) ->  00 4c -> 4c 00 
    #samsung: 117 (d) -> 00 75 -> 75 00

    #prefix (9 bytes)
    #uuid 16 bytes: (e20...)
    #major 2 bytes
    #minor 2 bytes
    #tx power 1 bytes   

    print 'Success!'

 else:
     print 'Please specify major and minor values to be between 0 and 65535'

Error:

May 16 08:25:59 raspberrypi CRON[375]: (pi) CMD (sudo python /home/pi/Desktop/BLE/beacon.py 7 7 2>&1 | /usr/bin/logger -t beacon)
May 16 08:25:59 raspberrypi sudo[380]:       pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/usr/bin/python /home/pi/Desktop/BLE/beacon.py 7 7
May 16 08:26:00 raspberrypi beacon[381]: Can't get device info: No such device
May 16 08:26:00 raspberrypi beacon[381]: Invalid device: No such device
May 16 08:26:00 raspberrypi beacon[381]: Can't get device info: No such device
May 16 08:26:00 raspberrypi beacon[381]: Major (in hex): 00 07
May 16 08:26:00 raspberrypi beacon[381]: Minor (in hex):00 07
May 16 08:26:00 raspberrypi beacon[381]: Success!
4
  • Review cron job output for errors. You can do it by adding ` 2>&1 | /usr/bin/logger -t beacon` at the end of your crontab line to output job to syslog. After reboot you can execute journalctl | grep beacon to view log output with given tag, so you can see what gone wrong. Please add this output to your post. May 16, 2018 at 8:08
  • It sounds like a path or a permissions issue. Just a couple of ideas you may want to try: 1.) put a statement in your .py file to log the $PATH env var, 2.) read this post re runtime permissions for cron jobs.
    – Seamus
    May 16, 2018 at 10:57
  • Oh, one other thing: you do understand that crontab -e is just the tool that manages your "table", right? the job/program that is scheduled in the "table" is run by a process named cron. just to be clear...
    – Seamus
    May 16, 2018 at 11:01
  • Sorry for late answer, I hope you don't give up with this:) I just speculate, but for my it looks like, when you execute script by cronjob, the bluetooth device is unavailable. Maybe the reason is that bluetooth module or device isn't yet initialized, when script starts. So, I would give few moments sleep at the begining or better loop with test for device appearance. Jun 5, 2018 at 8:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.