#https://docs.pycom.io/firmwareapi/micropython/_thread/#app
# stuur temperatuur via UART0=> USB => PC
import random
from time import sleep, ticks_ms
import _thread as thread
from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
from _get_input import get_input
#from _get_state import get_state
button = Pin(14, Pin.IN, Pin.PULL_UP)
sda = Pin(21)
scl = Pin(22)
i2c = I2C(0, sda=sda, scl=scl, freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
adc = ADC(Pin(27))
adc.read() # read value, 0-4095 across voltage range 0.0v - 1.0v
adc.atten(ADC.ATTN_11DB) # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_10BIT)
relay = Pin(25, Pin.OUT)
button_before = 1
hyst = 0.5
on_state = 0
time_stamp = 0
st = 0
t = ""
on_state = 0
t_meet = ""
t_wens = ""
t_pot = 0
temp = 14
hyst = 0.5
tekst6 = ""
def oled_text(t1,t2,t3,t4,t5,t6):
oled.fill(0)
oled.text(t1, 5, 10)
oled.text(t2, 70, 10)
oled.text(t3, 5, 30)
oled.text(t4, 70, 30)
oled.text(t5, 5, 50)
oled.text(t6, 70, 50)
oled.show()
def map_fct(x, in_min, in_max, out_min, out_max):
return round((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min,1)
def get_state():
global on_state
global button_before
while True:
button_now = button.value()
time_stamp = ticks_ms()
if button_now != button_before:
print("gedrukt")
button_before = button_now
if button_now == 0:
on_state = not on_state
while ticks_ms() - time_stamp < 200:
pass
return on_state
def send_temp():
global st
global t
rg = random.randint(-3,+3)
st += rg
if st < 0 or st > 30:
st = 15
#print(rg,st)
print(st)
t = str(st)
return t
get_input_thread = thread.start_new_thread(get_input, ()) #upython versie lege tuple indien geen argumenten worden doorgegeven
get_state_thread = thread.start_new_thread(get_state, ())
#get_send_temp_thread = thread.start_new_thread(send_temp, ())
while 1:
on_state = get_state()
#print("on_state=",on_state)
time_stamp = ticks_ms()
if on_state == 0:
oled_text("","","","","niet actief","")
while ticks_ms() - time_stamp < 50:
pass
else:
oled_text("gewenst","gemeten", t_wens,t_meet, "actief", tekst6)
while ticks_ms() - time_stamp < 50:
pass
if ticks_ms() - time_stamp > 2500 :
time_stamp = ticks_ms()
t = send_temp()
pot = adc.read()
t_pot = map_fct(pot, 0, 1023, 10, 30)
t_wens = str(t_pot)
temp = 14 #gemeten_temp()
print(temp)
t_meet = str(temp)
if temp + hyst <= t_pot:
print("temp + hyst =",temp + hyst)
tekst6 = "^^^"
relay.on()
else:
tekst6 = "000"
relay.off()