How to Display a Raspberry Pi Telegram from Your Phone to an LCD
2018-11-13 | By Maker.io Staff
License: GNU Lesser General Public License Breadboards Raspberry Pi
How to Display a Telegram From Your Phone on an LCD Display With a Raspberry Pi
This month, we learned how to use MicroPython with the Raspberry Pi, create a flashing LED, and use an LCD! In this project we will combine what we learned to create a message display system where telegram messages sent to the Raspberry Pi show up on the attached LCD.
BOM
- Raspberry Pi 3 Model B with Raspbian
- HD44780 16 x 2 LCD
- Jumper wires
- Breadboard
- RPLCD Python library (via PIP)
- Telegram (via PIP)
The Schematic
You can find the full Scheme-It schematic for this project here.
Installation
Before we can get this project to work, we need to obtain several Python libraries. PIP is a python installation tool that comes with Raspbian by default, so, first, we need to open a terminal window. Inside the terminal window, start by running the following command (this will install telegram):
sudo pip install python-telegram-bot
Next, we need to install a special LCD library that contains all the functions needed for the Raspberry Pi to interface with an LCD module using the GPIO pins. Inside the terminal window, run the following command:
sudo pip install RPLCD
Set Up a Telegram bot
With the needed Python libraries installed, we now need to create a new telegram bot. To do this, you will first need to get telegram on a mobile device and then search for a contact called “BotFather”. Once you have found this contact, send the message “/start”, and this will bring up a large reply that contains different commands.
- Send the message “/newbot”.
- It will ask you what you want to call it. Give it any name you want to.
- Then it will ask for a username. Again, call it whatever you wish
- If the username is unused, it will return a long string of characters called the token.
The API token helps us use the bot to make our program receive messages from other telegram devices and display those messages on the LCD. The message sent by the BotFather will also include a link that you can use on your phone to start a conversation with your bot.
How It Works
While there is some circuitry in this project, it is not complex at all, and it only requires connections between the LCD (HD44780 based) and the Raspberry Pi, as well as one potentiometer for controlling the on-screen contrast. The real magic behind this project lies in the software! The project code is written in Python and consists of several sections:
- Library imports
- Initialization
- Function definitions
- Telegram setup
- Clean-up
Library Imports
The first few lines in the program import several libraries, including RPi.GPIO, RPLCD.gpio, and telegram. RPi.GPIO is a library that is used to access the GPIO pins, the RPLCD.gpio library is used to control HD44780 LCDs using the GPIO pins, and the telegram library provides easy-to-use functions to get our bot working.
import RPi.GPIO as GPIO
from RPLCD.gpio import CharLCD
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
ConversationHandler)
Initialization
The next section of code is the initialization, which involves two different actions. First, we create an LCD object that will allow us to initialize the LCD, write text to it, and clear the display. Since the RPLCD library has no knowledge of which pins we chose on the GPIO, we have to tell it.
- pin_rs=19: We are using pin 19 as the RS pin.
- pin_rw=None: There is no RW pin (write only).
- pin_e = 16: We are using pin 16 as the E pin.
- pins_data=[21,18,23,24]: These are the data pins DB4, DB5, DB6, and DB7.
- numbering_mode=GPIO.BOARD: We are using pin numbers and not pin names.
Next, we must set the value of the variable token to the token of our bot. This value will be unique to your bot and should not be shared, as anyone with that token can control your bot.
lcd = CharLCD(pin_rs=19, pin_rw=None, pin_e=16, pins_data=[21, 18, 23, 24],
numbering_mode=GPIO.BOARD)
token = 'SUPERAWESOMERAREAMAZINGULTRADELICIOUSCARD'
Function Definitions
When our bot receives different commands and messages, we need a way to handle those events. The telegram library handles this by attaching a function to a handler that will call the specified function when the handler event fires. To put it simply, when the bot receives a specific message, the Python program will call a specific function.
The first function that we define is the “start” function, which will handle any clients who connect and send the command “/start”. When this is detected, the bot first sends a reply to the sender, saying “Send a message”. The function also clears the LCD and writes the string “USER ONLINE”.
The second function that we define is the message handler function that will handle non-command messages. The function is called “msgh”, which stands for “message handler”. When called, the first task involves getting the message and storing it into a variable called “string”. Then, the program attempts to reply “Got your message”, as well as display the obtained message on the LCD.
def start(bot, update):
update.message.reply_text("Send a message")
lcd.clear()
lcd.write_string("USER ONLINE")
print('Connection')
def msgh(bot, update):
string = update.message.text
try:
update.message.reply_text("Got your message")
lcd.clear()
lcd.write_string(string)
except:
print("whoops")
Telegram Startup
This section of code starts by creating an updater object, which takes the bot token. Then, a dispatcher object is created, which allows us to link specific events to the two functions we made earlier. The program then creates two handlers: “start_handler” and “msg_handler”. These handlers are then attached to the functions using the “.add_handler()” function. The updater is then started.
updater = Updater(token)
dispatcher = updater.dispatcher
start_handler = CommandHandler('Start', start)
msg_handler = MessageHandler(Filters.text, msgh)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(msg_handler)
updater.start_polling()
updater.idle()
Clean-up
The last piece of code is a single line of GPIO clean-up code that frees up any used pins so that other programs can use them.
GPIO.cleanup()
Complete Python Code
import RPi.GPIO as GPIO
from RPLCD.gpio import CharLCD
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
ConversationHandler)
lcd = CharLCD(pin_rs=19, pin_rw=None, pin_e=16, pins_data=[21, 18, 23, 24],
numbering_mode=GPIO.BOARD)
token = 'YOUR TOKEN HERE’
def start(bot, update):
update.message.reply_text("Send a message")
lcd.clear()
lcd.write_string("USER ONLINE")
print('Connection')
def msgh(bot, update):
string = update.message.text
try:
update.message.reply_text("Got your message")
lcd.clear()
lcd.write_string(string)
except:
print("whoops")
updater = Updater(token)
dispatcher = updater.dispatcher
start_handler = CommandHandler('Start', start)
msg_handler = MessageHandler(Filters.text, msgh)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(msg_handler)
updater.start_polling()
updater.idle()
GPIO.cleanup()
Testing It Out
With the Python program ready to go, it’s time to test everything out. The best way to launch the program is via a terminal window, as PIP does not install Python libraries into locations accessible to the Python IDLE. This may be because Python IDLE is using its own Python installation. Assuming that your document is held in “Documents”, you will first need to change the working directory.
sudo cd Documents
The next instruction is actually executing the code, which is done using the following code (assuming you called the Python file “aprilProject.py”.
sudo python aprilProject.py
At this point, send the message “/start” to the telegram bot that you created, and you should see a response on your phone. If you see this response, go ahead and type any message you want and send it; that message should now appear on the LCD.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum