Intel Edison as Azure IoTHub gateway

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

Intel® IoT Gateways Get Started

Things used in this project

Hardware components

Intel Edison
Intel Edison
× 1
Arduino MKR1000
Arduino MKR1000
× 1

Software apps and online services

Microsoft Azure
Microsoft Azure
Arduino Web Editor
Arduino Web Editor

Story

Code

main.js

JavaScript

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.ino

Arduino

#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");
}

Source : Intel Edison as Azure IoTHub gateway


About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation and engineering. My writing style is clear and simple, and I am skilled in using infographics and diagrams. I am a great researcher and am able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter
Scroll to Top