ESP8266 w/ Mongoose
Getting Started
Tech Prerequisites:
- NodeMCU USB/Serial Drivers: https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
- ESP32
-
mongoose os - setup
- Requires homebrew
- Set up an AWS Account and IoT Gateway
Create a AWS thing
Aside
AWS takes a few minutes to setup the IoT Gateway, adding your first device will kickstart that process.
The Device Gateway forms the backbone of communication between connected devices and the cloud capabilities such as the Rules Engine, Device Shadow, and other AWS and 3rd-party services.
The Device Gateway supports the pub/sub messaging pattern. [...] Pub/sub involves clients publishing messages on logical communication channels called ‘topics’ and clients subscribing to topics to receive messages. The device gateway enables the communication between publishers and subscribers
“
mos GUI
Open by running the `mos` program.
Command Line
Serial Debug Control from Chip
Select
Chip &
Port
Setup Steps
- Build demo image
- Flash built demo image
- Setup WiFi
- Setup AWS IoT Cloud connection
- Make sure demo code runs
Build and Flash
Build and Flash
Build: mos build
Flash: mos flash
Building is only needed for a change in libraries, but not for any changes in `fs/`
Setup Wifi & AWS IoT
Command to Setup WIFI:
Command to Setup AWS IoT:
mos wifi $NETWORK_NAME $NETWORK_PASSWORD
Important: only 2.4 ghz capable wifi networks are supported, do not choose the 5ghz network to connect.
brew install awscli
aws configure
mos aws-iot-setup --aws-region us-east-2 --aws-iot-policy mos-default
Pinout and GPIO
GPIO Docs
https://mongoose-os.com/docs/mos/api/core/mgos_gpio.h.md
GPIO.write#
GPIO.write(pin, level)
Set GPIO pin level to either 0 or 1. Return value: none.
GPIO.read#
GPIO.read(pin)
Read GPIO pin level. Return value: 0 or 1.
GPIO.set_int_handler#
GPIO.set_int_handler(pin, mode, handler)
Install GPIO interrupt handler. mode could be one of: GPIO.INT_NONE, GPIO.INT_EDGE_POS, GPIO.INT_EDGE_NEG, GPIO.INT_EDGE_ANY, GPIO.INT_LEVEL_HI, GPIO.INT_LEVEL_LO. Return value: 1 in case of success, 0 otherwise. Example:
// Set pin mode
GPIO.set_mode(pin, GPIO.MODE_INPUT);
// Set handler callback for edge
GPIO.set_int_handler(pin, GPIO.INT_EDGE_NEG, function(pin) {
print('Pin', pin, 'got interrupt');
}, null);
// Enable Interrupt
GPIO.enable_int(pin);
Update the demo code
let onhi = Cfg.get('board.led1.active_high');
// New code:
// Device state
let state = {on: false, uptime: 0, motion: false};
// Motion pin
let motionPin = 15; // maps to D8 on the dev board
1) Add new motion key to config and set pin id
Update the demo code
GPIO.set_mode(motionPin, GPIO.MODE_INPUT);
GPIO.set_int_handler(motionPin, GPIO.INT_EDGE_ANY, function(pin) {
print('Pin', motionPin, 'got interrupt ');
// Active HIGH therefore invert signal.
state.motion = !GPIO.read(motionPin);
reportState();
}, null);
GPIO.enable_int(motionPin);
2) Add interrupt handler to update Shadow state
Connect to the cloud!
Use the mos config utility to connect to wifi & the cloud
View your shadow state in AWS
{
"desired": {
"on": false
},
"reported": {
"ota": {
[...]
"app": "demo-sensor",
"arch": "esp8266",
"message": "idle",
"commit_timeout": 0,
"partition": 0,
"progress_percent": 0
},
"uptime": 247.719288,
"on": false,
"motion": 0
}
}
Setup Wifi & AWS IoT
Command to Setup WIFI:
Command to Setup AWS IoT:
mos wifi $NETWORK_NAME $NETWORK_PASSWORD
Important: only 2.4 ghz capable wifi networks are supported, do not choose the 5ghz network to connect.
mos aws-iot-setup --aws-region us-east-2 --aws-iot-policy mos-default
Connect to the cloud!
[Oct 29 01:01:34.934] online: false {"motion":0,"uptime":2.764663,"on":false}
[Oct 29 01:01:35.693] scandone
[Oct 29 01:01:36.578] state: 0 -> 2 (b0)
[Oct 29 01:01:36.594] online: false {"motion":0,"uptime":4.424042,"on":false}
[Oct 29 01:01:36.602] state: 2 -> 3 (0)
[Oct 29 01:01:36.606] state: 3 -> 5 (10)
[Oct 29 01:01:36.608] add 0
[Oct 29 01:01:36.608] aid 2
[Oct 29 01:01:36.609] cnt
[Oct 29 01:01:36.617]
[Oct 29 01:01:36.617] connected with allthethings, channel 3
[Oct 29 01:01:36.621] dhcp client start...
[Oct 29 01:01:36.622] mgos_net_on_change_c WiFi STA: connected
[Oct 29 01:01:36.934] online: false {"motion":0,"uptime":4.764134,"on":false}
[Oct 29 01:01:37.579] ip:192.168.0.218,mask:255.255.255.0,gw:192.168.0.1
[Oct 29 01:01:37.584] mgos_net_on_change_c WiFi STA: ready, IP 192.168.0.218, GW 192.168.0.1, DNS 192.168.0.1
[Oct 29 01:01:37.593] mgos_mqtt_global_con MQTT connecting to a37fgbdx2vwx5r.iot.us-east-2.amazonaws.com:8883
[Oct 29 01:01:37.630] mongoose_poll New heap free LWM: 27888
[Oct 29 01:01:37.685] mongoose_poll New heap free LWM: 27512
[Oct 29 01:01:37.772] mongoose_poll New heap free LWM: 26600
[Oct 29 01:01:37.783] mg_ssl_mbed_log 0x3fff3bac ciphersuite: TLS-RSA-WITH-AES-128-GCM-SHA256
[Oct 29 01:01:40.852] mongoose_poll New heap free LWM: 12088
[Oct 29 01:01:40.887] online: false {"motion":0,"uptime":8.717346,"on":false}
[Oct 29 01:01:40.898] mgos_sntp_query SNTP query to time.google.com
[Oct 29 01:01:40.918] mgos_mqtt_ev MQTT TCP connect ok (0)
[Oct 29 01:01:40.982] mgos_sntp_ev SNTP reply from 216.239.35.0: time 1540789300.887772, local 9.057504, delta 1540789291.830268
[Oct 29 01:01:40.999] mgos_mqtt_ev MQTT CONNACK 0
[Oct 29 01:01:41.006] do_subscribe Subscribing to 'esp8266_046823/rpc/#'
[Oct 29 01:01:41.013] do_subscribe Subscribing to 'esp8266_046823/rpc'
[Oct 29 01:01:41.159] mgos_aws_shadow_ev Subscribed
[Oct 29 01:01:41.221] mgos_aws_shadow_ev Requesting state, current version 0
[Oct 29 01:01:41.229] mgos_aws_shadow_ev Update: {"state": {"reported": {"ota":{"fw_version": "1.0", "fw_id": "20181029-041939", "mac": "3A2B78046823", "device_id": "esp8266_046823", "app": "demo-sensor", "arch": "esp8266"}}}, "clientToken": "3249b4
[Oct 29 01:01:41.251] mgos_aws_shadow_ev Update: {"state": {"reported": {"ota":{"message": "idle", "status": 0, "is_committed": true, "commit_timeout": 0, "partition": 0, "progress_percent": 0}}}, "clientToken": "3249b444"}
[Oct 29 01:01:41.353] mgos_aws_shadow_ev Version: 0 -> 18912 (2)
[Oct 29 01:01:41.404] mgos_aws_shadow_ev Version: 0 -> 18913 (5)
[Oct 29 01:01:41.443] mgos_aws_shadow_ev Version: 0 -> 18914 (5)
[Oct 29 01:01:41.889] online: true {"motion":0,"uptime":9.718672,"on":false}
[Oct 29 01:01:41.925] mgos_aws_shadow_ev Update: {"state": {"reported": {"motion":0,"uptime":9.718672,"on":false}}, "clientToken": "3249b444"}
[Oct 29 01:01:42.050] mgos_aws_shadow_ev Version: 0 -> 18915 (5)
Use the mos config utility to AWS cloud and then wifi
Sending an Email from Motion
Subtitle
Motion -> Email
-
AWS IoT Action SQL
-
select state from '$aws/things/+/shadow/update/accepted' where motion=1
-
-
AWS IoT SNS notification
- That then is set to 1) push notify, 2) email
- Lots of flexibility - can trigger Lambda functions, pipe to IoT analytics etc.
- Tools allow you to create new roles for needed permissions here.
Motion -> Email
-
AWS IoT Action SQL
-
select state from '$aws/things/+/shadow/update/accepted' where motion=true
- Potential Pitfall: Matching tests for JSON literals, 1 != true, 0 != false etc
- Selecting the entire state returns the whole object, pick a part of the object with a dot syntax: `state.reported.motion`
-
select state.reported.motion as motion, topic(3) as name from '$aws/things/+/shadow/update/accepted'
- Selects 3rd part of topic for device name, gets inner motion variable
- {"motion":false,"name":"esp8266_046823"}
-
Control from the Browser
MQTT Websockets
Auth? Format?
The better unofficial library docs:
Get a browser auth'd
- Create an cognito unauthenticated identity pool
- Get the cognito identity pool ID
-
aws cognito-identity list-identity-pools --max-results=5
- Get the ATS endpoint for the web socket endpoint
- The normal (non-ATS) endpoint uses old SSL Certs
-
aws iot describe-endpoint --endpoint-type iot:Data-ATS
AWS.config.region = 'us-east-2'; // your region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-2:YOUR_UUUID',
}) // See AWS Setup and Security below
const endpoint = 'xxxxxxxx-ats.iot.us-east-2.amazonaws.com';
ESP8266 w/ MongooseGetting Started
By Iain Nash
ESP8266 w/ MongooseGetting Started
- 1,456