Using MicroPython

1. Access the Serial REPL (more info below) and then reset the device. Device must be plugged into your computer!

    • REPL means read-eval-print-loop, and is essentially a single-line prompt that uses the MicroPython interpreter.
    • This is where you will get the output of print commands from your device as well as error messages.
    • For Mac: This is automatically configured in Terminal and can be accessed using the command screen /dev/cu.SLAB_USBtoUART 115200. To get back to Terminal, close the window and open Terminal again. You cannot use the exit() command as before since this is the MicroPython interpreter running on the board, not the Python interpreter running within Terminal itself.
    • For Windows: Use PuTTY (a free SSH and telnet client), which you can download from this totally legit website that isn't a scam, I promise (link). Once installed, you'll open PuTTY and fill out the options as below (note that COM# is the same as before in the previous tutorial) and then clicking "Open". The prompt should look like the second picture. Remember, whenever you see a prompt line beginning with >>>, you're in a Python interpreter.

2. "ampy" is a command line tool (much like pip) useful for accessing and manipulating the files on your HUZZAH. The storage on your device is organized with the same folder/directory structure as most computers, however it must be interacted with differently. ampy stands for Adafruit MicroPython tool.

    • Download ampy by running this command directly into Terminal or Command Prompt (not in the Serial REPL or the Python interpreter):
pip install adafruit-ampy
        • As before, you may need to use sudo pip, pip2, or pip3 for this to wortk.
    • You can confirm that it is has downloaded properly by running ampy --help in Terminal or Command Prompt. A list of commands will show up.
    • This is how to use ampy (you can go to step 3 but refer back to this for reference):
        • Enter commands as shown above. ampy --port (port) (command) (input) (occasional secondary input).
        • For help with individual commands, use ampy --port (port) (command) --help.
        • If you get an error saying "Invalid value for "local": Path "filename.extension" does not exist." or anything along the lines of "no such file", it is because Command Prompt/Terminal is running in a different directory (folder) than the file you're referencing.
            • You can see which directory is currently being accessed by checking the command prompt.
            • For Windows: To change directories, use the command cd (folder name)\(second folder name)\(etc). You can also return to the root folder (usually the C: drive) by running the command cd\.
            • For Mac: To change directories, use the command cd (folder name)/(second folder name)/(etc). You can also return to the root folder by running the command cd.
        • If you get an error saying "failed to access (port)", fear not. You cannot run an ampy command while also using the Serial REPL as both require access to the Serial port. Simply close out of the Serial REPL to run ampy commands and reopen it as needed. If the error persists, try a different USB port.

3. Check for and perform any ampy updates by running the command pip install adafruit-ampy --upgrade.

4. Using Anaconda, open Spyder and create a script with the following code.

import network
import ubinascii
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print(mac)

5. Then, save it to a Python file on your computer titled "getMAC.py".

6. Use ampy to copy "getMAC.py" to your device under the name "main.py":

    • Change to the directory that getMAC.py is saved within in Command Prompt/Terminal using the cd command as described in the "Using ampy" section above.
    • Ensure that PuTTY/screen (i.e. the Serial REPL) is closed.
    • ampy --port (port) put getMAC.py main.py
    • Upon reboot, your device will always run the file "boot.py" followed by the file "main.py" if available.
    • To understand what this command is doing, check out its individual help information by running ampy --port (port) put --help.

7. Open the Serial REPL and then reset your device (the HUZZAH has a RST button near the MicroUSB port). The MAC address should be displayed. Notice it is displayed above the >>> Python prompt because the code is performed automatically on startup before initializing the REPL.

8. Get permission to access Tufts_Wireless from your device by filing the MAC address with Tufts Network Registration (link).

9. Save the following code to a Python file on your computer and then copy it (using ampy) to your device under the name "boot.py". When doing so, make sure to use ampy --port (port) put boot.py.

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
#import webrepl
#webrepl.start()
gc.collect()

import esp
esp.osdebug(None)

def connect():
 import network
 import time
 ssid = 'Tufts_Wireless'    # insert your WiFi ssid here
 password = ''     # insert your WiFi pass here
 # Create Station interface
 sta_if = network.WLAN(network.STA_IF) 
 if not sta_if.isconnected():
  print('Connecting to',ssid,'...')
  # Enable the interface
  sta_if.active(True)
  # Connect
  sta_if.connect(ssid, password)
  # Wait till connection is established
  while not sta_if.isconnected():
   time.sleep_ms(300)
  print('Your device is now connected.')
 else:
  print('Already Connected.')   
  print('Network Settings:', sta_if.ifconfig())

connect()
    • To confirm that it is there, run the command ampy -p (port) ls and then ampy -p (port) get boot.py. These commands will list the files on your device and then show the contents of the boot.py file, respectively.
    • The changes made to boot.py here allow us to automatically connect to WiFi upon bootup. Feel free to adjust the ssid or password to connect to whichever network you choose. Additionally, we ensure that ESP8266 debug output is disabled so that it will not interfere with ampy.

10. We can now make changes to the main.py file to do whatever we want with MicroPython.

    • Use Spyder to write and edit your code and then copy it to the device with ampy. Then, you can always check the output of your code by opening the Serial REPL and resetting your device so that boot.py and main.py will be run.
    • TIP: Within main.py (or any other Python file), you can include code to run other files. This allows you to organize your code. To do this, use:
import filename
        • Be sure to leave off the ".py" file extension when doing an "import" command.

Alternatively, you can use ampy to run files by using the command ampy -p (port) run (file on computer to be run on board). In general, MicroPython will wait to show outputs until the file has finished running, so if you have loops or need outputs shown immediately, use the "ampy run" command with the modifier --no_output or simply -n. More information on this is available in the command's individual help menu.


Advance to the next tutorial to learn how to connect with Thingworx using MicroPython!