Skip to main content

Recepción mensajes Telegram

Podemos ahora enviar un mensaje a ArduinoAlvik y que ejecute un programa por ejemplo el evita obstáculos:

    Envía un mensaje por Telegram que está preparado Si le enviamos /go el contesta voy Ejecuta la rutina de evita obstáculos
    from arduino_alvik import ArduinoAlvik   #### IMPORTAMOS LAS FUNCIONES DE ALVIK
    
    from config import utelegram_config
    from config import wifi_config
    ##### AÑADIMOS LIBRERÍAS PARA LOS OBSTÁCULOS
    from time import sleep_ms
    import sys
    
    import utelegram
    import network
    import utime
    ################ añadimos urequest para enviar mensajes con url
    import urequests
    
    alvik = ArduinoAlvik()    #### CREAMOS UN OBJETO ALVIK
    alvik.begin()             #### LO INICIALIZAMOS
    ################# VARIABLES PARA EVITAR OBSTÁCULOS
    distance = 2010
    degrees = 45.00
    speed = 50.00
    ############################################################
    debug = True
    
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)
    sta_if.scan()
    sta_if.connect(wifi_config['ssid'], wifi_config['password'])
    
    if debug: print('WAITING FOR NETWORK - sleep 20')
    utime.sleep(20)
    ########################################################################################
    def get_message(message):
        bot.send(message['message']['chat']['id'], message['message']['text'].upper())
    ############################################################################################
    def enviarmensaje(mensaje):                                     ###FUNCION ENVIAR MENSAJE CON URL ojo cambiar PONAQUITUID
      url="https://api.telegram.org/bot"+utelegram_config['token']+"/sendMessage?chat_id=-4273278318PONAQUITUID&text="+mensaje
      respuesta = urequests.get(url)
    ###########################################################################################
    def reply_ping(message):
        print(message)
        bot.send(message['message']['chat']['id'], 'voy')      ### CAMBIAMOS EL MENSAJE
        evitamosobstaculos()                                   ### y EVITAMOS OBSTÁCULOS
    ############################################################################################
    def evitamosobstaculos():                                  ### FUNCIÓN EVITAR OBSTÁCULOS
      while (True):
    
        distance_l, distance_cl, distance_c, distance_r, distance_cr  = alvik.get_distance()
        sleep_ms(50)
        print(distance_c)
    
        if distance_c < distance:
          alvik.rotate(degrees, 'deg')
        elif distance_cl < distance:
          alvik.rotate(degrees, 'deg')
        elif distance_cr < distance:
          alvik.rotate(degrees, 'deg')
        elif distance_l < distance:
          alvik.rotate(degrees, 'deg')
        elif distance_r < distance:
          alvik.rotate(degrees, 'deg')
        else:
          alvik.drive(speed, 0.0, linear_unit='cm/s')
      
    ############################################################################################
    
    if sta_if.isconnected():
        bot = utelegram.ubot(utelegram_config['token'])
        bot.register('/go', reply_ping)                        ### LA CONSIGNA SERÁ GO
        bot.set_default_handler(get_message)
    
        print('BOT LISTENING')
        enviarmensaje("preparado")
        bot.listen()
    else:
        print('NOT CONNECTED - aborting')