In this post we will be covering how to connect to the MicroPython interpreter (REPL) over the wireless network. MicroPython includes a web interface to REPL and file transfer. MicroPython supports a web interface that provides REPL (Read Execute Print Loop) access and file transfer capability. Before we can access WebREPL we need to bring up the ESP’s wireless access point.
The ESP family supports both station and access point WiFi operation and a wide variety of encryption algorithms for WiFI security. In this post we are just going to cover bringing up the ESP as an access point to get WebREPL running.
Connect to your ESP REPL via USB and serial terminal. See the previous post if you need to flash and configure your ESP for access via USB.
At the REPL prompt:
>>> import network
>>> ap_if = network.WLAN(network.AP_IF)
>>> ap_if.active(True)
>>> ap_if.ifconfig()
('192.168.4.1', '255.255.255.0', '192.168.4.1', '208.67.222.222')
>>> import webrepl_setup
WebREPL daemon auto-start status: disabled
Would you like to (E)nable or (D)isable it running on boot?
(Empty line to quit)
> E
To enable WebREPL, you must set password for it
New password (4-9 chars): upythoN
Confirm password: upythoN
Changes will be activated after reboot
Would you like to reboot now? (y/n) y
... ( bunch of build dependent output ) ...
WebREPL daemon started on ws://192.168.4.1:8266
Started webrepl in normal mode
ESP32 Notes: As of 7/2018 the stable 5/11/2018 ESP32 build “webrepl_setup” import didn’t work for me. However, the daily ESP32 build did work correctly.
See the previous post if you need to flash your ESP32 with a new build. Another difference between the ESP32 build and ESP8266 build, the ESP32 network came up as an Open network, the ESP8266 as secure. I’m not sure if this behavior will change in a future build, just something to be aware of. This is OK for quick prototyping but in a production environment you would want to configure WiFI security.
You should now see an ESP access point available. Don’t connect to it yet! We need to copy a few files from GitHub before we disconnect from the internet connected WiFi ( assuming you are connected to a WiFi access point).
The MicroPython WebREPL uses a WebSocket to communication so we need to download a web client to access the board. You can download the WebREPL client from GitHub here. Unzip the file and navigate to the directory containing “webrepl.html”
Switch your WiFi connection to the ESP_XXXX access point (AP).
Open “webrepl.html” in your browser, chrome or firefox is recommended. The ip address is preset to match the MicroPython AP defaults. Click on the connect button. You should now have a working WebREPL session over WiFi to your ESP! You are no longer limited to a tethered connection! Freedom! WebREPL is great for updates, quick changes, and interactive testing. Here are a few useful command to test that your ESP and WebREPL are working correctly:
Congratulate yourself! You now have a working MicroPython ESP board with WiFi and wireless access to REPL and file transfer. We showed some commands to explore the ESP MicroPython environment, checking on the amount for free memory, listing files, and accessing system information.
The Expressif ESP8266 is THE inexpensive IoT platform for makers and professional IoT developers. Over 100 million ESP chips have been sold.
What makes the ESP8266 family so special? Why should you bother with it? The answer is: it has WiFi, a capable processor, great development options, great support, and finally, PRICE. All the Expressif SOCs (System On a Chip) are available on boards costing under $10 US in 2018.
The Expressif ESP family of SOCs launched a revolution in low cost WiFi development boards.
In 2014 Expressif released the ESP8266EX a SOC combining processor and WiFi in a low cost package. In 2016 the more powerful ESP32, adding Bluetooth, was released. A lower cost version of the ESP8266EX, the ESP8285 with 1MB of integrated flash was also released in 2016. A wide variety of development languages and environments support the ESP family: C/C++, Python, LUA, Arduino, MicroPython, MongooseOS, FreeRTOS. We are only going to be looking at Python for now.
So, how do you choose a Python development board and environment?
We will show you how! It’s easy!
We are only focused on Python, so that eliminates all the other development environments and leaves us with only Micropython and CircuitPython.
Adafruit developed CircuitPython, a variation of MicroPython, focused on supporting Adafruit boards, such as the Circuit Playground, Huzzah32, etc.
You can find out more about Adafruit CircuitPython boards here.
The next criteria is cost. Adafruit makes a great product, but they cost more than a mystery brand board available on ebay or Amazon. Don’t get me wrong I love Adafruit products and CircuitPython. Adafruit has some great low cost boards that support CircuitPython, the Trinket M0 is only $8.95! Unfortunately, the Trinket M0 doesn’t include WiFi. The Adafruit boards provide extra features for that additional cost, built in RGB LEDs, battery connectors, builtin sensors. Details of the differences between MicroPython and CircuitPython can be found here.
So, For “IoT on the Cheap” that leaves us with MicroPython and “generic” ESP boards. We still have three choices of boards based on Expressif chips: ESP8285, ESP8266, and ESP32. To keep costs low support circuitry around the ESP chips is kept to a minimum. The board designs are open source.
Wemos D1 Mini Lite
HiLetgo ESP8266 NodeMCU
HiLetgo ESP32 NodeMCU
Cost
$4.00
$6.00
$10.00
MCU
ESP-8285
ESP-8266
ESP-32S
ADC
1 10-Bit
1 10-Bit
18 12-Bit
GPIO
11
17
36
Wifi
Yes
Yes
Yes
Bluetooth
No
No
Yes
CPU
Xtensa 1 core L106
Xtensa 1 core L106
Xtensa 2 core LX6
Speed
80Mhz
80Mhz
160Mhz
SRAM
~50KB
~50KB
~520KB
Flash
1MB
4MB
4MB
Touch Sensor
No
No
Yes
SPI/I2C/I2S/UART
2/1/2/2
2/1/2/2
4/2/2/2
Temperature Sensor
No
No
Yes
The decision of which board to use is fairly straight forward, if you need more I/O, processing power, or storage, you need to move to a more expensive board.
The tool chains for all the boards are identical. However, there are some critical differences in the settings required to setup the boards. The ESP32 MicroPython image also has different defaults on some values than the ESP8266 boards.
Next steps: Setting up the boards for MicroPython on Windows.
What you will need:
ESP Board (one of the following)
ESP32 ESP-32S Development Board ($10.99 on Amazon)
ESP8266 NodeMCU LUA CP2102 ESP-12E ($6.50 on Amazon)
Wemos D1 Mini Development Board ESP8285 V1.0.0 1MB ($4.94)
The esptool -h command will also display version and full usage information.
Now for some fun! Connect the ESP board to your PC using a USB cable.
You should see a new serial port in the device manager. The ESP board should appear as a COM port under “Ports”.
If the firmware was written successfully you will see output similar to the following:
esptool.py v2.4.0Connecting....Detecting chip type... ESP8266Chip is ESP8285Features: WiFi, Embedded FlashMAC: 60:01:94:b0:f9:56Uploading stub...Running stub...Stub running...Configuring flash size...Auto-detected Flash size: 1MBFlash params set to 0x0320Compressed 604872 bytes to 394893...Wrote 604872 bytes (394893 compressed) at 0x00000000 in 35.2 seconds (effective 137.6 kbit/s)...Hash of data verified.
Install Putty (Tera Term can be used as an alternative. However, I found setup more error prone.) I created and saved my settings as “MicroPython”. The next time you connect you can just load the correct settings.
Make sure the Serial settings are configured as no parity or flow control.
Connect to the board. You should now see a REPL prompt.
A quick check of the Python version will verify that REPL is working.
>>> import sys
>>> sys.version
'3.4.0'
Congratulations you now have a working MicroPython installation!