-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrasperry_pi_connections.js
120 lines (103 loc) · 3.17 KB
/
rasperry_pi_connections.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* 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 = 'HostName=WaterValves.azure-devices.net;DeviceId=Valve1;SharedAccessKey=k/DtGT+J60IbObQGsfVU6oDSiKHmiWOPAAIoTBVYwOU=';
const LEDPin = 4;
var sendingMessage = false;
var messageId = 0;
var client, sensor;
var blinkLEDTimeout = null;
function getMessage(cb) {
messageId++;
sensor.readSensorData()
.then(function (data) {
cb(JSON.stringify({
messageId: messageId,
deviceId: 'Raspberry Pi Web Client',
temperature: data.temperature_C,
humidity: data.humidity
}), data.temperature_C > 30);
})
.catch(function (err) {
console.error('Failed to read out sensor data: ' + err);
});
}
function sendMessage() {
}
function onStart(request, response) {
console.log('Try to invoke method start(' + request.payload + ')');
sendingMessage = false;
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);
}
console.log("Connected")
});
}
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 toggleLED() {
// Toggle LED state
let currentState = wpi.digitalRead(LEDPin);
let newState = currentState === 0 ? 1 : 0;
wpi.digitalWrite(LEDPin, newState);
}
function receiveMessageCallback(msg) {
console.log(msg)
var message = msg.getData().toString('utf-8');
client.complete(msg, function () {
console.log('Receive message: ' + message);
message=JSON.parse(message);
wpi.digitalWrite(LEDPin, message['ValveStatus']);
});
}
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);
console.log("hello")
client.on('message', receiveMessageCallback);
});