IoT on the Cheap: ESP WebREPL Access

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.

D1 Mini Lite AP

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”

webrepl files

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:

Webrepl Session

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.

Resources:

https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/repl.html#webrepl-a-prompt-over-wifi
https://docs.micropython.org/en/latest/esp8266/library/index.html

ESP8285, ESP8266, ESP32

IoT on the Cheap: The ESP8266 Family

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.

ESP8285, ESP8266, ESP32
From the left: ESP8285, ESP8266, ESP32

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:

Download and install Python 3 from here.

Make sure Python is in the path and environment variables are set if needed.

Detailed Python installation instructions for Windows can be found here.

Verify Python by install right-clicking the start menu: Run->cmd

Enter python into the text box. You should see a command prompt and Python version information displayed.

Python REPL Prompt
Python Prompt

At the Windows Command Prompt type:

>pip install esptool
pip esptool install
esptool install

Verify esptool is working correctly by displaying it’s help:

>esptool.py --help

usage: esptool [-h] [--chip {auto,esp8266,esp32}] [--port PORT] [--baud BAUD]
[--before {default_reset,no_reset,no_reset_no_sync}]
[--after {hard_reset,soft_reset,no_reset}] [--no-stub]
[--trace] [--override-vddsdio [{1.8V,1.9V,OFF}]]

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”.

Device Manager ESP8266
Device Manager ESP8266

Dowload latest micropython stable version.
For ESP8285 and ESP8266 use: http://micropython.org/download#esp8266

For ESP32 use: http://micropython.org/download#esp32

Try to use the stable versions of the firmware. If you use a daily version of the firmware you will probably want to disable MicroPython debug output.

Next erase the flash memory on the ESP board. I like to specify the COM port as displayed in Device Manager.

esptool.py --port COM3 erase_flash

For the ESP32 you will need to specify the chip type as the default is the 8266.
You may need to press the flash button on some boards.

esptool.py --port COM3 --chip esp32 erase_flash

Write the new firmware to the ESP board:

esptool.py --port COM3 --baud 460800 write_flash --flash_size=detect 0 esp8266-xxxxxx-vX.Y.Z.bin

For Wemos D1 Mini Lite (ESP8285) you might need to lower the baud rate to 115200 and add the flash mode option:

>esptool.py --port COM4 --baud 115200 write_flash --flash_mode=dout --flash_size=detect 0 esp8266-xxxxxxxx-vX.Y.Z.bin

Here again the ESP32  requires a slightly different command to update the flash:

esptool.py --chip esp32 --port COM3 write_flash -z 0x1000 esp32-20180621-v1.9.4-189-g34344a41.bin

If the firmware was written successfully you will see output similar to the following:

esptool.py v2.4.0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8285
Features: WiFi, Embedded Flash
MAC: 60:01:94:b0:f9:56
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 1MB
Flash params set to 0x0320
Compressed 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.

Putty ESP8266 Connect
Putty ESP8266 Connect

Make sure the Serial settings are configured as no parity or flow control.

Putty ESP8266 Settings
Putty ESP8266 Settings

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!

Further Reading:
https://docs.micropython.org/en/latest/esp8266/index.html
https://learn.adafruit.com/micropython-basics-how-to-load-micropython-on-a-board/overview
https://learn.adafruit.com/micropython-basics-what-is-micropython/overview
http://docs.python-guide.org/en/latest/intro/learning/

 

3D Printing at Ocean State Maker Mill

Over the past few weeks I have been experimenting with 3D printing at the Ocean State Maker Mill ( OSMM ). My first step was to take one of the OSMM 3D printing classes.

OSMM Intro to 3D Printing Class:

The instructor, Matt Stultz, 3D printing expert, author, and enthusiast,  shared his experience and insights during this excellent overview of 3D printing.

What follows are my musings based on Matt’s class.
All errors: grammatical, factual, and social, are mine, not Matt’s.

Fuzzy cell phone photo of OSMM 3D printed test objects

The class provided a survey of the current state of additive manufacturing, also known as 3D printing, or FDM (Fused Deposition Modeling), or FFF (Fused Filament Fabrication).
FDM and FFF refer to the same thing. The two terms developed for legal, copyright, trademark, etc. reasons.
Guess what, there is a Wikipedia page that explains all this stuff:  acronyms, concepts, principles, etc.
So I don’t have to!
You can find all the gory details here:
https://en.wikipedia.org/wiki/Fused_filament_fabrication

The most common 3D printers use various types of plastic to fabricate objects. The most common plastic is PLA. A bio-plastic made from starch, corn starch in the USA.
Really, chemistry is so cool, plastic from corn.

