How to build a Wireless Rocket Launcher Starter with the Raspberry Pi and PiJuice HAT
You may have remembered in our original Kickstarter video for the PiJuice HAT our CEO Aaron launched a model rocket using a Raspberry Pi and PiJuice HAT. It’s now been a while since that Kickstarter video and now we are going to share with you the project that enabled us to do that.
The project uses a Raspberry Pi and PiJuice HAT that acted as a wireless hotspot with a web server that loaded a webpage to control a relay that launched the rocket. This Pi is known as the Launcher Pi. We have also included a safety switch on the Launcher Pi to prevent accidental launch during the setup procedure. When in the off position the relay cannot be activated and this has been programmed into the code.
The second Pi is known as the Command Pi, and this includes a Raspberry Pi with PiJuice HAT and our Media Centre HAT touchscreen display. The Command Pi boots up and automatically connects to the wireless access point on the Launcher Pi and then loads the webpage from the web server. You can then use the touchscreen interface to launch the rocket (toggle the relay) providing the safety switch is primed.
The rocket we are using in this project is a ESTES Riptide model rocket, which you can find in most model toy stores. You can use any rocket with this project providing the starting mechanism is the same. The Rocket works by connecting a battery to the rocket motor and when the terminals have been shorted it then launches the rocket. In our case we are using a PP3 9V battery connected to the relay that then starts the rocket motor when activated.
Note:This project can be dangerous if instructions are not followed correctly. If in doubt please seek advice and follow all safety information that is included in the purchase of a model rocket. Anyone under the age of 12 must seek adult supervision.
What you need
You will need the following components to build this project. Some components can be swapped out for others such as the relay I used could be swapped out for a HAT board rather than a module but the principle is the same.
Launcher Pi
- Raspberry Pi Computer
- PiJuice HAT
- Pi Crust Proto HAT
- Rocker Switch SPST
- PP3 9V Battery
- PiJuice Case Tall
- 2.1mm DC Socket (w/screw terminals)
- 3V Relay module
Command Pi
Rocket
- ESTA Riptide Model Kit
- Rocket Motors (B4-4,B6-4, C6-5)
- Wadding
Note: In this project we do not need the launch controller that was supplied with the rocket kit but we are going to use the cable with crocodile clips that are attached.
Getting Started
In this guide we are going to divide the project into two parts, the launcher Pi and the command Pi.
Launcher Pi
Hardware
Step 1 – Connect the Relay to the Pi Crust Proto HAT
In this project I have used a Grove module because it is simple to wire up and also it uses both 3V and 5V signal to trigger the relay. As the Pi GPIO uses 3V3 logic, this is perfect. Also the Grove module uses a jumper cable to interface between the module and the Grove HAT, we can use this cable so we don’t have to hack the module itself and we can keep it plug n play. Take one of the cable ends and cut the connector off on one end. Then strip back the red, black and yellow wires (white is not connected).
Now solder the black wire to one of the GND pins on the Pi Crust Proto HAT, red wire to the 5V pin and finally connect the yellow wire to GPIO 24 (pin 18).
Step 2 – Connecting the safety switch
The safety switch is designed to prevent any accidental launch when setting up the rocket with the Launcher Pi. The switch will be connected to one of the GPIO pins as an input to detect its switch state. You will need to solder two wires to the switch terminals (best to use two different colours) then solder one wire to 3V3 pin on the Pi Crust Proto HAT and then the other wire to GPIO pin 18 (pin 12).
Step 3 – Connect the trigger power
As explained in the intro, the launcher works by shorting two terminals of a battery connected to the starter. We do this in our circuit by triggering the relay which makes the circuit of a battery. Lets connect the battery to the tab to the relay, along with the DC barrel jack for ease of use. Connect the red wire of the battery terminal to the relay module pin 1 (left side) and then the black wire connect to the DC jack plus terminal. Then connect a short wires from the negative terminal of the DC jack to pin 2 of the relay (right side).
Now cut the cable off that came with the rocket kit that has two crocodile clips on and strip the wires back. Connect these wires to the DC plug terminals. At this point it doesn’t matter which way around you connect them to the terminals.
Step 4 – Assemble your hardware
Now we can begin to connect the PiJuice HAT to the Raspberry Pi and then place the Pi Crust Proto HAT on top of the PiJuice HAT.
We are going to assemble everything inside the PiJuice Tall case but before we do we need to make some minor modification to it so we can access the safety switch and also connect to the DC jack. You will see inside of the case on the inside there are some hollowed out parts for other connectors such as a ribbon cable, pi camera cable and PoE connectors. Luckily for us on the side where the SD card goes there is a cut out which was perfect to fit the safety switch. Push it though with a sharp object and then place the safety switch inside.
Now on the opposite side of the case you will need to drill roughly a 10mm hole to fit the DC barrel jack. The jack itself is about 10.2mm but it will squeeze through the hole just fine.
Assemble the rest of the case and connect the relay and the battery, also push the DC barrel jack through the hole in the case . You should finally have something that looks like below.
Software
The software was designed in a couple of different stages. The first was to get the Raspberry Pi to act as a Wi-Fi hotspot so the launcher Pi can connect to it. The second was to setup a webserver using Flask to host the webpage locally, which will load on the display of the launcher Pi.
Configure your access point
In order to work as an access point, the Raspberry Pi will need to have access point software installed, along with DHCP server software to provide connecting devices with a network address.
To create an access point, we’ll need DNSMasq and HostAPD. Install all the required software in one go with this command:
sudo apt install dnsmasq hostapd
Since the configuration files are not ready yet, turn the new software off as follows:
sudo systemctl stop dnsmasq sudo systemctl stop hostapd
Configuring a static IP
We are configuring a standalone network to act as a server, so the Raspberry Pi needs to have a static IP address assigned to the wireless port. This documentation assumes that we are using the standard 192.168.x.x IP addresses for our wireless network, so we will assign the server the IP address 192.168.4.1. It is also assumed that the wireless device being used is wlan0
.
To configure the static IP address, edit the dhcpcd configuration file with:
sudo nano /etc/dhcpcd.conf
Go to the end of the file and edit it so that it looks like the following:
interface wlan0 static ip_address=192.168.4.1/24 nohook wpa_supplicant
Now restart the dhcpcd daemon and set up the new wlan0
configuration:
sudo service dhcpcd restart
Configuring the DHCP server (dnsmasq)
The DHCP service is provided by dnsmasq. By default, the configuration file contains a lot of information that is not needed, and it is easier to start from scratch. Rename this configuration file, and edit a new one:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig sudo nano /etc/dnsmasq.conf
Type or copy the following information into the dnsmasq configuration file and save it:
interface=wlan0 # Use the require wireless interface - usually wlan0 dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
So for wlan0
, we are going to provide IP addresses between 192.168.4.2 and 192.168.4.20, with a lease time of 24 hours. If you are providing DHCP services for other network devices (e.g. eth0), you could add more sections with the appropriate interface header, with the range of addresses you intend to provide to that interface.
Start dnsmasq
(it was stopped), it will now use the updated configuration:
sudo systemctl start dnsmasq
Configuring the access point host software (hostapd)
You need to edit the hostapd configuration file, located at /etc/hostapd/hostapd.conf, to add the various parameters for your wireless network. After initial install, this will be a new/empty file.
sudo nano /etc/hostapd/hostapd.conf
Add the information below to the configuration file. This configuration assumes we are using channel 7, with a network name of NameOfNetwork, and a password AardvarkBadgerHedgehog. Note that the name and password should not have quotes around them. The passphrase should be between 8 and 64 characters in length.
To use the 5 GHz band, you can change the operations mode from hw_mode=g to hw_mode=a. Possible values for hw_mode are:
- a = IEEE 802.11a (5 GHz)
- b = IEEE 802.11b (2.4 GHz)
- g = IEEE 802.11g (2.4 GHz)
- ad = IEEE 802.11ad (60 GHz) (Not available on the Raspberry Pi)
interface=wlan0 driver=nl80211 ssid=NameOfNetwork hw_mode=g channel=7 wmm_enabled=0 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=AardvarkBadgerHedgehog wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
We now need to tell the system where to find this configuration file.
sudo nano /etc/default/hostapd
Find the line with #DAEMON_CONF, and replace it with this:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Start it up
Now enable and start hostapd
:
sudo systemctl unmask hostapd sudo systemctl enable hostapd sudo systemctl start hostapd
Do a quick check of their status to ensure they are active and running:
sudo systemctl status hostapd sudo systemctl status dnsmasq
Add routing and masquerade
Edit /etc/sysctl.conf and uncomment this line:
net.ipv4.ip_forward=1
Add a masquerade for outbound traffic on eth0:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Save the iptables rule.
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Edit /etc/rc.local and add this just above “exit 0” to install these rules on boot.
iptables-restore < /etc/iptables.ipv4.nat
Reboot.
Once rebooted, if you go to another device that has wireless and search for wireless networks you should be able to see your wireless access point and be able to connect to it using the credentials that you configured.
Installing Flask
Flask is a Python web framework that turns the Raspberry Pi into a dynamic web server. Whats great about Flask is that you can integrate standard Python libraries.
In order to install flask you will also need to install pip. If it isn’t already installed you can just ype the folowing command:
sudo apt-get install python3-pip
After pip has successfully installed, you can now go ahead and install flask:
sudo pip3 install flask
There are two main files to be used with flask. There is the main python code that runs the server and loads the webpage and then there is the html web page.
There is also a strict file structure that flask uses, it will look in certain places for the html files or css files if we were going to use css. You will need to create these directories:
cd ~ sudo mkdir controller_pi cd controller_pi sudo mkdir templates
The python program app.py goes inside the project directory controller_pi and the html files are located within the templates directory.
Here is the flask python code for this project:
import RPi.GPIO as GPIO from flask import Flask, render_template app = Flask(__name__) GPIO.setmode(GPIO.BCM) pins = { 24 : {'name' : 'lamp', 'state' : GPIO.LOW}} GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(22, GPIO.OUT) GPIO.output(22, GPIO.LOW) for pin in pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW) @app.route('/') def index(): # For each pin, read the pin state and store it in the pins dictionary: for pin in pins: pins[pin]['state'] = GPIO.input(pin) input_state = GPIO.input(18) if input_state == True: GPIO.output(22, GPIO.HIGH) else: GPIO.output(22, GPIO.LOW) # Put the pin dictionary into the template data dictionary: templateData = { 'pins' : pins, 'status' : input_state } return render_template('index.html', **templateData) @app.route("/<changePin>/<action>") def action(changePin, action): input_state = GPIO.input(18) if input_state == True: GPIO.output(22, GPIO.HIGH) else: GPIO.output(22, GPIO.LOW) if input_state == True: # Convert the pin from the URL into an integer: changePin = int(changePin) # Get the device name for the pin being changed: deviceName = pins[changePin]['name'] # If the action part of the URL is "on," execute the code indented below: if action == "on": # Set the pin high: GPIO.output(changePin, GPIO.HIGH) # Save the status message to be passed into the template: message = "Turned " + deviceName + " on." if action == "off": GPIO.output(changePin, GPIO.LOW) message = "Turned " + deviceName + " off." if action == "toggle": # Read the pin and set it to whatever it isn't (that is, toggle it): GPIO.output(changePin, not GPIO.input(changePin)) message = "Toggled " + deviceName + "." # For each pin, read the pin state and store it in the pins dictionary: for pin in pins: pins[pin]['state'] = GPIO.input(pin) # Along with the pin dictionary, put the message into the template data dictionary: templateData = { 'message' : message, 'pins' : pins, 'status' : input_state } return render_template('index.html', **templateData) templateData = { 'pins' : pins, 'status' : input_state } return render_template('index.html', **templateData) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
Here is the html file:
<!DOCTYPE html> <head> <title>Current Status</title> </head> <body> <h1>Device Listing and Status</h1> {% for pin in pins %} <p>The {{ pins[pin].name }} {% if pins[pin].state == true %} is currently on (<a href="/{{pin}}/off">turn off</a>) {% else %} is currently off (<a href="/{{pin}}/on">turn on</a>) {% endif %} </p> {% endfor %} {% if message %} <h2>{{ message }}</h2> {% endif %} <h2>Safety Switch = {{ status }}</h2> </body> </html>
To launch the webserver you will need to run the following command within the project directory:
sudo python3 app.py
You can add this at boot by editing the rc.local file and before the exit 0 add the following:
sudo python3 /home/pi/webapp/app.py
Install PiJuice Software
We need to configure the PiJuice so we can boot it up by pressing the SW1 button and then shutdown safely by pressing the SW2 button.
Type in the following command to install the pijuice software:
sudo apt-get install pijuice-cli
You will need to reboot your Raspberry Pi:
sudo reboot
From the command line type in the following to bring up the pijuice cli software:
pijuice_cli
Scroll down to buttons and select the first button SW1. Check to see if the following setting are set, which by default they should be:
Now go back and select button SW2. Under SINGLE_PRESS select the option SYS_FUNC_HALT_POW_OFF
The launcher pi is now all setup and configured, ready for use with the command pi.
Command Pi
Hardware
Step 1 – First you will need to snap off the corner mounts if you are not going to mount it flush with a flat surface or integrate it with your project. You will need a pair of pliers or similar to carefully bend and snap off the corners.
Step 2 – Insert the PiJuice HAT on top of the Raspberry Pi, carefully aligning the pins and push down as far as it will go.
Step 3 – Now add the Media Center HAT on top of the PiJuice HAT gpio pins, once again making sure it is pushed all the way down as far as it will go.
Step 4 – Take the Raspberry Pi and slot it into the bottom half of the PiJuice maker kit case. You should see grooves along each side where the boards edges slot into.
Step 5 – Finally add the top half of the case. You should hear it clip into place either side.
Tip: Slide over the side where the USB connectors are first.
Software
The software for the command pi is really straight forward. You will need to use Raspbian Buster Desktop version so we can display the webpage on the Media Center HAT. Then we open chronium web browser in kisok mode when the desktop boots.
Connect to the Wi-Fi hotspot
Step 1 – Power the command pi or switch on using the PiJuice HAT. Once you have booted to the desktop open up the terminal window and type in the following:
sudo raspi-config
In the Raspberry Pi Configuration menu, select option 2 Network Options
Then select N2 Wi-Fi and follow the on-screen prompts to enter the Wi-Fi SSID (name) and password
Install the Media Center HAT
To install the media center HAT software you will need to type in the following command in the terminal window or command line:
sudo su -c "bash <(wget -qO- https://pisupp.ly/mediacentersoftware)" root
When asked, select the option for TFT only.
Remove the HDMI cable if connected and reboot the Raspberry Pi.
Install PiJuice Software
We need to configure the PiJuice so we can boot it up by pressing the SW1 button and then shutdown safely by pressing the SW2 button.
Type in the following command to install the pijuice software:
sudo apt-get install pijuice-cli
You will need to reboot your Raspberry Pi:
sudo reboot
From the command line type in the following to bring up the pijuice cli software:
pijuice_cli
Scroll down to buttons and select the first button SW1. Check to see if the following setting are set, which by default they should be:
Now go back and select button SW2. Under SINGLE_PRESS select the option SYS_FUNC_HALT_POW_OFF
Configure Chromium
We want chromium web browser to load right after the desktop loads. We can do this by editing the autostart file:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
On the last line add the following:
@chromium-browser --noerrdialogs --disable-infobars --kiosk http://192.168.5.1:5000
Reboot the command pi and providing the launch pi is on, it will connect to its Wi-Fi and then load the webpage we created before.
Launching your Rocket
Now everything is setup and ready to go, unfortunately as of writing this we are currently on lockdown here in the UK so I cannot yet launch the rocket to show you how it works just yet. I can however guide you through the process of how it will work.
Step 1 – This goes with out saying but make sure that the Launch Pi and Command Pi are both fully charged before use. Simply connect a micro USB power supply to the PiJuice to charge and once the LED turns blue it is charged to 100%.
Step 2 – Follow the instructions for your model rocket to setup and install everything apart from the starter. You should have the parachute, motor and starter/igniter installed and ready to go.
Step 3 – Connect the clip lead that we prepared before to the Launcher Pi and then connect the clips to the igniter.
Note: Ensure that the safety switch is in the off position before connecting the clips
Tip: The original starter comes with a safety pins that needs to be pushed in before launching but is actually a very handy pin for accessing the PiJuice pins to turn the PiJuice on and off.
Step 4 – Switch on the command Pi. Wait until it connect and you can see the webpage.
Step 5 – The last step before launching is to switch the safety switch in the launcher pi which will enable the relay. We would recommend to not have the command pi in your hand whilst doing this.
Step 6 – Step back to a safe distance and press the “turn on” button on the webpage to launch the rocket into the sky.
Checkout the video below to see how it worked during our KickStarter campaign.