How to Find IP Address of Raspberry Pi using Python Script
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 16×2 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 16×2 LCD with Raspberry Pi:
Before we will find the IP address of the Raspberry PI, first we need to interface 16×2 LCD with Raspberry Pi. Here in this Project we have used an external Adafruit Library for interfacing the 16×2 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.
git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git cd ./Adafruit_Python_CharLCD sudo python setup.py install
Now the library for 16×2 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.
# Raspberry Pi pin setup lcd_rs = 18 lcd_en = 23 lcd_d4 = 24 lcd_d5 = 16 lcd_d6 = 20 lcd_d7 = 21 lcd_backlight = 2
Now you can directly use the functions provided by Adafruit Library to control the LCD. Some of the functions are given below; you can find more in example script:
- lcd.message(message) = To print the text on LCD.
- lcd.clear() = To clear the LCD.
- set_cursor(col, row) = Move the cursor to any position at column and row.
- lcd.blink(True) = To blink the cursor (True or False)
- lcd.move_left() = To move the cursor to Left by one position.
- lcd.move_right() = To move the cursor to Right by one position.
If you want to connect the LCD without using any external library then you can check our previous tutorial, where we have written all the functions for 16×2 LCD. Check this one to interface the LCD in 8-bit Mode and this one to interface the LCD in 4-bit mode.