Introduction: Raspberry Pi IoT Ticket Printer for Online Stores
- Do you have an online store?
- Are you a maker?
- Do you want to optimize the selling process?
If you answer yes to all the questions, please keep reading because this is the instructable you are looking for. Also if you say no to one or more questions keep reading too, maybe you can print your email or make a secret organization mission printer. The posibilities and applications of this are endless.
The IoT is everywhere, now almost everything is conected to the internet and makes our lives easier. So I decided why not make a ticket printer machine that connects directly to an email account where all the orders made in my online store arrives and the print the tickets automatically for you.
But, how it works?
In any e-commerce platform when a client makes an order you receive an email. So this printer is conected via IMAP service to an email account that only receives this notification emails.
When the email arrives it contains some html code, so I used string functions to delete all the html code and only get the information that can be used. Then, I search for the data that I need to generate the ticket and after that I save it into different strings. When I have the data that I want, the ticket is printed.
Also the printer can detect if there is paper and only marks the email as "readed" if the ticket is successfully printed. And if you need to print it again, you only need to go to your inbox an mark that mail as "unread".
Pretty simple, right?
I can not guarantee that my code is compatible with your specific e-commerce system, so in this Instructable I am only going to show you how to get the email and print the body. The transformation into a ticket going to be up to you, but dont worry is easy if you know how to work with strings.
Let's get started, shall we?
Step 1: Gathering All the Parts
For this build you are going to need:
- 1 x Raspberry Pi model B.
- 1 x wifi miniature dongle.
- 1x SD card with Raspbian installed.
- 1 x Logic level shifter.
- Some jumper wire (male to female and female to female).
- 1 x Thermal receipt printer and extras.
- 1 x Power supply (5v and al least 5A).
- 1 x Panel mount DC barrel jack.
- 1 x Pushbutton SPST.
- 1 x Neopixel (WS2812B).
- Enclosure and hardware.
You can get all the parts from Adafruit, and the files for laser-cutting the case can be found here: Acrilic Case
Also Adafruit offers a kit with almost everything to build this project, you can see it here.
Step 2: Making All the Wiring and Assembling the Case
Now it's time to make the conections, I recommend you to assemble the case first but without the front plate (the one with the cloud). The wiring is as follows:
- Raspberry pin 1 -> LV (low voltage) pin in the level shifter.
- Raspberry pin 2 -> 5v from the barrel jack.
- Raspberry pin 6 -> GND from the barrel jack.
- Raspberry pin 8 -> Printer RX (yellow wire).
- Raspberry pin 9 -> button common terminal.
- Raspberry pin 10 -> level shifter RXO (rx output)
- Raspberry pin 12 -> level shifter TXI (tx input)
- Raspberry pin 14 -> Printer logic GND ( the gnd wire that is with rx and tx from the printer).
- Raspberry pin 16 -> button normally open terminal.
- Level shifter TXO -> Neopixel signal input.
- Level shifter RXI -> Printer TX (green wire).
- Level shifter HV -> 5v from the barrel jack.
- Level shifter GND -> GND from the barrel jack.
- Neopixel GND -> GND from the barrel jack.
- Neopixel Vin -> 5v from the barrel jack.
- Printer Vin -> 5v from the barrel jack.
- Printer GND -> GND from the barrel jack.
And thats all, double check the connections and make sure everything is right. When you finish put the front plate of the printer and tight the screws. I glued a white acrilic at the back of the cloud and them the neopixel, so the entire cloud has a backlight powered by the neopixel.
Step 3: We Have the Hardware, It's Time for the Software
In this step I will assume that you already have installed Raspbian in the SD card and you have the wireless network up and running. A SSH link will be helpfull too. We will need to install some libraries and change a few settings, so let's begin:
Execute: "sudo raspi-config" and them go to Advanced options/serial and select "No" this will allow us to control, the serial port in the raspberry. We need it for controlling the thermal printer. After doing that run: "sudo reboot"
Now it's time for making the Neopixel works with the Raspberry, just follow the guide in this link and come back when you have the Neopixel up and running (stranstest), you don't need no rewire anything.
Now we will install all libraries and resources needed for our program, execute the following commands:
- "sudo apt-get install python-pip"
- "sudo pip install imapclient"
- "sudo apt-get install python-serial python-imaging python-unidecode"
- "sudo apt-get install git"
- "git clone https://github.com/DAFRELECTRONICS/IoTprinter"
Now we are going to test the printer, run "cd IoTprinter" and then "sudo python printertest.py". If your printer works you have made everything right. Now we almost finished, but we need to configure our email account, we will be using gmail.
Step 4: Configuring Gmail Account for the Printer
The step number one is go into your gmail, then in in "My account" and after that in "Sign-in & security". Search for "allow less secure apps" an turn it on.
And that all for a basic setup, but we are going to create an special folder with filtering. So we can guarantee that the printer only gets the desired emails.
To create our special folder we are going to click in the arrow in the search field of our inbox and define the parameters that we want. When you enter your parameters for the filter click in "Create filter with this search" at the botton.
In the next screen mark the options "skip inbox" and then "apply the label" and create a new label. This will be the folder name, I named it "Secret messages" but it can be anything. Just be sure that the name matches the one in the script in the raspberry pi. After that create the filter and you will see the new folder in your inbox, all the email with the parameters you defined will end in this folder. In my real aplication for my store th filter paramaters are the store email as sender and in subject is "new order".
Ok, we are ready to configure our script in the raspberry.
Step 5: Configuring the Script in the Raspberry
We need to do a few configurations in order to get things working. Firts in the project folder edit the file mailverifier.py, execute "sudo nano mailverifier.py" Now search for this parameters and change it:
- Firts of all search for Username, Password and Mailbox. Them put in your account data.
- If you want enter your custom string for filtering unwanted data in body2 processing.
When you are donde quit editor and save changes. After that run and test the script with "sudo python mailverifier.py" If you have problems when printing searh for the line "body = server.fecth....." and read the comments for fixing the problem.
If everything works well its time for running it automatically at boot. For that run "cd" then "sudo nano /etc/rc.local"
and after the line "#By default this script does nothing" add the following lines:
python mailverifier.py &
Exit and save the changes, now reboot the Raspberry Pi and enjoy your new IoT Printer.
Step 6: Color Codes of the Printer and Other Useful Info
If you push the button the inbox gets checked for new data and if you hold it the Raspberry is shut down(the neopixel goes off when this happen).
These are the color error codes:
- Green: booting script
- White: starting up the scrip
- Yellow: no paper in the printer.
- Red: no emails for printing.
- Blue: emails for printing available.
- Purple: no internet connection or error retrieving or porcessing data.
Step 7: Thank You for Reading, Enjoy :)
Thats all That I am going to cover in this Instructable, feel free to start hacking and adapt this base script to your own e-commerce platform. Remenber, I can´t do that for you, because your system may be different from mine.
If you like it, and want to support me give me a like in my sore fanpage: DAFR ELECTRONICS and also vote for this instructable in the contest.
Thank you, I hope you find it helpful. If you have a question feel free to ask .)
Daniel Fernandez R.
Runner Up in the
Raspberry Pi Contest