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

Theremino HAL 7.2 – Hardware Abstraction Layer.
Easy
  • 47
  • 3

Full instructions

The HAL connects the hardware of InOut with high-level software via USB and Slots.

Theremino, Windows 10 and HAL. The link with the real world
Easy
  • 130
  • 3

Full instructions

Windows 10 and the HAL (Hardware Abstraction Layer). How to communicate with sensors and actuators in the most easy way.

Read the signal of the AB quadrature encoder with Theremino.
Easy
  • 250
  • 3

Full instructions

The simplest way to read A B quadrature encoder using Theremino Master.

Theremino SlotViewer, the simplest way to manage slots.
Easy
  • 137
  • 4

Full instructions

Theremino SlotViewer is the very simple way to manage the slots, read the value and modify in real time the value of each slot.

Eddystone beacons with the micro:bit
Easy
  • 171
  • 2

Protip

Experiment with the Physical Web by turning your micro:bit into an Eddystone beacon.

Simple Light LED using Theremino Master
Easy
  • 312
  • 4

The simplest way to switch on a LED or dimming it.

Add projectSign up / Login
Respect project