0% found this document useful (0 votes)
3 views

Wifi Remote Final

Uploaded by

paashakhan888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Wifi Remote Final

Uploaded by

paashakhan888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

20

Wi-Fi Remote-
Controlled
Robot
In this proj ect you ’ re
going to bu i ld a two -
wh eel, battery-powered
robot with a Raspberry Pi
Zero W an d th e MotoZero
add - on . You can control it
over Wi - Fi using a web app
you ’ ll make with Node-RED.
Cost: $ $ Ti me : 3 h o u r s

Pa r t s R e q u i r e d S o ftwa r e R e q u i r e d
Raspberr y Pi Zero W (or other Node-RED dashboard
4 0 G PIO R a s p b e r r y P i )
Smart robot car chassis kit
MotoZero add-on board (or other
motor controller add-on)
F o u r AA b a t t e r i e s
Por table charger
Jumper wires

24 8 • p roj ec t 2 0
P r oj e ct O u tlin e
Rather than going straight into the project, we’ll highlight the most
important parts of the robot to give you an idea of how it will all work.
Wi-Fi
You’ll control the robot with a Node-RED application, so your
Raspberry Pi needs to have Wi-Fi. Raspberry Pi models 3
and Zero W have built-in Wi-Fi, but if your board doesn’t,
you can use a Wi-Fi dongle compatible with the Pi.
Raspberry Pi Board
We’re using the Raspberry Pi Zero W because its small size makes
it perfect for the small robot chassis. But any Raspberry Pi version
with 40 GPIOs is compatible with this project as long as it has Wi-Fi.
Robot Chassis Kit
We’re using a robot chassis kit that comes with everything you
need to build the robot, including wheels, motors, and screws.
You can find the kit in online marketplaces like Amazon or
eBay by searching for Smart Car Robot Chassis Kit. You
need the kit with two DC motors.
MotoZero Add-on
The DC motors will make the robot move, and you’ll control them
using an add-on board called MotoZero. One place to find the
board is online at The Pi Hut (https://thepihut.com/motozero/ ).
You can also use any other Raspberry Pi–compatible motor
driver add-on for this project or build a circuit with the LC293D
IC chip. We won’t cover how to build that circuit here, but there
are plenty of tutorials online if you want to make your own.
Power
We don’t want to connect the Pi robot to a wall socket, because
we want it to be portable, so we need to power the robot with a
portable charger, or power bank. The power bank must be capable
of outputting 5 V and 2 A. We tested this project with a power bank
that has 2,200 mAh capacity and it worked fine; incorporating a
power bank with more capacity will make the robot run for longer.
The DC motors need to be powered independently from the
Pi, meaning you need two independent power sources. To power
up the motors, we’re using the battery holder that comes with the
chassis kit along with four AA batteries, not included in the kit.

249 • p roj ec t 2 0
Node-RED Application
The Node-RED application you’ll use to control your robot
should be able to make the robot go forward and backward,
move right and left, and stop. Since you’re not running the Pi
as a desktop computer, the Pi needs to automatically start
Node-RED when it boots. You’ll also add an off button to the
application so you can turn the Raspberry Pi off remotely.
Figure 20-1 shows a high-level overview of how your robot
will work.

Figure 20-1:
The robot structure RASPB ERRY PI ZER O W (WI TH WI -F I )

DC M OT OR
CO NNE CTE D
TO M OTOZ E RO

A A B ATTE RY

POWER BA NK

F RONT
B ACK

AA B AT TERY

A A B ATTE RY M OT OZER O

AA B AT TERY

POWER B ANK POWERI NG


THE R ASPB ER RY PI
BATTE RIE S P OWERI NG M OTOZERO CONNECTED
THE M OTOZ E RO TO THE RASPBERRY PI

P r e pa r in g t h e Ra s p b e r r y Pi
We’re using the Raspberry Pi Zero W board, shown in Figure 20-2,
which is a variant of Raspberry Pi Zero that has built-in wireless LAN
and Bluetooth, but remember that you can use another Wi-Fi compat-
ible board or a Wi-Fi dongle. The Raspberry Pi Zero W measures only
2.56 inches × 1.18 inches × 0.20 inches (65 mm × 30 mm × 5 mm)
and costs around $10.
The Pi Zero has 40 GPIO pins with the same pinout as the Pi 3.
As you can see in Figure 20-2, it features a mini HDMI connector
and two micro USB connectors, one of which is used exclusively for
power. To use the Pi Zero as a desktop computer, you need a few
extra accessories like a USB hub, a USB-to-micro-USB adapter, and
an HDMI-to-mini-HDMI adapter to connect the peripherals. To save

25 0 • p roj ec t 2 0
you some money, we’ll prepare everything on our regular Raspberry
Pi 3 and then switch the micro SD card to the Pi Zero W.

Figure 20-2:
Raspberry Pi Zero W

We recommend using a new micro SD card for this project.


Refer to “Uploading the Operating System” on page 10 to see how
to install the latest Raspbian release on your new micro SD card.
After installing the operating system, insert the micro SD card
on your regular Pi. Power up the Pi and wait a few seconds for the
system to start. Then configure the Wi-Fi from the desktop’s top-right
corner by clicking Wi-Fi. Next, enter your Wi-Fi password, and wait a
few seconds for the Wi-Fi connection to successfully establish.
The Node-RED software is preinstalled on the Pi’s operating
system, but you still need to install the Node-RED dashboard. For
that, first update the library repositories, and then install npm (Node
Package Management) by entering the following at your command
line:

pi@raspberrypi:~ $ sudo apt update


pi@raspberrypi:~ $ sudo apt install npm

When prompted, type Y and press enter. The installation may


take a few minutes. Then enter the following commands to upgrade
npm to the latest 3.x version, which is the version recommended for
use with Node-RED:

pi@raspberrypi:~ $ sudo npm install -g [email protected]


pi@raspberrypi:~ $ hash –r

Finally, enter the following to install the Node-RED dashboard:

pi@raspberrypi:~ $ sudo npm install --unsafe-perm -g node-red-


dashboard

Again, Node-RED needs to start automatically when the Pi


boots. For that, enter the following command in the terminal.

251 • p roj ec t 2 0
pi@raspberrypi:~ $ sudo systemctl enable nodered.service

With that done, shut down your Pi and switch the micro SD card
to the Raspberry Pi Zero W.

W i r in g t h e C i r c u it
To build the robot structure you need a chassis for the robot, two DC
motors with corresponding wheels, the MotoZero add-on, jumper
wires, and your Pi (with Wi-Fi). Use Figure 20-1 as a reference. We’ll
start by mounting the MotoZero at the top of the Raspberry Pi and
then wire the motors to MotoZero.

Wiring the DC Motors to MotoZero


MotoZero allows you to control four motors independently, but
you need to control just two DC motors. The MotoZero will come
unassembled, so you need to solder its parts. The Pi Hut provides
an assembly manual on the product’s page, so go to https://thepihut
.com/motozero/ and follow the instructions there before continuing.
Your MotoZero should look like Figure 20-3 after assembly.

Figure 20-3:
Assembled MotoZero
add-on

Figure 20-3 shows the connections you can make to MotoZero:


positive (+) and negative (–) connections for four DC motors, and a
positive (+) and negative (–) connection for the power supply. You need
an external power source for driving the motors. The motors require
a big jump in current to move, so using a separate power source pre-
vents the Pi from suddenly losing power when this jump occurs.
Follow these instructions and refer to Figure 20-1 to wire the
motors and battery holder.

1. Connect the right DC motor’s red wire to the Motor 1 positive (+)
pin on the MotoZero, and its black wire to the Motor 1 negative
(–) pin. You’ll need to loosen the screws, place the wires in the
pin slot, and then tighten the screws again.

252 • p roj ec t 2 0
2. Repeat the previous instruction for the left motor, connecting the Note
power wires to the MotoZero Motor 2 connections. If you find the robot’s
wheels are spinning in
3. Without inserting the batteries, connect the battery holder’s red
the opposite direction of
wire to the positive (+) pin on the MotoZero power connector what you intended, you
and its black wire to the negative (–) pin, shown at the bottom of may have to switch the DC
the board in Figure 20-3. motors’ red wires with the
black wires on the positive
Controlling the Motors with MotoZero (+) and negative (–) Motor 1
Each DC motor has three GPIO pins associated with it. One pin, or Motor 2 terminals. You’ll

known as the enable pin, enables the motor and is like an on and off know if you need to do this
when you test the appli-
switch. The other two pins control the power to the positive and nega-
cation at the end of the
tive motor wires. Applying power to one wire and GND to the other project.
makes the motor turn in one direction, while applying power and GND
to the opposite motor wires moves the motor in the opposite direction.
We’re just using the Motor 1 and Motor 2 terminals, which are Note
controlled by the GPIOs shown in the following table, when you For information about

mount the MotoZero on the top of the Pi. the Motor 3 and Motor
4 GPIOs, you can check

Motor 1 Motor 2 the MotoZero manual at


The Pi Hut’s product page
enable: GPIO 5 enable: GPIO 6 (https://thepihut.com/

Motor 1 (+): GPIO 27 Motor 2 (+): GPIO 22 motozero/).

Motor 1 (–): GPIO 24 Motor 2 (–): GPIO 17

To make a motor spin, the enable pin must be HIGH to turn the
motor on, and one—and only one—of the positive or negative pins
should be HIGH. For example, if you want Motor 1 to spin in one
direction, use the following setup:
• GPIO 5: HIGH
• GPIO 27: HIGH
• GPIO 24: LOW

To make the same motor spin in the opposite direction, use this:
• GPIO 5: HIGH
• GPIO 27: LOW
• GPIO 24: HIGH

To turn off the motor, you’d send a LOW signal to all the GPIOs.
The same logic applies to the other motors.

25 3 • p roj ec t 2 0
W r itin g t h e A p p licati o n
Once you’ve built your hardware, it’s time to create the Node-RED
application. As your Pi is already in your robot chassis, the most
practical way to create the robot Node-RED application is to use
your regular desktop or laptop computer and control it from there.
First you’ll need to find your Raspberry Pi Zero W IP address;
you’ll use it to access the Pi’s Node-RED application dashboard,
where you can create a robot application.
You need to make sure the Raspberry Pi is turned on and that
your computer and Pi are connected to the same network before
continuing.

Finding the Raspberry Pi IP Address


Warning Power up the Raspberry Pi by connecting the 5 V power adapter to
The Raspberry Pi Zero W a wall socket. You’ll only use this power source while creating the
has two mini USB ports, Node-RED application; once it’s ready, you should change to the
and one of them, labeled
portable power source.
PWR IN, is designated for
powering up the Pi.
You’ll find the Pi’s IP address using Angry IP Scanner software.
Download it onto your regular desktop or laptop computer for free
from http://angryip.org/download/, and then follow the prompts to
install it.
Once the installation is complete, open Angry IP Scanner and
click the Start button. Wait a few seconds until it shows the available
IP addresses. Your Pi IP address should have raspberrypi.lan as a
hostname, so jot down the corresponding IP address. Figure 20-4
highlights our Raspberry Pi IP address, which is 192.168.1.122.

Figure 20- 4:
Finding the Raspberry
Pi IP address with
the Angry IP Scanner
software

254 • p roj ec t 2 0
Creating the Node-RED Flow
On your regular computer, making sure it’s on the same net- Note
work as your Pi, open a web browser tab and go to http://<Pi IP For an introduction to

address>:1880, replacing <Pi IP address> with the Raspberry Node-RED, see Project 17.

Pi IP address you noted earlier. In our case, we navigated to


http://192.168.1.122:1880. Your Raspberry Pi Node-RED web
server should open.
In the top-right corner of the window, select the dashboard tab
and, inside the Layout tab, create a tab called Robot. Next, create
two groups inside that Robot tab, called Main and Poweroff. The
Main group is where you’ll organize the buttons that control the robot,
and the Poweroff group is where you’ll add the button to remotely turn
off your Raspberry Pi. Once you’ve completed these tabs and groups,
your layout should look like Figure 20-5.

Figure 20-5:
Node-RED application
dashboard layout

Add five buttons, a function, six rpi gpio output nodes, and an
exec node to the flow. Wire the nodes and edit their names to match
the ones in Figure 20-6.

Figure 20-6:
Node-RED application
nodes

25 5 • p roj ec t 2 0
Edit the function’s properties so that it has six outputs, assigning
all of the nodes’ properties as shown in Table 20-1.

Table 20-1: Node Properties


Properties assigned
Group: Main [Robot]
to each node
Size: auto
Forward Icon: fa-arrow-up
Label: Forward
Payload: forward
Group: Main [Robot]
Size: auto
Left Icon: fa-arrow-left
Label: Left
Payload: left
Group: Main [Robot]
Size: auto
Right Icon: fa-arrow-right
Label: Right
Payload: right
Group: Main [Robot]
Size: auto
Reverse Icon: fa-arrow-down
Label: Reverse
Payload: reverse
Group: Main [Robot]
Size: auto
Stop
Icon: fa-hand-paper-o
Label: Stop
Payload: stop
Function: enter the code in Listing 20-1
f
Outputs: 6
GPIO: GPIO5 – 29
Enable M1
Type: Digital output
GPIO: GPIO27 – 13
+ M1
Type: Digital output
GPIO: 18 – GPIO24
– M1
Type: Digital output
GPIO: GPIO17 – 11
Enable M2
Type: Digital output
GPIO: GPIO6 – 31
+ M2
Type: Digital output

25 6 • p roj ec t 2 0
Node Properties

GPIO: GPIO22 – 15
– M2
Type: Digital output
Group: Poweroff [Robot]
Size: auto
Poweroff Icon: fa-power-off
Label: Poweroff
Background: red
Command: /usr/bin/sudo
+ Append: not checked
exec
poweroff
Name: Poweroff

Figure 20-7 shows how the exec node is set up.

Figure 20-7:
exec node properties

All nodes should be in the Main group, except the Poweroff


button, which should be part of the Poweroff group.

Entering the Script


Insert the JavaScript code in Listing 20-1 (also available for download
from https://www.nostarch.com/RaspberryPiProject/ ) into the func-
tion node:

 var msg1 = { payload: 0 }; Listing 20-1:


var msg2 = { payload: 0 };
The remote-controlled
var msg3 = { payload: 0 };
robot script

257 • p roj ec t 2 0
var msg4 = { payload: 0 };
var msg5 = { payload: 0 };
var msg6 = { payload: 0 };
 if (msg.payload === "forward") {
msg1.payload = 1;
msg2.payload = 1;
msg4.payload = 1;
msg5.payload = 1;
}
else if (msg.payload === "left") {
msg1.payload = 1;
msg2.payload = 1;
}
else if (msg.payload === "right") {
msg4.payload = 1;
msg5.payload = 1;
}
else if (msg.payload === "reverse") {
msg1.payload = 1;
msg3.payload = 1;
msg4.payload = 1;
msg6.payload = 1;
}
 return [msg1, msg2, msg3, msg4, msg5, msg6];

This function sends messages to the connected rpi gpio out-


put nodes in the order they’re connected to the function node. This
means that msg1 is sent to the Enable M1 node, msg2 to + M1, msg3
to – M1, and so on (see Figure 20-6).
First you initialize all the payload message variable values to 0 .
Then the series of if and else if statements checks which button
was pressed , depending on the payload received by the func-
tion, and sets the message values according to the action the robot
should take. For example, if you press the Forward button, the pay-
load received by the function node is forward, so the condition at 
is met and the code changes the msg1, msg2, msg4, and msg5 payload
values to 1, while msg3 and msg6 remain 0.
Then, the function node sends the msg.payload values to the
corresponding nodes . For the robot to go forward, the payloads
would need to be:
• Enable M1: 1
• + M1: 1
• – M2: 0

25 8 • p roj ec t 2 0
• Enable M2: 1
• + M2: 1
• – M2: 0

Here, both motors are enabled and moving in the same


direction—forward. The following table shows the messages
the function should send to each node for each action.

Action En a b l e + M1 – M1 En a b l e + M2 – M2
M1 M2

Forward 1 1 0 1 1 0
Left 1 1 0 0 0 0
Right 0 0 0 1 1 0
Reverse 1 0 1 1 0 1
Stop 0 0 0 0 0 0

When the Stop button is clicked, none of the conditions set in


the code is met, and the function sends the values initialized at the
start .
Outside the function node, when the Poweroff button is clicked,
the exec node executes the poweroff command to turn off the Pi.
Remember that you’ve filled the exec command property with /usr/
bin/sudo/poweroff—see Table 20-1.
Once everything is in place, click the Deploy button at the top-
right corner to save the changes and run the flow.

Running the Application


Now your Node-RED application is ready. Go to http://<Pi IP address>
:1880/ui (replacing <Pi IP address> with your own) to see your applica-
tion dashboard. It should look something like Figure 20-8.
Test the controls to see if the wheels are moving in the right
direction, and don’t forget that you need to insert the four AA batter-
ies into the battery holder in order to power the motors.
If one or both motors are spinning in the wrong direction, switch
the black and red wires on the MotoZero for that motor terminal, or
change the payload messages to match the required directions.

25 9 • p roj ec t 2 0
Figure 20- 8:
Node-RED application
to remotely control the
robot

P o w e r in g U p t h e R o b ot
Now that the application is ready, click the Poweroff button to shut
down the Pi. Then wait a few seconds for it to shut down.
Change the Pi’s power source from the wall socket to the
power bank. Wait a few minutes for the Pi to power up and autostart
Node-RED. On a smartphone or other device that’s on the same
network as your Pi, open a new browser tab and go to http://<Pi IP
address>:1880/ui. Then click on the buttons to remotely control your
robot.
Congratulations—you now have a Wi-Fi-controlled robot!

Ta k in g I t F u r t h e r
There’s a lot of room for upgrades on your robot. Here are some ideas
for upgrades that will need both hardware and software changes.
You’ll need to experiment a bit with Node-RED to get these working:
• Get a robot chassis with four wheels and control four motors
instead of two.
• Add LEDs and buzzers to the robot to make it more interactive.
• Add sensors, like an ultrasonic sensor, so the robot can avoid
obstacles by itself.

2 6 0 • p roj ec t 2 0

You might also like