>>>
>>> # Connect to a WiFi network.
>>> esp.connect('YOUR WIFI SSID NAME', 'YOUR WIFI SSID PASSWORD')
>>>
>>> # Define function to print data received from socket.
>>> def socket_printer(socket, data):
>>> print(data)
>>>
>>> # Create a socket and setup the print function.
>>> soc = esp.socket()
>>> soc.onrecv(socket_printer)
>>>
>>> # Connect to adafruit.com at port 80.
>>> soc.connect(('207.58.139.247', 80))
>>>
>>> # Send a request for the wifi test page.
>>> soc.send('GET /testwifi/index.html HTTP/1.0\r\n\r\n')
37
>>> b'HTTP/1.1 200 OK\r\nDate: Tue, 12 May 2015 18:44:49 GMT\r\nServer: Apache\r\nAccess-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Authorization, Referer, User-Agent\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Max-Age: 1728000\r\nAccept-Ranges: bytes\r\nX-Mod-Pagespeed: 1.9.32.3-4448\r\nVary: Accept-Encoding\r\nCache-Control: max-age=0, no-cache\r\nContent-Length: 74\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nThis is a test of the CC3000 module!\nIf you can read this, its working :)\n'
As you can see the esp module provides a simple socket-like interface to access internet services. You'll need to implement protocols like HTTP
yourself to access web pages, etc.
If you're curious what other functions exist on the esp.socket class (or any other object in MicroPython) you can see them by using the dir function:
>>> dir(esp.socket)
['__del__', 'close', 'bind', 'listen', 'accept', 'connect', 'send', 'recv', 'sendto', 'recvfrom', 'onconnect', 'onrecv', 'onsent', 'ondisconnect']
Or check out the code for the module (http://adafru.it/fag) to see exactly how it works. MicroPython code is similar to writing a C extension to
Python (http://adafru.it/fah) so it will help to be familiar with the process of extending Python. You'll also want to be aware of the key differences
between MicroPython and normal desktop Python (http://adafru.it/fai).
Running Code From A Script
You might have noticed all of the Python code so far has been entered by hand at the serial console. Can you save a Python script and have it run
with MicroPython on the ESP8266? It turns out you can, but be aware support for saving code and running it is very limited and only allows for
one file to be compiled in to the MicroPython firmware and run at boot. MicroPython on the ESP8266 does not currently support running
Python code off a SD card or other file system like other more mature MicroPython boards!
To make a script run at boot you'll need to compile a new slightly modified version of MicroPython's firmware. Start up the VM for compiling
MicroPython that you created in the previous steps (run the 'vagrant up' command and then 'vagrant ssh' command in the directory with the VM's
Vagrantfile). Once connected navigate to the MicroPython ESP8266 scripts directory and open the main.py script in a text editor like nano:
cd ~/micropython/esp8266/scripts/
nano main.py
You should see a comment that says this script is run at boot:
# This script is run on boot
Now you can enter any Python script you'd like to have run as soon as the board boots. You can use the network, access GPIO, etc. For example
here's code to blink an LED connected to pin 14 forever:
# This script is run on boot
import pyb
pin = pyb.Pin(14, pyb.Pin.OUT_PP)
while True:
pin.value(1)
pyb.delay(1000)
pin.value(0)
pyb.delay(1000)
Note if you use an infinite loop in the main script then the serial console REPL will not be accessible! MicroPython doesn't have threading
or multiprocessing support so it can only do one thing at a time. If the main code never returns then the serial console REPL will never run! You'll
need to reflash the ESP8266 with firmware that has an empty main.py to bring back access to the serial console.
Save the modified main.py and exit nano by typing Ctrl-O and Ctrl-X. Then rebuild the MicroPython ESP8266 firmware by executing:
cd ~/micropython/esp8266
make
Again the output file is the ./build/firmware-combined.bin file. You can copy this to the /vagrant folder and back to your main machine, then
flash to an ESP8266 board just like in the previous steps.
That's all there is to running a script on boot with MicroPython on the ESP8266!