Erroneous empty line in python script output.

Python and python framework

Moderator: leecollings

Post Reply
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Erroneous empty line in python script output.

Post by erem »

L.S,

Don’t laugh, this is what you get learning python off the web. Some (many) things I do not get….

Can someone explain to me where the extra empty line comes from when I press the android key?
The code looks the same as the iOS key if statement, and it drives me nuts not understanding.

the output

Code: Select all

pi@RPi-Rob-usb:~ $ ./detectABshutter.py 
Checking Tue, 23 Jun 2020 17:43:40
AB Shutter3
Device detected by bluetooth
File /dev/input/event0 detected
Device device /dev/input/event0, name "AB Shutter3", phys "b8:27:eb:52:13:49" detected
Key iOS pressed
Key iOS pressed
Key Android pressed
                             <- why this extra empty line
Key Android pressed
                             <- why this extra empty line
^C
the python code
Spoiler: show

Code: Select all

#!/usr/bin/python3

# First of all, make sure Python has the ability to work with Bluetooth:
# sudo apt-get install python-bluez
# pip3 install evdev

import bluetooth
import time
import evdev
import os

# from evdev import InputDevice, categorize, ecodes

while True:
    # check if the device is switched on
    print("Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
    result = bluetooth.lookup_name('11:22:33:44:5D:6A', timeout=5)
    print(result)
    if (result != None):
        print("Device detected by bluetooth")
        # Wait for /dev/eventx file to be created
        while (os.path.exists('/dev/input/event0') == False):
            print("File /dev/input/event0 NOT detected")  
            time.sleep(1)
        if (os.path.exists('/dev/input/event0')):
            print("File /dev/input/event0 detected")
        else:
            print("File /dev/input/event0 NOT detected")
            
        device = evdev.InputDevice('/dev/input/event0')
        #txt="Device {} detected"
        print("Device {} detected".format(device)) # for debug
        for event in device.read_loop():
            if (event.type == evdev.ecodes.EV_KEY):
                #print(evdev.categorize(event))
                if (evdev.ecodes.KEY[event.code] == evdev.ecodes.KEY[115] and event.value == 1 ):
                    print("Key iOS pressed")
                if (evdev.ecodes.KEY[event.code] == evdev.ecodes.KEY[28] and event.value == 1 ):
                    print("Key Android pressed")
                      
    else:
        print("Device NOT detected")
        time.sleep(2)

any help is greatly appreciated.

Thanks for reading,
Regards,

Rob
Post Reply