visit
index.html
We'll now start writing the code for the index.html file:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./index.css">
<title>Find My Dog</title>
</head>
<body>
<header>I have no clue where they went.</header>
<main>
<button id='beeper-btn' type="button">Activate the noise</button>
</main>
<script src="./index.js"></script>
</body>
</html>
Pretty easy so far, right? The most important thing here is to have that id attribute on the button element. Other than that, feel free to add more elements to the page so it suits your style.index.css
Now, let's write the CSS file:body {
font-family: 'Lato', 'Trebuchet MS', sans-serif;
}
header, main {
text-align: center;
}
header {
font-size: 36px;
}
button {
background-color: rgb(255, 0, 0);
border: unset;
font-size: 24px;
margin-top: 24px;
padding: 12px;
}
button:hover {
background-color: rgba(255, 0, 0, 0.5);
font-size: 24px;
}
If you didn't make any edits to the index.html and index.css files, your page should look like this:
index.js
Now it's time to write some basic JavaScript. The best part? You don't need to install any dependencies!
const beeperBtn = document.querySelector('#beeper-btn');
let isBeeperActivated = false;
const toggleBeeper = () => {
isBeeperActivated = !isBeeperActivated;
isBeeperActivated ?
fetch('//localhost:3000/api/startBeeper')
.then(console.log)
:
fetch('//localhost:3000/api/stopBeeper')
.then(console.log)
}
beeperBtn.addEventListener('click', toggleBeeper);
This file only does three things:Once you've set up your board, you can add power to it with the battery pack and connect your piezo-buzzer. Connect the red wire of your battery pack to the board's Vin pin and its black wire to one of the board's GND pins on the Power side. Your HC 05 Module should be connected to the 5V pin and another GND pin. Finally, you can connect your piezo-buzzer to pin 13 and a GND pin.
heroku --version
Next, you'll need to login to your Heroku account with the command:heroku login
This will redirect you to a web page where you can login with your GitHub credentials. Now you have everything set up to deploy this app directly to Heroku.But first, you'll need to install a few packages to get this working. Inside the root folder, create the following new files:Now, open a terminal and get ready to install all the packages you need. I prefer npm but feel free to use yarn.
npm install cors express johnny-five
These are the only packages you'll need, and it's just like setting up any other Node app.server.js
You'll need to create two endpoints to control when the beeper is on or off. These are the endpoints you referenced on the front end in those fetch requests. Open your server.js file and add the following code:
const { Board, Led } = require('johnny-five');
const cors = require('cors');
const express = require('express');
const PORT = process.env.PORT || 3000;
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const board = new Board({
port: "COM4",
});
let beeper;
board.on("ready", () => {
console.log("Ready!");
beeper = new Led(13);
});
app.get('/api/startBeeper', (req, res) => {
console.log('on');
beeper.on();
res.send({
message: 'on'
});
});
app.get('/api/stopBeeper', (req, res) => {
console.log('off');
beeper.off();
res.send({
message: 'off'
});
});
app.listen(PORT, () => {
console.log(`endpoints running on port ${PORT}`);
});
This code sets up a normal express app and connects it to localhost:3000. It also creates a new instance of the Board object so you can connect to your Arduino and declare a variable representing the beeper. You'll want to get the Bluetooth address and use it in the port option for the board. This will allow you to connect to your board after you deploy to a Heroku server. It then starts up the Board with the on method and the ready option. Inside this code block, you can send a message to the console so you know the board should be on.Lastly, you need to set the value for beeper. I defined it as an LED because we only need a simple "high/low" signal sent to the board to make it work. When it comes to hardware, it's often better to keep things simple. Beeper is also connected to pin 13, similar to its physical counterpart.The last part of the back end is writing the two endpoints. Since we're toggling a beeper on and off with the button click on the front end, one endpoint turns it on, and the other turns it off. I usually don't leave console.log in my code, but these can really help with debugging. Sometimes, the hardware works immediately. Other times, a little tinkering might be necessary to get it all working together. This code prepares you for both scenarios and is everything you need for the beeper to work. You technically don't even need the front end if you remember the endpoint names!Johnny-Five does some magic with the Board object. When you call the board.on() method, it automatically finds and connects to the Bluetooth address of the HC 05 module as long as there is power to it and the Arduino. Once the code is connected to the physical board, Johnny-Five then looks for the physical beeper based on the pin number you passed to the LED object. When you click the button on the front end, it calls the corresponding endpoint sending either a high or low signal to pin 13, which turns the beeper on or off.Procfile
Finally, enter the following in the Procfile. This is a file specifically used by Heroku that specifies the commands to be executed by the app on startup. In this case, we define our web process as our server.web: node server.js
If you want to , you can use the start field from the package.json file.Now, you can finally deploy your app so you can find your dog anywhere, any time.{
"name": "dog-locator",
"version": "0.1.0",
"scripts": {
"build": "npm install",
"dev": "npm run build && npm run start",
"start": "node server.js"
},
"dependencies": {
"concurrently": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"johnny-five": "^1.4.0",
"path": "^0.12.7"
}
}
Once you have the settings you need in place, you can deploy your app. The app will search for the HC 05 and find the Bluetooth address to use for the connection. Make sure that your Arduino and HC 05 have power before you deploy or you might run into some errors with the initial connection.If you want to clone the code for this project, you can get it from GitHub here: Now open a terminal and get ready to use the Heroku CLI. Since you already have it installed and you've logged in, you should be able to run the command to make a new, empty application on Heroku:heroku create
Then run the following command to deploy the project directly to Heroku:git push heroku master
After pushing the project in the last line, you should see something in your terminal similar to this and then your project will be live!