Print Friendly, PDF & Email

Difficulty level: Beginner

Approx reading time:

Components Required:

1. Raspberry Pi 3 model B

2. MicroSD card 8 or 16 GB (Class 4 and above) with Raspbian

3. Windows PC / Linux PC (Tested on Windows 10, Ubuntu 14.04 LTS)

4. Ethernet cable (Category 5 – also called Cat 5)

5. Micro USB cable

Way to go ->

1. Login to your Pi using PuTTy or your Ubuntu terminal.

2. Login to your Pi GUI using VNC server (Linux / Windows).

3. In your Pi create a new folder on the Desktop called ipemail by right mouse clicking on the desktop.

3_NewFolder

4. Open the folder: ipemail, and create new .txt file. Make sure name it as last_ip.txt.

4_LastIP

5. Close the folder ipemail.

6. From the menu on the top left open Python 3 (IDLE).

6_OpenIdle3

7. In IDLE, create a new file by clicking on File -> New File in the menu bar.

7_NewFile

8. Save the file by clicking on File -> Save As… in the menu bar. The file should be saved in folder: ipemail. The name could be: ipemail.py.

8_FileSaved_2

9. In the file ipemail.py, write the following code with comments (line starting with “#”) for clear understanding and save the file (press Cntrl + S on your PC keyboard).

9_Code

from urllib.request import urlopen  # in python3
# from urllib import urlopen - In python2
import re
import smtplib


# Setup our login credentials
from_address = "pineoserver@gmail.com"
to_address = "pineoserver@gmail.com"
subject = "RPi3 IP"
username = "pineoserver@gmail.com"
password = "Password"  # password of your newly created gmail account.

# Setup where we will get our IP address
url = "http://checkip.dyndns.org"
print ("Our chosen IP address service is:", url)

# Open upthe url, then read the content, and take away to IP address
request = urlopen(url).read().decode("utf-8")
# Extract the IP address only
ourIP = re.findall("d{1,3}.d{1,3}.d{1,3}.d{1,3}", request)
ourIP = str(ourIP)
print ("Our IP address is:", ourIP)


def send_email(ourIP):
    # Body of the email
    body_text = ourIP + " is our RPi3 IP address"
    msg = "rn".join(["To: %s" % to_address,
                       "From: %s" % from_address,
                       "Subject: %s" % subject,
                       "", body_text])

    # Actually send the email!
    server = smtplib.SMTP("smtp.gmail.com:587")
    server.starttls()  # Our security for transmission of credentials
    server.login(username, password)
    server.sendmail(from_address, to_address, msg)
    server.quit()
    print ("Our email has been sent!")

# send_email(ourIP)

# Open up last_ip.txt, and extract the contents
with open("/home/pi/Desktop/ipemail/last_ip.txt", "rt") as last_ip:
    last_ip = last_ip.read()  # Read the text file

# Check to see if our IP address has really changed
if last_ip == ourIP:
    print ("Our IP address has not changed.")
else:
    print ("We have a new IP address.")
    with open("/home/pi/Desktop/ipemail/last_ip.txt", "wt") as last_ip:
        last_ip.write(ourIP)
    print ("We have written the new IP adddress to the text file.")
    send_email(ourIP)

10. Before running the code, create a new email ID at gmail.com, eg:- pineoserver@gmail.com.

11. Login to your newly created gmail account.

12. Open google.com, while being logged in to your newly created email account.

13. Google search: Allowing less secure apps to access your gmail account.

14. Follow the instructions by clicking on the first link that google search throws up. (Note:- Following these instructions will enable the python code that you wrote to login to your gmail account and send you your IP address.)

14_ReduceSecurity

15. After reducing the security of your gmail account. Run the code by clicking on Run -> Run Module in the menu bar. 

 15_RunCode

16. Check your gmail email account. There you will have received your IP address.

16_CheckEmail

17. You can also check last_ip.txt for your ip address. This file is only updated if your ip address changes.

17_IPInFile

18. You can run your python script repeatedly or the script can be automated to send an email periodically (eg:- every 2 minutes).

19. To automate the script, type:

crontab -e

in your Pi’s terminal. Type the following at the end of the file to send an email every 2 minutes, if the IP address changes (this what the if-else condition in the python script does.):

*/2 * * * * /usr/bin/python3 /home/pi/Desktop/ipemail/ipemail.py

19_CrontabEdit

20. Press Cntrl + X then Y and finally Enter to save the file.

21. To check whether this works, go to last_ip.txt, make some changes in the IP address and save the file.

22. After 2 minutes, check your newly created gmail email account for your current IP address.

23. You can also check last_ip.txt for your ip address. This file is only updated if your ip address changes.

Read full tutorial at

Rating widget: