/***********************************************************/
/* Librerias Y Macros */
/***********************************************************/
/*********************Adafruit SSD1306**********************/
// Librerias para Adafruit SSD1306
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Macros para Adafruit SSD1306
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
#define SCREEN_ADDRESS 0x3C // Dirección
/************************PZEM004Tv30************************/
// Librerias para PZEM004Tv30
//#include <PZEM004Tv30.h> //Comentar para probar en Wokwi
//#include <SoftwareSerial.h> //Comentar para probar en Wokwi
// Macros para PZEM004Tv30
#define PZEM_RX_PIN 16 // Pin RX para el PZEM
#define PZEM_TX_PIN 17 // Pin TX para el PZEM
#define ALARM_ACTIVE 1 // Alarma Inactiva
#define ALARM_INACTIVE 0 // Alarma Activa
#define ALARM_LIMIT 5.0 // Límite de la alarma
/*******************UniversalTelegramBot********************/
// Librerias para UniversalTelegramBot
//#include <WiFi.h>
//#include <WiFiClientSecure.h>
//#include <UniversalTelegramBot.h>
// Macros para UniversalTelegramBot
//#define WIFI_SSID "Wokwi-GUEST" // WiFi SSID
//#define WIFI_PASSWORD "" // WiFi Password
//#define BOT_TOKEN "6882478918:AAHZanBsME8E_Up9og2Gl56p835nLoZZvmE" // Telegram BOT Token (Get from Botfather)
//#define CHAT_ID "841110064" // Telegram Chat ID (Get from IDBot)
#define RELAY_PIN 12 // Pin del relé
/***********************************************************/
/* Variables y Funciones */
/***********************************************************/
/*********************Adafruit SSD1306**********************/
// create an OLED display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// 'imagen rayo', 48x48px
const unsigned char thunderbolt_48x48 [] = {
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, 0x01, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00,
0x00, 0x00, 0x03, 0xff, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 0x07, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x1f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
0xc0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x01, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01,
0xff, 0xff, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0x00, 0x00, 0x00, 0x03, 0xff, 0xfe, 0x00, 0x00,
0x00, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00,
0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 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
};
/************************PZEM004Tv30************************/
//SoftwareSerial pzemSWSerial(PZEM_RX_PIN, PZEM_TX_PIN); //Comentar para probar en Wokwi
//PZEM004Tv30 pzem(pzemSWSerial); //Comentar para probar en Wokwi
// Variables del sensor PZEM
float voltage = 240.0;
float current = 1.03;
float power = 25.5;
float energy = 104.0;
float frequency = 50.0;
float pf = 0.8;
// Variable para controlar la alarma
int alarma = 0;
// Variables para el tiempo
const unsigned long PZEM_MTBS = 2000; // mean time between PZEM messages
unsigned long pzem_lasttime; // last time messages' PZEM
/*******************UniversalTelegramBot********************/
//WiFiClientSecure secured_client;
//UniversalTelegramBot bot(BOT_TOKEN, secured_client);
// Variables para el tiempo
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
unsigned long bot_lasttime; // last time messages' scan has been done
// Función para manejar las respuestas del BOT
/*void handleNewMessages(int numNewMessages)
{
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = bot.messages[i].chat_id;
//Verificar el usuario
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (text == "/start")
{
String welcome = "Bienvenido/a " + from_name + ".\n";
welcome += "Lista de comandos para el BOT:\n\n";
welcome += "/start : muestra este mensaje.\n";
welcome += "/relay_on : activa el relé.\n";
welcome += "/relay_off : desactiva el relé.\n";
welcome += "/get_sensor : envia un mensaje con los valores de los sensor.\n";
bot.sendMessage(chat_id, welcome);
}
if (text == "/relay_on")
{
digitalWrite(RELAY_PIN, HIGH);
String message_1 = "Relé activado.\n";
bot.sendMessage(chat_id, message_1);
}
if (text == "/relay_off")
{
digitalWrite(RELAY_PIN, LOW);
String message_2 = "Relé desactivado.\n";
bot.sendMessage(chat_id, message_2);
}
if (text == "/get_sensor")
{
String message_3 = "Valor del sensor PZEM:\n\n";
message_3 += ("Voltage: " + String(voltage, 1) + " V\n");
message_3 += ("Current: " + String(current, 2) + " A\n");
message_3 += ("Power: " + String(power, 2) + " W\n");
message_3 += ("Energy: " + String(energy, 3) + " kWh\n");
message_3 += ("Frequency: " + String(frequency, 1) + " Hz\n");
message_3 += ("PF: " + String(pf, 2) + "\n");
bot.sendMessage(chat_id, message_3);
}
}
}*/
/***********************************************************/
/* SETUP */
/***********************************************************/
void setup() {
Serial.begin(115200);
/********** Inicializando Relé **********/
pinMode(RELAY_PIN, OUTPUT);
/********** Inicializando SSD1306 **********/
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay(); //clear the display buffer
display.setTextSize(1); // text size = 1
display.setTextColor(SSD1306_WHITE); // set text color to white
/********** Conexión a la red WiFi **********/
Serial.print("Connecting to Wifi SSID ");
//WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
//while (WiFi.status() != WL_CONNECTED)
//{
//Serial.print(".");
//delay(500);
//}
Serial.print("\nWiFi connected.");
/***************** Telegram ****************/
//secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
}
/***********************************************************/
/* LOOP */
/***********************************************************/
void loop() {
/***************** Telegram ****************/
/*if (millis() - bot_lasttime > BOT_MTBS)
{
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);
}
bot_lasttime = millis();
}*/
/***************** SSD1306 y PZEM004Tv30 *****************/
if (millis() - pzem_lasttime > PZEM_MTBS)
{
/******** Descomentar para el sensor PZEM004Tv30 *******/
/*
voltage = pzem.voltage();
current = pzem.current();
power = pzem.power();
energy = pzem.energy();
frequency = pzem.frequency();
pf = pzem.pf();
*/
/****************** Control de la alarma **************/
if (power >= ALARM_LIMIT && alarma == ALARM_INACTIVE)
{
Serial.println("Alarma activada");
alarma = ALARM_ACTIVE;
String alarmMenssage = "Se ha excedido la potencia de alarma.\n";
//bot.sendMessage(CHAT_ID, alarmMenssage);
}
else if (power < ALARM_LIMIT && alarma == ALARM_ACTIVE)
{
Serial.println("Alarma desactivada");
alarma = ALARM_INACTIVE;
}
/******************** Display SSD1306 ****************/
display.clearDisplay();
display.drawBitmap(0, 0, thunderbolt_48x48, 48, 48, 1);
display.setCursor(50, 0);
display.print("Vlt: " + String(voltage, 1) + "V");
display.setCursor(50, 15);
display.print("Amp: " + String(current, 2) + "A");
display.setCursor(50, 30);
display.print("Pwr: " + String(power, 2) + "W");
display.setCursor(50, 45);
display.print("Frq: " + String(frequency, 1) + "Hz");
display.display();
pzem_lasttime = millis();
}
}