From be56a5f7914823f699c04b2edd67aa12d9110736 Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Fri, 16 May 2025 14:37:44 -0600 Subject: [PATCH] Adding mqtt control --- index.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/index.js b/index.js index 0db1fae..5aeb1c1 100644 --- a/index.js +++ b/index.js @@ -3,10 +3,18 @@ const schedule = require('node-schedule') const fs = require('fs') const sharp = require('sharp') const inky = require('@aeroniemi/inky') +const mqtt = require('mqtt') const frame = new inky.Impression73() const folder = './gallery/' +// Connect to MQTT +const mqttclient = mqtt.connect('mqtt://mqtt.srv.jnf.me', { + clientId: 'pictureframe_' + Math.random().toString(16).substr(2, 8), + username: 'pictureframe', + password: 'pictureframe' +}) + var ticker = false var filelist = false @@ -57,6 +65,31 @@ function displayImage(filename) { }) } +function displayWifi() { + console.log('Displaying wifi connection information for 10 minutes') + clearTimeout(ticker) + frame.display_png('img/wifi.png') + frame.show() + ticker = setTimeout(displayAlbum, 600000) +} + +// Subscribe to our MQTT topic... +mqttclient.on('connect', () => { + mqttclient.subscribe('pictureframe/command') +}) + +// Listen for MQTT command messages +mqttclient.on('message', (topic, message) => { + message = message.toString() + console.log('MQTT command received: ' + message) + + if (message == 'wifi') displayWifi() + if (message == 'reset') { + clearTimeout(ticker) + downloadImgs() + } +}) + // We start by downloading images. Other functions will be called if this is successful... downloadImgs()