Created
January 18, 2018 18:00
-
-
Save JayGoldberg/3c8bb253f14eb85b786b66d7ac9133ce to your computer and use it in GitHub Desktop.
Turn a neopixel strip on and off via MQTT, in Espruino embedded Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function mqttConnect(connVar) { | |
| var mqtt = require("MQTT"); | |
| const MQTT_OPTIONS = { | |
| host: 'mqtt.host.net', | |
| keep_alive: 15, | |
| client_id: "espruino", | |
| username: "mqtt_username", | |
| password: "mqtt_password", | |
| path: '/doordash-temp' | |
| }; | |
| if (!connVar) { | |
| var mqttConnection = mqtt.create(MQTT_OPTIONS.host, MQTT_OPTIONS); | |
| mqttConnect(mqttConnection); | |
| return mqttConnection; | |
| } | |
| if (connVar.connected) return connVar; | |
| connVar.on('error', function(err) { | |
| console.log(err); | |
| }); | |
| connVar.on('connected', function() { | |
| console.log('MQTT connected, setting up subscriptions and polling'); | |
| setTimeout(function() { | |
| connVar.subscribe(MQTT_OPTIONS.path, null, function() { | |
| console.log('subscribed to topic ' + MQTT_OPTIONS.path); | |
| }); | |
| }, 1000); | |
| }); | |
| connVar.on('publish', function(mqttMessage) { | |
| console.log(mqttMessage.message); | |
| ledMsg(mqttMessage.message); | |
| }); | |
| /*connVar.on('message', function(mqttMessage) { // echo the channel name | |
| console.log('message: ' + mqttMessage); | |
| });*/ | |
| connVar.on('close', function(err) { | |
| console.log(err); | |
| }); | |
| connVar.on('end', function() { | |
| console.log('the end'); | |
| }); | |
| console.log('connecting'); | |
| connVar.connect(); | |
| } | |
| function onInit() { | |
| ledMsg('off'); | |
| var wifi = require('Wifi'); | |
| const WIFI_OPTIONS = { | |
| ssid: 'your_SSID', | |
| password: { password : 'password' }, | |
| hostname: 'espruino-flashwork' | |
| }; | |
| console.log('WiFi connecting'); | |
| wifi.setHostname(WIFI_OPTIONS.hostname); | |
| wifi.stopAP(); | |
| wifi.connect(WIFI_OPTIONS.ssid, WIFI_OPTIONS.password, function(ap){ | |
| console.log('Wifi connected'); | |
| }); | |
| wifi.on('connected', function() { | |
| console.log(wifi.getIP()); | |
| setInterval(function() { | |
| var connVar = mqttConnect(); // need to feed previous object | |
| }, 30 * 1000); | |
| }); | |
| wifi.on('disconnected', function() { | |
| console.log('Disconnected from WiFi'); | |
| //connVar.disconnect(); | |
| }); | |
| } | |
| function ledMsg(msg) { | |
| var led_count = 8; | |
| var rgb = new Uint8ClampedArray(led_count * 3); | |
| var neoPin = NodeMCU.D4; | |
| var pos = 0; | |
| function getPattern() { | |
| pos++; | |
| for (var i=0;i<rgb.length;) { | |
| rgb[i++] = (1 + Math.sin((i+pos)*0.1324)) * 127; | |
| rgb[i++] = (1 + Math.sin((i+pos)*0.1654)) * 127; | |
| rgb[i++] = (1 + Math.sin((i+pos)*0.1)) * 127; | |
| } | |
| return rgb; | |
| } | |
| if (msg === 'off') { | |
| clearInterval(interval); | |
| for (var i=0;i<rgb.length;) { | |
| rgb[i++] = 0; | |
| rgb[i++] = 0; | |
| rgb[i++] = 0; | |
| } | |
| require("neopixel").write(neoPin, rgb); | |
| } else { | |
| // Produce an animated rainbow | |
| interval = setInterval(function() { | |
| require("neopixel").write(neoPin, getPattern()); | |
| }, 100); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment