-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcaTest.js
190 lines (162 loc) · 4.01 KB
/
pcaTest.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
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
8 countries
each with 3 colour types, red,green, or orange (for inbetween)
3 different colour levels
*/
const SG_CODE = "singapore" ; //country codes to be used
const CHINA_CODE = "china";
const INDIA_CODE = "india";
const INDO_CODE = "indonesia";
const BRAZIL_CODE = "brazil";
const INSERT_TOKEN_HERE = process.argv()
const fetch = require('node-fetch'); //required to use fetch function and async await
//async await to call the country, to be replicated inside the board
async function getAir(country){
const response = await fetch("https://api.waqi.info/feed/" + country + "/?token=INSERT_TOKEN_HERE");
const data = await response.json();
let airQuality = data.data.aqi;
return airQuality;
}
const { EtherPortClient} = require('etherport-client');
const five = require('johnny-five');
const Led = require('johnny-five');
const { Board } = require("johnny-five");
const board = new five.Board({ //intitalise board using the internet client host
port: new EtherPortClient({
host: '192.168.137.99', //ip address is based on your own machine
port: 3030
}),
repl: true
});
//defining pins, make sure they are pwm (can test out one by one on your own)
board.on("ready", () => {
const SG_RED = new five.Led({
pin: process.argv[2] || 0,
address: 0x40,
controller: "PCA9685"
});
const SG_GREEN = new five.Led({
pin: process.argv[2] || 1,
address: 0x40,
controller: "PCA9685"
});
const INDIA_RED = new five.Led({
pin: process.argv[2] || 2,
address: 0x40,
controller: "PCA9685"
});
const INDIA_GREEN = new five.Led({
pin: process.argv[2] || 3,
address: 0x40,
controller: "PCA9685"
});
const INDO_RED = new five.Led({
pin: process.argv[2] || 4,
address: 0x40,
controller: "PCA9685"
});
const INDO_GREEN = new five.Led({
pin: process.argv[2] || 5,
address: 0x40,
controller: "PCA9685"
});
const CHINA_RED = new five.Led({
pin: process.argv[2] || 6,
address: 0x40,
controller: "PCA9685"
});
const CHINA_GREEN = new five.Led({
pin: process.argv[2] || 7,
address: 0x40,
controller: "PCA9685"
});
const BRAZIL_RED = new five.Led({
pin: process.argv[2] || 8,
address: 0x40,
controller: "PCA9685"
});
const BRAZIL_GREEN = new five.Led({
pin: process.argv[2] || 9,
address: 0x40,
controller: "PCA9685"
});
/* function aqiCheck(aqiValue){
if (aqiValue >200){
}
} */
getAir(SG_CODE).then( result =>{
console.log(result + " =singapore api");
SG_RED.on();
SG_GREEN.on();
if (result > 200){
SG_RED.pulse();
}
else if (result > 100){
SG_GREEN.pulse();
SG_RED.pulse();
}
else{
SG_GREEN.pulse();
}
})
getAir(INDO_CODE).then( result =>{
console.log(result + " =indo api");
INDO_RED.on();
INDO_GREEN.on();
if (result > 200){
INDO_RED.pulse();
}
else if (result > 100){
INDO_GREEN.pulse();
INDO_RED.pulse();
}
else{
INDO_GREEN.pulse();
}
})
getAir(INDIA_CODE).then( result =>{
console.log(result + " =india aqi");
INDIA_RED.on();
INDIA_GREEN.on();
if (result > 200){
INDIA_RED.pulse();
}
else if (result > 100){
INDIA_GREEN.pulse();
INDIA_RED.pulse();
}
else{
INDIA_GREEN.pulse();
}
})
getAir(CHINA_CODE).then( result =>{
console.log(result + " =china aqi");
CHINA_RED.on();
CHINA_GREEN.on();
if (result > 200){
CHINA_RED.pulse();
}
else if (result > 100){
CHINA_GREEN.pulse();
CHINA_RED.pulse();
}
else{
CHINA_GREEN.pulse();
}
})
getAir(BRAZIL_CODE).then( result =>{
console.log(result + " =brazil aqi");
BRAZIL_RED.on();
BRAZIL_GREEN.on();
if (result > 200){
BRAZIL_RED.pulse();
}
else if (result > 100){
BRAZIL_GREEN.pulse();
BRAZIL_RED.pulse();
}
else{
BRAZIL_GREEN.pulse();
}
})
});