Fork me on GitHub

Tips and tricks

List all available commands and flags

Some mos commands are very specific - for example, commands dealing with cryptochip setup or commands for ESP32 flash encryption. They are not shown in the mos --help output. In order to see all possible commands and all possible flags, use -X and --helpfull options:

mos -X --helpfull
The Mongoose OS command line tool, v. 20170605-165827/master@735cb15a.
  ...

Connecting over serial or network: mos --port option

mos tool connects to the device specified by --port flag, which is set to auto by default. That means, mos auto-detects the serial port for the device. You can specify this value manually. It could be a serial device, e.g. --port COM3 on Windows or --port /dev/ttyUSB0 on Linux.

It is possible to set --port value to be a network endpoint instead of serial port. Device listens for commands on serial, Websocket, and MQTT transports (unless they are disabled). Therefore, --port ws://IP_ADDR/rpc connects to the remote device via Websocket, and --port mqtt://MQTT_SERVER/DEVICE_ID via the MQTT protocol. That gives an ability to use mos tool as a remote device management tool.

Setting default mos options via environment variables

The default values for any mos flag could be overridden via the environment variable MOS_FLAGNAME. For example, to set the default value for --port flag, export MOS_PORT variable - on Mac/Linux, put that into your ~/.profile:

export MOS_PORT=YOUR_SERIAL_PORT  # E.g. /dev/ttyUSB0

Using Mongoose OS and Grove SeedStudio kit

See YouTube playlist with video tutorials.

List all RPC services exported by device

mos call RPC.List
[
  "FS.Umount",
  ...

See what arguments an RPC service expects

mos call RPC.Describe '{"name": "Config.Set"}'
{
  "name": "Config.Set",
  "args_fmt": "{config: %M}"
}

Trigger Over-The-Air update

mos --port ws://DEVICE_IP/rpc call OTA.Update '{"url": "http://MYSITE/fw.zip", "commit_timeout": 100}'
mos --port ws://DEVICE_IP/rpc call OTA.Commit

Also, see YouTube video.

MQTT publish and subscribe in JavaScript

load('api_mqtt.js');
load('api_gpio.js');
let pin = 0, topic = 'my/topic';
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
  MQTT.pub('my/topic', JSON.stringify({ a: 1, b: 2 }));
}, null);
MQTT.sub('my/topic/#', function(conn, topic, msg) {
  print('Topic:', topic, 'message:', msg);
}, null);

Also, see YouTube video.

Use AWS IoT device shadow in JavaScript

let state = { on: false, counter: 0 };  // device state: shadow metadata
let buttonPin = 0;
GPIO.set_button_handler(buttonPin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
  AWS.Shadow.update(0, {desired: {on: state.on, counter: state.counter + 1}});
}, null);

Also, see YouTube video.

Using ESP32 flash encryption

mos flash esp32
mos flash-read --arch esp32 0x190000 2000 -
mos -X esp32-gen-key flash_encryption_key fe.key \
  --esp32-enable-flash-encryption --dry-run=false
mos flash esp32 --esp32-encryption-key-file fe.key

Also, see YouTube video.

Get MAC address and other system info in JavaScript

The solution is to call Sys.GetInfo RPC service. From your terminal, run:

mos call Sys.GetInfo
[Aug 21 08:29:17.016] {
...
[Aug 21 08:29:17.016]   "mac": "5ECF7F060046",
[Aug 21 08:29:17.016]   "arch": "esp8266",
[Aug 21 08:29:17.016]   "wifi": {
[Aug 21 08:29:17.016]     "sta_ip": "192.168.1.31",
...

You can see e.g. MAC and IP addresses in the output. In order to call the same function in JS, do:

load('api_rpc.js');
RPC.call(RPC.LOCAL, 'Sys.GetInfo', null, function(resp, ud) {
  print('Response:', JSON.stringify(resp));
  print('MAC address:', resp.mac);
}, null);

The device would print to the output:

[Aug 21 08:23:33.162] Response: {... "arch":"esp8266",... } 
[Aug 21 08:23:33.170] MAC address: 5ECF7F060046