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

goMake
4 min readSep 26, 2017

--

When last we left our hero…

Recap

In Part I we:

  • Setup the drivers and toolchain for working with ESP32-DevKitC
  • Installed Mongoose OS and tested connectivity of the ESP32-DevKitC through the web client IDE
  • Configured AWS IoT and added the ESP32 as a “thing” in its registry

Phew! Now what?

Source: http://galleryhip.com/space-cat-wallpapers.html

Goals

  • Create a simple “Internet Button” that emails a quote from spacecat
  • Setup a Rule in AWS IoT to trigger another process

Concepts

MQTT Topics for Interacting with a Shadow: Link

MQTT

A lightweight messaging protocol.

  • Pub/Sub: Entities publish new information to a topic and subscribers to that topic receive updates from those publishes.
  • Topics/Subscriptions: Topics are “channels” that entities either push information to or receive information via subscription to that topic. A topic in AWS IoT might look like this:
$aws/things/thingName/shadow/update
  • QoS: “Quality of Service” — this is the level of “handshaking” that occurs to ensure a valid message transmission. QoS=0 is no checking whatsoever, QoS=2 is a 4 step handshake.
Slide referenced from: https://www.slideshare.net/BryanBoyd/mqtt-austin-api

MQTT Tutorial: Link

Requirements

  • ESP32-DevKitC
  • Mongoose OS
  • AWS Account
  • Completed Steps in Part I

Setup

1. Setup an SNS Topic

UI Interface

Click Create New Topic on the SNS dashboard.

Input a Topic name and Display name for SpaceCat’s mail notifications:

After clicking Create Topic you will see your topic added to the Topics tab grid.

Switch to the Subscriptions tab, and click Create subscription. Select Email for your protocol and enter your email address for your Endpoint.

After clicking Create subscription, you should see your subscription show up in the Subscriptions tab:

2. Setup a Rule

Navigate to the Rules tab on AWS IoT. Click Create a rule.

Fill out the Name and Description fields.

Set the Attribute field in the rule to *.

Set the Topic filter to an appropriate MQTT topic. In our example we will create a topic spacecatmail as:

$aws/things/esp32_068A80/spacecatmail

Next, click on Add action.

From the list of available actions, click Send a message as an SNS push notification. Click Configure action.

At the bottom of your rule configuration, you should now see an action added. Click Create rule.

3. Code

Launch the mos tool IDE:

$ ~/.mos/bin/mos

Select init.js from the Device File Manager:

Type in the following code:

load('api_config.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_net.js');
load('api_sys.js');
load('api_timer.js');
let button = ffi('int get_button_gpio_pin()')();GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
let topic = '$aws/things/' + Cfg.get('device.id') + '/spacecatmail';
let message = 'Im feline paw-some.';
let ok = MQTT.pub(topic, message, 1);
print('Published:', ok ? 'yes' : 'no');
}, null);

You can change the string for message to whatever you want your spacecat quote to be.

4. Flash the ESP

To flash your code, click Save + Reboot.

5. Test

Momentarily press the right button on your esp32 devkit and you should see an email from spacecat hit your inbox in a few moments. Huzzah!

What’s next?

In Part III we will explore:

  • how to manipulate the device shadow
  • how to create a battery powered thing and expose the deep sleep functionality of the esp32 to preserve power.

--

--

goMake

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