meta data de esta página
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| electronica:wemos:boton_ayuda [2016/12/18 22:59] – lc | electronica:wemos:boton_ayuda [2023/01/18 13:36] (actual) – editor externo 127.0.0.1 | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| {{tag> | {{tag> | ||
| ===== Botón de emergencia con Wemos ===== | ===== Botón de emergencia con Wemos ===== | ||
| - | Después de leer el artículo de **jeanotP1314** sobre como construir un botón de ayuda para personas mayores, decidí hacer algo similar pero con la ayuda de telegram ya que me permite tanto enviar como recibir | + | Después de leer el artículo de **jeanotP1314** sobre como construir un botón de ayuda para personas mayores, decidí hacer algo similar pero utilizando telegram en vez de gmail y añadiendole |
| - | De momento sólo tengo creado un pequeño código de ejemplo, pero la lista de comandos | + | |
| + | De momento sólo tengo implementado algunos comandos, pero la lista de acciones | ||
| Para realizar este proyecto sólo necesitamos un Wemos y un Button Shield | Para realizar este proyecto sólo necesitamos un Wemos y un Button Shield | ||
| {{ : | {{ : | ||
| - | Simplemente ponemos el button | + | Simplemente ponemos el button |
| {{ : | {{ : | ||
| Lo primero como siempre es crear nuestro bot en telegram para poder enviar y recibir los mensajes. Si no sabes como hacerlo está explicado [[electronica: | Lo primero como siempre es crear nuestro bot en telegram para poder enviar y recibir los mensajes. Si no sabes como hacerlo está explicado [[electronica: | ||
| + | |||
| + | Alimentamos el Wemos y cargamos el siguiente código: | ||
| + | <sxh c> | ||
| + | /* Código original de la conexión con telegram de https: // | ||
| + | Modificado por LC (wiki.intrusos.info) | ||
| + | */ | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | |||
| + | // Iniciamos la conexión con el router | ||
| + | const char *ssid = "ssid de la wifi"; | ||
| + | const char *pass = " | ||
| + | int status = WL_IDLE_STATUS; | ||
| + | |||
| + | // Datos del Bot de Telegram | ||
| + | String BOTtoken = " | ||
| + | String Chat_id = " | ||
| + | String Texto_enviar = ""; | ||
| + | String Texto_recibido = ""; | ||
| + | String Update_id = ""; | ||
| + | String anterior_upd = ""; | ||
| + | String Nueva_upd = ""; | ||
| + | String Respuesta = ""; | ||
| + | |||
| + | // Variables de tiempo | ||
| + | int Inicio; | ||
| + | int Termino; | ||
| + | int Intervalo = 15000; | ||
| + | unsigned long elapsed = 0; | ||
| + | unsigned long previous; | ||
| + | boolean respondio = false; | ||
| + | |||
| + | //variables de estado | ||
| + | int estadoboton = 0; | ||
| + | |||
| + | // Pin del wemos al que está conectado el botón | ||
| + | const int botonpin = D3; | ||
| + | const int ledpin = BUILTIN_LED; | ||
| + | |||
| + | WiFiClientSecure client; // inicio del cliente seguro | ||
| + | IPAddress server(149, 154, 167, 200); // IP de api.telegram.org telegram | ||
| + | |||
| + | |||
| + | void setup() { | ||
| + | |||
| + | Serial.begin(115200); | ||
| + | WiFi.begin(ssid, | ||
| + | |||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(" | ||
| + | } | ||
| + | Serial.println("" | ||
| + | Serial.println(" | ||
| + | Serial.println(" | ||
| + | Serial.println(WiFi.localIP()); | ||
| + | |||
| + | // Comprobamos la conexión a Telegram | ||
| + | if (client.connect(server, | ||
| + | Serial.println(" | ||
| + | } | ||
| + | // y enviamos el texto de inicio | ||
| + | Enviar_texto(" | ||
| + | |||
| + | // | ||
| + | Ultimo_msg(); | ||
| + | previous = millis(); | ||
| + | |||
| + | // inicializamos el led pin | ||
| + | pinMode(ledpin, | ||
| + | |||
| + | // inicializamos el botón pin | ||
| + | pinMode | ||
| + | |||
| + | // | ||
| + | digitalWrite(ledpin, | ||
| + | |||
| + | // | ||
| + | pinMode (5, OUTPUT); | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | void loop() { | ||
| + | estadoboton = digitalRead(botonpin); | ||
| + | |||
| + | if (estadoboton == LOW) { //Cuando presionamos el botón | ||
| + | digitalWrite(ledpin, | ||
| + | Enviar_texto(" | ||
| + | Serial.println(" | ||
| + | } | ||
| + | |||
| + | while (estadoboton == LOW ) { //Una vez pulsado el botón está a la espera de nuestros mensajes | ||
| + | |||
| + | elapse(); | ||
| + | Leer_msg(); // leemos el último mensaje | ||
| + | |||
| + | |||
| + | // Comprobamos que haya pasado xx seg desde la última vez | ||
| + | if (elapsed > 500) { | ||
| + | } | ||
| + | anterior_upd = Update_id; // Guardamos la anterior Update | ||
| + | Ultimo_msg (); // comprobamos el último mensaje | ||
| + | delay(1000); | ||
| + | Leer_msg(); // Leemos los datos | ||
| + | busca_upd_id(Respuesta); | ||
| + | busca_texto(Respuesta); | ||
| + | |||
| + | // Si ha cambiado la Update_id seguimos con el código | ||
| + | if (anterior_upd != Nueva_upd) { | ||
| + | Responder_mensaje(Texto_recibido); | ||
| + | } else { | ||
| + | } // No hacemos nada si es el mismo Upd_id | ||
| + | } | ||
| + | } // Fin Loop | ||
| + | |||
| + | |||
| + | // Orden para buscar el texto del mensaje | ||
| + | void busca_texto( String Rsp ) { | ||
| + | Texto_recibido = ""; | ||
| + | int start = Rsp.indexOf(" | ||
| + | int fin = Rsp.indexOf(" | ||
| + | Texto_recibido = (Rsp.substring(start, | ||
| + | } | ||
| + | |||
| + | //Orden para buscar la Update_id | ||
| + | void busca_upd_id( String Rsp ) { | ||
| + | anterior_upd = Update_id; // Guardamos la anterior Update_id para comprobar | ||
| + | int start = Rsp.indexOf(" | ||
| + | int fin = Rsp.indexOf(" | ||
| + | Update_id = Rsp.substring(start, | ||
| + | Nueva_upd = Rsp.substring(start, | ||
| + | } | ||
| + | |||
| + | // Orden para pedir el ultimo mensaje, vemos que se usa el Offset=-1& | ||
| + | void Ultimo_msg () { | ||
| + | if (client.connect(server, | ||
| + | // client.println(" | ||
| + | client.println(" | ||
| + | } | ||
| + | previous = millis(); // Guardamos los milisegundos para comprobar que haya pasado X tiempo entre lecturas | ||
| + | } | ||
| + | |||
| + | //Leemos el mensaje completo y lo añadimos a una variable carácter por carácter | ||
| + | void Leer_msg () { | ||
| + | Respuesta = ""; | ||
| + | while (client.available()) { // Mientras no lo lea todo seguirá leyendo | ||
| + | char inChar = client.read(); | ||
| + | Respuesta += inChar; // Añadimos carácter a carácter el mensaje | ||
| + | } | ||
| + | } | ||
| + | |||
| + | //Orden para comprobar el tiempo entre lecturas | ||
| + | void elapse() { | ||
| + | elapsed = millis() - previous; | ||
| + | } | ||
| + | |||
| + | |||
| + | // Función para hacer sonar el Buzzer | ||
| + | void beep(unsigned char pausa) | ||
| + | { | ||
| + | analogWrite(5, | ||
| + | delay(pausa); | ||
| + | analogWrite(5, | ||
| + | delay(pausa); | ||
| + | } | ||
| + | |||
| + | //Orden para enviar cualquier texto a Telegram | ||
| + | void Enviar_texto( String Texto_enviar ) { | ||
| + | if (client.connect(server, | ||
| + | client.println(" | ||
| + | Serial.println(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | //Aquí añadiremos las ordenes de respuesta del arduino | ||
| + | void Responder_mensaje ( String mensaje ) { | ||
| + | |||
| + | if (mensaje == " | ||
| + | Enviar_texto(" | ||
| + | respondio = true; | ||
| + | } | ||
| + | else if (mensaje == " | ||
| + | Enviar_texto(" | ||
| + | beep(500); | ||
| + | respondio = true; | ||
| + | } | ||
| + | else if (mensaje == " | ||
| + | Enviar_texto(" | ||
| + | Serial.println(" | ||
| + | respondio = true; | ||
| + | digitalWrite(ledpin, | ||
| + | estadoboton = 1; | ||
| + | } | ||
| + | |||
| + | if (respondio == true) { // mostramos el texto que se ha entendido | ||
| + | Serial.println(" | ||
| + | } | ||
| + | else { | ||
| + | Serial.println(" | ||
| + | |||
| + | } | ||
| + | respondio = false ; // Dejamos en falso que entendió el mensaje | ||
| + | } | ||
| + | |||
| + | ////////// Fin del código | ||
| + | </ | ||
| + | |||
| + | En este ejemplo cuando se presiona el botón se envía un mensaje pidiendo ayuda y se entra en un bucle para ejecutar los comandos que le enviamos por mensajes de telegram hasta que le llegue un mensaje de **Cerrar** . | ||
| + | |||
| + | Para hacer pruebas he conectado un buzzer al PIN D1 (GPIO5) para hacerlo sonar como aviso y en cuanto tenga en mi poder el módulo de GPS y un transceiver con **Lora** intentaré ampliar el código de ejemplo, para obtener las coordenadas de posición y a su vez utilizar Lora en vez de una conexión Wifi. | ||