Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- // -------- Define Pins -----------
- #define Soil_Moisture_PIN 34 // ✓
- #define Pump_PIN 2 // ✓
- int Sensor_Value;
- int onFlag = 0;
- const unsigned long BOT_MTBS = 1000; // mean time between scan messages
- unsigned long bot_lasttime; // last time messages' scan has been done
- // Wifi network station credentials
- #define WIFI_SSID "network"
- #define WIFI_PASSWORD "123456789"
- #define BOT_TOKEN "6330967028:AAFpK8lrNqvqm9sRZPAgBpJGSNEBto8ctpg"
- #define CHAT_ID "-833531923"
- WiFiClientSecure secured_client;
- UniversalTelegramBot bot(BOT_TOKEN, secured_client);
- //////////////////////////////////////////////////////////
- void handleNewMessages(int numNewMessages) {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i = 0; i < numNewMessages; i++) {
- String chat_id = 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 == "/send_test_action") {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, "Did you see the action message?");
- }
- if (text == "/Sensor_Data") {
- int value1 = check_Soil_Moisture_Sensor();
- bot.sendMessage(chat_id, "Sensor reading is : " + String(value1) + "%");
- }
- if (text == "/options") {
- String keyboardJson = "[[\"/Sensor_Data\"]]";
- bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
- }
- if (text == "/start") {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Chat Action Bot example.\n\n";
- welcome += "/Sensor_Data : to return the current sensors data\n";
- welcome += "/options : returns a custom reply keyboard\n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- //////////////////////////////////////////////////////////
- void setup() {
- onFlag = 0;
- Serial.begin(115200);
- Serial.println();
- pinMode(Soil_Moisture_PIN, INPUT);
- pinMode(Pump_PIN, OUTPUT);
- digitalWrite(Pump_PIN, 0);
- // attempt to connect to Wifi network:
- Serial.print("Connecting to Wifi SSID ");
- Serial.print(WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.print("\nWiFi connected. IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("Retrieving time: ");
- configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
- time_t now = time(nullptr);
- while (now < 24 * 3600) {
- Serial.print(".");
- delay(100);
- now = time(nullptr);
- }
- Serial.println(now);
- bot.sendMessage(CHAT_ID, "Bot started up", "");
- }
- int check_Soil_Moisture_Sensor() {
- int value1 = analogRead(Soil_Moisture_PIN);
- value1 = map(value1, 0, 4095, 100, 0);
- Serial.print("Sensor value: ");
- Serial.print(value1);
- Serial.println("%");
- return value1;
- }
- void loop() {
- ///////////////////////////////////////////
- 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();
- }
- ///////////////////////////////////////////
- delay(50);
- Sensor_Value = check_Soil_Moisture_Sensor();
- if (Sensor_Value < 50 && onFlag == 0) {
- onFlag = 1;
- bot.sendMessage(CHAT_ID, "Soil need Water " + String(Sensor_Value) + "% - Pump is turned [ON]");
- Pump_ON();
- }
- else if (Sensor_Value < 50 && onFlag == 1) {
- Pump_ON();
- }
- else if(Sensor_Value > 50 && onFlag == 1){
- onFlag = 0;
- bot.sendMessage(CHAT_ID, "Soil Full of Water " + String(Sensor_Value) + "% - Pump is turned [OFF]");
- Pump_OFF();
- }
- else {
- onFlag = 0;
- Pump_OFF();
- }
- delay(50);
- } //end of Loop
- void Pump_ON() {
- digitalWrite(Pump_PIN, 1);
- }
- void Pump_OFF() {
- digitalWrite(Pump_PIN, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement