not logged in | [Login]

This is little tutorial to get you started with the cc3k wlan module. You can buy one of these from sparkfun, adafruit, etc. I used this one.

Wiring

The wiring depends on the SPI-bus you are using. One way to do it is like that:

pyb cc3k-board
X3 IRQ
X4 EN
X5 CS
X6 SCK
X7 MISO
X8 MOSI

Code

Connecting to the network

import pyb
import network

# this inits the module, the arguments contain the wiring information
wifi = network.CC3k(pyb.SPI(1), pyb.Pin('X5'), pyb.Pin('X4'), pyb.Pin('X3'))
# this connects to your wifi
wifi.connect('ssid', 'password')

Reading stuff from the web

Now that we are connected to the wifi, let's browse the web (assuming your wifi is connected to the web).

addr = socket.getaddrinfo('micropython.org', 80)
s = socket.socket()
s.connect(addr[0][-1])
s.send('GET /\r\n\r\n')
r = s.recv(1024)
s.close()

A tiny server

Let's serve something.

host = ''
port = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(1)
 
# Loop forever, listening for requests:
while True:
    try:
        csock, caddr = sock.accept()
        print('Connection from:', caddr)
        csock.send('Server running on the pyboard!!!\n')
        csock.close()
    except OSError:
        pass

Now on your computer type: telnet ip-address 8080 ip-address should be the address of your pyboard, you can find it by asking your router.

Note

If you are getting OSError: Failed to init wlan module - a hard reset of the pyboard solves that.