Connecting the ESP32-DevKitC and AWS IoT Using Mongoose OS, Part I

goMake
7 min readAug 2, 2017

--

Jump to Part II

Once you’ve stopped drooling over the extensive ESP32 feature list, its time to get down to connecting your ESP32-DevKitC to something capable of doing some heavier lifting than the chip itself — a cloud offering like AWS IoT is the starting place for a cloud-based solution to communicating with and analyzing data from your ESP.

Our Goals

  • Setup a development environment for working with the ESP32 Development Board.
  • Configure an ESP32 as an AWS IoT Thing and make it ready to communicate

Concepts

What is the ESP32?

A lower power, low cost microcontroller unit (MCU) with 2.4Ghz WiFi and Bluetooth BLE onboard. Currently a single ESP32 module (without breakout) is around $6.50 USD.

The ESP32-DevKitC (Amazon, $14.99) is a breakout board offering from Espressif to facilitate development and prototyping of IoT devices from design to market. They take the core SoM (System on Module) that could be used in a commercial application and wrap it with a serial comms chip, USB connector, buttons wired for controlling the programming state, power LED, and breadboard compatible pin headers. Wanna dig in further? Check out the massive datasheet.

One of the big differentiators that sets the ESP32 apart from its Espressif predecessors and WiFi-enabled MCU competitors is its plethora of security features:

  • SPI Flash Encryption: Tutorial
  • Secure Boot: Documentation
  • Hardware-accelerated Encryption for SHA, AES, RSA, etc
  • Secure Over-the-air Code Updates: Tutorial
  • Support for ATECC508A Integration: Tutorial

NOTE: The ESP32-DevKitC that I purchased seemed to be missing an onboard LED for the SoM (or the power LED on the breakout board was meant to be the GPIO addressable LED and wasn’t working).

What is AWS IoT?

A platform for providing bi-directional communication between internet connected devices and the cloud. It provides:

  • Device Gateway: enables other devices and applications to communicate with a device.
  • Message Brokering: pub/sub mechanism for devices to communicate over
  • AWS Integrations: allows (almost) seamless integration with other AWS offerings like DynamoDB or S3
  • Shadows: persistent object based representations of device state and managed data. IMHO, reminiscent of the idea of a Virtual DOM representing a single device.

What is Mongoose OS?

An “open-source operating system for the Internet of Things.” While that sounds cool, that really breaks down into:

  • Consolidated toolchain for reading and writing to the ESP32 (or other supported devices)
  • Libraries and tools for working with cloud integrations like AWS IoT and Google Cloud IoT.
  • Simple web client IDE for interacting with your device.
  • Javascript support for your device, including libraries for doing common tasks, and utilities for porting C code (think arduino libraries or C-only helper methods) into your Javascript!
  • Ready to flash custom firmware for ESP32 and supported devices

Requirements

Setup

1. Setup Your Laptop

The second line of text on the serial chip determines the driver you need. CP2102 indicates a Silicon Labs Driver.

Look at the serial chip on your ESP32-DevKitC (If you are an old man like me, you may need to break out the magnifying lens) and identify the second line of text on the chip — if you have CP2102 then you should download the Silicon Labs drivers, otherwise if you see CH34x you should download the corresponding CH34x drivers.

macOS:

  • Silicon Labs Drivers (CP2102 on serial chip): Download
  • CH34x Drivers (CH340 or CH341 on serial chip): Download

Windows:

  • Silicon Labs Drivers (CP2102 on serial chip): Download
  • PC CH34x Drivers (CH340 or CH341 on serial chip): Download

Installing the macOS Silicon Labs Driver:

Testing your Driver Installation:

Making sure your Micro USB cable connector is plugged into the mating connector on the ESP32-DevKitC, and the USB A connector is unplugged from your laptop. Plug the USB A connector back into the laptop and open a a new terminal:

$ ls /dev | grep tty.SLAB
tty.SLAB_USBtoUART

Note the output of that command — its the name of the serial device we will attempt to connect to via Mongoose OS. In this case the output was tty.SLAB_USBtoUART -> this translates to a full device path of:

 /dev/tty.SLAB_USBtoUART

If you aren’t seeing any output like the one above, disconnect and reconnect the USB cable to the laptop, and try a more generic command like $ ls /dev | grep tty. You will see many devices listed this time; look for the device name with USB or UART in it. If you STILL aren’t seeing your device, something else is wrong.

