#define trigPin 5
#define echoPin 18
#define SOUND_SPEED 0.034
long duration;
float distanceCm;
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3C
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define ledPin 2
bool ledState = LOW;
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BOTtoken "7662177674:AAF-0aA7WerbVp7Fvb652CHe0Zbq08O2isc" // your Bot Token
#define CHAT_ID "5243833752" //ID
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int botRequestDelay = 1000, Intervalo = 1000; // Checks for new messages every 1 s
unsigned long lastTimeBotRan, lastTime;
int valor=1 ;
String chat_id;
int ancho_icon = 64 ;
int alto_icon = 64 ;
//Imagen puerta abierta
static const unsigned char epd_bitmap_2 [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xfb, 0xdf, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfb, 0xdf, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x6f, 0xe0, 0x07, 0xf6, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe1, 0x87, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe3, 0xc7, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xec, 0x27, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe3, 0xc7, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xf6, 0x00, 0x00, 0x00, 0x0e, 0x7f, 0xe0, 0x07, 0xfe, 0x70, 0x00,
0x00, 0x1f, 0xff, 0xe0, 0x07, 0xff, 0xf8, 0x00, 0x00, 0x0e, 0x00, 0xe0, 0x07, 0x00, 0x70, 0x00,
0x00, 0x04, 0x7f, 0xe0, 0x07, 0xfe, 0x20, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xe0, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xe0, 0x07, 0xff, 0xfc, 0x00,
0x00, 0x3f, 0xff, 0xc0, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//Imagen puerta cerrada
static const unsigned char epd_bitmap_Design_sans_titre [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x98, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x06, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00,
0x01, 0x80, 0x00, 0x03, 0x80, 0x00, 0x01, 0x80, 0x01, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xf1, 0x80,
0x01, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xf9, 0x80, 0x01, 0x18, 0x00, 0x03, 0xc0, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x06, 0x01, 0x80, 0x80, 0x19, 0x80,
0x01, 0x18, 0x07, 0x01, 0x83, 0x80, 0x19, 0x80, 0x01, 0x18, 0x07, 0xc1, 0x87, 0xc0, 0x19, 0x80,
0x01, 0x1b, 0xff, 0xe1, 0x8f, 0xff, 0x19, 0x80, 0x01, 0x1b, 0xff, 0xf1, 0x9f, 0xff, 0x19, 0x80,
0x01, 0x1b, 0xff, 0xe1, 0x9f, 0xff, 0x19, 0x80, 0x01, 0x1b, 0xff, 0xc1, 0x87, 0xff, 0x19, 0x80,
0x01, 0x18, 0x07, 0x81, 0x83, 0x80, 0x19, 0x80, 0x01, 0x18, 0x07, 0x01, 0x81, 0x80, 0x19, 0x80,
0x01, 0x18, 0x04, 0x01, 0x80, 0x80, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80, 0x01, 0x18, 0x00, 0x01, 0x80, 0x00, 0x19, 0x80,
0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
//Inicializar
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); //ultrasonidos
pinMode(echoPin, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); //pantalla OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
pinMode(ledPin, OUTPUT); //LED color
digitalWrite(ledPin, ledState);
WiFi.mode(WIFI_STA); //Telegram
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi..");
}
display.display();
}
//codigo recibir y enviar mensajes
void handleNewMessages(int numNewMessages)
{ Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Bienvenido " + from_name + ".\n";
welcome += "Abby detecta a cualquier persona a menos de 1,5 metros.\n\n";
welcome += "Si Abby detecta a alguien se encenderá el LED \n";
welcome += "/Distancia para saber la distancia a la que estas \n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/Distancia")
{
bot.sendMessage(chat_id, "Te encuentras a " + String(distanceCm) + " cm");
}
}
}
void loop() {
if (millis() > lastTime + Intervalo) {
digitalWrite(trigPin, LOW); //Ultrasonido
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * SOUND_SPEED / 2;
Serial.println("Distancia " + String(distanceCm) + " cm");
display.clearDisplay();
display.setCursor(10, 10);
display.print("Distancia " + String(distanceCm) + " cm");
if (distanceCm <= 10) {
display.drawBitmap(30, 10, epd_bitmap_2, ancho_icon, alto_icon, 1);
if (valor == 1) {
bot.sendMessage(chat_id, "LED encendido. \n Abby detecta a alguien! \n", "");
ledState = HIGH;
digitalWrite(ledPin, ledState);
valor=0;
}
}
else {
display.drawBitmap(30, 10, epd_bitmap_Design_sans_titre, ancho_icon, alto_icon, 1);
ledState = LOW;
digitalWrite(ledPin, ledState);
valor=1;
}
display.display();
lastTime = millis();
}
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}