Intel Edison as Azure IoTHub gateway

Intel Edison as Azure IoTHub gateway © LGPL

Using Intel Edison as gateway and redirecting data from Arduimo MKR1000 to Microsoft Azure IoTHub.

  • 1,563 views
  • 0 comments
  • 17 respects

Components and supplies

Apps and online services

About this project

Background

Workshop project during Intel Edison/Microsoft Azure meetup.

Prerequisites

To make this project please prepare :

  • Microsoft Azure IoT Hub (link)
  • Arduino web editor account (link)

Description

Arduino MKR1000 send JSON message with data to Intel Edison using UDP protocol. On receving event Intel Edison resend message to Azure IoTHub. For developing Arduino MKR1000 sketch we were using new Arduino web editor! NodeJS app for Intel edison has been developed by Microsoft Visual Studio Code.

Screenshots

-

Code

main.jsJavaScript
Edison gateway code
"use strict";


var jsonDevice = { "DeviceId": "<YOUR-DEVICE-ID>","DeviceKey" : "<YOUR-DEVICE-KEY>"};
var deviceFromConnectionString = require('azure-iot-device-amqp').clientFromConnectionString;

var Message = require('azure-iot-device').Message;
var deviceConnectionString = '<YOUR-IOTHUB-HOST-NAME>;DeviceId='+jsonDevice.DeviceId+';SharedAccessKey='+jsonDevice.DeviceKey;
var client = deviceFromConnectionString(deviceConnectionString);

var azureConnected = false;

var connectCallback = function (err) {
  if (err) {
    console.log('Could not connect: ' + err);
  } else {
    console.log('Client connected');
      azureConnected = true;
  }
};

client.open(connectCallback);

function sendObservation(name, value, on){
    console.log("Sending observation: ");
}


// listen for UDP message from local agent
var dgram = require("dgram");

var server = dgram.createSocket("udp4");

server.on("error", function (err) {
  console.log("server error:\n" + err.stack);
  server.close();
});

server.on("message", function (msg, rinfo) {
  console.log("server got: " + msg + " from " +
  rinfo.address + ":" + rinfo.port);
    
    if (!azureConnected) 
    { 
        return 
    }
    
    var message = new Message(msg);
    console.log("Sending message: " + message.getData());
    client.sendEvent(message, function printResult(err, res) {
        if (err) {
            console.log(' error: ' + err.toString());
        }
        if (res) {
            console.log(' status: ' + res.constructor.name);
        }
    });
    
});

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " +
      address.address + ":" + address.port);
});

var port     = 41235;

server.bind(port);
Arduino.inoArduino
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
#include <DHT.h>

int status = WL_IDLE_STATUS;
char ssid[] = "TP-LINK"; //  your network SSID (name)
char pass[] = "Samba2015";    // your network password (use for WPA, or use as key for WEP)
char buf[200];

unsigned int localPort = 3030;      // local port to listen on

IPAddress edisonServer(10,10,0,82);

char ReplyBuffer2[] = "acknowledged";       // a string to send back

WiFiUDP Udp;
#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  //while (!Serial) {
    //; // wait for serial port to connect. Needed for native USB port only
  //}
  
  dht.begin();
    
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid,pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);
}

void loop() {

  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
StaticJsonBuffer<200> jsonBuffer; // reserve spot in memory

   JsonObject& root = jsonBuffer.createObject(); // create root objects
     root["sdid"] = "<YOUR DEVICE ID>"; // FIX 
     root["type"] = "message";

   JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
     dataPair["temp"] = t;  
     dataPair["humid"] = h;

   root.printTo(buf, sizeof(buf)); // JSON-print to buffer
   // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(edisonServer, 41235);
    Udp.write(buf);
    Udp.endPacket();
    
    
    delay(1*60*1000);
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Comments

Similar projects you might like

Azure Stream Analytics saving lives!

Project tutorial by Asad Zia

  • 6,589 views
  • 3 comments
  • 28 respects

A fall detection system based on Arduino, Windows and Azure

Project tutorial by Jiong Shi

  • 21,612 views
  • 7 comments
  • 46 respects

GPS Tracking Using Helium, Azure IoT Hub, and Power BI

Project tutorial by Team Helium

  • 11,749 views
  • 17 comments
  • 34 respects

SmartQ Notification with Azure IOT Hub and Virtual Shields

Project showcase by vincent wong

  • 1,570 views
  • 0 comments
  • 6 respects

GPS Datalogger, Spatial Analysis, and Azure IoT Hub.

Project tutorial by Shawn Cruise

  • 17,853 views
  • 4 comments
  • 66 respects
Add projectSign up / Login