Things used in this project

Hardware components:
Pi 3 02
Raspberry Pi 3 Model B
×1
12002 04
Breadboard (generic)
×1
09590 01
LED (generic)
×1
Mfr 25fbf52 221r sml
Resistor 221 ohm
×1
Software apps and online services:
W9gt7hzo
Microsoft Azure

Schematics

Circuit diagram
Connect LED, wires and resistor
Img 1273 ytk7lmxykv

Code

Blinking LED Node JSJavaScript
Blinking LED Node JS
/*
* IoT Hub Raspberry Pi NodeJS 
*/
'use strict';

var wpi = require('wiring-pi');

// GPIO pin of the LED
var CONFIG_PIN = 7;
// Blink interval in ms
var INTERVAL = 2000;
// Total blink times
var MAX_BLINK_TIMES = 20;

wpi.setup('wpi');
wpi.pinMode(CONFIG_PIN, wpi.OUTPUT);

var blinkTimes = 0;
/**
 * Blink LED and log information to console.
 */
function blinkLED() {
  // Terminate process after blinking LED for max allowed times
  if (blinkTimes >= MAX_BLINK_TIMES) {
    process.exit(0);
  }

  blinkTimes++;
  console.log("[Device] #" + blinkTimes + " Blink LED \n");

  // Light up LED for 100 ms
  wpi.digitalWrite(CONFIG_PIN, 1);
  setTimeout(function () {
    wpi.digitalWrite(CONFIG_PIN, 0);
  }, 100);
}

// Blink LED every other INTERVAL time
setInterval(blinkLED, INTERVAL);
Send Msg to Azure IoT JavaScript
Send Message to IoT
App.js
/*
* IoT Hub Raspberry Pi NodeJS - 
*/
'use strict';

var wpi = require('wiring-pi');
var Message = require('azure-iot-device').Message;
// Use AMQP client to communicate with IoT Hub
var clientFromConnectionString = require('azure-iot-device-amqp').clientFromConnectionString;

// Get device id from IoT device connection string
function getDeviceId(connectionString) {
  var elements = connectionString.split(';');
  var dict = {};
  for (var i = 0; i < elements.length; i++) {
    var kvp = elements[i].split('=');
    dict[kvp[0]] = kvp[1];
  }
  return dict.DeviceId;
}

// Read device connection string from command line arguments
var iotDeviceConnectionString = process.argv[2];
var deviceId = getDeviceId(iotDeviceConnectionString);

// GPIO pin of the LED
var CONFIG_PIN = 7;
// Blink interval in ms
var INTERVAL = 2000;
// Total messages to be sent
var MAX_MESSAGE_COUNT = 20;
var sentMessageCount = 0;

wpi.setup('wpi');
wpi.pinMode(CONFIG_PIN, wpi.OUTPUT);

/**
 * Start sending messages after getting connected to IoT Hub.
 * If there is any error, log the error message to console.
 * @param {string}  err - connection error
 */
function connectCallback(err) {
  if (err) {
    console.log('[Device] Could not connect: ' + err);
  } else {
    console.log('[Device] Client connected\n');
    // Wait for 5 seconds so that host machine gets connected to IoT Hub for receiving message.
    setTimeout(sendMessage, 5000);
  }
}

/**
 * Blink LED.
 */
function blinkLED() {
  // Light up LED for 100 ms
  wpi.digitalWrite(CONFIG_PIN, 1);
  setTimeout(function () {
    wpi.digitalWrite(CONFIG_PIN, 0);
  }, 100);
}

/**
 * Construct device-to-cloud message and send it to IoT Hub.
 */
function sendMessage() {
  sentMessageCount++;
  var message = new Message(JSON.stringify({ deviceId: deviceId, messageId: sentMessageCount }));
  console.log("[Device] Sending message #" + sentMessageCount + ": " + message.getData());
  client.sendEvent(message, sendMessageCallback);
}

/**
 * Blink LED after message is sent out successfully, otherwise log the error message to console.
 * If sent message count is less than max message count allowed, schedule to send another message.
 * Else, exit process after several seconds.
 * @param {object}  err - sending message error
 */
function sendMessageCallback(err) {
  if (err) {
    console.log('[Device] Message error: ' + err.toString());
  } else {
    // Blink once after successfully sending one message.
    blinkLED();
  }

  if (sentMessageCount < MAX_MESSAGE_COUNT) {
    setTimeout(sendMessage, INTERVAL);
  } else {
    // Wait 5 more seconds to exit so that Azure function has the chance to process sent messages.
    setTimeout(function () {
      process.exit();
    }, 5000);
  }
}

// Construct IoT Hub device client and connect to IoT Hub.
var client = clientFromConnectionString(iotDeviceConnectionString);
client.open(connectCallback);

Credits

Replications

Did you replicate this project? Share it!

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Winforms+Lattepanda=Control Everything With Your Mouse!
Easy
  • 1,015
  • 6

Protip

If you use other things instead of the LED, then you can use your mouse to control everything!!

One Small Step For a Yedi, One Giant Leap for Mankind
Easy
  • 165
  • 4

Work in progress

Counting your steps with the Calliope Mini. Hearing the Star Wars theme is always a huge motivator.

Cortana Skills and Bot Framework on Raspberry Pi
Easy
  • 369
  • 2

Protip

Using Bot Framework on Raspberry Pi, using Cortana Skills to Echo sent message.

Microsoft Workshop Setup Guide
Easy
  • 953
  • 9

Protip

A collection of screencasts for setting up your laptop for the Hackster/Microsoft IoT Roadshow events.

Sorting Hat: Circuit Playground Express Starter Project
Easy
  • 85
  • 2

Protip

Which Hogwarts House do you belong in? A quick first project for this beginner-friendly board from Adafruit.

The Factory at Exhibition - 4.0 and beyond - [Part 2]
Easy
  • 737
  • 6

Protip

PowerBerry/POWER-KI on 9 Raspberry PI3 and 10 LattePanda with Win10 to make a true 4.0 Control System for a factory that worked at BIE2017.

Add projectSign up / Login