ESP8266 w/ Mongoose

Getting Started

Tech Prerequisites:

Focus for the workshop:

  1. MQTT    (Message Queuing Telemetry Transport)
  2. NodeMCU Board
  3. GPIO and Signal Protocol
  4. Mongoose OS Dev Environment
  5. MJS

 

MQTT Overview

  1. Developed by IBM
  2. Based on the hub & spoke model
  3. Server =~ Broker
  4. Based on TCP Protocol
  5. Short, Simple Messages
  6. Pub/Sub protocol

MQTT Hub and Spoke

MQTT & TCP

Play w/ MQTT!

Server: iain.in:1883

Digital Signals

GPIO Standards

Wiring a Button

Pull Up and Pull Down

DHT22

One-Wire Modified Protocol

Serial Protocol

Setup Steps

  • Build demo image
  • Flash built demo image
  • Setup WiFi for the dev board
  • Setup adafruit mqtt connection
  • Test demo code image

mos GUI

Command Line

Serial Debug Control from Chip

Select

Chip &

Port

Create the Demo App

mos clone https://github.com/mongoose-os-apps/demo-js app1

Clone the MOS demo app into your shared folder

Build and Flash

> mos build

Build and Flash

> mos flash

Building is only needed for a change in libraries, but not for any changes in `fs/`

Update JS Code

> mos put fs/init.js

Building is only needed for a change in libraries, but not for any changes in `fs/`

Update JS Code

> mos put fs/init.js
> mos call Sys.Reboot

Building is only needed for a change in libraries, but not for any changes in `fs/`

JS Code Reference

Building is only needed for a change in libraries, but not for any changes in `fs/`

Setup Wifi

Command to Setup WIFI:

mos wifi $NETWORK_NAME $NETWORK_PASSWORD

Important: only 2.4 ghz capable wifi networks are supported, do not choose the 5ghz network to connect.

Setup Wifi & MQTT Server

Command to Setup WIFI:

Command to setup MQTT:

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 config.set mqtt.enable=true mqtt.server=server:1883

Setup MQTT

Command to Setup MQTT:

mos wifi $NETWORK_NAME $NETWORK_PASSWORD

Important: only 2.4 ghz capable wifi networks are supported, do not choose the 5ghz network to connect.

Pinout and GPIO

GPIO Docs

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!

[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 connect to wifi

Programming Environments

MJS

Micropython

Esprino

Lua (NodeMCU)

 

MJS

 

load('lib.js');
// load(lib name, object to assign)
load('lib.js', MyLib);
// Like const lib = require('lib.js'), but global.

let testVar = 'test';
// Only let is supported, no const or var.

function testVar() {
// functions and anonymous functions are supported.
  print()
}

'abc'.at(2) // Returns byte value at index.
'some_string'.slice(start, end) // slices string
'test'.indexOf('te') === 0

// No == or !=, only === and !==.

let f = ffi('int foo(int)'); // import c function

Esprino

 

load('lib.js');
// load(lib name, object to assign)
load('lib.js', MyLib);
// Like const lib = require('lib.js'), but global.

let testVar = 'test';
// Only let is supported, no const or var.

function testVar() {
// functions and anonymous functions are supported.
  print()
}

'abc'.at(2) // Returns byte value at index.
'some_string'.slice(start, end) // slices string
'test'.indexOf('te') === 0

// No == or !=, only === and !==.

let f = ffi('int foo(int)'); // import c function

ESP8266 Getting started copy

By Iain Nash

ESP8266 Getting started copy

  • 235
Loading comments...

More from Iain Nash