-
Notifications
You must be signed in to change notification settings - Fork 1
/
rainservo.ino
115 lines (101 loc) · 2.81 KB
/
rainservo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#define WIFI_SSID "wifi-name" //change this accordingly
#define WIFI_PASSWORD "password" //change this accordingly
#define BOT_TOKEN "bot-token" //change this accordingly
const unsigned long BOT_MTBS = 1000;
Servo myservo;
#define rainAnalog 35
int analogVal;
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime;
int servoStatus;
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(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 == "/close")
{
myservo.write(100);
servoStatus = 1;
bot.sendMessage(chat_id, "Recieved", "");
}
if (text == "/open")
{
myservo.write(5);
servoStatus = 0;
bot.sendMessage(chat_id,"Recieved", "");
}
if (text == "/report")
{
if(analogVal<2000)
{
bot.sendMessage(chat_id, "IT IS RAINING", "");
}
else
{
bot.sendMessage(chat_id,"IT IS NOT RAINING", "");
}
}
if (text == "/start")
{
String welcome = "Welcome to the water sensing bot, " + from_name + ".\n";
welcome += "This is your bot.\n\n";
welcome += "/report : Returns current status of the weather\n";
welcome += "/close : to close the motor\n";
welcome += "/open : to open the motor\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(rainAnalog,INPUT);
myservo.attach(13);
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);
}
void loop() {
analogVal = analogRead(rainAnalog);
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();
}
}