2017-07-13 04:18:04 +00:00
|
|
|
load('api_config.js');
|
|
|
|
load('api_gpio.js');
|
|
|
|
load('api_mqtt.js');
|
|
|
|
load('api_sys.js');
|
2017-07-13 09:31:50 +00:00
|
|
|
load('api_rpc.js');
|
2017-07-13 04:18:04 +00:00
|
|
|
load('api_timer.js');
|
2017-07-13 09:31:50 +00:00
|
|
|
load("animateLights.js");
|
2017-07-13 04:18:04 +00:00
|
|
|
|
|
|
|
// Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO
|
2017-07-13 09:31:50 +00:00
|
|
|
let onBoardLed = ffi('int get_led_gpio_pin()')();
|
2017-07-13 04:18:04 +00:00
|
|
|
|
|
|
|
let getInfo = function() {
|
|
|
|
return JSON.stringify({total_ram: Sys.total_ram(), free_ram: Sys.free_ram()});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
|
|
|
|
GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
|
|
|
|
let topic = '/devices/' + Cfg.get('device.id') + '/events';
|
|
|
|
let message = getInfo();
|
|
|
|
let ok = MQTT.pub(topic, message, 1);
|
|
|
|
print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message);
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
2017-07-13 09:31:50 +00:00
|
|
|
// Blink built-in LED every second
|
|
|
|
GPIO.set_mode(onBoardLed, GPIO.MODE_OUTPUT);
|
2017-07-13 04:18:04 +00:00
|
|
|
|
2017-07-13 09:31:50 +00:00
|
|
|
// Tick-Tock generation
|
|
|
|
Timer.set(1000 /* 1 sec */, true /* repeat */, function() {
|
|
|
|
let value = GPIO.toggle(onBoardLed);
|
|
|
|
print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());
|
|
|
|
}, null);
|