This repository contains all the code for interfacing with a 16x2 character I2C liquid-crystal display (LCD). This accompanies a Youtube tutorial: Raspberry Pi - Mini LCD Display Tutorial.
You can buy one of these great little I2C LCD on eBay or somewhere like the Pi Hut.
Please refer to Original repo LCD from The Raspberry Pi Guy and do a test before try my script.
I'm sorry for the length of the explanations. It’s a “big home automation project”.
I separated it into 3 files to go step by step:
HApart1_temperature.pyHApart2_now_playing.pyHApart3_telegram.py
This means that what you learn in HApart1_temperature.py will be used in HApart2_now_playing.py and HApart2_now_playing.py in HApart3_telegram.py. This project is above all a proof of concept. But by going step by step you will understand that you can modify it for your needs.
Please note that the SECRETS.json and .cache files (see below for more information) must be placed at the root of the LCD project
Install all python packages from requirements.txt
Part of the documentation is this README.md. I also wanted to make modular scripts. Some features won't be useful to everyone, so I wanted to make it easy to "pick and choose" features.
Please read all notes.
In each script you would find sections like this:
# ----------------------------DS18B20--------------------------- # You can disable this feature by adding # in front of each line house_temp = get_temp_ds18b20(temp_file) # ----------------------------DS18B20---------------------------In this example, if you don't want house_temp, you need to had # in front of the line:
# ----------------------------DS18B20--------------------------- # You can disable this feature by adding # in front of each line # house_temp = get_temp_ds18b20(temp_file) # ----------------------------DS18B20---------------------------To get the CPU temperature it's not very complicated. Answers were given in the Issues of this GitHub Repo too.
On the other hand, to get the temperature of my house (especially my apartment), I used a DS18B20 thermal probe.
For the installation of the DS18B20 probe, I refer you to this Tutorial. I followed it for the probe part. Then for the display on the LCD I used this GitHub repo.
If you followed the steps, the temperatures save to w1_slave. In the script, change the path of temp_file depending on where your w1_slave is located
Note: if you don't have a DS18B20 probe, disable all DS18B20 section by adding # in front of each line and switch:
# ----------------------------DS18B20--------------------------- # You can disable this feature by adding # in front of each line display.lcd_display_extended_string(' {0x00} ' + cpu[0:4] + ' {0x01} ' + house_temp[0:4], 2) # Line 2 with CPU and DS18B20 temps # display.lcd_display_extended_string(' {0x00} ' + cpu[0:4], 2) # Line 2 with only CPU temp # ----------------------------DS18B20---------------------------to:
# ----------------------------DS18B20--------------------------- # You can disable this feature by adding # in front of each line # display.lcd_display_extended_string(' {0x00} ' + cpu[0:4] + ' {0x01} ' + house_temp[0:4], 2) # Line 2 with CPU and DS18B20 temps display.lcd_display_extended_string(' {0x00} ' + cpu[0:4], 2) # Line 2 with only CPU temp # ----------------------------DS18B20---------------------------For this step we will need to use the APIs.
APIs generally require a token and an ID. The "token" represents the instance that you will call to obtain the information and the ID is you. For security reasons I never write the token and ID directly in the code. You never know.
To avoid writing API information in the code, I use a SECRETS.json in the same place as the LCD script. The scripts then read the SECRETS.json to get the information.
{ "TELEGRAM_ID_OWNER": YOUR_TELEGRAM_ID, "TELEGRAM_BOT_TOKEN": "YOUR_TELEGRAM_BOT_TOKEN", "SPOTIFY_CLIENT_ID": "YOUR_SPOTIFY_CLIENT_ID", "SPOTIFY_CLIENT_SECRET": "YOUR_SPOTIFY_CLIENT_SECRET", "SPOTIFY_REDIRECT_URI": "YOUR_SPOTIFY_REDIRECT_URI", "TRAKT_CLIENT_ID": "YOUR_TRAKT_CLIENT_ID", "TRAKT_USERNAME": "YOUR_TRAKT_USERNAME" }Note 1: There are also already the keys for Telegram which we will see later
Note 2: You can use Trakt, Spotify or both. Don't forget to disable/enable section depending on your purpose
Trakt.tv is a smartphone application and website that allows you to follow the progress of our films and series. They have a function that allows you to directly track when an episode or film is playing. You can use Trakt.tv with Kodi, Netflix, Plex, VLC... I refer you directly to Trakt.tv
- Create an account on Trakt.tv
- Replace
YOUR_TRAKT_USERNAMEin theSECRETS.jsonwith yourUsername - Go to
Settings > Your API appsand create an API app - In your API app, you will find
Client ID. ReplaceYOUR_TRAKT_CLIENT_IDin theSECRETS.jsonwith yourClient ID
For Spotify, it's a little more complicated. But we'll take it easy.
-
Create a Spotify API, configure
Redirect URI(e.g http://localhost:9090), selectWeb API -
Go to the
Settingsof your Spotify API. -
In the
Basic information tab, you will find theClient ID, theClient Secretand theRedirect URI(which you set by configuring the API) -
Replace the information from
SECRETS.jsonas you did for Trakt.tv -
Add your Spotify account in the
User Management tab -
For this step I suggest you do it on Windows or with a graphic interface. The Spotify API needs a connection to a web page for proper authentication. Once the connection is verified, a
.cachefile is created (note:.cacheis a hidden file). So, let's go on !- If you have follow previews steps, you have a
SECRETS.jsonwith Spotify parts filled. - Run:
python3 get_cache_spotify.py- A hidden
.cachefile has just been created in the same folder
- If you have follow previews steps, you have a
The script will read the SECRETS.json and .cache files. To avoid any permission conflict problems:
sudo chmod 7777 SECRETS.json sudo chmod 7777 .cache Note: I added security which automatically gives all permissions to .cache. I realized that restarting the RPi can change the permissions of this file
Home Automation Now Playing Music Youtube Video
Home Automation Now Playing Movie/Show Youtube Video
Why use Telegram? Because it's quite simple to use. You can code whatever you want in python and since the bot runs on the RPi it allows you to execute commands without opening an SSH or other connection. I'm sure there are ways to make it simpler but with this you can even control the RPi without being at home. You can automate events based on the time, the temperature of your CPU, your apartment... You can also write a text directly on the LCD by sending a message to the Telegram Bot (not implemented but easy to do)
- How to get your ID:
- How to get your TOKEN:
- Config a bot with @BotFather:
- Create a bot with
/newbotand follow instructions - Get API token with
/mybots, select your bot and get API token - Copy/paste your token in
SECRETS.jsonatTELEGRAM_BOT_TOKENbetween (') or (")
- Create a bot with
- Don't forget to send
/startat your Telegram bot
- Config a bot with @BotFather:
My script is clearly a mix of the functions I use the most. Some won't suit you and some you will miss.
Just send /help to your Telegram bot and see all command ! 😊
Tips: you can set command from /help with @BotFather to have a quick access
- copy message from
/help - send
/mybotsto @BotFather and select your Telegram bot - select
Edit Bot - select
Edit Commands - paste message from
/helpwithout/
| Command | Description |
|---|---|
/temp | Get CPU temperature and house temperature from DS18B20 |
/quick_update | To update and upgrade without autoremove and reboot |
/update | To update, upgrade, autoremove AND REBOOT |
/reboot | Sometimes it's good |
/shutdown | As expected |
/lcd_on | Turn ON backlight of LCD |
/lcd_off | Turn OFF backlight of LCD |
/test | Is my Telegram bot still working? |
/help | A little reminder |
Home Automation Telegram Bot Youtube Video
RPi can heat, so to prevent an overheat I had a command to control a fan plugged at GPIO_PIN 17 to cooldown temperature:
- You can set critical temperature:
GPIO_PIN_FAN = 17 fan = OutputDevice(GPIO_PIN_FAN) def get_temp_cpu(): cpu = CPUTemperature() temp = cpu.temperature cpu = str(temp) if temp > 65 and not fan.value: # Warning hot temperature fan.on() bot.sendMessage(TELEGRAM_ID_OWNER, f"WARNING! Temperature too HOT! {cpu[0:4]}°C") elif temp > 85: # Alert too hot temperature + shutdown fan.on() bot.sendMessage(TELEGRAM_ID_OWNER, f"ALERT! CRITICAL TEMPERATURE! {cpu[0:4]}°C ! SHUTDOWN !") os.system('sudo shutdown now') elif temp < 55 and fan.value: # Temperature under control fan.off() bot.sendMessage(TELEGRAM_ID_OWNER, f"Temperature under control. {cpu[0:4]}°C. Good job !") return cpu
I add a function to auto-update your Raspberry every monday @ 02:30 and major update with reboot appends every 1st of each month @ 02:00. I always plugged a LED at GPIO_PIN 27 to see when RPi do the update.
- Day: M=0; T=1; W=2...
GPIO_PIN_UPDATE = 27 update = OutputDevice(GPIO_PIN_UPDATE) # Quick update every monday @ 2:30 if day == 0 and hour == '02:30': bot.sendMessage(TELEGRAM_ID_OWNER, 'Starting weekly update...') os.system('sudo apt-get update -y') bot.sendMessage(TELEGRAM_ID_OWNER, 'Weekly update done.\nStarting weekly upgrade...') os.system('sudo apt-get upgrade -y') bot.sendMessage(TELEGRAM_ID_OWNER, 'Weekly upgrade done') # Update every 1st of month @ 2:00 if date == '1' and hour == '02:00': bot.sendMessage(TELEGRAM_ID_OWNER, 'Starting monthly update...') os.system('sudo apt-get update -y') bot.sendMessage(TELEGRAM_ID_OWNER, 'Monthly update done.\nStarting monthly upgrade...') os.system('sudo apt-get upgrade -y') bot.sendMessage(TELEGRAM_ID_OWNER, 'Monthly upgrade done.\nStarting monthly autoremove...') os.system('sudo apt-get autoremove -y') bot.sendMessage(TELEGRAM_ID_OWNER, 'Monthly autoremove done.\nStarting reboot...\nSee U soon')
Note: GPIO pin for Fan and LED update can be disabled if you don't want this feature.
If you have any questions, I invite you to create Issues. I would be happy to help you. I tried to make nice documentation and a fairly quick and concise tutorial.
But I hope that with 3 little scripts, you will find what you are looking for



