-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (98 loc) · 3.69 KB
/
main.py
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
from machine import I2C, PWM, Pin
import network, time
from max6675 import MAX6675
from ssd1306 import SSD1306_I2C
from oled_image import find_image
from api import api
def connect_wifi(connection, password) -> bool:
global my_network
my_network = network.WLAN(network.STA_IF)
if not my_network.isconnected(): # if it is not connected
my_network.active(True) # activate interface
my_network.connect(connection, password) # try to connect to network
print('Connecting to Wifi', connection +"…")
timeout = time.time()
while not my_network.isconnected(): #while it's not connected
if (time.ticks_diff(time.time(), timeout) > 10):
return False
return True
wifi_connection = "Claro_61039A"
password = "M5C9A3W7P3W8"
# setup the oled
width = 128 # oled width
height = 64 # oled height
i2c = I2C(0, scl = Pin(22), sda = Pin(21))
oled = SSD1306_I2C(width, height, i2c)
# setup the buzzer
buzzer = PWM(Pin(32), freq = 500)
buzzer.duty(0) # don't produce a sound
# setup the blue and red leds
blue_led = Pin(18, Pin.OUT)
red_led = Pin(19, Pin.OUT)
# setup the thermocuple with MAX6675 module
sck = 15
cs = 2
so = 4
thermocouple = MAX6675(cs, sck, so)
#shows a welcome message
oled.fill(0) #limpiar
oled.blit(find_image("images/welcome.pbm"), 0, 0)
oled.show() #mostrar
time.sleep(2)
api = api() # starts api
# the method that will be executed at runtime
def main() -> None:
if connect_wifi(wifi_connection, password): # connect to wifi
oled.fill(0)
oled.blit(find_image("images/check.pbm"), 0, 0)
oled.text("Connected", 55, 20)
oled.show()
print ("Conexión exitosa!")
print('Datos de la red (IP/netmask/gw/DNS):', my_network.ifconfig())
time.sleep(1.5)
while True:
#clear the screen
oled.fill(0)
# read temperature
# when there are some errors with sensor, it return "-" sign and CS pin number
# in this case it returns "-22"
temp = thermocouple.read_temp()
# format the temperature
text = "{:.2f} C".format(temp)
if (temp >= 90): # if temperature is 90 or greater, turn on red led and play a sound for half second
red_led.on()
blue_led.off()
buzzer.duty(5)
time.sleep(.5)
buzzer.duty(0)
red_led.off()
oled.blit(find_image("images/fire.pbm"), 0, 0)
elif (temp > 75): # if temperature is greater than 75, turn on blue led
red_led.off()
blue_led.on()
if (buzzer.duty() != 0):
buzzer.duty(0)
oled.blit(find_image("images/warning.pbm"), 0, 0)
else: # temperature is normal
blue_led.off()
red_led.off()
if (buzzer.duty() != 0):
buzzer.duty(0)
oled.blit(find_image("images/temperature.pbm"), 0, 0)
oled.text(text, 55, 20)
oled.show()
print("Temperature", text)
api.graph_temp(temp)
if (temp >= 90): # send an alert if temperature is more than 90°C
api.send_email(temp)
time.sleep(1)
else: # if it is not connected
oled.fill(0)
oled.blit(find_image("images/error.pbm"), 0, 0)
oled.text("Can't", 60, 20)
oled.text("connect", 60, 30)
oled.show()
print("Can't connect :(")
my_network.active(False)
if __name__ == "__main__":
main()