Created
October 20, 2019 23:50
-
-
Save sabicalija/0f21037497fdeb23a01336069a172ec3 to your computer and use it in GitHub Desktop.
Johnny 5 goes Raspberry Pi 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Board, Led} = require("johnny-five"); | |
const { RaspiIO } = require("raspi-io"); | |
const board = new Board({ | |
io: new RaspiIO() | |
}); | |
const http = require("http"); | |
const host = "127.0.0.1"; | |
const port = 80; | |
let led = null; | |
let ledState = false; | |
board.on("ready", () => { | |
console.log("Setting up LED on Port GPIO23"); | |
led = new Led("GPIO23"); | |
server.listen(port, host, () => { | |
console.log(`Server listening on http://${host}:${port}/`); | |
}); | |
}); | |
const page = (state) => { | |
return `<html> | |
<head> | |
<title>Raspberry Pi 3 - IO</title> | |
<style> | |
.active { | |
background-color: green; | |
color: white; | |
} | |
</style> | |
</head> | |
<body> | |
<form method="POST" action="/led/toggle"> | |
<button ${state ? "class='active'" : ""} type="submit">Toggle</button> | |
</form> | |
</body> | |
</html>`; | |
}; | |
const toggle = () => { | |
console.log("Toggling LED..."); | |
if (ledState) { | |
ledState = false; | |
led.off(); | |
} else { | |
ledState = true; | |
led.on(); | |
} | |
}; | |
const server = http.createServer((req, res) => { | |
const { url } = req; | |
if (url === "/led/toggle") { | |
res.writeHead(302, { Location: "/" }); | |
toggle(); | |
return res.end(); | |
} | |
res.end(page(ledState)); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Board, Led} = require("johnny-five"); | |
const { RaspiIO } = require("raspi-io"); | |
const board = new Board({ | |
io: new RaspiIO() | |
}); | |
board.on("ready", () => { | |
console.log("Setting up LED on Port GPIO23"); | |
const led = new Led("GPIO23"); | |
led.blink(500); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment