Mongoose OS IoT Starter Kit is
recommended by Google IoT Core portal
and provides you with a quick way to start IoT development on microcontrollers.
This quick start guide and reference materials include the following:
mos
tool on your computerIn this tutorial, we'll use the DHT22 temperature/humidity sensor and send measurements to the Google IoT Core.
mos
Web UI, open the
init.js
fileload('api_config.js');
load('api_dht.js');
load('api_mqtt.js');
load('api_timer.js');
let topic = '/devices/' + Cfg.get('device.id') + '/events';
let dht = DHT.create(21, DHT.DHT22);
Timer.set(5000, true, function() {
let msg = JSON.stringify({ t: dht.getTemp(), h: dht.getHumidity() });
let ok = MQTT.pub(topic, msg, 1);
print(ok, msg);
}, null);
gcloud beta pubsub subscriptions pull --auto-ack iot-subscription --max-messages=999
+---------------+------------+-----------------+-----------------------+
| DATA | MESSAGE_ID | ATTRIBUTES |
+----------------------------+-----------------+-----------------------+
| {"h":42.599998,"t":24.23} | 144935373963880 | deviceId=dev1 dev.... |
| {"h":42.599998,"t":24.22} | 144937730266979 | deviceId=dev1 dev.... |
...
In this tutorial, we'll send a message to Google IoT Core on a button press.
mos
Web UI, open the
init.js
fileload('api_config.js');
load('api_gpio.js');
load('api_timer.js');
load('api_mqtt.js');
let topic = '/devices/' + Cfg.get('device.id') + '/events';
let pin = 21;
GPIO.set_mode(pin, GPIO.MODE_INPUT);
GPIO.set_pull(pin, GPIO.PULL_DOWN);
GPIO.set_button_handler(pin, GPIO.PULL_DOWN, GPIO.INT_EDGE_NEG, 200, function() {
let msg = JSON.stringify({ time: Timer.now() });
let ok = MQTT.pub(topic, msg, 1);
print(ok, msg);
}, null);
sub1
PubSub subscription programmatically, catch those messages and perform any custom
actionIn this tutorial, we'll demonstrate how to control the device remotely by sending commands from the cloud to switch an LED on/off.
init.js
, paste this code, click "Save + Reboot":
load('api_config.js');
load('api_gpio.js');
load('api_timer.js');
load('api_mqtt.js');
let topic = '/devices/' + Cfg.get('device.id') + '/config';
let pin = 21;
GPIO.set_mode(pin, GPIO.MODE_OUTPUT);
MQTT.sub(topic, function(conn, top, msg) {
print('Got config update:', msg.slice(0, 100));
let obj = JSON.parse(msg);
if (obj) GPIO.write(pin, obj.on);
}, null);
gcloud beta iot devices configs update --device $DEVICE_ID \
--project $PROJECT_ID --region europe-west1 --registry registry1 \
--config-data '{"on": true}'
gcloud beta iot devices configs update --device $DEVICE_ID \
--project $PROJECT_ID --region europe-west1 --registry registry1 \
--config-data '{"on": false}'
In this tutorial, we'll use a door sensor to generate messages and send them to Google IoT Core.
init.js
, paste this code, click "Save + Reboot":
load('api_config.js');
load('api_gpio.js');
load('api_mqtt.js');
let pin = 21, state = 0;
let topic = '/devices/' + Cfg.get('device.id') + '/events';
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_POS, 200, function() {
let value = GPIO.read(pin);
let msg = JSON.stringify({ state: value ? 'open' : 'closed' });
if (value !== state) {
state = value;
let ok = MQTT.pub(topic, msg, 1, true);
print(ok, topic, msg);
}
}, null);
[Aug 21 16:08:29.942] 1 /devices/esp32_0C1E40/events {"state":"open"}
[Aug 21 16:08:40.971] 1 /devices/esp32_0C1E40/events {"state":"closed"}
init.js
, paste this code, click "Save + Reboot":
load('api_config.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_timer.js');
let pin = 21;
let topic = '/devices/' + Cfg.get('device.id') + '/events';
GPIO.set_button_handler(pin, GPIO.PULL_DOWN, GPIO.INT_EDGE_ANY, 200, function() {
let msg = JSON.stringify({ motion: GPIO.read(pin) });
let ok = MQTT.pub(topic, msg, 1);
print(ok, msg);
}, null);
3. Note when a motion sensor detects a motion, it sets the signal pin (in our case, GPIO 21) to
the high voltage
state (1), then keeps it high for some time, then drops to low voltage (0). We detect both events:
[Aug 25 13:35:38.364] 1 {"motion":0}
[Aug 25 13:35:42.558] 1 {"motion":1}
init.js
, paste this code, click "Save + Reboot":
load('api_pwm.js');
load('api_rpc.js');
RPC.addHandler('PWM', function(args) {
print('Got RPC:', JSON.stringify(args));
PWM.set(args.pin, args.freq, args.duty);
return true;
});
3. This installs a Remote Procedure Call (RPC) handler which you can trigger.
{ "pin": 21, "freq": 50, "duty": 7 }
and click on the "Call RPC service" button:
[Aug 25 15:22:27.967] Got RPC: {"duty":7,"freq":50,"pin":21}
5. Experiment with
different values of
duty
e.g. from 2 to 15, to rotate the servo at different angles