コード例 #1
0
def main():

    zoom = 15
    tft = m5stack.Display()

    tft.text(10, 10, "> Reading settings.\n")
    with open("/flash/settings.json") as fp:
        settings = json.loads(fp.read())

    tft.text(10, tft.LASTY, "> Connecting to wifi.\n")
    wifi = Wifi(settings["username"], settings["password"])

    tft.text(10, tft.LASTY, "> Scanning networks.\n")
    networks = wifi.scan()

    tft.text(10, tft.LASTY, "> Locating current position.\n")
    geolocation = Geolocation(settings["api_key"], networks)
    coordinates = geolocation.coordinates()

    tft.text(10, tft.LASTY, "> Downloading the map.\n")
    map = Map(coordinates)
    map.save("/flash/map.jpg")

    tft.image(0, 0, "/flash/map.jpg")

    button_a = DigitalInput(Pin(m5stack.BUTTON_A_PIN, Pin.IN),
                            callback=lambda pin: zoom_in_handler(map, tft))

    button_c = DigitalInput(Pin(m5stack.BUTTON_C_PIN, Pin.IN),
                            callback=lambda pin: zoom_out_handler(map, tft))
コード例 #2
0
class wifi(Command):
    
    def __init__(self):
        super().__init__('Handle Wifi connection and settings',
                         {'-init': ['Run initialize sequence'],
                          '-sta, --station': ['<ssid>', '<pass>', 'Disadle AP and try to connect to Wifi network. Uses knowns list if no SSID provided, if no connection estabilished - enables AP. If network has no password, type in only SSID argument'],
                          '-ap, --startAP': ['Disadle STA and activate AP'],
                          '-i, --info': ['Show STA and AP state and IP\'s'],
                          '-s, --scan': ['Show available Wifi networks'],
                          '-cap, --changeAP': ['<ssid>', '<password>', 'Change AP SSID and password'],
                          '-a, --add': ['<ssid>', '<password>', 'Add new Wifi network to knowns list'],
                          '-d, --delete': ['<ssid>', 'Delete Wifi network from knowns list'],
                          '-t, --timeout': ['<timeout>', 'Get or set Wifi connection timeout (10 seconds by default)'],})
    
    def __call__(self, *args):
        if len(args[1]) > 0:
            key = args[1][0]
            file = open('/flash/etc/settings.txt')
            settings = json.loads(file.read())
            file.close()
            if key == '-init':
                self.wifi_handler = Wifi()
            elif key in ['-sta', '--station']:
                if len(args[1]) > 1:
                    password = ''
                    ssid = args[1][1]
                    if len(args[1])> 2:
                        password = args[1][2]
                    self.wifi_handler.connect_given(ssid, password)
                else:
                    self.wifi_handler.connect()
            elif key in ['-ap', '--startAP']:
                self.wifi_handler.startAP()
            elif key in ['-i', '--info']:
                sta_ssid = self.wifi_handler.current_ssid
                sta_ip = self.wifi_handler.sta.ifconfig()[0]
                ap_ssid = self.wifi_handler.ap.config('essid') if self.wifi_handler.ap.active() else 'N/A'
                ap_ip = self.wifi_handler.ap.ifconfig()[0]
                print('STA:', sta_ssid, makeTab(sta_ssid, 15),
                      sta_ip + makeTab(sta_ip, 15),
                      green('Connected') if self.wifi_handler.sta.isconnected() else red('Not connected'))
                print('AP: ', ap_ssid, makeTab(ap_ssid, 15),
                      ap_ip + makeTab(ap_ip, 15),
                      green('Active') if self.wifi_handler.ap.active() else red('Inactive'))
            elif key in ['-s', '--scan']:
                self.wifi_handler.scan()
                print(*[i[0] + makeTab(i[0], 20) + str(i[1]) + ' dBm \t' + i[2] for i in self.wifi_handler.scanned], sep ='\n')
            elif key in ['-cap', '--changeAP'] and len(args[1]) > 1:
                settings['network']['ap'][0] = args[1][1]
                if len(args[1]) > 2:
                    settings['network']['ap'][1] = args[1][2]
                else:
                    settings['network']['ap'][1] = ''
            elif key in ['-a', '--add'] and len(args[1]) > 1:
                if len(args[1]) > 2:
                    settings['network']['wifi'][args[1][1]] = args[1][2]
                else:
                    settings['network']['wifi'][args[1][1]] = ''
            elif key in ['-d', '--delete'] and len(args[1]) > 1:
                res = settings['network']['wifi'].pop(args[1][1], -1)
                if res == -1:
                    print(red('No such SSID found'))
            elif key in ['-t', '--timeout']:
                if len(args[1]) > 1 and args[1][1].isdigit():
                    timeout = int(args[1][1])
                    if timeout != 0:
                        settings['network']['wifiConnectionTimeout'] = timeout
                print('Timeout:', settings['network']['wifiConnectionTimeout'])
            else:
                print(red('No valid key provided or arguments are missing'))
            file = open('/flash/etc/settings.txt', 'w')
            file.write(json.dumps(settings))
            file.close()