/*#include <DHT.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Initialize Telegram BOT
#define botToken "5711693392:AAEUASm_bSmfnqUdoef3hN8ZjUnp4dWHV6A" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define chat_id "542689770"
/*******************************************************************
Control an LED strip using Inline Keyboard on Telegram
By Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
*******************************************************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Library for interacting with the Telegram API
// Search for "Telegegram" in the Library manager and install
// The universal Telegram library
// https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
// Library used for parsing Json from the API responses
// NOTE: There is a breaking change in the 6.x.x version,
// install the 5.x.x version instead
// Search for "Arduino Json" in the Arduino Library manager
// https://github.com/bblanchon/ArduinoJson
#include<DHTesp.h>
#define DHTPIN 12
DHTesp dht;
#define DHTTYPE DHT11 // DHT 11
float temperature;
float limit;
float humidity;
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
unsigned long bot_lasttime; // last time messages' scan has been done
unsigned long temptime;
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char password[] = ""; // your network password
#define TELEGRAM_BOT_TOKEN "5711693392:AAEUASm_bSmfnqUdoef3hN8ZjUnp4dWHV6A" // your Bot Token (Get from Botfather)
//#define chat_id "542689770"
//------- ---------------------- ------
// This is the Wifi client that supports HTTPS
WiFiClientSecure client;
UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
#define LED_PIN 2 // Same as D4 for Wemos
unsigned long TimeButton = 0;
unsigned long TimeRelay = 0;
unsigned long TimeDht = 0;
unsigned long TimeOled = 0;
unsigned long TimeBatt = 0;
int State = 0;
float batVolt;
int batPer;
int barrs=0;
long rssi = WiFi.RSSI();
int delayBetweenChecks = 1000;
unsigned long lastTimeChecked; //last time messages' scan has been done
unsigned long lightTimerExpires;
boolean lightTimerActive = false;
void drawUpdateOLED(){//float temp, float hum, int batPer, float lim) {
//delay(100);
display.clearDisplay();
// wifilevel();
if (rssi > -55) {
barrs = 5;
} else if (rssi < -55 & rssi > -65) {
barrs = 4;
} else if (rssi < -65 & rssi > -70) {
barrs = 3;
} else if (rssi < -70 & rssi > -78) {
barrs = 2;
} else if (rssi < -78 & rssi > -82) {
barrs = 1;
} else {
barrs = 0;
}
///Serial.print("bars ");
//Serial.println(barrs);
// Serial.print("rssi ");
// Serial.println(rssi);
for (int b=0; b <= barrs; b++) {
display.drawLine(115+b*2, 7, 115+b*2,7-b,WHITE);}
display.setTextSize(1);
display.setCursor((limit-10)*5,51);
display.print(limit,1);
display.drawLine(13, 63, 105,63,WHITE);
display.setTextSize(3);
display.setCursor((limit-10)*5,44) ;
display.println(".");
display.setTextSize(1);
display.setCursor(20, 1);
display.print("off");
display.setCursor(60, 1);
display.print("%");
display.setCursor(70, 1);
display.print(batPer,1);
display.drawLine(1, 9, 127,9,WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(5, 12);
display.println("Temp:");
display.setCursor(65, 12);
display.print(temperature,1);
display.setCursor(5, 28);
display.println("Hum.:");
display.setCursor(65, 28);
display.print(humidity,1);
display.display();
}
void IRAM_ATTR ButtonA() {
if (millis() - TimeButton > 200) {
if (State == 0) limit = limit + 1;
if (limit > 30)limit = 30;
TimeButton = millis();
}
// Serial.println(limit);
}
void IRAM_ATTR ButtonB() {
if (millis() - TimeButton > 200) {
if (State == 0) limit = limit - 1;
if (limit < 10)limit = 10;
TimeButton = millis();
}
Serial.println(limit);
}
void setup() {
Serial.begin(115200);
pinMode(35, INPUT_PULLUP);
attachInterrupt(35, ButtonA, FALLING);
pinMode(34, INPUT_PULLUP);
attachInterrupt(34, ButtonB, FALLING);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);}
dht.setup(DHTPIN, DHTesp::DHT11);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
client.setInsecure();
bot.longPoll = 60;
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
// If the type is a "callback_query", a inline keyboard button was pressed
if (bot.messages[i].type == F("callback_query")) {
String text = bot.messages[i].text;
Serial.print("Call back button pressed with text: ");
Serial.println(text);
if (text == F("18")) {
limit=18;
} else if (text == F("19")) {
limit=19;
} else if (text == F("20")) {
limit=20;
} else if (text == F("21")) {
limit=21;
} else if (text == F("22")) {
limit=22;
} else if (text == F("23")) {
limit=23;
}
} else {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
Serial.println(chat_id) ;
if (text == F("/options")) {
String temp = "Temperature: " + String(temperature) + " ºC \n"+ "Humidity: "+String(humidity) + " % \n"+"Limit :"+String(limit) +" ºC \n";
String keyboardJson = F("[[{ \"text\" : \"18\", \"callback_data\" : \"18\" }, { \"text\" : \"19\", \"callback_data\" : \"19\" }, { \"text\" : \"20\", \"callback_data\" : \"20\" }, { \"text\" : \"21\", \"callback_data\" : \"21\" }, { \"text\" : \"22\", \"callback_data\" : \"22\" }, { \"text\" : \"23\", \"callback_data\" : \"23\" }]]");
bot.sendMessageWithInlineKeyboard(chat_id, temp, "Markdown", keyboardJson);
}
if (text == F("/start")) {
bot.sendMessage(chat_id, "/options : returns the inline keyboard\n", "Markdown");
}
}
}
}
void loop() {
TempAndHumidity data;
if (millis() - TimeDht > 1000) {
data = dht.getTempAndHumidity();
temperature = data.temperature;
humidity = data.humidity;
// Serial.println(temperature);
}
if (millis() - TimeRelay > 1000)
{
if(temperature>limit)
digitalWrite(2, LOW);
else {
digitalWrite(2, HIGH);
}
}
if ((millis() - TimeOled) >= 1000)
{
drawUpdateOLED();//temp, hum, batPer, lim);
TimeOled = millis();
}
if ((millis() - TimeBatt) >= 500) {
float Calbr=0.4;
//sensorVal=analogRead(A0);
// batVolt = (sensorVal * (3.3 / 1023) * 2 + Calbr)*10; //multiply by two as voltage divider network is 100K & 100K Resistor
batPer = 67;//map(batVolt, 28, 42, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage
// if (batPer >= 100) {batPer = 100;}
// if (batPer <= 0) {batPer = 0;}
//Serial.println(batPer);
TimeBatt = millis();
}
if (millis() > lastTimeChecked + delayBetweenChecks) {
// getUpdates returns 1 if there is a new message from Telegram
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
}
lastTimeChecked = millis();
if (lightTimerActive && millis() > lightTimerExpires) {
lightTimerActive = false;
digitalWrite(LED_PIN, LOW);
}
}
}
Loading
esp-01
esp-01