Simplify3d has a nice overview of 3D printing materials here.

You don’t have to have a 3D printer to take advantage of additive manufacturing, and you don’t have to be limited to plastics. Fabrication services like: ShapewaysSculpteo, and other services, will take your design files and fabricate your design and ship you the results.

There are other 3D printing technologies available besides FFF/FDM. Most are outside the realm of what hobbyists can afford. However, they do offer an amazing range of capabilities and materials. You can find an overview of more advanced additive manufacturing technologies here.

Once the landscape of 3D printing was covered we moved on to the details of how you actually create an object.

The process involves several steps: design using CAD, CAM using a “slicer”, and finally the 3D printer and controller.

Your design has to take into account the limitations of 3D printing. One key limitation is “overhangs”.  You can’t print on nothing! It is possible to generate support structures that can be removed after printing. If your design has features that are not directly supported you need to plan on how to deal with the issue. Sometimes you can change the orientation of design. For example, printing a mushroom upside down. Other times you will need to add support structures to the design. Sometimes the CAD/CAM software can generate supports for you. A detailed explanation is available here.

If you are designing your own objects there are free CAD (Computer Aided Design) programs available:
OnShape  ( Classes sometimes offered at OSMM )
Sketchup
Fusion360
TinkerCAD

You can also use existing designs for free or a small fee.  There are thousands of designs available. If you can imagine it, the chances are someone else has too.
Here are a few websites that provide access to designs:
Thingiverse
Youmagine
Myminifactory

Now that you have a design file (.stl) you need to translate it into instructions (gcode) for the specific printer you will be using. Most 3D printers come with a “slicer” package based on the CURA open source slicer that has been adapted by the manufacturer for it’s printers. CURA will allow you to modify the design, tune the print settings, and format the data for the printer.  For the class we used Lulzbot Cura to explore the features and settings available.

Lulzbot Taz

My first 3D printing projects:

So, Why bother with 3D printing?
Other than because it’s fun and just so darn cool to make your own stuff…
And could save you money, really, a study was done that estimated between $300 and $2000 a year in savings. The article is here, the study here.

So, what can you make with a 3D printer? Just about anything.
Personally, I am interested in designing custom cases for my art and electronics projects.

For my first attempts I chose simple, small, designs that had already been printed successfully. I download files from Thingiverse for a Raspberry Pi Zero case and a miniature flower pot.

My first attempt was to print a Raspberry Pi Zero case on the Lulzbot Taz printer.
I found the Lulzbot Cura software easy to use and intuitive to learn. The files are transferred to the printer via SD card and selected via a front panel interface. Again, I found it easy to use and understand.

Two important points:
The first, getting the first layer to adhere to the bed of the printer is critical, and one of the most common issues for novices.  I discovered this firsthand on my initial print attempt:

Matt came to the rescue and demonstrated how a bit of Elmer’s washable stick glue applied to the bed can solve the adhesion problem.

The second point, 3d printing can be a slow process. There is a trade-off between quality and speed. I selected the fastest, lowest quality setting, the small 1.5″ x 3″ x .5″ box took about an hour.

The final result a useful Rpi Zero case:

My second project, a small flower pot, printed on the Lulzbot Mini with an Octoprint interface running on a Raspberry Pi. The only challenge was getting my Windows laptop to see the Raspberry Pi. It turned out I needed to install “bonjour print services for windows” as described in the FAQ. Once I connected to the Octoprint interface I found it easy and intuitive to use. Oh, another tip, make sure you are targeting the right printer in CURA. I forgot to change the printer from Taz to Mini, no harm done, but my print would have failed if I hadn’t noticed the printer acting “funny” when attempting to calibrate and start the print.
Which brings me to another tip from the class, always make sure you watch the first layer being printed so you can detect problems early.

The finished product in use:

CNC Fun at Ocean State Maker Mill

I attended an “Intro to CNC” class at the Ocean State Maker Mill on Saturday January 20th.

The class provided an overview of the CNC (Computer Numerical Control) process. The entire workflow: design, CAD, CAM, CNC set up, and finishing.  Many small CNC machines use the Dewalt 611 Router. So if you have experience using a router you are off to a good start. I used a router for some small home projects, but never really gave much thought to router bits other than the shape. There is more to router bits and end mills than I ever imagined: up cut, down cut, compression. Tomes have been written on the subject. Why? Because it turns out to be really important when you want to reliably produce a quality result. If you are yearning to learn more about bits, um, I mean, end mills, you can check out this article in Popular Woodworking.

When you have a robot doing the work for you it can’t always compensate when things go astray the way a human can. Attention to detail is a must: measure the thickness of your material, make sure it is fastened down securely, check clearances, calibrations. Safety first: ear and eye protection, dust mask, NO gloves !

