-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.js
177 lines (158 loc) · 5.22 KB
/
demo.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var awsIot = require('aws-iot-device-sdk');
var mraa = require('mraa');
var thingName = "Intel_Edison";
var lcd = require('jsupm_i2clcd');
var servoModule = require("jsupm_servo");
var groveSensor = require('jsupm_grove');
var sensor1 = require('jsupm_th02');
var buzzer, display, servo, button, th, relay;
var buttonPressed = false;
var exec = require('child_process').exec;
var cmd = "fswebcam -r 1280x720 --jpeg 100 -S 13 - | "
cmd = cmd + "aws s3 cp - s3://edison-hackday/uploads/test.jpg --content-type image/jpeg";
var device = awsIot.device({
keyPath: '/home/root/aws_certs/privateKey.pem',
certPath: '/home/root/aws_certs/cert.pem',
caPath: '/home/root/aws_certs/rootCA.pem',
clientId: 'intel_edison',
region: 'ap-southeast-1',
reconnectPeriod: '10'
});
device.on('connect', function() {
console.log('Connecting to AWS IoT edison/+ topic');
device.subscribe('edison/commands');
mystring = "{\"Message\":\"Your Edison is online\"}";
device.publish('edison/messages', mystring);
});
device.on('message', function(topic, payload) {
console.log(topic + ":" + payload.toString());
msg = JSON.parse(payload.toString());
switch (topic) {
case 'edison/commands':
switch (msg.Action) {
case 'beep':
beep();
break;
case 'relay':
if (msg.State == "on") {
relay.on();
} else {
relay.off();
}
break;
case 'lcd':
display.setCursor(0, 0);
display.write(' ');
display.setCursor(0, 0);
display.write(msg.Message);
break;
case 'color':
if (msg.Color == 'red')
display.setColor(255, 0, 0);
if (msg.Color == 'green')
display.setColor(0, 255, 0);
if (msg.Color == 'blue')
display.setColor(0, 0, 255);
if (msg.Color == 'white')
display.setColor(255, 255, 255);
break;
case 'servo':
servo.setAngle(Number(msg.Angle));
break;
case 'cam':
cmd = "fswebcam -r 1280x720 --jpeg 100 -S 13 - | aws s3 cp - s3://edison-";
cmd = cmd + "hackday/uploads/" + guid() + ".jpg --content-type image/jpeg";
exec(cmd, function(error, stdout, stderr) {
console.log(stdout);
});
break;
default:
console.log(msg);
break;
}
break;
default:
break;
}
});
function initEdison() {
//initialize buzzer
buzzer = new mraa.Gpio(4);
buzzer.dir(mraa.DIR_OUT);
//initialize lcd
display = new lcd.Jhd1313m1(0, 0x3E, 0x62);
display.setCursor(0, 0);
display.write('ON');
//initialize Servo
servo = new servoModule.ES08A(5);
//initialize button
button = new groveSensor.GroveButton(0);
setInterval(readButtonValue, 10);
//initialize temperature and Humidity
th = new sensor1.TH02();
//initialize motion sensor1
motion = new mraa.Gpio(7);
motion.dir(mraa.DIR_IN);
//readMotionSensor();
// initialize relay switch
relay = new groveSensor.GroveRelay(2);
setTimeout(readTemperature, 5000);
}
function beep() {
setTimeout(function() {
buzzer.write(1);
}, 0);
setTimeout(function() {
buzzer.write(0);
}, 200);
}
function readButtonValue() {
if (button.value() == 1) {
notifyButtonPress();
} else {
buttonPressed = false;
}
}
function notifyButtonPress() {
if (buttonPressed == false) {
device.publish('edison/messages', JSON.stringify({
"Temperature": th.getTemperature(),
"Humidty": th.getHumidity()
}));
console.log(button.name() + " has been pressed at " + new Date().toString());
}
buttonPressed = true;
}
function readMotionSensor() {
var sensorValue = motion.read();
if (sensorValue == 1) {
console.log("Motion detected");
//device.publish('edison/messages', JSON.stringify({
// "Notification": "Motion detected at " + new Date().toString()
//}));
}
setTimeout(readMotionSensor, 1000);
}
function readTemperature() {
//console.log(th.getTemperature());
display.setCursor(1, 1);
display.write("Temp:" + th.getTemperature().toFixed(1) + " / " + th.getHumidity().toFixed(0) + "%");
device.publish('$aws/things/Intel_Edison/shadow/update', JSON.stringify({
"state": {
"reported": {
"temperature": th.getTemperature()
}
}
}));
setTimeout(readTemperature, 5000);
}
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
initEdison();