from machine import Pin, SoftI2C, PWM
from utime import sleep, sleep_us, ticks_us, sleep_ms
from i2c_lcd import I2cLcd
from servo import Servo
import network
import utelegram
from dht import DHT22
i2c = SoftI2C(sda=Pin(21), scl=Pin(22))
lcd = I2cLcd(i2c, 0x27, 4, 20)
servo1 = Servo(19)
servo1.write_angle(0)
trigger1 = Pin(32, Pin.OUT)
echo1 = Pin(33, Pin.IN)
led = Pin(12, Pin.OUT)
weather = DHT22(Pin(25))
door = 'Close'
def door_open(message):
global door
servo.write_angle(180)
door = 'Open'
bot.send(message['message']['chat']['id'], f'Door is {door}!')
def status(message):
global door
def show_text(message):
lcd.clear()
lcd.putstr(message['message']['text'])
bot.send(message['message']['chat']['id'], 'Ok!')
def ultra(trigger, echo):
trigger.off()
sleep_us(2)
trigger.on()
sleep_us(5)
trigger.off()
while echo.value() == 0:
signal_off = ticks_us()
while echo.value() == 1:
signal_on = ticks_us()
try:
time_pass = signal_on - signal_off
except NameError:
return 0
return time_pass/58
def check_distance(message):
distance = ultra(trigger1, echo1)
bot.send(message['message']['chat']['id'], f'Distance to object: {distance:.1f}cm')
def kalk(message):
a=input("Введіть математичний вираз")
b=eval(a)
bot.send(message['message']['chat']['id'], b)
def led_on(message):
led.on()
bot.send(message['message']['chat']['id'], 'Led is ON!')
def led_off(message):
led.off()
bot.send(message['message']['chat']['id'], 'Led is OFF!')
def show_weather(message):
weather.measure()
temp = weather.temperature()
hum = weather.humidity()
bot.send(message['message']['chat']['id'], f'Temperature: {temp}C\nHumidity: {hum}%')
def find(message):
bot.send(message['message']['chat']['id'], 'Buzzer making sound')
for i in range(15):
buzzer = PWM(Pin(26), 1000, duty=512)
sleep_ms(300)
buzzer.deinit()
sleep_ms(201)
def found(message):
bot.send(message['message']['chat']['id'], 'Congratulations')
buzzer = PWM(Pin(26), 500, duty=512)
lcd.putstr("Ulitsa, 21")
sleep_ms(250)
buzzer.deinit()
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.scan()
wifi.connect('Wokwi-GUEST', '')
print('Connecting.', end='')
while not wifi.isconnected():
sleep(0.1)
print('.', end='')
print('\nConnected!')
token ='7559016297:AAH4iVDEmFHhk5LvYtda4BXyaG40-lNDJvo'
print('Bot starting...')
bot = utelegram.ubot(token)
bot.register('/on', led_on)
bot.register('/off', led_off)
bot.register('/check_door', check_distance)
bot.register('/check_weather', show_weather)
bot.register('/find', find)
bot.register('/found', found)
bot.register('/k',kalk)
bot.set_default_handler(show_text)
print('\nGO-GO-GO!!!!!')
bot.listen()