After covering the general principles we got down to the specifics using a small sign as a sample project targeting the Inventables X-Carve CNC. The project was designed in the Inventables Easel free design system.

Matt setting up the project on the CNC:

Time to relax while the CNC does the work:

Almost done:

The final product:

Arduino vs. Netduino vs. Raspberry Pi vs. Beaglebone Black (Part 4)

BeagleBone Black

BeagleBone Black Board
BeagleBone Black

The BeagleBone Black (BBB) is based on a 1Ghz TI Sitara XAM3359AZCZ100 Cortex A8 ARM processor. Similar to the Raspberry Pi, the BeagleBone Black includes: HDMI output, Ethernet, and it runs a version of Linux. The default distribution is Angstrom Linux. You can find other BeagleBone Black compatible distributions here. The BBB excels at I/O it supports: 65 GPIOs, 3 I2C buses, a CAN bus, an SPI bus, 5 serial ports, 8 PWM outputs, and 7 analog inputs. The BBB also supports expansion boards, called “capes”.  However, Not all capes are compatible with the BeagleBone Black so be sure to check compatibility ( http://www.elinux.org/BeagleBone_Black_Capes ). One unique feature of the BeagleBone Black is built in support for the Javascript node.js BoneScript library.

Advantages: Low cost, Linux based, ARM7, Performance, Large number of GPIOs

Disadvantages: Small community, Poorly supported default Linux distribution

Summary

The Arduino and the AVR microcontrollers are best for low cost, hard real time, low power, standalone applications, such as wearable electronics, driving LEDs, and simple control applications.

The Netduino and NETMF systems are best at networked complex soft real time control applications. The NETMF microcontrollers are particularly strong if you are interfacing to other Microsoft technologies, such as DWPS or WCF web services.

If your project is graphics intensive the Raspberry Pi is the clear winner. The RPi running Raspbian Linux also makes a great low end development platform.

The BeagleBone Black has the most capable hardware. If you are pushing the limits of performance or available I/Os the BBB could provide the extra capacity you need.

Resources

Arduino
http://forum.arduino.cc/

.Net Micro Framework
http://netmf.codeplex.com/
http://www.netmf.com/
http://www.ghielectronics.com/community
http://forums.netduino.com/

Raspberry Pi
http://www.raspberrypi.org/phpBB3/

Beaglebone Black
http://elinux.org/Beagleboard:BeagleBoneBlack

 

Arduino vs. Netduino vs. Raspberry Pi vs. Beaglebone Black (Part 3)

Raspberry Pi

Raspberry Pi Board
Raspberry Pi

 

The Raspberry Pi is an inexpensive , under $50, small single board computer using a Broadcom BCM2835 chip that includes a 700Mhz ARM processor. The system also supports HDMI graphics and ethernet.  You will need to supply your own 4GB or larger SD card for storage. The board supports several Linux distributions. The easiest way to get started is with the NOOBS (New Out Of the Box Software) install. Raspbian, a version of Debian Linux targeted for the Raspberry Pi that’s maintained by the Raspberry Pi Foundation, is recommended for the initial install. The Pi is inexpensive enough to use as an embedded system and powerful enough to use as a low end Linux development system. In fact I have my Pi configured as an Arduino development system! I also use CuteCom to debug serial communications with my NetDuino and XBee modules. Clementine also makes the Pi into a great internet radio. I have to admit that I have been having so much fun using my Pi as a low end Linux box that I have not spent much time interfacing it to hardware. The Pi can support: 8 GPIOs, 1 serial port, 1 SPI bus, 1 I2C bus. There are some really interesting projects available to support device control with the Pi. Adafruit has a customized Linux distro targeted at educators and makers: Occidentalis. You can use just about any programming language to program the Pi, hey it is Linux after all. However, out of the box Python is probably the best supported. Python and the Idle3 IDE are installed by default on Raspbian. The RPi. GPIO library supports access to Pi GPIO pins. A good description of RPi I/O is available here: http://elinux.org/Rpi_Low-level_peripherals . The Pi was designed for “serious” I/O to be located on a daughter board. A list of expansion boards is available at elinux.org.

Advantages: Low cost, Graphics, Linux based, large community

Disadvantages: No default hard realtime support, Linux overhead, GPIO support still evolving, no analog I/O, ARM6

 

Arduino vs. Netduino vs. Raspberry Pi vs. Beaglebone Black (Part 2)

Netduino

NetDuino Plus 2
NetDuino Plus 2

The Netduino ecosystem should more appropriately be called the .Net Micro Framework (NETMF) ecosystem. The .Net Micro Framework is an open source subset of the .Net Framework created by Microsoft. NetDuino boards have the same pin configuration as the Arduino Uno, and are compatible with a large number of Arduino shields. The NetDuino is manufactured by Secret Labs LLC based in NYC. Secret Labs also hosts an active support forum. The full range of NetDuino hardware can be found on the NetDuino website. Additional NETMF hardware is available from GHI Electronics, including the Cerbuino. The Cerbuino is also compatible with Arduino shields. Most NETMF boards use a member of the STMicro STM32 family, or another ARM Cortex-Mx MCU. Despite using more powerful hardware NetDuino and Cerbuino boards are cost competitive with AVR based Arduino boards.

The real strength of the NETMF ecosystem is the .NET framework, and the Visual Studio IDE. Visual C# is the preferred development language. Visual Basic is also supported but to a lesser extent. Microsoft Visual Studio Express is available as a free development environment from here. The .Net Micro Framework is a comprehensive well documented development environment. The base documentation is provided by Microsoft and can be found here. This is a real advantage, in many other microcontroller development ecosystems documentation is inconsistent and functionality fragmented across multiple libraries with varying levels of support. NETMF contains support for advanced features not usually found in the base versions of other microcontroller ecosystems, such as: http servers, http clients, file access, XML, GUIs, firmware updates, communication protocols, etc.

Advantages: Multithreading support, Full debugging support, .Net API, Visual Studio IDE

Disadvantages: No default hard real-time support, large performance overhead due to interpreted code, large memory footprint, small community

Arduino vs. Netduino vs. Raspberry Pi vs. Beaglebone Black ( Part 1 )

Choosing a microcontroller for a project can be at once both daunting and exhilarating.
There are so many factors and variables to consider. Besides the obvious hard
factors of processor speed, number and type of IOs, unit cost, there are a
number of soft factors such as familiarity with tools and architecture, software
support, development environment, supported languages, library support, and
community support. Each microcontroller consists of a constellation of hardware
and software tools that compose its ecosystem. We will look at some of the most popular options available: the Arduino, the Raspberry Pi, the Netduino, and the Beaglebone Black.

Arduino

The Arduino ecosystem consists of a wide variety of Arduino compatible hardware, shields (add on boards) the Arduino IDE, and Arduino libraries. The Arduino board is really just a thin wrapper around Atmel’s AVR processor. One 16Mhz ATmega328, plus a USB interface, power regulators, and standard pinout equals an Arduino Uno. The Atmel processors supported by the Arduino software tools range from the ATTiny85 on the low end (Trinket,$8) to the Atmel ARM Cortex-M3 SAM3X8E (Arduino Due,$49) and ATMega2560 (Arduino Mega,$59 ) on the high end.

 

ArduinoTrinketGemma
Size comparison of AVR microcontroller boards

The current trend is to extend the upper end of the Arduino performance spectrum by combining an AVR processor with a higher end processor running a Linux variant on the same board. Examples of this “two for one” strategy are: The $80 YUN (ATMega32u4 + Atheros AR9331), the Tre (ATMega32u4 + TI Sitara AM3359AZCZ100 (ARM Cortex-A8)), and the $100 Udoo (Freescale i.MX 6 ARM Cortex-A9 + Atmel SAM3X8E ARM Cortex-M3 ). 

The Arduino software tools consist of the Arduino IDE, a multiplatfrom simplied development environment. The IDE is written in Java andruns on Windows, Linux, and Mac. The Arduino IDE supports a simplified version of C, derived from the Processing language. Under the hood, the development environment is comprised of various open source tools, such as avr-gcc, avrdude. Arduino compatible boards are programmed with a special bootloader that simplifies loading programs on to the Arduino. A more flexible and powerful alternative to the Arduino IDE is The Atmel Studio IDE produced by Atmel, the manufacturer of the AVR family of semiconductors. Atmel Studio can take full advantage of all of the microcontroller features. No bootloader is required, so all of memory is available for user programs. 

There are a wide variety of specialized and less popular development environments available. A list can be found on the Arduino Development Tools webpage.

Advantages: Low cost, large community, migration to professional IDE possible, hard real time, multiplatform IDE

Disadvantages: Primitive default IDE, pseudo C language, lack of multithreading, primitive / lack of debugging support, no built in graphics.

Welcome

Welcome to my technology blog. I’m Robert “Jack” Babb, software engineer, maker, and technology enthusiast. At the moment I am working on a variety of microcontroller projects. On the bench we have: a Beaglebone Black, a Raspberry Pi, a Netduino, and an assortment of Arduinos.