Last active
July 8, 2020 01:52
-
-
Save IdrisCytron/424a1f73df346fc86699ba10e2a9a7e0 to your computer and use it in GitHub Desktop.
Monitor Temperature Humidity Using IoT Telegram Bot on ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Project: Monitor Temperature Humidity Using IoT Telegram Bot on ESP32 | |
Board: ESP32 Dev Module (TTGO T-Display ESP32) | |
Connections: | |
ESP32 | DHT22 | |
5V - VCC | |
GND - GND | |
IO27 - DAT | |
External libraries: | |
- UniversalTelegramBot by Brian Lough V1.1.0 (Manager) | |
- ArduinoJson by Benoit Blanchon V5.13.5 (Manager) | |
*/ | |
#include <TFT_eSPI.h> | |
#include <SPI.h> | |
#include <SimpleDHT.h> | |
#include <WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <UniversalTelegramBot.h> | |
// Initialize Wifi connection to the router | |
char ssid[] = "YourWiFiSSID"; // your network SSID (name) | |
char password[] = "YourWiFiPassword"; // your network key | |
// Initialize Telegram BOT | |
#define BOTtoken "YourTelegramBotToken" // your Bot Token (Get from Botfather) | |
WiFiClientSecure client; | |
UniversalTelegramBot bot(BOTtoken, client); | |
TFT_eSPI tft = TFT_eSPI(); | |
#define DHT22_PIN 27 | |
SimpleDHT22 dht22(DHT22_PIN); | |
#define ADC_EN 14 | |
#define ADC_PIN 35 | |
#define VREF 1100 | |
#define BUTTON1 35 | |
#define BUTTON2 0 | |
#define BUZZER 13 | |
#define FF17 &FreeSans9pt7b | |
#define FF21 &FreeSansBold9pt7b | |
#define ROW1 0,16 | |
#define ROW2 0,38 | |
#define ROW3 0,60 | |
#define ROW4 0,82 | |
#define ROW5 0,104 | |
#define ROW6 0,126 | |
#define NOTE_C4 262 | |
#define NOTE_D4 294 | |
#define NOTE_G4 392 | |
int melody1[] = {NOTE_C4, NOTE_G4}; | |
int melody1Dur[] = {12, 8}; | |
int melody3[] = {NOTE_D4}; | |
int melody3Dur[] = {8}; | |
#define playReadyMelody() playTone(melody1, melody1Dur, 2) | |
#define playNoteD4() playTone(melody3, melody3Dur, 1) | |
long previousMillis = 0; | |
int interval = 2000; | |
void setup() | |
{ | |
pinMode(BUTTON1, INPUT_PULLUP); | |
pinMode(BUTTON2, INPUT_PULLUP); | |
Serial.begin(115200); | |
// Attempt to connect to Wifi network: | |
Serial.print("Connecting Wifi: "); | |
Serial.println(ssid); | |
tft.init(); | |
tft.setRotation(1); | |
tft.fillScreen(TFT_BLACK); | |
tft.setFreeFont(FF21); | |
tft.setTextColor(TFT_GREEN); | |
tft.setCursor(ROW1); | |
tft.print("Initializing..."); | |
// Set WiFi to station mode and disconnect from an AP if it was Previously | |
// connected | |
WiFi.mode(WIFI_STA); | |
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()); | |
tft.fillScreen(TFT_BLACK); | |
tft.setFreeFont(FF21); | |
tft.setTextColor(TFT_GREEN); | |
tft.setCursor(ROW1); | |
tft.print("Telegram Bot"); | |
tft.setTextColor(TFT_WHITE); | |
tft.setCursor(ROW2); | |
tft.print("---------------------------------------"); | |
tft.setTextColor(TFT_BLUE); | |
tft.setCursor(ROW3); | |
tft.print("Temperature:"); | |
tft.setCursor(ROW5); | |
tft.print("Humidity:"); | |
tft.setTextColor(TFT_RED); | |
tft.setFreeFont(FF17); | |
playReadyMelody(); | |
} | |
void loop() | |
{ | |
if (millis() - previousMillis > interval) { | |
float temperature = 0; | |
float humidity = 0; | |
int err = SimpleDHTErrSuccess; | |
if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { | |
Serial.print("Read DHT22 failed, err="); | |
Serial.println(err); | |
delay(2000); | |
return; | |
} | |
tft.fillRect(0, 62, 240, 22, TFT_BLACK); | |
tft.setCursor(ROW4); | |
tft.print(temperature); | |
tft.print(" Celsius"); | |
tft.fillRect(0, 106, 240, 22, TFT_BLACK); | |
tft.setCursor(ROW6); | |
tft.print(humidity); | |
tft.print(" %RH"); | |
int newMessages = bot.getUpdates(bot.last_message_received + 1); | |
if (newMessages) { | |
playNoteD4(); | |
String chatId = bot.messages[0].chat_id; | |
String message = bot.messages[0].text; | |
String statusText = ""; | |
if (message == "STATUS") { | |
statusText = "Temperature: " + String(temperature) + " °C" + | |
"\nHumidity: " + String(humidity) + " %RH"; | |
} | |
else if (message == "/start") { | |
statusText = "Welcome to Idris Bot. Reply STATUS to get temperature and humidity data. Thank you."; | |
} | |
else { | |
statusText = "Invalid command. Reply STATUS to get temperature and humidity data."; | |
} | |
bot.sendMessage(chatId, statusText, ""); | |
} | |
previousMillis = millis(); | |
} | |
} | |
const int FREQUENCY = 2000; | |
const int CHANNEL = 0; | |
const int RESOLUTION = 8; | |
void playTone(int *melody, int *melodyDur, int notesLength) | |
{ | |
for (int i = 0; i < notesLength; i++) { | |
ledcAttachPin(BUZZER, CHANNEL); | |
int noteDuration = 1000 / melodyDur[i]; | |
ledcWriteTone(CHANNEL, melody[i]); | |
int pauseBetweenNotes = noteDuration * 1.30; | |
delay(pauseBetweenNotes); | |
ledcDetachPin(BUZZER); | |
ledcWrite(CHANNEL, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment