Lately I proudly got hold of a WiPy2.0 board and this post is about how I got started with it, established a connection to my Wifi and made the WiPy to connect automatically on boot.

The WiPy2 is the successor of the WiPy board and is specified by the manufacturer pycom as an enterprise grade IoT development platform. The main features are listed below, a comprehensive list of all features can be found on the WiPy2.0 product page.

  • Espressif ESP32 chip
  • 1 kilometer WiFi range
  • MicroPython already installed
  • low power usage

Expansionboard

When you acquire a WiPy2 be sure to also supply the Expansionboard. After I received the WiPy2 from ExpTech I ended up with no possibility to communicate to it because the plain board comes without USB connector.

Features of the Expansionboard:

  • USB and LiPo power connection
  • FT234XS usb serial converter
  • Micro SD card slot
  • 1 user LED, 1 user button test

Firmware Upgrade

After a few more days I finally received the missing piece and was able to plug the WiPy in my computer via USB cable. The manufacturer strongly recommends to upgrade the firmware to the latest version. I followed the instructions on the Pycom Quickstart Guide. It is really straight forwared: Download the Upgrade tool, start it and follow the instructions. Nothing special here.

Connecting to the board via serial port

I connected to the board using picocom a minimal dumb-terminal emulation program. On my Mac I installed it with homebrew via $ brew install picocom. On Debian you can use $ apt-get install picocom. To establish a connection to your WiPy2 you need to know which device it is on. This should be something like /dev/tty.usbserial-DQ008T3G. To discover the available device use the following command.

$ ls /dev/tty.*
/dev/tty.Bluetooth-Incoming-Port    /dev/tty.usbserial-DQ008T3G

Now we can connect to our WiPy with picocom and finally see Micropythons REPL.

$ picocom /dev/tty.usbserial-DQ008T3G -b 115200
MicroPython v1.8.6-480-g4e970fe3 on 2017-02-26; WiPy with ESP32
Type "help()" for more information.
>>>

WiFi connection

Now we want to establish a connection to our WiFi. To do this we first scan for all available networks.

>>> from network import WLAN
>>> wlan = WLAN(mode=WLAN.STA)
I (9358594) wifi: mode : sta (24:0a:c4:00:b1:aa)
I (9358595) wifi: sleep disable
>>> nets = wlan.scan()
>>> for net in nets:
...     print(net.ssid)

This will get us a list of networks in our environment. We choose the network we want to connect to.

>>> net = [net for net in nets if net.ssid == 'mywifi'][0]
>>> wlan.connect(net.ssid, auth=(net.sec, 'secretpassword'))
I (9494645) wifi: n:12 0, o:6 0, ap:255 255, sta:12 0, prof:6
I (9495631) wifi: state: init -> auth (b0)
I (9495634) wifi: state: auth -> assoc (0)
I (9495639) wifi: state: assoc -> run (10)
I (9495654) wifi: connected with mywifi, channel 12

That's it. Now we' re connected to the world.

Autoconnect to WiFi on boot

The next time we switch off our WiPy we will have to repeat the above steps to reconnect to our network. But we want the connection to be established automatically each time the WiPy boots up. For this we will create a file called boot.py on our computer and place it on the WiPy. The boot.py gets executed when the WiPy is switched on.

# boot.py -- run on boot-up
import os
from machine import UART
from network import WLAN
import machine


# Enable REPL over UART
uart = UART(0, 115200)
os.dupterm(uart)


# Connect WiFi
wlan = WLAN()
ssid = 'mywifi'
password = 'secretpassword'
ip = '192.168.1.142'
subnetmask = '255.255.255.0'
gateway = '192.168.1.1'
dns = '192.168.1.1'


def wifi_init():
    wlan.mode(WLAN.STA)
    # wlan.ifconfig(config='dhcp')  # Uncomment this for dynamic ipd
    wlan.ifconfig(config=(ip, subnetmask, gateway, dns))  # Uncomment this for static ip


def wifi_connect():
    wlan.connect(ssid, auth=(WLAN.WPA2, password), timeout=5000)
    while not wlan.isconnected():
        machine.idle()
    cfg = wlan.ifconfig()
    print('WLAN connected to ip {} gateway {}'.format(cfg[0], cfg[2]))


def wifi_set():
    wifi_init()
    if not wlan.isconnected():
        wifi_connect()


wifi_set()

I got the boot.py script from the Adafruit guide on Getting Started with WiPy and modified it to my needs. To place the script on the WiPy we will transfer it with an FTP client. The easiest way ist to use FileZilla for this. But you can also use a command line tool like ftp for this. When using Filezilla don't use the Quick Connect option. Instead create a new server in the Server Manager. Make sure to use the Encryption option "Only use plain ftp (insecure)". In the Transfer Settings tab choose Passive transfer mode and limit the number of connections to 1. After connecting to our WiPy we can simply drag the created boot.py to the target directory /flash/.

Now that we' re done we can reset the WiPy by pressing the reset button or calling the machine.reset() function from our REPL. The WiPy should automatically connect to our WiFi and print out the IP address and gateway.

WLAN connected to ip 192.168.1.142 gateway 192.168.1.1
Micropython v1.8.6-480-g4e970fe3 on 2017-02-26; WiPy with ESP32
Type "help()" for more information.
>>>

Links