/*
* IoT Hub Raspberry Pi NodeJS - Microsoft Sample Code - Copyright (c) 2017 - Licensed MIT
*/
const wpi = require('wiring-pi');
const Client = require('azure-iot-device').Client;
const Message = require('azure-iot-device').Message;
const Protocol = require('azure-iot-device-mqtt').Mqtt;
const BME280 = require('bme280-sensor');
const BME280_OPTION = {
i2cBusNo: 1, // defaults to 1
i2cAddress: BME280.BME280_DEFAULT_I2C_ADDRESS() // defaults to 0x77
};
const connectionString = '******connection string ********';
const LEDPin = 4;
var sendingMessage = false;
var messageId = 0;
var client, sensor;
var blinkLEDTimeout = null;
function getMessageTelem(cb) {
messageId++;
sensor.readSensorData()
.then(function (data) {
cb(JSON.stringify({
temperature: data.temperature_C,
humidity: data.humidity
}), data.temperature_C > 30);
})
.catch(function (err) {
console.error('Failed to read out sensor data: ' + err);
});
}
function getMessageLocation(cb) {
cb(JSON.stringify({
location:{
lat: 27.4698,
lon: 153.0251,
alt:0
}
}));
}
function sendMessage() {
if (!sendingMessage) { return; }
getMessageTelem(function (content, temperatureAlert) {
var message = new Message(content);
message.properties.add('temperatureAlert', temperatureAlert.toString());
console.log('Sending message: ' + content);
client.sendEvent(message, function (err) {
if (err) {
console.error('Failed to send message to Azure IoT Hub');
} else {
blinkLED();
console.log('Message sent to Azure IoT Hub');
}
});
});
getMessageLocation(function (content) {
var message = new Message(content);
console.log('Sending message: ' + content);
client.sendEvent(message, function (err) {
if (err) {
console.error('Failed to send message to Azure IoT Hub');
} else {
blinkLED();
console.log('Message sent to Azure IoT Hub');
}
});
});
}
function onStart(request, response) {
console.log('Try to invoke method start(' + request.payload + ')');
sendingMessage = true;
response.send(200, 'Successully start sending message to cloud', function (err) {
if (err) {
console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
}
});
}
function onStop(request, response) {
console.log('Try to invoke method stop(' + request.payload + ')');
sendingMessage = false;
response.send(200, 'Successully stop sending message to cloud', function (err) {
if (err) {
console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
}
});
}
function receiveMessageCallback(msg) {
blinkLED();
var message = msg.getData().toString('utf-8');
client.complete(msg, function () {
console.log('Receive message: ' + message);
});
}
function blinkLED() {
// Light up LED for 500 ms
if(blinkLEDTimeout) {
clearTimeout(blinkLEDTimeout);
}
wpi.digitalWrite(LEDPin, 1);
blinkLEDTimeout = setTimeout(function () {
wpi.digitalWrite(LEDPin, 0);
}, 500);
}
// set up wiring
wpi.setup('wpi');
wpi.pinMode(LEDPin, wpi.OUTPUT);
sensor = new BME280(BME280_OPTION);
sensor.init()
.then(function () {
sendingMessage = true;
})
.catch(function (err) {
console.error(err.message || err);
});
// create a client
client = Client.fromConnectionString(connectionString, Protocol);
client.open(function (err) {
if (err) {
console.error('[IoT hub Client] Connect error: ' + err.message);
return;
}
// set C2D and device method callback
client.onDeviceMethod('start', onStart);
client.onDeviceMethod('stop', onStop);
client.on('message', receiveMessageCallback);
setInterval(sendMessage, 2000);
});
rasppisimulator's Introduction
rasppisimulator's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.