-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramFinale.py
More file actions
172 lines (140 loc) · 5.28 KB
/
Copy pathprogramFinale.py
File metadata and controls
172 lines (140 loc) · 5.28 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Importation des bibliothèques nécessaires
import RPi.GPIO as GPIO
import time
import serial
import board
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
# --- CONFIGURATION ---
BAUDRATE = 115200
SERIAL_PORT = '/dev/ttyS0' # Port série pour le module GSM
PIN_TRIGGER = 17 # GPIO17
# Initialisation I2C
i2c = busio.I2C(board.SCL, board.SDA)
# NUMERO ET MESSAGE à ENVOYER
NUMERO = "+33612345678"
MESSAGE = "Test message simple"
message1 = "BerryWatt test !"
message2 = "affichage OLED "
# Définir la largeur et la hauteur de l'écran
WIDTH = 128
HEIGHT = 64
# Initialisation de l'écran OLED
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)
# Effacer l'écran
oled.fill(0)
oled.show()
# Créer une image blanche
image = Image.new("1", (WIDTH, HEIGHT))
draw = ImageDraw.Draw(image)
# Définir une police
font = ImageFont.load_default()
# Envoie une commande AT au module 4G et retourne la réponse reçue.
def send_at_command(ser, command, timeout=2):
ser.write((command + '\r\n').encode())
time.sleep(0.1)
end_time = time.time() + timeout
response = b""
while time.time() < end_time:
if ser.in_waiting:
response += ser.read(ser.in_waiting)
if b'OK' in response or b'ERROR' in response or b'>' in response:
break
time.sleep(0.1)
return response.decode(errors='ignore')
# Envoie un SMS au numéro donné via le module 4G en utilisant les commandes AT.
def send_sms(phone_number, message):
with serial.Serial(SERIAL_PORT, BAUDRATE, timeout=1) as ser:
print(send_at_command(ser, 'AT')) # Vérification de la connexion au module
time.sleep(0.5)
print(send_at_command(ser, 'AT+CMGF=1')) # mode texte
time.sleep(0.5)
print(send_at_command(ser, 'AT+CSCS="GSM"')) # jeu de caractères GSM
time.sleep(0.5)
response = send_at_command(ser, f'AT+CMGS="{phone_number}"', timeout=5)
print(response)
if '>' in response:
time.sleep(1)
ser.write(message.encode('ascii') + b"\x1A")
time.sleep(5)
response = ser.read_all().decode(errors='ignore')
print(response)
else:
print("Erreur : pas de prompt '>' reçu.")
def draw_image1():
image = Image.new("1", (WIDTH, HEIGHT))
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
# Triangle attention gauche
draw.polygon([(10, 2), (3, 14), (17, 14)], outline=255, fill=255)
draw.line((10, 6, 10, 10), fill=0, width=1)
draw.rectangle((10, 12, 10, 12), fill=0)
# Texte
draw.text((28, 2), "AVERTISSEMENT", font=font, fill=255)
# Triangle attention droite
draw.polygon([(118, 2), (111, 14), (125, 14)], outline=255, fill=0)
draw.line((118, 6, 118, 10), fill=255, width=1)
draw.rectangle((118, 12, 118, 12), fill=255)
# Ligne
draw.line((0, 18, WIDTH, 18), fill=255, width=1)
# Messages texte
draw.text((15, 22), "Coupure detectee", font=font, fill=255)
draw.text((10, 34), "Mode secours actif", font=font, fill=255)
# Batterie
draw.rectangle((54, 50, 72, 60), outline=255, fill=0)
draw.rectangle((72, 53, 74, 57), outline=255, fill=255)
draw.rectangle((57, 53, 69, 57), outline=255, fill=255)
return image
def draw_image2():
image = Image.new("1", (WIDTH, HEIGHT))
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
draw.text((35, 2), "BerryWatt", font=font, fill=255)
draw.line((0, 18, WIDTH, 18), fill=255, width=1)
draw.text((12, 22), "Mode normal actif", font=font, fill=255)
# Logo prise européenne
plug_w, plug_h = 28, 28
plug_x, plug_y = 50, 35
draw.ellipse((plug_x, plug_y, plug_x+plug_w, plug_y+plug_h), outline=255, width=2)
pin_radius = 3
pin1_x = plug_x + 7
pin2_x = plug_x + 21
pin_y = plug_y + 14
draw.ellipse((pin1_x-pin_radius, pin_y-pin_radius, pin1_x+pin_radius, pin_y+pin_radius), fill=255, outline=0)
draw.ellipse((pin2_x-pin_radius, pin_y-pin_radius, pin2_x+pin_radius, pin_y+pin_radius), fill=255, outline=0)
# Contact de terre (rond plein au sommet)
earth_radius = 2
earth_x = plug_x + plug_w // 2
earth_y = plug_y + 7
draw.ellipse((earth_x - earth_radius, earth_y - earth_radius, earth_x + earth_radius, earth_y + earth_radius), fill=255, outline=0)
return image
# --- FONCTION DE DETECTION FRONT GPIO ---
def front_detecte(channel):
etat = GPIO.input(channel)
if etat == 0:
print("⚡ Front descendant détecté sur GPIO17 ! --> Envoi SMS")
oled.image(image1)
oled.show()
send_sms(NUMERO, MESSAGE)
else:
oled.image(image2)
oled.show()
print("⚡ Front montant détecté sur GPIO17 !")
# --- PROGRAMME PRINCIPAL ---
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_TRIGGER, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(PIN_TRIGGER, GPIO.BOTH, callback=front_detecte, bouncetime=200)
image1 = draw_image1()
image2 = draw_image2()
oled.image(image2)
oled.show()
try:
print("En attente d'un front montant ou descendant sur GPIO17 (3,3V ↔ 0)...")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Interruption par l'utilisateur")
finally:
GPIO.cleanup()