11
11
#include < WiFiClientSecure.h>
12
12
#include < UniversalTelegramBot.h>
13
13
14
- // Initialize Wifi connection to the router
15
- const char *ssid = " mySSID" ;
16
- const char *password = " myPASSWORD" ;
14
+ // Wifi network station credentials
15
+ #define WIFI_SSID " YOUR_SSID"
16
+ #define WIFI_PASSWORD " YOUR_PASSWORD"
17
+ // Telegram BOT Token (Get from Botfather)
18
+ #define BOT_TOKEN " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
17
19
18
- // Initialize Telegram BOT
19
- #define BOTtoken " xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxx" // your Bot Token (Get from Botfather)
20
- WiFiClientSecure client;
21
- UniversalTelegramBot bot (BOTtoken, client);
20
+ // LED parameters
21
+ const int ledPin = 2 ; // Internal LED on DevKit ESP32-WROOM (GPIO2)
22
+ const unsigned long BOT_MTBS = 1000 ; // mean time between scan messages
22
23
23
- int Bot_mtbs = 1000 ; // mean time between scan messages
24
- long Bot_lasttime; // last time messages' scan has been done
24
+ WiFiClientSecure secured_client;
25
+ UniversalTelegramBot bot (BOT_TOKEN, secured_client);
26
+ unsigned long bot_lasttime; // last time messages' scan has been done
25
27
int last_message_id = 0 ;
26
-
27
- // LED parameters
28
- const int ledPin = 2 ; // Internal LED on DevKit ESP32-WROOM (GPIO2)
29
28
int ledState = LOW;
30
29
31
30
void handleNewMessages (int numNewMessages)
@@ -134,35 +133,39 @@ void handleNewMessages(int numNewMessages)
134
133
void setup ()
135
134
{
136
135
Serial.begin (115200 );
136
+ Serial.println ();
137
137
138
- // Attempt to connect to Wifi network:
139
- Serial.print (" Connecting Wifi: " );
140
- Serial.println (ssid);
141
-
142
- // Set WiFi to station mode and disconnect from an AP if it was Previously
143
- // connected
144
- WiFi.mode (WIFI_STA);
145
- WiFi.begin (ssid, password);
146
-
138
+ // attempt to connect to Wifi network:
139
+ Serial.print (" Connecting to Wifi SSID " );
140
+ Serial.print (WIFI_SSID);
141
+ WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
142
+ secured_client.setCACert (TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
147
143
while (WiFi.status () != WL_CONNECTED)
148
144
{
149
145
Serial.print (" ." );
150
146
delay (500 );
151
147
}
152
-
153
- Serial.println (" " );
154
- Serial.println (" WiFi connected" );
155
- Serial.print (" IP address: " );
148
+ Serial.print (" \n WiFi connected. IP address: " );
156
149
Serial.println (WiFi.localIP ());
157
150
151
+ Serial.print (" Retrieving time: " );
152
+ configTime (0 , 0 , " pool.ntp.org" ); // get UTC time via NTP
153
+ time_t now = time (nullptr );
154
+ while (now < 24 * 3600 )
155
+ {
156
+ Serial.print (" ." );
157
+ delay (100 );
158
+ now = time (nullptr );
159
+ }
160
+ Serial.println (now);
161
+
158
162
pinMode (ledPin, OUTPUT); // initialize ledPin as an output.
159
163
digitalWrite (ledPin, ledState); // initialize pin as low (LED Off)
160
164
}
161
165
162
166
void loop ()
163
167
{
164
- // run this in loop to poll new messages
165
- if (millis () > Bot_lasttime + Bot_mtbs)
168
+ if (millis () - bot_lasttime > BOT_MTBS)
166
169
{
167
170
int numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
168
171
@@ -173,6 +176,6 @@ void loop()
173
176
numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
174
177
}
175
178
176
- Bot_lasttime = millis ();
179
+ bot_lasttime = millis ();
177
180
}
178
181
}
0 commit comments