#include <PZEM004Tv30.h>
#define PZEM_RX_PIN 16
#define PZEM_TX_PIN 17
#define PZEM_SERIAL Serial2
PZEM004Tv30 pzem(PZEM_SERIAL, PZEM_RX_PIN, PZEM_TX_PIN);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include "images.h"
#define SCREEN_ADDRESS 0x3C
#define SDA 21
#define SCL 22
#define RelePin 25
bool ReleState = LOW;
DHT dht(DHTPIN, DHTTYPE);
BH1750 lightMeter; //I2C
// create an OLED display object connected to I2C
SSD1306Wire display(SCREEN_ADDRESS, SDA, SCL); // o SSD1306Wire display(0x3c,4, 15, GEOMETRY_128_64);
void mostrar_OLED();
float v=224.1, i=1.03, p=20.5, e=103, f=50.0, pf=0.8;
unsigned long previousTime = 0, previousTime2 = 0;
#define intervalRead 2000
// Initialize Telegram BOT
#define BOTtoken "6882478918:AAHZanBsME8E_Up9og2Gl56p835nLoZZvmE" // your Bot Token (Get from Botfather)
#define CHAT_ID "841110064"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
int enviado = 0;
unsigned long lastTimeBotRan;
// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "HOLA, " + from_name + ".\n";
welcome += "Use the following commands to control your outputs.\n\n";
welcome += "/on \n";
welcome += "/off \n";
welcome += "/medidas\n";
bot.sendMessage(chat_id, welcome, "");
}
if (text == "/on") {
bot.sendMessage(chat_id, "LED state set to ON", "");
ReleState = HIGH;
digitalWrite(RelePin, ReleState);
}
if (text == "/off") {
bot.sendMessage(chat_id, "LED state set to OFF", "");
ReleState = LOW;
digitalWrite(RelePin, ReleState);
}
if (text == "/medidas") {
String msg = "Medidas \n";
msg += "V "+ String(v,2) + " V\n";
msg += "I "+ String(i,2) + " A\n";
msg += "P "+ String(p,2) + "W\n";
msg += "E "+ String(e,2) + "KWh\n";
bot.sendMessage(chat_id, msg , "");
}
}
}
void setup() {
enviado = 0;
Serial.begin(115200);
dht.begin();
display.init(); // hay que hace el reset antes
// Wire.begin(SDA, SCL); //no es necesario si antes display.init();
// initialize RELE
pinMode(RelePin, OUTPUT);
digitalWrite(RelePin, LOW);
Serial.println("Test sensores");
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 0, "MEDIDAS");
display.display();
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");
delay(500);
}
Serial.println(WiFi.localIP());
}
void loop() {
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();
}
if (millis() - previousTime > intervalRead) {
previousTime = millis();
// Read the data from the sensor
//v = pzem.voltage();
//i = pzem.current();
//p = pzem.power();
//e = pzem.energy();
//f = pzem.frequency();
//pf = pzem.pf();
if(i >1 && !enviado)
{
bot.sendMessage(CHAT_ID, "Corriente excesiva", "");
enviado = 1;
}
if(i < 1 && enviado)
enviado = 0;
mostrar_OLED();
}
}
void mostrar_OLED() {
display.clear(); // clear display
display.drawXbm(0, 0, icon_lux_width, icon_lux_height, bitmap_bombilla_mini);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(40, 0, String(v, 0) + " V");
display.drawString(40, 20, String(i, 2) + " A");
display.drawString(40, 40, String(p, 1) + " W");
display.display();
}