Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- const char* ssid = "";
- const char* password = "";
- WiFiClient client;
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- WiFiClientSecure client1;
- #include <DHT.h>
- #define DHTPIN D8
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- #define BOTtoken "850655730:AAFB90ZCiMTDaHMjUw6Zux8pln-RTSIAJv8"
- String thingSpeakAddress = "api.thingspeak.com";
- String writeAPIKey;
- String tsfield1Name;
- String request_string;
- float h, t, f;
- String myData;
- UniversalTelegramBot bot(BOTtoken, client1);
- int Bot_mtbs = 1000;
- long Bot_lasttime;
- bool Start = false;
- const int ledPin = D2;
- const int ledPin1 = D9;
- const int ledPin2 = D10;
- int ledStatus = 0;
- const int buzzer = D11;
- int buzzerStatus = 0;
- void handleNewMessages(int numNewMessages) {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i = 0; i < numNewMessages; i++) {
- String chat_id = String(bot.messages[i].chat_id);
- String text = bot.messages[i].text;
- String from_name = bot.messages[i].from_name;
- if (from_name == "") from_name = "Guest";
- if (text == "Cek suhu") {
- myData = "Suhu :";
- myData += t;
- myData += " C";
- digitalWrite(ledPin, HIGH);
- ledStatus = 1;
- delay(2000);
- digitalWrite(ledPin, LOW);
- ledStatus = 0;
- digitalWrite(buzzer, HIGH);
- tone(buzzer, 2000);
- buzzerStatus = 1;
- delay (2000);
- digitalWrite(buzzer, LOW);
- noTone(buzzer);
- buzzerStatus = 0;
- bot.sendMessage(chat_id, myData, "");
- }
- if (text == "Cek kelembapan") {
- myData = "Kelembapan :";
- myData += h;
- myData += " %RH";
- digitalWrite(ledPin1, HIGH);
- ledStatus = 1;
- delay(2000);
- digitalWrite(ledPin1, LOW);
- ledStatus = 0;
- digitalWrite(buzzer, HIGH);
- tone(buzzer, 2000);
- buzzerStatus = 1;
- delay (2000);
- digitalWrite(buzzer, LOW);
- noTone(buzzer);
- buzzerStatus = 0;
- bot.sendMessage(chat_id, myData, "");
- }
- if (text == "Cek semua") {
- myData = "Suhu :";
- myData += t;
- myData += " C";
- myData += "\nKelembapan :";
- myData += h;
- myData += " %RH";
- digitalWrite(ledPin2, HIGH);
- ledStatus = 1;
- delay(2000);
- digitalWrite(ledPin2, LOW);
- ledStatus = 0;
- digitalWrite(buzzer, HIGH);
- tone(buzzer, 2000);
- buzzerStatus = 1;
- delay (2000);
- digitalWrite(buzzer, LOW);
- noTone(buzzer);
- buzzerStatus = 0;
- bot.sendMessage(chat_id, myData, "");
- }
- if (text == "Start") {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "Menggabungkan Telegram dan Thingspeak.\n\n";
- welcome += "Cek suhu : untuk melihat data suhu\n";
- welcome += "Cek kelembapan : untuk melihat data kelembapan\n";
- welcome += "Cek semua : untuk mengecek semuanya\n";
- bot.sendMessage(chat_id, welcome, "Markdown");
- }
- }
- }
- void setup()
- {
- Serial.begin(115200);
- delay(10);
- dht.begin();
- lcd.begin();
- lcd.backlight();
- lcd.print("Selamat Datang");
- delay(2000);
- lcd.clear();
- pinMode(ledPin, OUTPUT);
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(buzzer, OUTPUT);
- digitalWrite(ledPin, HIGH);
- digitalWrite(ledPin1, HIGH);
- digitalWrite(ledPin2, HIGH);
- delay (1000);
- digitalWrite(ledPin, LOW);
- digitalWrite(ledPin1, LOW);
- digitalWrite(ledPin2, LOW);
- pinMode(buzzer, LOW);
- WiFi.begin(ssid, password);
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- delay(10);
- }
- void loop()
- {
- delay(2000);
- h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- t = dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- lcd.setCursor(0, 0);
- lcd.print("Temp : ");
- lcd.print(t);
- lcd.print("C");
- lcd.setCursor(0, 1);
- lcd.print("Hum : ");
- lcd.print(h);
- lcd.print("%");
- kirim_thingspeak(t, h);
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- 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();
- }
- }
- void kirim_thingspeak(float suhu, float hum) {
- if (client.connect("api.thingspeak.com", 80)) {
- request_string = "/update?";
- request_string += "key=";
- request_string += "BZQEJGS155GDQ3QV"; // api key thingspeak
- request_string += "&";
- request_string += "field1";
- request_string += "=";
- request_string += suhu;
- request_string += "&";
- request_string += "field2";
- request_string += "=";
- request_string += hum;
- Serial.println(String("GET ") + request_string + " HTTP/1.1\r\n" +
- "Host: " + thingSpeakAddress + "\r\n" +
- "Connection: close\r\n\r\n");
- client.print(String("GET ") + request_string + " HTTP/1.1\r\n" +
- "Host: " + thingSpeakAddress + "\r\n" +
- "Connection: close\r\n\r\n");
- unsigned long timeout = millis();
- while (client.available() == 0) {
- if (millis() - timeout > 5000) {
- Serial.println(">>> Client Timeout !");
- client.stop();
- return;
- }
- }
- while (client.available()) {
- String line = client.readStringUntil('\r');
- }
- Serial.println();
- Serial.println("closing connection");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement