import machine
from machine import Pin, I2C, ADC
from time import sleep
import ssd1306
import dht
import math
#### HALL BUTTON AND MOTION SENSOR #####################
button = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
ms_hall = Pin(5, Pin.IN)
led_blue = Pin(18, Pin.OUT)
button_falling = 0
ms_hall_rising = False
button_state = 1
ms_hall_state = False
button_before = 1
ms_hall_before = False
hall_door = 0
hall_counter = 0
#### ROOM MOTION SENSORS ###############################
ms_room_1 = Pin(4, Pin.IN)
ms_room_2 = Pin(2, Pin.IN)
led_red = Pin(22, Pin.OUT)
ms_room_1_rising = False
ms_room_2_rising = False
ms_room_1_state = False
ms_room_2_state = False
ms_room_1_before = False
ms_room_2_before = False
room_door = 0
room_counter = 0
#senso de temp ntc
sensorNTCIn = ADC(Pin(12))
sensorNTCOut = ADC(Pin(27))
janela = 0
AC = 0
#ESP32 Pin assignment
#OLED
i2c = I2C(0, scl=Pin(19), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.text('starting...', 0, 0)
#oled.setCursor(57, 4);
#oled.print(temp, 1);
#oled.print("C ");
#oled.setCursor(57, 5);
#oled.print(hum, 1);
#oled.print("% ");
oled.show()
#sensor de temp e humidade
sensor = dht.DHT22(Pin(15))
hum_anterior = 0
hum = 0
#sensor de luz ldr
ldrIn= ADC(Pin(14))
ldrOut= ADC(Pin(26))
cortina = 0
luz = 0
#ldr.atten(ADC.ATTN_11BD)
print('«Energy efficiency monitoring system»')
while True:
try:
#### HALL BUTTON AND MOTION SENSOR #####################
#read button and motion sensor
button_state = button.value()
ms_hall_state = ms_hall.value()
#detect falling edge of button and rising of sensor
if (button_state==0) and (button_before==1):
button_falling = 1
elif (ms_hall_state) and not(ms_hall_before):
ms_hall_rising = True
#detect person entering or exiting
if (button_falling==1) and (hall_door==0): #start entering
hall_door = 1
print("Hall door opening")
elif (ms_hall_rising) and (hall_door==1): #end entering
hall_door = 0
hall_counter += 1
print("Hall door closing")
elif (ms_hall_rising) and (hall_door==0): #start exiting
hall_door = 1
print("Hall door opening")
elif (button_falling==1) and (hall_door==1): #end exiting
hall_door = 0
print("Hall door closing")
if (hall_counter>=1):
hall_counter -= 1
#variables refresh
button_before = button_state
ms_hall_before = ms_hall_state
button_falling = 0
ms_hall_rising = False
#refresh led value
if (hall_door==1):
led_blue.on()
else:
led_blue.off()
#print hall counter
#print('HALL COUNTER ', hall_counter)
#### ROOM MOTION SENSORS ###############################
#read motion sensors
ms_room_1_state = ms_room_1.value();
ms_room_2_state = ms_room_2.value();
#detect rising edge of motion sensors
if (ms_room_1_state) and not(ms_room_1_before):
ms_room_1_rising = True
elif (ms_room_2_state) and not(ms_room_2_before):
ms_room_2_rising = True
#detect person entering or exiting
if (ms_room_1_rising) and (room_door==0): #start entering
room_door = 1
print("Room door opening")
elif (ms_room_2_rising) and (room_door==1): #end entering
room_door = 0
room_counter+= 1
print("Room door closing")
elif (ms_room_2_rising) and (room_door==0): #start exiting
room_door = 1
print("Room door opening")
elif (ms_room_1_rising) and (room_door==1): #end exiting
room_door = 0
print("Room door closing")
if (room_counter >= 1):
room_counter -= 1
#variables refresh
ms_room_1_before = ms_room_1_state
ms_room_2_before = ms_room_2_state
ms_room_1_rising = False
ms_room_2_rising = False
#refresh led value
if (room_door==1):
led_red.on()
else:
led_red.off()
#print room counter
#print('ROOM COUNTER', room_counter)
#Sensor ntc temp
analogValue = sensorNTCIn.read()
celsiusIn = 1 / (math.log(1 / (4095. / analogValue - 1)) / 3950 + 1.0 / 298.15) - 273.15
#print('temperatureNTRIn :', round(celsiusIn,1),'ºC')
analogValue1 = sensorNTCOut.read()
celsiusOut = 1 / (math.log(1 / (4095. / analogValue1 - 1)) / 3950 + 1.0 / 298.15) - 273.15
#print('temperatureNTROut :', round(celsiusOut,1),'ºC')
if(janela == 0) and (AC == 0):
if (celsiusOut >= 25 and celsiusIn >= 25):
print('AC Arrefece');
AC = -1;
elif (celsiusIn <= 17):
print('AC Aquece');
AC = 1;
elif (celsiusOut <= 22 and celsiusIn >= 25):
print('Abrir Janela');
janela = 1;
elif(janela == 1):
if(celsiusIn <= 20 or celsiusOut <= 15):
print('Fechar Janela');
janela = 0;
else:
if((celsiusIn >= 22 and AC == 1) or (celsiusIn <= 20 and AC == -1)):
print('AC Desliga');
AC = 0;
ACAnterior = AC
janelaAnterior = janela
#Sensor LDR
#Convert the analog value into lux value:
analogValue = ldrIn.read();
voltage = analogValue / 4096. * 5;
resistance = 2000 * voltage / (1 - voltage / 5);
luxIn = int(pow(50 * 1e3 * pow(10, 0.7) / resistance, (1 / 0.7)));
#print('ldr: ',lux)
analogValue = ldrOut.read();
voltage = analogValue / 4096. * 5;
resistance = 2000 * voltage / (1 - voltage / 5);
luxOut = int(pow(50 * 1e3 * pow(10, 0.7) / resistance, (1 / 0.7)));
if (luxIn >= 500):
if (luxOut >= 2000 and cortina == 1):
print('Cortinas Fechadas')
cortina = 0
elif (luz == 1):
print('Luzes Desligadas')
luz = 0
elif (luxIn <= 300 and luz == 0):
if (luxOut >= 400 and cortina == 0):
print('Cortinas Abertas')
cortina = 1
else:
print('Luzes Ligadas')
luz = 1
#Sensor temp e humidade
sensor.measure()
temp=sensor.temperature()
hum=sensor.humidity()
if(hum>60) and (hum_anterior<=60):
print("Acender deshumidificador")
elif(hum<=60) and (hum_anterior>60):
print("Apagar deshumidificador")
#print('temperature :', temp, 'humidity:', hum)
hum_anterior=hum
oled.fill(0) #limpa o OLED
#mostra temp e humidade no oled
show_string= 'T :'+ str(temp)+' C '
oled.text(show_string, 0, 10)
show_string= 'humidity: '+ str(hum)+'% '
oled.text(show_string, 0, 30)
oled.show()
sleep(1)
except OSError as err:
print('falha na leitura')