|
| 1 | +#include <WiFiNINA.h> |
| 2 | +#include <UniversalTelegramBot.h> |
| 3 | +#include <Adafruit_Sensor.h> //for sensor |
| 4 | +#include <DHT.h> //for sensor |
| 5 | +#include <DHT_U.h> //for sensor |
| 6 | +#include <Servo.h> //only for Servo |
| 7 | + |
| 8 | + |
| 9 | +char SSID[] = " "; // ENTER NETWORK SSID & PASS |
| 10 | +char PASS[] = " "; |
| 11 | + |
| 12 | +#define BOTtoken " " //YOUR BOT TOKEN |
| 13 | +#define LEDPIN 2 //YOUR PIN NUMBER ON BOARD |
| 14 | +#define DHTPIN 5 |
| 15 | +#define LDRPIN A0 |
| 16 | +#define DHTTYPE DHT11 |
| 17 | +#define servoPin 13 |
| 18 | + |
| 19 | + |
| 20 | +WiFiSSLClient client; |
| 21 | +UniversalTelegramBot bot(BOTtoken, client); |
| 22 | +int Bot_mtbs = 1000; |
| 23 | +long Bot_lasttime; |
| 24 | + |
| 25 | +Servo myservo; // create servo object to control a servo |
| 26 | + |
| 27 | +int pulse = 0; //pulse initial value |
| 28 | + |
| 29 | +// Seensor configuration |
| 30 | +DHT dht(DHTPIN, DHTTYPE); |
| 31 | +float myTemperature; |
| 32 | +float myHumidity; |
| 33 | +int LDRvalue; |
| 34 | + |
| 35 | + |
| 36 | +void setup() |
| 37 | +{ |
| 38 | + Serial.begin(9600); //YOUR SERIAL MONITOR SHOULD BE 9600 |
| 39 | + delay(2000); |
| 40 | + Serial.print("Connecting to Wifi Network: "); |
| 41 | + Serial.println(SSID); |
| 42 | + while (WiFi.begin(SSID, PASS) != WL_CONNECTED) |
| 43 | + { |
| 44 | + Serial.println(".. Loading connection .. "); |
| 45 | + delay(500); |
| 46 | + } |
| 47 | + |
| 48 | + Serial.println(""); |
| 49 | + Serial.println("WiFi connected"); |
| 50 | + Serial.println("IP address: "); |
| 51 | + IPAddress ip = WiFi.localIP(); |
| 52 | + Serial.println(ip); |
| 53 | + Serial.println(" "); |
| 54 | + pinMode(LEDPIN, OUTPUT); |
| 55 | + myservo.attach(servoPin); // attaches the servo on pin 13 to the servo object |
| 56 | + myservo.write(0); // sets the servo position |
| 57 | +} |
| 58 | +void loop() { |
| 59 | + |
| 60 | + LDRvalue = analogRead(LDRPIN); |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + if (millis() > Bot_lasttime + Bot_mtbs) |
| 65 | + { |
| 66 | + dht.begin(); |
| 67 | + myTemperature = dht.readTemperature(); |
| 68 | + myHumidity = dht.readHumidity(); |
| 69 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 70 | + while (numNewMessages) { |
| 71 | + Serial.println("Got response from Telegram bot"); |
| 72 | + for (int i = 0; i < numNewMessages; i++) { |
| 73 | + |
| 74 | + if (bot.messages[i].text == "/ON") { |
| 75 | + pulse = 255; |
| 76 | + bot.sendMessage(bot.messages[i].chat_id, "LED is ON!", ""); |
| 77 | + |
| 78 | + } |
| 79 | + if (bot.messages[i].text == "/Increase") { |
| 80 | + if (pulse <= 225 ) |
| 81 | + { |
| 82 | + pulse = pulse + 30; // add 30 to the pulse |
| 83 | + bot.sendMessage(bot.messages[i].chat_id, "Brightness Increased!", ""); // the message the bot will send to you |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + else |
| 88 | + { |
| 89 | + pulse = 255; // if the first condition did not apply then pulse will be 255 |
| 90 | + bot.sendMessage(bot.messages[i].chat_id, "Cannot increase further! ", ""); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + if (bot.messages[i].text == "/Decrease") { // if you sent this command to your bot |
| 95 | + if (pulse >= 30 ) // if the pulse is greater than or equal to 30 |
| 96 | + { |
| 97 | + pulse = pulse - 30 ; // substract 30 |
| 98 | + bot.sendMessage(bot.messages[i].chat_id, "Brightness Decreased!", ""); |
| 99 | + |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + pulse = 0; // otherwise the pulse is 0 |
| 104 | + bot.sendMessage(bot.messages[i].chat_id, "Cannot decrease further! ", ""); // the bot will notifiy you that LED cannot be decreased further |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + } |
| 109 | + if (bot.messages[i].text == "/OFF") { |
| 110 | + pulse = 0; //pulse will be set to 0 (it will be turned off) |
| 111 | + bot.sendMessage(bot.messages[i].chat_id, "LED is OFF!", ""); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + if (bot.messages[i].text == "/FADE") |
| 117 | + { |
| 118 | + //fades to full brightness then goes back to being off |
| 119 | + |
| 120 | + pulse = 0; |
| 121 | + while (pulse < 255) |
| 122 | + { |
| 123 | + pulse = pulse + 40; |
| 124 | + analogWrite(LEDPIN, pulse); |
| 125 | + delay(300); |
| 126 | + } |
| 127 | + while (pulse > 0) |
| 128 | + { |
| 129 | + pulse = pulse - 40; |
| 130 | + analogWrite(LEDPIN, pulse); |
| 131 | + delay(300); |
| 132 | + } |
| 133 | + pulse = 0; |
| 134 | + |
| 135 | + bot.sendMessage(bot.messages[i].chat_id, "LED has faded!", ""); |
| 136 | + } |
| 137 | + |
| 138 | + if (bot.messages[i].text == "/TEMP") |
| 139 | + bot.sendMessage(bot.messages[i].chat_id, "Temperature is " + String(myTemperature) + "*C", ""); |
| 140 | + |
| 141 | + if (bot.messages[i].text == "/HUMID") |
| 142 | + bot.sendMessage(bot.messages[i].chat_id, "Humidity is " + String(myHumidity) + "%", ""); |
| 143 | + |
| 144 | + if (bot.messages[i].text == "/LDR") |
| 145 | + { |
| 146 | + if (LDRvalue <= 200) //least value of detection |
| 147 | + bot.sendMessage(bot.messages[i].chat_id, "DARK : Analog Value = " + String(LDRvalue), ""); |
| 148 | + |
| 149 | + else if (LDRvalue > 200 && LDRvalue <= 500) |
| 150 | + bot.sendMessage(bot.messages[i].chat_id, "DIM LIGHT : Analog Value = " + String(LDRvalue), ""); |
| 151 | + |
| 152 | + else if (LDRvalue > 500 && LDRvalue <= 800) |
| 153 | + bot.sendMessage(bot.messages[i].chat_id, "BRIGHT LIGHT : Analog Value = " + String(LDRvalue), ""); |
| 154 | + |
| 155 | + else if (LDRvalue > 800) |
| 156 | + bot.sendMessage(bot.messages[i].chat_id, "FULL DAY LIGHT : Analog Value = " + String(LDRvalue), ""); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + if (bot.messages[i].text == "/ONservo") |
| 161 | + { |
| 162 | + myservo.attach(servoPin); |
| 163 | + myservo.write(90); // sets the servo position |
| 164 | + bot.sendMessage(bot.messages[i].chat_id, "Servo is ON", ""); |
| 165 | + } |
| 166 | + |
| 167 | + if (bot.messages[i].text == "/OFFservo") { |
| 168 | + myservo.write(0); |
| 169 | + myservo.detach(); // sets the servo position |
| 170 | + bot.sendMessage(bot.messages[i].chat_id, "Servo is OFF", ""); |
| 171 | + } |
| 172 | + |
| 173 | + if (bot.messages[i].text == "/TRYservo") { |
| 174 | + myservo.write(0); // sets the servo position |
| 175 | + delay(700); |
| 176 | + myservo.write(90); // sets the servo position |
| 177 | + delay(700); |
| 178 | + myservo.write(0); |
| 179 | + delay(700); |
| 180 | + myservo.write(90); // sets the servo position |
| 181 | + delay(700); |
| 182 | + myservo.write(0); // sets the servo position |
| 183 | + bot.sendMessage(bot.messages[i].chat_id, "Servo tested", ""); |
| 184 | + } |
| 185 | + |
| 186 | + if (bot.messages[i].text == "help") |
| 187 | + { |
| 188 | + bot.sendMessage(bot.messages[i].chat_id, "Hello ASSEMBLY attendee! I am your bot! \n Use following commands: \n /ON - turns on the LED connected \n /OFF - turns off LED connected \n /TEMP - gives temperature \n /HUMID - gives humidity \n /Increase - increases LED brightness \n /Decrease - decreases LED brightness \n /FADE - shows led fading \n /ONservo - pushes servo out \n /OFFservo - pulls servo back \n /TRYservo - test servo", ""); |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + Serial.println("User sent " + bot.messages[i].text); |
| 193 | + |
| 194 | + } |
| 195 | + numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 196 | + } |
| 197 | + Bot_lasttime = millis(); |
| 198 | + } |
| 199 | + analogWrite(LEDPIN, pulse); |
| 200 | +} |
0 commit comments