Connecting my WiPy to my WiFi

By default the WiPy 2.0 advertises it self as a wireless access point with a default SSID, IP address, username and password. This makes sense as the factory default since unless you use the USB serial console the only way to update the scripts in the first place is to connect wirelessly, though you really ought to change the username and password otherwise it isn’t very secure.

However, I wanted the device to automatically connect to an existing wireless access point as I normally use a desktop machine without a wireless interface – and even when I am using my laptop I don’t really want to lose my internet connection just to update my WiPy!

You will need to change the <ssid> and <wireless-password> to match your WiFi settings, and it is a good idea to change the default <username> and <password> used by the FTP (and telnet) service on the WiPy.

The 10 second wait at the end is optional – I added it to make sure that the system finishes displaying any console messages before displaying the REPL prompt, and it also lets me check the device is working before running main.py as the heartbeat LED should normally flash every 4 seconds.

# boot.py
#
# This  program  is free software: you can redistribute it and/or modify  it
# under the terms of the GNU General Public License as published by the Free
# Software  Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This  program  is  distributed  in the hope that it will  be  useful,  but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  License
# for more details.
#
# You  should  have received a copy of the GNU General Public License  along
# with this program. If not, see <http://www.gnu.org/licenses/>
#
import os
import machine
import network
import time
  
# Duplicate output on UART
uart = machine.UART(0, 115200)
os.dupterm(uart)
 
# Disable telnet and FTP server before connecting to the network
server = network.Server()
server.deinit()
  
# Connect to WLAN
wlan = network.WLAN()
wlan = network.WLAN(mode=network.WLAN.STA)
nets = wlan.scan()
for net in nets:
  if net.ssid == '<ssid>':
    wlan.connect(net.ssid, auth=(net.sec, '<wireless-password>'),
      timeout=5000)
    while not wlan.isconnected():
      machine.idle() # save power while waiting
 
# Enable telnet and FTP server with new settings
server.init(login=('<user>', '<password>'), timeout=600)
time.sleep(10) # Wait 10 seconds before continuing

Note – If you are using the USB serial connection you can also enter these commands manually to establish an initial connection to your wireless access point which you can then use to allow you to update boot.py via FTP.

After modifying boot.py you will need to press the reset button or enter ‘Ctrl-d’ to have the device execute the script when it reboots.

ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:QIO, clock div:2
load:0x3fff9010,len:8
load:0x3fff9018,len:248
load:0x40078000,len:4056
load:0x4009fc00,len:920
entry 0x4009fde4
I (1554) wifi: wifi firmware version: 90b1b8b
I (1555) wifi: config NVS flash: disabled
I (1555) wifi: config nano formating: disabled
I (1570) wifi: Init dynamic tx buffer num: 32
I (1571) wifi: wifi driver task: 3ffbd688, prio:23, stack:3584
I (1571) wifi: Init static rx buffer num: 10
I (1573) wifi: Init dynamic rx buffer num: 0
I (1577) wifi: Init rx ampdu len mblock:7
I (1581) wifi: Init lldesc rx ampdu entry mblock:4
I (1585) wifi: wifi power manager task: 0x3ffd9910 prio: 21 stack: 2560
I (1592) wifi: sleep disable
I (2581) wifi: wifi timer task: 3ffda9a4, prio:22, stack:3584
I (2609) wifi: Init ampdu: 0
I (2609) wifi: mode : softAP (24:0a:c4:00:6b:bf)
I (2664) wifi: Init ampdu: 0
I (2665) wifi: mode : sta (24:0a:c4:00:6b:be)
I (2665) wifi: sleep disable
I (4991) wifi: n:1 0, o:6 0, ap:255 255, sta:1 0, prof:6
I (5978) wifi: state: init -> auth (b0)
I (5980) wifi: state: auth -> assoc (0)
I (5982) wifi: state: assoc -> run (10)
I (15983) wifi: state: run -> init (2)
I (15983) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:6
I (16104) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:6
I (16104) wifi: state: init -> auth (b0)
I (16106) wifi: state: auth -> assoc (0)
I (16109) wifi: state: assoc -> run (10)
I (16299) wifi: connected with <ssid>, channel 1
I (26110) wifi: pm start, type:0

MicroPython v1.8.6-556-g989d5ac9 on 2017-03-30; WiPy with ESP32
Type “help()” for more information.
>>>

Advertisements
This entry was posted in Hardware, PyCom and tagged , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s