The main problem while working with Raspberry Pi is to know the IP address of the Raspberry Pi, which will be needed to login into it using some SSH or file transfer client. So today we will share some Python scripts to find the local IP address of your Raspberry Pi on the network and display it on the 16x2 LCD Screen. We will also add the script in the Crontab so that it can be run on every 10 minutes and we will have the updated IP address every time.

Interfacing 16x2 LCD with Raspberry Pi:

Before we will find the IP address of the Raspberry PI, first we need to interface 16x2 LCD with Raspberry Pi. Here in this Project we have used an external Adafruit Library for interfacing the 16x2 LCD with Raspberry Pi, using which you don’t need to write many lines of code to drive the LCD and you can directly print on LCD by just using one line of code. However this Library is created by Adafruit but it can used for any LCD module which has HD44780 controller.

To use the Adafruit Library, we first need to install it by using below commands. First command will clone the CharLCD repository (by Adafruit) on your Raspberry Pi, second command will take you inside that downloaded directory and finally we need to execute setup.py script, presented inside the Adafruit_Python_CharLCD directory, to install the library.

Now the library for 16x2 LCD has been installed and you can use its functions by just importing this library in your python program using the below line:

import Adafruit_CharLCD as LCD

There are some example scripts inside the ‘examples’ folder which is present in the library folder (Adafruit_Python_CharLCD). You can test the setup by running char_lcd.py example script. But before that, you need to connect the LCD pins with the Raspberry Pi as given below in the circuit diagram in next section.

You can also connect LCD with some other GPIO pins of Raspberry Pi, all you need to mention the correct interfacing pins in your python program like below. Learn more about Raspberry Pi GPIO Pins here.

Circuit Diagram:

Display IP Address of Raspberry Pi on LCD:

After interfacing 16x2 LCD with Raspberry Pi, now we need to get the IP address of Raspberry Pi and print it on LCD using Python Script. There are lot of ways to get the local IP address of Raspberry Pi, here we are describing three Python Scripts to get the IP address, you can use any of them.

Using Linux Commands:

On terminal, we can easily get the IP address by using hostname –I command, so if we can run the linux command from the python then we can get the IP address. So to run Linux commands from Python we need to import a library named commands, so the complete program will be like below:

Note: Repalce ‘wlan0’ with ‘eth0’ if your Raspberry Pi is on the Ethernet.

Using Socket programming:

Here in this program we will create a socket of family AF_INET and type SOCK_DGRAM using this line: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM), then we will create connection through the socket using some hostname or ip address like 8.8.8.8, you can also use some other website to connect with the socket like gmail.com. So we can get the local IP address of RPi from the socket which has been created for communication between Raspberry Pi and dummy IP address. Below is the full Program: