Skip to content

Commit c320b9b

Browse files
author
Stewest
committed
Updates React App to send back a click event to socketio server to turn on LEDs on Arduino board
1 parent 3f994a8 commit c320b9b

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

j5ardi/socket-io-client/src/App.css

+6-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
}
6161

6262
.light {
63-
background-color: #fff286;
63+
background-color: aliceblue;
6464
border-radius: 50% 50% 30px 30px;
6565
width: 100%;
6666
height: 100%;
@@ -90,7 +90,7 @@
9090
display: block;
9191
position: absolute;
9292
width: 100%;
93-
background: aliceblue;
93+
background: #fff286;
9494
bottom: 0;
9595
z-index: 2;
9696
transition: height 0.3 ease-in-out;
@@ -107,6 +107,10 @@
107107
margin: 10px auto 0;
108108
}
109109

110+
.toggle-led {
111+
margin-top: 10px;
112+
}
113+
110114
.led-red {
111115
border: 3px solid red;
112116
}

j5ardi/socket-io-client/src/components/ClientComponent.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default function ClientComponent() {
77
const [light, setLight] = useState("");
88
const [led, setLed] = useState("");
99
const [temp, setTemp] = useState("");
10+
const [changedLED, toggleLED] = useState("");
1011

1112
const ENDPOINT = process.env.REACT_APP_ENDPOINT;
1213

@@ -25,7 +26,9 @@ export default function ClientComponent() {
2526
setTemp(data);
2627
});
2728

28-
}, [ENDPOINT]);
29+
socket.emit("handleLED", changedLED);
30+
31+
}, [ENDPOINT, changedLED]);
2932

3033
let lightVar = 'OFF';
3134
let luminosity;
@@ -36,7 +39,7 @@ export default function ClientComponent() {
3639

3740
if (light.item === 'light') {
3841
lightVar = light.msg;
39-
lightNumb = parseFloat(lightVar / 10.24).toFixed(2);
42+
lightNumb = 100 - (parseFloat(lightVar / 10.24).toFixed(0));
4043
luminosity = lightNumb;
4144
}
4245

@@ -50,7 +53,11 @@ export default function ClientComponent() {
5053

5154
if (temp.c || temp.f) {
5255
temperatureC = temp.c;
53-
temperatureF = parseFloat((temp.c * 9 / 5) + 32).toFixed(2);
56+
temperatureF = parseFloat((temp.c * 9 / 5) + 32).toFixed(1);
57+
}
58+
59+
function handleLED() {
60+
toggleLED(prevState => !prevState);
5461
}
5562

5663
return (
@@ -65,7 +72,8 @@ export default function ClientComponent() {
6572
LEDs: {ledStatus ? 'ON':'OFF'}
6673
{ledStatus ? <div className="led-red is-on"></div> : <div className="led-red"></div> }
6774
{ledStatus ? <div className="led-green is-on"></div> : <div className="led-green"></div> }
68-
{ledStatus ? <div className="led-blue is-on"></div> : <div className="led-blue"></div> }
75+
{ledStatus ? <div className="led-blue is-on"></div> : <div className="led-blue"></div>}
76+
<button className="toggle-led" onClick={handleLED}>Toggle LED</button>
6977
</div>
7078

7179
<div className="item temp"><h3>TEMP:</h3><br />

j5ardi/socket-io-server/app.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ const socketIo = require("socket.io");
55
require('dotenv').config();
66
const port = process.env['PORT'];
77
const local = process.env['CORSLOCAL'];
8+
89
const index = require("./routes/index");
910
const app = express();
1011

12+
const events = require('events');
13+
const eventEmitter = new events.EventEmitter();
14+
1115
app.use(index);
1216

1317
const server = http.createServer(app);
1418
const io = socketIo(server, {
1519
cors: {
16-
// you may need to change this if the client starts on a different port
20+
// You may need to change this in .ENV if the client starts on a different port.
1721
origin: local,
1822
methods: ["GET", "POST"]
1923
},
@@ -34,16 +38,24 @@ io.on("connection", (socket) => {
3438

3539
interval = setInterval(() => getApiAndEmit(socket), 1000);
3640

41+
socket.on("handleLED", (arg) => {
42+
console.log("toggleLED", arg);
43+
44+
// Fire the 'toggleLED' event.
45+
eventEmitter.emit('toggleLED');
46+
});
47+
3748
socket.on("disconnect", () => {
3849
console.log("Client disconnected");
3950
clearInterval(interval);
40-
socket.offAny();
51+
socket.off();
4152
});
4253
});
4354

4455
// https://stackabuse.com/using-global-variables-in-node-js/
4556
global.lightStatus = {};
4657
global.ledStatus = {};
58+
global.tempStatus = {};
4759

4860
function setLight(item, msg) {
4961
global.lightStatus = {
@@ -100,7 +112,7 @@ board.on("ready", function () {
100112
freq: 5000
101113
});
102114

103-
//// "data" get the current reading from the photoresistor
115+
// "data" is the current reading from the photoresistor.
104116
photoresistor.on("data", () => {
105117
setLight('light', photoresistor.value)
106118
});
@@ -132,11 +144,10 @@ board.on("ready", function () {
132144
}
133145

134146
/**
135-
*
136147
* @param {*} colour
137148
* usage example: pulseLed(green);
138149
*/
139-
const pulseLed = function (colour) {
150+
const pulseLed = function (colour = array) {
140151
colour.pulse();
141152
setLED('led', true);
142153

@@ -146,19 +157,14 @@ board.on("ready", function () {
146157
}, 5000);
147158
}
148159

149-
// Turn on Rainbow.
150-
// To turn off: array.stop().off();
160+
// Turn on Rainbow at start.
151161
rainbox();
152162

163+
// Turn on Array of LEDs at start.
153164
pulseLed(array);
154165

155-
board.on("exit", () => {
156-
// Turn off the rgb LED.
157-
rgbOn.off();
158-
// Turn off each led in the array of individual leds.
159-
array.stop().off();
160-
});
161-
166+
//Assign the event handler to an event:
167+
eventEmitter.on('toggleLED', pulseLed);
162168

163169
const thermometer = new five.Thermometer({
164170
controller: "LM35",
@@ -170,4 +176,10 @@ board.on("ready", function () {
170176
setTEMP(celsius / 10, fahrenheit / 10);
171177
});
172178

179+
board.on("exit", () => {
180+
// Turn off the rgb LED.
181+
rgbOn.off();
182+
// Turn off each led in the array of individual leds.
183+
array.stop().off();
184+
});
173185
});

0 commit comments

Comments
 (0)