Hardware components | ||||||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
|
Background
-
Read moreWorkshop project during Intel Edison/Microsoft Azure meetup.
PrerequisitesTo make this project please prepare :
- Microsoft Azure IoT Hub (link)
- Arduino web editor account (link)
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"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);
#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");
}
Zoran Roncevic
19 projects • 127 followers
Hackster Live ambassador in Serbia. Organizer of Maker NS community.
Comments