Get current Wifi object

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
alanb
Posts: 6
Joined: Sat Oct 17, 2015 3:39 pm

Re: Get current Wifi object

Post by alanb » Sat Oct 24, 2015 2:07 am

This is working well for me on 1.1.0. My boot.py is now happy and working well over telnet even when I want to Ctl-D to reset the board. Here it is, for people's reference:

Code: Select all

import machine
import os
import time
from network import WLAN

BAUD = 2457600 # fast when directly connected on NooElec PL2303
SSID = 'myssid'
AUTH = (WLAN.WPA2, 'mypass')
# w/fixed IP settings (ip, netmask, gateway, dns)
IFCFG = ('192.168.1.123','255.255.255.0','192.168.1.1','8.8.8.8')

def enable_uart(baudrate=BAUD):
    # Enable the USB-to-serial port UART
    uart = machine.UART(0, baudrate=baudrate)
    # duplicate the terminal on the UART
    os.dupterm(uart)
    print('UART initialized @ {0} baud.'.format(baudrate))
    return uart
    
def connect_wifi():
    # Connect to WiFi with a fixed IP address
    wifi = WLAN()
    if machine.reset_cause() != machine.SOFT_RESET:
        print('Initializing WLAN in station mode @ {0} ... '.format(IFCFG), end='')
        wifi.mode(WLAN.STA)
        print(' done.')
        wifi.ifconfig(config=IFCFG)

    if wifi.isconnected():
        print('Already connected to WiFi network.')
    else:
        print('Connecting to WiFi network ...', end='')
        wifi.connect(ssid=SSID, auth=AUTH)
        while not wifi.isconnected():
            time.sleep_ms(200)
            print('.', end='')
        print(' connected.')

    ip, mask, gateway, dns = wifi.ifconfig()
    print('IP address: ', ip)
    print('Netmask:    ', mask)
    print('Gateway:    ', gateway)
    print('DNS:        ', dns)
    mac = ':'.join([('%x'%b).upper() for b in wifi.mac()]);
    print('MAC addr:   ', mac)

    return wifi

if __name__=='__main__':
    uart = enable_uart()
    wifi = connect_wifi()
    print('Hello there from boot.py')
    print('WiPy release {}'.format(os.uname().release))

User avatar
bmarkus
Posts: 111
Joined: Tue Oct 21, 2014 5:58 am

Re: Get current Wifi object

Post by bmarkus » Sat Oct 24, 2015 5:08 pm

J. J útq
Tiny Core Linux (piCore) developer
HAM radio call: HA5DI (Béla)

Post Reply