Communication Cubes With Raspberry Pi and Esp32
by CĂ©dricCallant in Circuits > Raspberry Pi
214 Views, 1 Favorites, 0 Comments
Communication Cubes With Raspberry Pi and Esp32
I am a student at Multimedia & Creative Technologies at Howest Kortrijk. This year we had to make our first (big) IoT project. I usually have a hard time interpretting messages (especially when people don't use emoji's). This sketch shows the problem slightly exagerated. Something else that gave me inspiration was being able to send messages in a more unique way instead of just texting.
The cubes make u able to send short messages to the person with the other cube, a message contains text and a color, the color should give a feeling to the message.
While messages aren't sent the cube is in idle mode, the idle mode can be changed, and the color can also be changed.
Note that this design is made for short distances (unfortunatly) since it uses Serial communication. You can always change this yourself, but I won't handle that in this tutorial.
Supplies
Main computing units
1 Raspberry Pi 4 (model B)
1 ESP32
2 small breadboards
1 PCB developement board
Sensors and actuators
6 TTP223 Touch sensors
2 RC522 rfid readers
2 KY-038 soundsensors
2 MPU3060 accelerosensors
2 LCD I2C displays
2 DC-motors
1 WS2812b Ledstrip (5V)
Powersupplies
2 5V 4A adapter
2 Breadboard powersupplies
2 9V 1A adapter
2 Female DC power adapters
1 USB to micro b USB cable
Assembly
MDF wood (8mm thickness):
4 150mm x 150mm planks
2 134mm x 134mm planks
4 150mm x 50mm planks
2 134mm x 50mm planks
2 LED cubes (I used these specific cubes)
2 32mm (inner diameter) PVC
some type of small stick (see step 3)
Wood glue
Hot glue
Fast drying glue
Tools
Soldering iron
Milling machine
Drill
Hot glue gun
Saw
Sandpaper
Downloads
Wiring
Here you can see 2 different wire schemes for both the Raspberry Pi and the Esp32.
In the schemes I use different colors of wires so it'd be easier to make out what type of wire it is.
RED: 5V wires
ORANGE: 3V3 wires
BLACK: GND wires
CYAN/BLUE: I2C wires
GREEN: I/O wires
PINK/PURPLE: SPI wires
Note: make sure to check if the ground and 3v3/5v are connected right, otherwise you might break something
Normalised Database
This is the database that is implemented in the project. I'll give a brief explanation of what gets stored in which table.
Device
This stores all the info of the different sensors and actuators.
CubeDevice
Here we store the each device that is present in the different cubes.
Cube
In this table we store the info of each cube. You will need to change the 2 users of the cube to the tags/cards
ChatMessage
Here we store each sent message with the color.
Color
Stores each color available on the website.
History
Here we store the different things the sensors and actuators did.
Action
In this table we store the different possible actions.
Housing
For the housing we'll use MDF wood, you should be able to get this at your closest DIY store.
Note: I decided to let the wood be sawn to pieces at the diy store so you won't see this here.
Note: Everything done here needs to be done twice
First thing we are going to do is mill some depth, this depth will be for the rfid reader. Take a short side of the wood planks, Draw where you should mill and start milling. Make sure to mill at a depth that is less deep than 8mm, otherwise you'll create a hole. Milling at about 2mm would be ideal. (Image 1)
After that, we'll use wood glue to put the other pieces together. I used ductape to hold the pieces together so that they stay in a square shape. You can then use this square shape to measure where the top small square needs to be, on top of the larger square, now glue these two squares together. After you've measured that you can apply wood glue on the bottom square so that one is also part of our box. Use some glue clamps to keep everything together. (images 2, 3 and 4)
When the glue is dry, we can make the hole for the lcd. Take a long side of the cube, and measure out how big the hole needs to be. Once you know this, you can start milling the hole. (images 5,6 and 7)
Now take your pvc tube and saw about 11cm off
take a drill and drill a hole (35mm) in the middle of the top squares so that the tube fits inside.
Now take some hot blue and glue the tube into the tube. Make sure that the longest part of the tube comes out on to, so that when you place the lid on top, the tube pokes out. (image 8, 9 and 10)
After that drill a hole (same size as earlier) in the bottom of your LED cube (Take the electronics out first) (image 11)
Slide it over the tube, to the bottom.
You can now take the LED cube and put it on top of that. If everything went well, i should fit and reach the lid of the woode box. If this is not the case you should sand down the tube. (image 12)
Note: the tube should NOT touch the top of the LED cube, it should stay off by +- 1mm
if this went well, screw some screws in the bottom of the lid, through the holes of the LED cube's lid, and into the holes of the LED cube itself. (in the image I use 6 screws, but 3 is easier) (image 13)
I decide to saw down the Led cube's lid, since it'd give a nicer result. (image 14)
Once all this is done, you can unscrew the LED cube. On top of the tube place the stick. If the stick is too thick, you can make small notches to fit it into.
Check if the cube still fits, and if it does, glue the stick on top of the tube (with hot glue). (image 15)
Drill 1 hole at the bottom of the tube (for the ledstrip) and drill 1 hole in the front and back at the top (image 16) (image 17 shows the top holes)
After this, you can add 3 touch sensors on top of the stick, one on the left, 1 in the middle and 1 on the right. and glue it with hot glue. Make sure the wires are already soldered, because once the hot glue is applied it'll be hard to solder (also make sure the wires are long enough)
Pull the wires through the 2 holes you made earlier and pull them to the bottom off the tube. You might also want to tape the wires that lead to the same sensor together.
Then glue the leds in a spiral form on the sides of the tube, and keep it together with hot glue. Apply tape to the top of the ledstrip to make sure it stays in the same position. and pull the wires through the bottom hole.(image 17,18 and 19)
Once all that is dried up, you can paint the wooden box (using primer first). (image 20)
Configuring Raspberry Pi
To configure the raspberry pi you can find a guide on github.
Here we: add your own wifi network onto the pi, install the most recent updates, install apache (for our website), install and protect MariaDB (Database).
After that we configure: the MySQLWorkbench, Visual Studio Code and cloning the github repo.
Once the gihub repo is cloned onto your pi, you can create a virtual environment, in this venv you need to install the needed packages.
Now copy the code in the file in the database folder, and run it in your MySQLWorkbench.
You can see all these steps and the needed commands in this github file.
THESE NEXT STEPS ARE VERY IMPORTANT
We will also need to enable certain interfaces for the pi since we use those to "talk" to our sensors.
1 Enabeling I2C
Open your pi with putty, login and type:
sudo raspi-config
this command should get you to Raspberry pi configuration tool (image 1)
Now go to: 3 Interface Options and select: I5 I2C. (image 2)
2 Enabeling SPI
On the same screen of step 1, go back to: 3 Interface Options
There select: I4 SPI
3 Enabeling Serial Port
Repeating the same start of the previous steps, we'll go back to: 3 Interface Options.
This time we'll select: I6 Serial Port.
4 Restart your pi
This step will save the previously set configurations. You can do this by running this command:
sudo reboot
Now you can connect the esp32 to one of the usb ports of the pi. To check in which port you plugged in the usb, you can run the following command:
ls /dev/tty*
Once you run this you will see different tty ports, and at the end you should see ttyUSBx.
The x should be 0, you can change the x by plugging the usb in a different port.
Coding
Now onto the actual code. There are 2 different codes, one for the raspberry pi and one for the ESP32.
The esp code you can find in the esp folder of the github repo. Upload this code to your esp using the Arduino ide. You might need to install certain libraries, these libraries are imported at the top of the code, so you can just check the names and install them via the ide.
NOTE: To get this code working for you, you'll need to make a few changes to the code
1 Change the cubeid
The cubeid is based of off the id of your rfid tag/card. To check the id of the cards, you can use the rfid example code or you can use the nxp taginfo app.
Once you have the id, to to line 23 in app.py and set the cubeid to your tagid
cubeid = "<YOUR FIRST TAGID HERE>"
Go to the MySQLWorkbench, and go to the Cube table, and change the first id to your first tagid.
Now scan the second tag, and put the id on line 23 in the esp code
String cubeid = "<YOUR SECOND TAGID HERE>"
Go to the MySQLWorkbench, and go to the Cube table, and change the second id to your second tagid.
2 Connecting the ESP
Now take the cable you used earlier for the esp32 code and plug it into one of the usb ports of the pi. If your pi is plugged in your esp should now be operational.
Assembeling
Now lastly put all the electronics in the wooden boxes, I decided to put everything in with the breadboards (though it is not recomended). I used screws glued to the side to hold the mpu up and I used silicone to keep the rfid in place.
If you made the hole for the lcd well enough it should fit nicely otherwise, you'll have to glue that too.
The breadboards already have a sticky pad at the bottom, these stick really well.