Skip to content

Instantly share code, notes, and snippets.

var azureCognitiveServicesAuthEndpoint = 'https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issueToken';
var azureCognitiveServicesSubscriptionKey = "[...]";
[...]
// Azure Cognitive Services interface
var _cachedAzureToken = null;
var _cachedAzureTokenExpires = null;
function azureGetAuthToken(callback) {
if (_cachedAzureToken && _cachedAzureTokenExpires) {
var DeviceClient = require('azure-iot-device').Client
var DeviceProtocol = require('azure-iot-device-amqp').AmqpWs
[...]
var connectionString = "HostName=.....";
[...]
var client = DeviceClient.fromConnectionString(connectionString, DeviceProtocol);
@jamesbulpin
jamesbulpin / leds.js
Created June 16, 2018 10:58
Demo program for connecting an LED-illuminated Minifig to CheerLights
var SerialPort = require("serialport");
var tinycolor2 = require("tinycolor2");
var request = require('request');
var portdev = "/dev/ttyACM0";
var port = new SerialPort(portdev, {
baudRate: 9600,
});
port.on('error', function(err) {
@jamesbulpin
jamesbulpin / xmas.js
Created December 19, 2017 17:34
Node.js program to update LEDs via a serial-over-USB connected Arduino for my Christmas Jumper project
var SerialPort = require("serialport");
var tinycolor2 = require("tinycolor2");
var request = require('request');
var portdev = "/dev/ttyACM0";
var port = new SerialPort(portdev, {
baudRate: 9600,
});
port.on('error', function(err) {
@jamesbulpin
jamesbulpin / index.js
Created December 17, 2017 12:08
Azure Function code for a demo Alexa Skill handler for the Christmas Jumper project
// Alexa skill handler for the "Citrix" demo skill.
//
// This runs as an Azure Function and must have the NPM libraries installed into the
// Function App image: ensure that package.json is populated with the required
// dependencies and follow the instructions at
// https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#node-version-and-package-management
//
// Example package.json:
// {
// "dependencies": {
@jamesbulpin
jamesbulpin / xmasjumper.ino
Created December 17, 2017 10:30
Arduino sketch to drive WS281x LEDs as part of my Christmas jumper project
#include <Adafruit_NeoPixel.h>
#define WS2811_PIN 6
#define WS2811_LED_COUNT 20
// Set to true to flash the LEDs on and off at approx 1Hz
boolean flash = false;
// The target color for the LED string, indexed by LED number
int target_r[WS2811_LED_COUNT];
@jamesbulpin
jamesbulpin / xmas.js
Created December 15, 2017 20:28
Simple Azure IoT client to relay messages to an Arduino via serial-over-USB
var SerialPort = require("serialport");
var clientFromConnectionString = require('azure-iot-device-amqp').clientFromConnectionString;
var tinycolor2 = require("tinycolor2");
var connectionString = "<IoT Hub connection string>";
var portdev = "/dev/ttyACM0";
var port = new SerialPort(portdev, {
baudRate: 9600,
});
@jamesbulpin
jamesbulpin / index.js
Created November 23, 2017 23:09
Azure Function to handle device-to-cloud messages
var request = require('request');
function jrbHomeAutomation(context, event) {
context.log("jrbHomeAutomation event " + JSON.stringify(event));
if ((event.topic) && (event.message)) {
switch (event.topic) {
case "Alert":
request({
url: "<logic app trigger URL>",
{
"dependencies": {
"azure-iothub": "^1.1.17",
"azure-iot-common": "^1.2.0"
}
}
@jamesbulpin
jamesbulpin / index.js
Created November 23, 2017 22:48
Azure Function to publish a message to my local MQTT broker via Azure IoT Hub
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
function printResultFor(op) {
return function printResult(context, err, res) {
context.log('printResult for ' + op);
if (err) context.log(op + ' error: ' + err.toString());
if (res) context.log(op + ' status: ' + res.constructor.name);
if (err) {
context.res = {