const char* ssid = "Wokwi-GUEST";
const char* password = "";
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
#include <BH1750.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include "images.h"
#define SCREEN_ADDRESS 0x3C
#define DHTPIN 15
#define DHTTYPE DHT22 // DHT 11
#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 h, t, lux;
unsigned long previousTime = 0, previousTime2 = 0;
#define intervalRead 2000
//#define intervalRele 13000
// 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 = "Welcome, " + from_name + ".\n";
welcome += "Use the following commands to control your outputs.\n\n";
welcome += "/on \n";
welcome += "/off \n";
welcome += "/sensores\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 == "/sensores") {
String msg = "Sensores \n";
msg += "T "+ String(t,0) + " C\n";
msg += "H "+ String(h,0) + " %\n";
msg += "lux "+ String(lux,0) + "\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();
lightMeter.begin();
// 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, "SENSORES");
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
lux = lightMeter.readLightLevel();
if(lux <500 && !enviado)
{
bot.sendMessage(CHAT_ID, "Poco nivel de luz", "");
enviado = 1;
}
if(lux > 600)
enviado = 0;
// si lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE);
// lightMeter.configure(BH1750::ONE_TIME_HIGH_RES_MODE);
h = dht.readHumidity();
t = dht.readTemperature();
mostrar_OLED();
}
}
void mostrar_OLED() {
display.clear(); // clear display
display.drawXbm(0, 0, icon_temp_width, icon_temp_height, myBitmaptemperature_icon);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(40, 0, String(t, 0) + " C");
display.drawString(40, 20, String(h, 0) + " %");
display.drawXbm(0, 32, icon_lux_width, icon_lux_height, bitmap_bombilla_mini);
display.drawString(40, 40, String(lux, 0) + " lux");
display.display();
}