├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── fs └── init.js └── mos.yml /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: [ linux ] 2 | dist: xenial 3 | sudo: enabled 4 | language: c 5 | 6 | # Install mos tool 7 | addons: 8 | apt: 9 | sources: 10 | - sourceline: "ppa:mongoose-os/mos" 11 | packages: 12 | - mos-latest 13 | 14 | script: 15 | - mos version 16 | - mos build --local --clean --platform esp8266 17 | - mos build --local --clean --platform esp32 18 | - mos build --local --clean --platform cc3220 19 | - mos build --local --clean --platform stm32 --board B-L475E-IOT01A 20 | - mos build --local --clean --platform stm32 --board DISCO-F746NG 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Cesanta Software Limited 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JS-enabled demo Mongoose OS firmware 2 | 3 | This is the JS demo Mongoose OS app. It gets installed by default at 4 | [Mongoose OS installation step](https://mongoose-os.com/docs/). It has 5 | a lot of functionality enabled - cloud integrations, JavaScript engine, etc. 6 | Its main purpose is to demonstrate the capabilities of Mongoose OS. 7 | -------------------------------------------------------------------------------- /fs/init.js: -------------------------------------------------------------------------------- 1 | load('api_aws.js'); 2 | load('api_azure.js'); 3 | load('api_config.js'); 4 | load('api_dash.js'); 5 | load('api_events.js'); 6 | load('api_gcp.js'); 7 | load('api_gpio.js'); 8 | load('api_mqtt.js'); 9 | load('api_shadow.js'); 10 | load('api_timer.js'); 11 | load('api_sys.js'); 12 | load('api_watson.js'); 13 | 14 | let btn = Cfg.get('board.btn1.pin'); // Built-in button GPIO 15 | let led = Cfg.get('board.led1.pin'); // Built-in LED GPIO number 16 | let onhi = Cfg.get('board.led1.active_high'); // LED on when high? 17 | let state = {on: false, btnCount: 0, uptime: 0}; // Device state 18 | let online = false; // Connected to the cloud? 19 | 20 | let setLED = function(on) { 21 | let level = onhi ? on : !on; 22 | GPIO.write(led, level); 23 | print('LED on ->', on); 24 | }; 25 | 26 | GPIO.set_mode(led, GPIO.MODE_OUTPUT); 27 | setLED(state.on); 28 | 29 | let reportState = function() { 30 | Shadow.update(0, state); 31 | }; 32 | 33 | // Update state every second, and report to cloud if online 34 | Timer.set(1000, Timer.REPEAT, function() { 35 | state.uptime = Sys.uptime(); 36 | state.ram_free = Sys.free_ram(); 37 | print('online:', online, JSON.stringify(state)); 38 | if (online) reportState(); 39 | }, null); 40 | 41 | // Set up Shadow handler to synchronise device state with the shadow state 42 | Shadow.addHandler(function(event, obj) { 43 | if (event === 'UPDATE_DELTA') { 44 | print('GOT DELTA:', JSON.stringify(obj)); 45 | for (let key in obj) { // Iterate over all keys in delta 46 | if (key === 'on') { // We know about the 'on' key. Handle it! 47 | state.on = obj.on; // Synchronise the state 48 | setLED(state.on); // according to the delta 49 | } else if (key === 'reboot') { 50 | state.reboot = obj.reboot; // Reboot button clicked: that 51 | Timer.set(750, 0, function() { // incremented 'reboot' counter 52 | Sys.reboot(500); // Sync and schedule a reboot 53 | }, null); 54 | } 55 | } 56 | reportState(); // Report our new state, hopefully clearing delta 57 | } 58 | }); 59 | 60 | if (btn >= 0) { 61 | let btnCount = 0; 62 | let btnPull, btnEdge; 63 | if (Cfg.get('board.btn1.pull_up') ? GPIO.PULL_UP : GPIO.PULL_DOWN) { 64 | btnPull = GPIO.PULL_UP; 65 | btnEdge = GPIO.INT_EDGE_NEG; 66 | } else { 67 | btnPull = GPIO.PULL_DOWN; 68 | btnEdge = GPIO.INT_EDGE_POS; 69 | } 70 | GPIO.set_button_handler(btn, btnPull, btnEdge, 20, function() { 71 | state.btnCount++; 72 | let message = JSON.stringify(state); 73 | let sendMQTT = true; 74 | if (Azure.isConnected()) { 75 | print('== Sending Azure D2C message:', message); 76 | Azure.sendD2CMsg('', message); 77 | sendMQTT = false; 78 | } 79 | if (GCP.isConnected()) { 80 | print('== Sending GCP event:', message); 81 | GCP.sendEvent(message); 82 | sendMQTT = false; 83 | } 84 | if (Watson.isConnected()) { 85 | print('== Sending Watson event:', message); 86 | Watson.sendEventJSON('ev', {d: state}); 87 | sendMQTT = false; 88 | } 89 | if (Dash.isConnected()) { 90 | print('== Click!'); 91 | // TODO: Maybe do something else? 92 | sendMQTT = false; 93 | } 94 | // AWS is handled as plain MQTT since it allows arbitrary topics. 95 | if (AWS.isConnected() || (MQTT.isConnected() && sendMQTT)) { 96 | let topic = 'devices/' + Cfg.get('device.id') + '/events'; 97 | print('== Publishing to ' + topic + ':', message); 98 | MQTT.pub(topic, message, 0 /* QoS */); 99 | } else if (sendMQTT) { 100 | print('== Not connected!'); 101 | } 102 | }, null); 103 | } 104 | 105 | Event.on(Event.CLOUD_CONNECTED, function() { 106 | online = true; 107 | Shadow.update(0, {ram_total: Sys.total_ram()}); 108 | }, null); 109 | 110 | Event.on(Event.CLOUD_DISCONNECTED, function() { 111 | online = false; 112 | }, null); 113 | -------------------------------------------------------------------------------- /mos.yml: -------------------------------------------------------------------------------- 1 | author: mongoose-os 2 | description: A JS-enabled demo Mongoose OS firmware 3 | # arch: PLATFORM 4 | version: 1.0 5 | manifest_version: 2017-05-18 6 | libs_version: ${mos.version} 7 | modules_version: ${mos.version} 8 | mongoose_os_version: ${mos.version} 9 | 10 | config_schema: 11 | - ["mqtt.server", "mqtt.eclipseprojects.io:1883"] 12 | - ["i2c.enable", true] 13 | 14 | tags: 15 | - js 16 | 17 | filesystem: 18 | - fs 19 | 20 | libs: 21 | - location: https://github.com/mongoose-os-libs/boards 22 | - location: https://github.com/mongoose-os-libs/js-demo-bundle 23 | 24 | conds: 25 | - when: mos.platform == "esp32" 26 | apply: 27 | build_vars: 28 | # Icrease app szie for ESP32 29 | APP_SLOT_SIZE: 0x190000 30 | --------------------------------------------------------------------------------