import uasyncio as asyncio
from machine import Pin,ADC,PWM,UART
import utime
import math
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
#uart = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
uart.init(bits=8, parity=None, stop=1)
async def readuart():
while True:
if uart.any():
data = uart.read()
print(data)
await asyncio.sleep_ms(1000)
async def heartbeat():
temp_sensor = ADC(4)
while True:
current_time = utime.localtime()
hours = current_time[3]
minutes = current_time[4]
seconds = current_time[5]
temp = 27 - (temp_sensor.read_u16() * 3.3 / (65535) - 0.706)/0.001721
#print("{:02d}:{:02d}:{:02d} t={:3.1f}".format(hours, minutes, seconds, temp))
uart.write("{:02d}:{:02d}:{:02d} t={:3.1f}".format(hours, minutes, seconds, temp))
await asyncio.sleep_ms(60000)
async def ledlight():
#led = Pin(25, Pin.OUT)
led = PWM(Pin(25, Pin.OUT))
led.freq(2048)
start_value = 1
end_value = 65535
num_values = 100
growth_factor = (end_value / start_value) ** (1 / (num_values - 1))
decay_factor = (start_value / end_value) ** (1 / (num_values - 1))
while True:
for i in range(num_values):
value = start_value * (growth_factor ** i)
led.duty_u16(int(value))
await asyncio.sleep_ms(int(500/num_values))
for i in range(num_values):
value = end_value * (decay_factor ** i)
led.duty_u16(int(value))
await asyncio.sleep_ms(int(500/num_values))
async def main():
loop = asyncio.get_event_loop ()
loop.create_task(heartbeat())
loop.create_task(ledlight())
loop.create_task(readuart())
loop.run_forever ()
if __name__ == '__main__':
asyncio.run(main())