2. Install Mongoose OS

This will assume you have already installed the serial drivers from the previous step. Mongoose OS is a combination of command line tool chain and web IDE for interacting with a USB serial connected device (i.e. ESP32).

macOS

$ curl -fsSL https://mongoose-os.com/downloads/mos/install.sh | /bin/bash

Windows

Installer Executable: Download

$ mos console

3. Flash ESP32 Firmware

NOTE: flashing through the CLI was the only way I could get the flash operation to work (macOS Sierra, Chrome 59).

macOS

$ ~/.mos/bin/mos flash esp32 --port /dev/cu.SLAB_USBtoUART

NOTE: cu.DEVICENAME is the call-up device that represents the way we connect to an external device

Windows

$ mos flash mos-esp32

Example output:

4. Setup WiFi

NOTE: The ESP32 WiFi radio only supports 2.4ghz. 5ghz WiFi networks will not work.

macOS

$ ~/.mos/bin/mos wifi YOUR_WIFI_SSID YOUR_WIFI_PASSWORD

Windows

$ mos wifi YOUR_WIFI_SSID YOUR_WIFI_PASSWORD

5. Setup AWS IoT

Mongoose OS’ CLI tool has provided a handy setup command that does the following things:

  • Checks your stored AWS credentials for what account to use
  • Creates a default policy (AWS IoT Policies) for Mongoose OS and the ESP32 to use in communicating with AWS IoT
  • Provides the certificate, public, and private keys required for communication and writes them to the ESP32
  • Creates a “thing” on AWS IoT for your device

At this point, make sure you have your AWS credentials stored locally that you want to use, and that your ESP32 is connected to your laptop. Replace REGION in the commands below with us-east-1 or your appropriate AWS region. In your terminal, run:

macOS

$ ~/.mos/bin/mos aws-iot-setup --aws-region REGION --aws-iot-policy mos-default

Windows

$ mos aws-iot-setup --aws-region REGION --aws-iot-policy mos-default

NOTE: If you renamed mos-default to another policy name you may run into errors. Try creating a policy on AWS IAM console with the name you want before running this with a different name.

Example output:

If you go into your AWS Console -> AWS IoT -> Registry, you should see a device registered like this:

6. Launch the IDE

Run the following command in your terminal:

macOS

$ ~/.mos/bin/mos ui

Windows

$ mos ui

Your default browser should open a tab with something like the following:

There’s a lot going on here, and there’s about to be a whole lot more.

NOTES:

  • Device status reports whether the connected device chosen in Step 1 is connected to the internet. It does not mean the connection between the device and the computer
  • The Select button in Step 1 did not work the way I assumed it would. It confirms the device that you enter into the drop down/textbox in Step 1.
  • Device logs (in the shaded out bottom left corner of the screen) is the serial monitor output. Watch this for communications from your device.
  • MOS tool logs (bottom right corner of the screen) is the area for messages from the toolchain (IDE, compiler, flasher, etc)

7. Configure the IDE for the ESP32

In Step 1 section, use the device we noted down from “Testing your Driver Installation” (Remember we used the command: ls /dev | grep tty.SLAB) we enter its full path into the dropdown/textbox like so:

/dev/cu.SLAB_USBtoUART

Press the Select button. You should see the Device Status show a connected status (has a green or yellow dot). You should also see some device information show up in the Step 2 section.

In the Step 2 section, select esp32. DO NOT HIT THE FLASH BUTTON. We have already flashed our device in a previous step.

If your WiFi in Step 1 left you with a yellow dot and “No IP” in the Device Status, you may need to modify your WiFi information in Step 3 section and hit the Save button.

Success!

Continuing in Part II

Now that we have our device and environment up and running, we will tackle communication examples between the ESP and cloud.

  • Modifying a thing’s shadow state
  • Pushing shadow state back to the ESP32
  • Subscribing to MQTT events in AWS IoT

Goto Part II Now…

About the Author

Jonathan Barton (@jellyfishtree) is a software engineer, hardware hacker, and a co-founder of GoMake. LinkedIn

--

--

goMake

Through project-based learning, goMake empowers students to build, launch, recover, and analyze data from high-altitude balloons.