1

My RBPi zero has a spotty reception close to the door where it keeps monitoring registered phones on bluetooth and trigger the door opener. What are the options to make it work without/irregular wifi connection? What part of code keeps it from running?

There is no way to add a wifi repeater between the Pi and the router (no outlets). So either I improve wifi range on one of the two sides or change the code to make it internet-independent.

here is the code:

import automationhat
import bluetooth
import time
import sys
import telegram

bot = telegram.Bot(token='xx')


# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient


# Set to your Adafruit IO key & username below.
ADAFRUIT_IO_KEY      = 'xx'
ADAFRUIT_IO_USERNAME = 'xx'

# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.

    print('Connected to Adafruit IO!  Listening for LockReg changes...')
    # Subscribe to changes on a feed named LockReg.
    client.subscribe('LockReg')

def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print('Disconnected from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload, retain):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print('Feed {0} received new value: {1}'.format(feed_id, payload))

# Create an MQTT client instance.
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Setup the callback functions defined above.
client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message



PHONES = ['xxxxxxxxxxxx']


while True:

    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())

    for device in PHONES:
            result = bluetooth.lookup_name(device, timeout=5)
            if (result != None):
                    # Connect to the Adafruit IO server.
                    client.connect()
                    print('Door unlocked. Nearby: %s (%s)' % (result, device))
                    client.publish('lock.reg', result)
                    automationhat.relay.one.on()
                    time.sleep(1)
                    automationhat.relay.one.off()
                    bot.sendMessage(-269345557, result)
                    time.sleep(300)
            else:
                    print "User out of range"
                    print('Door locked: No Bluetooth device detected. ' )
    time.sleep(6)
12
  • why did you post the code? ... do you get errors?
    – jsotola
    Commented Nov 23, 2019 at 7:02
  • Use a Wi-Fi repeater to boost your signal.
    – CoderMike
    Commented Nov 23, 2019 at 8:21
  • @jsotola No errors. To make it clear whats going on.
    – Paolo
    Commented Nov 23, 2019 at 16:51
  • @CoderMike No power in the staircase, hence not an option. Would be nice to change the code since the priority is opening the door, the rest is a nice to have.
    – Paolo
    Commented Nov 23, 2019 at 16:53
  • 1
    Add all these important details to your question.
    – CoderMike
    Commented Nov 23, 2019 at 18:53

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.