4
4
* written by Giacarlo Bacchio (Gianbacchio on Github) *
5
5
* adapted by Brian Lough *
6
6
*******************************************************************/
7
-
8
7
#include < ESP8266WiFi.h>
9
8
#include < WiFiClientSecure.h>
10
9
#include < UniversalTelegramBot.h>
11
10
#include < ArduinoJson.h>
12
-
13
11
#include < SPI.h>
14
12
#include < SD.h>
15
13
16
- bool isMoreDataAvailable ();
17
- byte getNextByte ();
18
-
19
14
// Initialize Wifi connection to the router
20
- char ssid[] = " xxxxxxxxxxxxxxxxxxxxxx" ; // your network SSID (name)
21
- char password[] = " yyyyyyyy" ; // your network key
22
-
23
- File myFile;
15
+ char ssid[] = " XXXXXX" ; // your network SSID (name)
16
+ char password[] = " YYYYYY" ; // your network key
24
17
25
18
// Initialize Telegram BOT
26
19
#define BOTtoken " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
@@ -31,83 +24,87 @@ UniversalTelegramBot bot(BOTtoken, client);
31
24
int Bot_mtbs = 1000 ; // mean time between scan messages
32
25
long Bot_lasttime; // last time messages' scan has been done
33
26
27
+ File myFile;
28
+ bool isMoreDataAvailable ();
29
+ byte getNextByte ();
34
30
35
31
void setup () {
36
-
37
32
Serial.begin (115200 );
38
33
39
- Serial.print (" Initializing SD card..." );
34
+ Serial.print (" Initializing SD card.... " );
40
35
41
36
if (!SD.begin (D0)) {
42
- Serial.println (" initialization failed!" );
37
+ Serial.println (" failed!" );
43
38
return ;
44
39
}
45
- Serial.println (" initialization done." );
46
40
41
+ Serial.println (" done." );
47
42
48
- // Set WiFi to station mode and disconnect from an AP if it was Previously
49
- // connected
43
+ // Set WiFi to station mode and disconnect from an AP if it was Previously connected
50
44
WiFi.mode (WIFI_STA);
51
45
WiFi.disconnect ();
52
46
delay (100 );
53
47
54
- // Attempt to connect to Wifi network:
48
+ // attempt to connect to Wifi network:
55
49
Serial.print (" Connecting Wifi: " );
56
50
Serial.println (ssid);
57
51
WiFi.begin (ssid, password);
52
+
58
53
while (WiFi.status () != WL_CONNECTED) {
59
54
Serial.print (" ." );
60
55
delay (500 );
61
56
}
57
+
62
58
Serial.println (" " );
63
59
Serial.println (" WiFi connected" );
64
- Serial.println (" IP address: " );
65
- IPAddress ip = WiFi.localIP ();
66
- Serial.println (ip);
67
-
68
-
69
- }
70
-
71
- bool isMoreDataAvailable (){
72
- return myFile.available ();
73
- }
74
-
75
- byte getNextByte (){
76
- return myFile.read ();
60
+ Serial.print (" IP address: " );
61
+ Serial.println (WiFi.localIP ());
77
62
}
78
63
79
64
void loop () {
80
-
81
65
if (millis () > Bot_lasttime + Bot_mtbs) {
82
66
int numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
67
+
83
68
while (numNewMessages) {
84
69
Serial.println (" got response" );
85
70
86
-
87
71
String chat_id = bot.messages [0 ].chat_id ;
72
+ String file_name = " box.jpg" ;
73
+
74
+ myFile = SD.open (file_name);
88
75
89
- myFile = SD.open (" box.jpg" );
90
76
if (myFile) {
91
- Serial.println (" box.jpg:" );
77
+ Serial.print (file_name);
78
+ Serial.print (" ...." );
92
79
93
80
// Content type for PNG image/png
94
81
bool sent = bot.sendPhotoByBinary (chat_id, " image/jpeg" , myFile.size (),
95
82
isMoreDataAvailable,
96
83
getNextByte);
97
84
98
- if (sent){
99
- Serial.println (" Succesfully sent photo " );
85
+ if (sent) {
86
+ Serial.println (" was successfully sent " );
100
87
} else {
101
- Serial.println (" Error sending photo " );
88
+ Serial.println (" was not sent " );
102
89
}
103
90
104
91
myFile.close ();
105
- } else {
106
- // if the file didn't open, print an error:
107
- Serial.println (" error opening photo.jpg " );
92
+ } else {
93
+ // if the file didn't open, print an error:
94
+ Serial.println (" error opening photo" );
108
95
}
96
+
109
97
numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
110
98
}
99
+
111
100
Bot_lasttime = millis ();
112
101
}
113
102
}
103
+
104
+ bool isMoreDataAvailable (){
105
+ return myFile.available ();
106
+ }
107
+
108
+ byte getNextByte (){
109
+ return myFile.read ();
110
+ }
0 commit comments