""" Microservices available at http://www.saltlake71.eu/urpc Author: massimo.sala.71 AT gmail.com """ import network from time import sleep from urequests import post RECORDS_MAX = 30 # fewer Access Points reduce memory consumption, but location will be less precise # from uRPC 0.97, no authentication required #AUTH_TOKEN = "Basic ... insert here your access token ..." def get_access_points(wlan, limit) : n = 0 r = b'' for ap in wlan.scan() : mac = ap[1] rssi = ap[3] # dBm if rssi < 0 and rssi > -150 : # sanity checks r += b"%02d" % n + b"%04d" % rssi + mac # fixed records of 12 bytes n += 1 if n == 100 or n == limit : break return n, r wlan = network.WLAN(network.STA_IF) if not wlan.active() : # mandatory, sometimes the wifi nic is sleeping wlan.active(True) n_ap, pkt = get_access_points(wlan, RECORDS_MAX) print("Found", n_ap, "access points") retries = 10 while retries and not wlan.isconnected() : sleep(1) print('.', end = '') print() if n_ap and wlan.isconnected() : # from uRPC 0.97, no authentication required #resp = post("http://saltlake71.eu/urpc?function=geoloc", data = pkt, headers = {"Authorization": AUTH_TOKEN}) resp = post("http://saltlake71.eu/urpc?function=geoloc", data = pkt) if resp.status_code == 200 : print("Location: accuracy lat long =", resp.text) else : print("Error", resp.text)