-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_ip_and_email
133 lines (101 loc) · 3.78 KB
/
display_ip_and_email
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import time
import subprocess
import os
import smtplib
from email.mime.text import MIMEText
from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
# Path to the credentials file and the file storing the last known IP
credentials_file_path = '/home/pi/ip_display/credentials.txt'
ip_file_path = '/home/pi/ip_display/last_ip.txt'
# Function to read email credentials from the credentials file
def read_credentials():
credentials = {}
if os.path.exists(credentials_file_path):
with open(credentials_file_path, 'r') as file:
for line in file:
key, value = line.strip().split('=')
credentials[key] = value
else:
raise FileNotFoundError(f"Credentials file not found at {credentials_file_path}")
return credentials
# Create the I2C interface and OLED display setup
i2c = busio.I2C(SCL, SDA)
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
# Clear display.
disp.fill(0)
disp.show()
# Create blank image for drawing on OLED.
width = disp.width
height = disp.height
image = Image.new("1", (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Load default font for OLED.
font = ImageFont.load_default()
def get_internal_ip():
try:
# Run the hostname -I command and capture the output
result = subprocess.run(['hostname', '-I'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Get the IP address from the output (assuming the first IP is the one you need)
internal_ip = result.stdout.strip().split()[0]
return internal_ip
except Exception as e:
return f"Error getting internal IP: {str(e)}"
def send_email(internal_ip, credentials):
from_address = credentials['from_address']
to_address = credentials['to_address']
subject = credentials['subject']
username = credentials['username']
password = credentials['password']
body = f"Oh no, not this again—our IP decided to play hide and seek! Here's the new one: {internal_ip}"
msg = MIMEText(body)
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = subject
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(username, password)
server.sendmail(from_address, to_address, msg.as_string())
server.quit()
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")
def read_last_ip():
if os.path.exists(ip_file_path):
with open(ip_file_path, 'r') as file:
return file.read().strip()
return None
def write_last_ip(internal_ip):
with open(ip_file_path, 'w') as file:
file.write(internal_ip)
def update_oled(ip_address):
# Clear the OLED display
draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Display the IP address
draw.text((0, 0), "IP: " + ip_address, font=font, fill=255)
# Show the updated image on the OLED display
disp.image(image)
disp.show()
def main():
time.sleep(20) # Wait for 20 seconds to ensure network is ready
current_internal_ip = get_internal_ip()
if "Error" in current_internal_ip:
print(current_internal_ip)
return
# Update the OLED display with the current IP
update_oled(current_internal_ip)
# Check if IP has changed, if so, send an email
last_internal_ip = read_last_ip()
if current_internal_ip != last_internal_ip:
print(f"IP changed! New IP: {current_internal_ip}")
# Load credentials and send email
credentials = read_credentials()
send_email(current_internal_ip, credentials)
# Save the new IP to the file
write_last_ip(current_internal_ip)
if __name__ == "__main__":
main()