load('api_config.js'); load('api_gpio.js'); load('api_mqtt.js'); load('api_sys.js'); load('api_timer.js'); load('api_arduino_ssd1306.js'); // Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO let led = ffi('int get_led_gpio_pin()')(); let getInfo = function() { return JSON.stringify({total_ram: Sys.total_ram(), free_ram: Sys.free_ram()}); }; let i = 0; // Initialize Adafruit_SSD1306 library (I2C) let d = Adafruit_SSD1306.create_i2c(4 /* RST GPIO */, Adafruit_SSD1306.RES_128_64); // Initialize the display. d.begin(Adafruit_SSD1306.SWITCHCAPVCC, 0x3D, true /* reset */); d.display(); let showStr = function(d, str) { d.clearDisplay(); d.setTextSize(2); d.setTextColor(Adafruit_SSD1306.WHITE); d.setCursor(d.width() / 4, d.height() / 4); d.write(str); d.display(); }; Timer.set(1000 /* milliseconds */, true /* repeat */, function() { showStr(d, "i = " + JSON.stringify(i)); print("i = ", i); i++; }, null); // Blink built-in LED every second GPIO.set_mode(led, GPIO.MODE_OUTPUT); Timer.set(1000 /* 1 sec */, true /* repeat */, function() { let value = GPIO.toggle(led); print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo()); }, null); // 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'); let message = getInfo(); let ok = MQTT.pub(topic, message, 1); print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message); }, null); print('************************* Starting fastclock stuff *********************'); load('api_net.js'); load('api_rpc.js'); load('api_fastclock.js'); print('loaded api modules'); Fastclock.set_update_handler(function (userdata) { print('Fastclock update received! ', Fastclock.isFastclock() ? 'FASTCLOCK' : 'REAL CLOCK'); print('Name:', Fastclock.getName(), ' from ', Fastclock.getServerIp(), Fastclock.getServerPort()); print('Clock:', Fastclock.getHours(), Fastclock.getMinutes(), Fastclock.getSeconds(), ' at day ', Fastclock.getSpeed(), Fastclock.isActive() ? '(running)' : '(--stopped--)'); print('Message:', Fastclock.getMessage()); }, "test"); let port=2000; let lastMessage = ""; let udpConnection = null; let udpConnectString = "udp://239.50.50.20:2000"; print('initialized globals'); Timer.set(10000, false, function() { print('*** Binding to multicast port'); Net.bind(':2000', function(conn, ev, ev_data) { print('UDP Event', ev); if (ev !== Net.EV_RECV) return; print('UDP Received:', ev_data); }, true); }, null); print('UDP server is listening on port ', port); RPC.addHandler('Clock.Update', function(args) { lastMessage = args.message; // JSON.stringify(args); print("Received message: ", lastMessage); return true; }); print('Added RPC handler for Clock.Update'); /*Timer.set(300, true, function() { if (lastMessage !== "") { print("Last Message: ", lastMessage); lastMessage = ""; } }, null);*/ print('Last-Message timer started'); RPC.addHandler('Clock.Broadcast', function(args) { Net.connect(udpConnectString, function (conn, event, data, user_data) { if (event === Net.EV_CONNECT) { Net.send(conn, user_data.message); Net.close(conn); lastMessage = 'SENT: ' + user_data.message; // JSON.stringify(args); print("Last Message: ", lastMessage); } }, args); return true; }); print('Broadcast/Multicast RPC method added');