1
- const express = require ( " express" ) ;
2
- const http = require ( " http" ) ;
3
- const socketIo = require ( " socket.io" ) ;
1
+ const express = require ( ' express' ) ;
2
+ const http = require ( ' http' ) ;
3
+ const socketIo = require ( ' socket.io' ) ;
4
4
5
5
require ( 'dotenv' ) . config ( ) ;
6
6
const port = process . env [ 'PORT' ] ;
7
7
const local = process . env [ 'CORSLOCAL' ] ;
8
8
9
- const index = require ( " ./routes/index" ) ;
9
+ const index = require ( ' ./routes/index' ) ;
10
10
const app = express ( ) ;
11
11
12
12
const events = require ( 'events' ) ;
@@ -19,34 +19,34 @@ const io = socketIo(server, {
19
19
cors : {
20
20
// You may need to change this in .ENV if the client starts on a different port.
21
21
origin : local ,
22
- methods : [ " GET" , " POST" ]
22
+ methods : [ ' GET' , ' POST' ] ,
23
23
} ,
24
- maxHttpBufferSize : 1e8
24
+ maxHttpBufferSize : 1e8 ,
25
25
} ) ;
26
26
27
- const five = require ( " johnny-five" ) ;
27
+ const five = require ( ' johnny-five' ) ;
28
28
const board = new five . Board ( ) ;
29
29
30
30
let interval ;
31
31
32
32
// Init Socket.io.
33
- io . on ( " connection" , ( socket ) => {
34
- console . log ( " New client connected" ) ;
33
+ io . on ( ' connection' , ( socket ) => {
34
+ console . log ( ' New client connected' ) ;
35
35
if ( interval ) {
36
36
clearInterval ( interval ) ;
37
37
}
38
38
39
39
interval = setInterval ( ( ) => getApiAndEmit ( socket ) , 1000 ) ;
40
40
41
- socket . on ( " handleLED" , ( arg ) => {
42
- console . log ( " toggleLED" , arg ) ;
41
+ socket . on ( ' handleLED' , ( arg ) => {
42
+ console . log ( ' toggleLED' , arg ) ;
43
43
44
44
// Fire the 'toggleLED' event.
45
45
eventEmitter . emit ( 'toggleLED' ) ;
46
46
} ) ;
47
47
48
- socket . on ( " disconnect" , ( ) => {
49
- console . log ( " Client disconnected" ) ;
48
+ socket . on ( ' disconnect' , ( ) => {
49
+ console . log ( ' Client disconnected' ) ;
50
50
clearInterval ( interval ) ;
51
51
socket . off ( ) ;
52
52
} ) ;
@@ -59,29 +59,33 @@ global.tempStatus = {};
59
59
60
60
function setLight ( item , msg ) {
61
61
global . lightStatus = {
62
- item, msg
62
+ item,
63
+ msg,
63
64
} ;
64
- } ;
65
+ }
65
66
66
67
function getLight ( ) {
67
68
return global . lightStatus ;
68
69
}
69
70
70
71
function setLED ( item , msg ) {
71
72
global . ledStatus = {
72
- item, msg
73
+ item,
74
+ msg,
73
75
} ;
74
- } ;
76
+ }
75
77
76
78
function getLED ( ) {
77
79
return global . ledStatus ;
78
80
}
79
81
80
- function setTEMP ( c , f ) {
82
+ function setTEMP ( c , f , k ) {
81
83
global . tempStatus = {
82
- c, f
84
+ c,
85
+ f,
86
+ k,
83
87
} ;
84
- } ;
88
+ }
85
89
86
90
function getTEMP ( ) {
87
91
return global . tempStatus ;
@@ -93,32 +97,40 @@ const getApiAndEmit = (socket) => {
93
97
const temp = getTEMP ( ) ;
94
98
95
99
// Emitting a new message. Will be consumed by the client
96
- socket . emit ( " FromLight" , light ) ;
97
- socket . emit ( " FromLed" , led ) ;
98
- socket . emit ( " FromTemp" , temp ) ;
100
+ socket . emit ( ' FromLight' , light ) ;
101
+ socket . emit ( ' FromLed' , led ) ;
102
+ socket . emit ( ' FromTemp' , temp ) ;
99
103
} ;
100
104
101
105
server . listen ( port , ( ) => console . log ( `Listening on port ${ port } ` ) ) ;
102
106
103
107
// Arduino Johnny 5.
104
- board . on ( " ready" , function ( ) {
108
+ board . on ( ' ready' , function ( ) {
105
109
// I've put 3 separate LEDs on Digital pins 3 Blue, 5 Green, 6 Red.
106
110
// And a rgb LED, controlled by 9, 10 , 11.
107
111
const rgb = new five . Led . RGB ( [ 9 , 10 , 11 ] ) ;
108
112
109
113
// Create a new `photoresistor` hardware instance on Analog 2.
110
114
const photoresistor = new five . Sensor ( {
111
- pin : "A2" ,
112
- freq : 5000
115
+ pin : 'A2' ,
116
+ freq : 5000 ,
113
117
} ) ;
114
118
115
119
// "data" is the current reading from the photoresistor.
116
- photoresistor . on ( " data" , ( ) => {
117
- setLight ( 'light' , photoresistor . value )
120
+ photoresistor . on ( ' data' , ( ) => {
121
+ setLight ( 'light' , photoresistor . value ) ;
118
122
} ) ;
119
123
120
124
let index = 0 ;
121
- const rainbow = [ "FF0000" , "FF7F00" , "FFFF00" , "00FF00" , "0000FF" , "4B0082" , "8F00FF" ] ;
125
+ const rainbow = [
126
+ 'FF0000' ,
127
+ 'FF7F00' ,
128
+ 'FFFF00' ,
129
+ '00FF00' ,
130
+ '0000FF' ,
131
+ '4B0082' ,
132
+ '8F00FF' ,
133
+ ] ;
122
134
// Raibow RGA LED
123
135
const rgbOn = new five . Leds ( [ 12 ] ) ;
124
136
// Array of the single LEDs.
@@ -141,7 +153,7 @@ board.on("ready", function () {
141
153
rgbOn . off ( ) ;
142
154
} , 5000 ) ;
143
155
console . log ( 'Ended Rainbow' ) ;
144
- }
156
+ } ;
145
157
146
158
/**
147
159
* @param {* } colour
@@ -153,9 +165,9 @@ board.on("ready", function () {
153
165
154
166
setTimeout ( function ( ) {
155
167
colour . stop ( ) . off ( ) ;
156
- setLED ( 'led' , false )
168
+ setLED ( 'led' , false ) ;
157
169
} , 5000 ) ;
158
- }
170
+ } ;
159
171
160
172
// Turn on Rainbow at start.
161
173
rainbox ( ) ;
@@ -167,16 +179,17 @@ board.on("ready", function () {
167
179
eventEmitter . on ( 'toggleLED' , pulseLed ) ;
168
180
169
181
const thermometer = new five . Thermometer ( {
170
- controller : "LM35" ,
171
- pin : "A5"
182
+ controller : 'ANALOG' ,
183
+ pin : 'A5' ,
184
+ freq : 2000 ,
172
185
} ) ;
173
186
174
- thermometer . on ( " change" , ( ) => {
175
- const { celsius, fahrenheit} = thermometer ;
176
- setTEMP ( celsius / 10 , fahrenheit / 10 ) ;
187
+ thermometer . on ( ' change' , ( ) => {
188
+ const { celsius, fahrenheit, kelvin } = thermometer ;
189
+ setTEMP ( celsius , fahrenheit , kelvin ) ;
177
190
} ) ;
178
191
179
- board . on ( " exit" , ( ) => {
192
+ board . on ( ' exit' , ( ) => {
180
193
// Turn off the rgb LED.
181
194
rgbOn . off ( ) ;
182
195
// Turn off each led in the array of individual leds.
0 commit comments