Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8609

MicroPython • Re: pico w - aioble - httpd

$
0
0
Ok, after a whole lot of dead ends, I've got something that is ugly as heck, isn't quite perfect, but I think I can make it work.

It didn't get the names of the devices, only the MAC's and RSSI, but I think I can make it work.

If anyone has any suggestions for improvements, let me know.

Code:

import selectimport bluetoothimport binasciiimport timeimport socketimport networkimport binasciifrom time import sleepfrom picozero import pico_ledimport machineimport rp2import sysssid = 'XXXXXXX'password = 'ZZZZZZZZZZZ'def connect():    #Connect to WLAN    wlan = network.WLAN(network.STA_IF)    wlan.active(True)    print('Connecting...')    wlan.connect(ssid, password)    while wlan.isconnected() == False:        print('Waiting for connection...')        pico_led.on()        sleep(0.5)        pico_led.off()        sleep(0.5)                    pico_led.on()    ip = wlan.ifconfig()[0]    print(f'Connected on {ip}')    return ipip = connect()server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)server_socket.bind(('', 80))server_socket.listen(5)read_list = [server_socket]while True:            _IRQ_SCAN_RESULT = const(5)    devices = {}    def bt_irq(event, data):        if event == _IRQ_SCAN_RESULT:                    addr_type, addr, connectable, rssi, adv_data = data            address = binascii.hexlify(addr).decode()            addrmac = binascii.hexlify(addr, ':').decode()            devices[addrmac] = rssi            ble = bluetooth.BLE()     ble.active('active')    ble.irq(bt_irq)    ble.gap_scan(1000,100)    time.sleep(1)    print(len(devices))        readable, writable, errored = select.select(read_list, [], [], 1)    for s in readable:        if s is server_socket:            client_socket, address = server_socket.accept()            read_list.append(client_socket)            print ('Connection from', address)        else:            data = s.recv(1024)                    s.send(str(devices))            s.send('\r\n')             s.close()            read_list.remove(s)                    

Statistics: Posted by megadave — Tue Feb 11, 2025 11:17 pm



Viewing all articles
Browse latest Browse all 8609

Trending Articles