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

MicroPython • Re: pico w - aioble - httpd

$
0
0
Well, I guess no one is interested, but in the event someday someone else finds this, I thought I'd share the much improved version that I'll be deploying .

Code:

import selectimport micropythonimport bluetoothimport binasciiimport timeimport socketimport networkimport binasciifrom time import sleepfrom picozero import pico_ledimport machineimport rp2import sys # Use your own :)ssid = 'XXXXX'password = 'XXXXX'devices = {}started=time.time() 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]    _IRQ_SCAN_RESULT = const(5)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, time.time() - started ]# Set up BT scanning, scan forever in background        ble = bluetooth.BLE()ble.active('active')ble.irq(bt_irq)ble.gap_scan(0,100)time.sleep(1)loopcount = 0# Blink LED as a heartbeatwhile True:    if (loopcount == 0):        loopcount = 1        pico_led.on()    else:        loopcount = 0        pico_led.off()        print(time.time() - started, len(devices))    listout = []        # Iterate through all the stored devices detected    for mac in devices:        entry = devices[mac]        age = ( time.time() - started ) - entry[1]        rssi = entry[0]                if ( age > 720 ):            # Remove old entries            devices.pop(mac)        else:            listout.append([age, mac, rssi])        # non-blocking check for HTTP connection    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:            request_d = s.recv(1024)                        # flicker the LED to indicate HTTP access            pico_led.on()            sleep(0.1)            pico_led.off()            sleep(0.1)                        # Read the request. Doesn't do anything with it for now, all requests get the same response.            request_s = str(request_d)            try:                request = request_s.split()[1]            except IndexError:                pass                        print(request)                        # HTTP headers            s.send('HTTP/1.0 200 OK\r\n')            s.send('Connection: close\r\n')            s.send('Content-Type: text/plain\r\n')            s.send('\r\n')                        # Some info            s.send('\r\n')            s.send('UPTIME=')            s.send(str(time.time() - started))            s.send('\r\nIP=')            s.send(str(ip))            s.send('\r\n')                        # flicker the LED to indicate HTTP access            pico_led.on()            sleep(0.1)            pico_led.off()            sleep(0.1)                        # dump out the list of detected BT devices sorted by age.            for disp in sorted(listout):                 s.send('MAC=')                 s.send(str(disp[1]))                 s.send(', ')                 s.send(str(disp[2])) #RSSID                 s.send(', ')                 s.send(str(disp[0])) #AGE                 s.send('\r\n')                                s.send('\r\n')             s.close()            read_list.remove(s)

Statistics: Posted by megadave — Thu Feb 13, 2025 12:55 am



Viewing all articles
Browse latest Browse all 8041

Trending Articles