-
Notifications
You must be signed in to change notification settings - Fork 6
/
pi-gpio-to-cloud.js
65 lines (54 loc) · 1.76 KB
/
pi-gpio-to-cloud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var gpio = require("pi-gpio");
var Protocol = require('azure-iot-device-http').Http;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
var connectionString = '<device connection string>';
var client = Client.fromConnectionString(connectionString, Protocol);
var connectCallback = function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
gpio.setDirection(7, "output", function(err,value){
if(err) throw err;
console.log('Pin 7 set to output successful');
});
gpio.write(7,0, function(err, value){
if(err) throw err;
});
setInterval(function() {
gpio.read(3, function(err, value) {
if(err) throw err;
if(value==1)
{
console.log("You have registered your vote!"); // The current state of the pin
gpio.write(7,1, function(err, value){
if(err) throw err;
});
value=0;
var data = JSON.stringify({
VoteCount:1,
TimeFlag:new Date()
});
var message = new Message(data);
console.log('Sending message back to the IoT Hub: ' + message.getData());
client.sendEvent(message, printResultFor('send'));
}
else
{
console.log(".....");
gpio.write(7,0, function(err, value){
if(err) throw err;
});
}
});
}, 1000)
}};
client.open(connectCallback);
// Helper function to print results in the console
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString() + "----");
if (res) console.log(op + ' status: ' + res.constructor.name + "----");
};
}