Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
Programming on Raspberry Pi with Python: Sending IP address on Telegram channel on Raspberry Pi reboot

In the first article, we completed the Raspberry Pi Setup, installed OS on SD card and updated the packages. We were connected to Raspberry Pi using LAN cable and external monitor via HDMI cable.

In the second article we configured WIFI and enabled SSH. We were not using LAN cable to connect the system. We were able to ssh into Raspberry Pi from our laptop. 

Programming on Raspberry Pi with Python: Raspberry Pi Setup

Programming on Raspberry Pi with Python: WIFI and SSH configuration

But there was one problem. How do we know the local IP address of Raspberry Pi when it reboots. We will address the same issue in this article.

We will create a python script which will run on every reboot and will send the IP address to a telegram channel.

We are using the Raspberry Pi 3 B+ model.

Telegram Channel and Bot:

 - First, we need to create a telegram bot and add it as an administrator to the desired telegram channel where you wish to receive the messages.

 - Follow this step by step article on how to create a telegram channel and add a telegram bot to it as administrator

Python Script:

 - Login to your Raspberry pi.

 - First, create a virtual environment using python3. We will install all the required packages and dependencies in this virtual environment using pip.

 - Please refer this article to create a virtual environment, to activate it and installing the packages.

 - Create a file, requirements.txt and paste below content in it. It is a list of packages you will require for your future python programs.


 - Activate the virtual environment and install the telegram package in it using the command pip install -r requirements.txt.

 - Now create a python script with below content.

import telegram
import subprocess
import time

bot = telegram.Bot(token="YOUR BOT TOKEN GOES HERE")
msg = subprocess.check_output("hostname -I", shell=True).decode('ascii')
status = bot.send_message(chat_id="@YOUR CHANNEL ID", text=msg, parse_mode=telegram.ParseMode.HTML)

We are using telegram package.

Since this script will be executed after Raspberry pi reboot, we introduced sleep time of 60 seconds in the scripts so that by then Raspberry Pi is connected to the available network.

Then in the next line, we created the bot. Use your bot token. Refer this article to find out how to get the bot token.

Then run the command hostname -I and collect its output and send it to the channel.

Scheduling Script:

 - Once the script is ready, run it manually inside the virtual environment using python3 to test the output.

 - Now edit the crontab using the command crontab -e.

 - Append the below line in crontab.

@reboot ~/py3venv/bin/python3 ~/practice/startup_scripts/

Use the python3 path and script path accordingly.

Now, this script will be executed at every reboot of Raspberry pi and the newly assigned IP address will be sent to you on your telegram channel which you can use to SSH into it.

In the screenshot below you can see, IP address reported by the Python script and temperature of Raspberry Pi every 10 minutes.

telegram channel raspberry pi ip address and temprature


 - How to create a completely automated telegram channel with python

Purchase Raspberry Pi Starter Kit Here.

0 comments on 'Programming On Raspberry Pi With Python: Sending Ip Address On Telegram Channel On Raspberry Pi Reboot'
Login to comment

Related Articles:
Programming on Raspberry Pi with Python: Controlling LED
Starting with Raspberry Pi programming with Python, Controlling LED with Python Program, Blinking LED with Raspberry Pi, Using GPIO pins to control the LED on Raspberry Pi with Python Program, Raspberry Pi and python...
Programming on Raspberry Pi with Python: WIFI and SSH configuration
How to setup Raspberry Pi, Installing Raspbian on Raspberry Pi 3 B+ model and configure settings, Starting with Raspberry Pi programming with Python, Configuring WIFI on Raspberry PI, Enabling SSH on Raspberry PI, RaspberryPi and Python...
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap