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 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 w_1(message):
a = int(input("Введіть перше число"))
b= int(input("Введіть друге число"))
c=input("Введіть дію")
m=0
if c=="-":
m=a-b
bot.send(message['message']['chat']['id'], f'Результат: {m}')
elif c=="+":
m=a+b
bot.send(message['message']['chat']['id'], f'Результат: {m}')
elif c=="/":
m=a/b
bot.send(message['message']['chat']['id'], f'Результат: {m}')
elif c=="*":
m=a*b
bot.send(message['message']['chat']['id'], f'Результат: {m}')
def hello_1(message):
bot.send(message['message']['chat']['id'], 'Hellow!')
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.scan()
wifi.connect('Wokwi-GUEST', '')
def sound_on(message):
buzzer = PWM(Pin(23))
buzzer.freq(440)
bot.send(message['message']['chat']['id'], 'sound is ON!')
def sound_off(message):
buzzer = PWM(Pin(23))
buzzer.freq(1)
bot.send(message['message']['chat']['id'], 'sound is OFF!')
print ('Connecting.', end='')
while not wifi.isconnected():
sleep(0.1)
print('.', end='')
print('\nConnected!')
token ='8074812847:AAF4dlb-gt1p2ceII2KmAahbzuIbWLk5SJk'
print('Bot strating...')
bot = utelegram.ubot(token)
bot.register('/hello', hello_1)
bot.register('/w',w_1)
bot.register('/on',led_on)
bot.register('/off',led_off)
bot.register('/s_on',sound_on)
bot.register('/s_off',sound_off)
bot.register('/check_door', check_distance)
bot.register('/check_weather', show_weather)
bot.set_default_handler(show_text)
print('\nGO-Go-GO!!!!!!!')
bot.listen()