Skip to content

Commit f34af40

Browse files
authored
Merge pull request witnessmenow#107 from MrSuhov/master
Posting image from serial
2 parents b4ebcae + 49d6df8 commit f34af40

File tree

6 files changed

+515
-282
lines changed

6 files changed

+515
-282
lines changed

examples/ESP8266/BulkMessages/BulkMessages.ino

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ UniversalTelegramBot bot(BOTtoken, secured_client);
2323
int Bot_mtbs = 1000; // mean time between scan messages
2424
long Bot_lasttime; // last time messages' scan has been done
2525

26+
DynamicJsonDocument usersDoc(1500);
27+
2628
int bulk_messages_mtbs = 1500; // mean time between send messages, 1.5 seconds
2729
int messages_limit_per_second = 25; // Telegram API have limit for bulk messages ~30 messages per second
2830

2931
String subscribed_users_filename = "subscribed_users.json";
3032

31-
DynamicJsonBuffer jsonBuffer;
32-
3333
void handleNewMessages(int numNewMessages) {
3434
Serial.println("handleNewMessages");
3535
Serial.println(String(numNewMessages));
@@ -94,7 +94,7 @@ void handleNewMessages(int numNewMessages) {
9494
}
9595
}
9696

97-
JsonObject& getSubscribedUsers() {
97+
JsonObject getSubscribedUsers() {
9898
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "r");
9999

100100
if (!subscribedUsersFile) {
@@ -104,53 +104,52 @@ JsonObject& getSubscribedUsers() {
104104
File f = SPIFFS.open("/"+subscribed_users_filename, "w");
105105
f.close();
106106

107-
JsonObject& users = jsonBuffer.createObject();
107+
JsonObject users;
108108

109109
return users;
110110
} else {
111111

112112
size_t size = subscribedUsersFile.size();
113113

114-
if (size > 1024) {
114+
if (size > 1500) {
115115
Serial.println("Subscribed users file is too large");
116116
//return users;
117117
}
118118

119119
String file_content = subscribedUsersFile.readString();
120-
121-
JsonObject& users = jsonBuffer.parseObject(file_content);
122-
123-
if (!users.success()) {
120+
DeserializationError error = deserializeJson(usersDoc, file_content);
121+
JsonObject users = usersDoc.to<JsonObject>();
122+
if (error) {
124123
Serial.println("Failed to parse subscribed users file");
125124
return users;
126125
}
127126

128127
subscribedUsersFile.close();
129-
130128
return users;
131129
}
132130
}
133131

134132
bool addSubscribedUser(String chat_id, String from_name) {
135-
JsonObject& users = getSubscribedUsers();
133+
JsonObject users = getSubscribedUsers();
136134

137135
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "w+");
138136

139137
if (!subscribedUsersFile) {
140138
Serial.println("Failed to open subscribed users file for writing");
141139
//return false;
142140
}
143-
144-
users.set(chat_id, from_name);
145-
users.printTo(subscribedUsersFile);
141+
142+
//users.getOrCreateMember(chat_id, from_name);
143+
users[chat_id] = from_name;
144+
serializeJson(users, subscribedUsersFile);
146145

147146
subscribedUsersFile.close();
148147

149148
return true;
150149
}
151150

152151
bool removeSubscribedUser(String chat_id) {
153-
JsonObject& users = getSubscribedUsers();
152+
JsonObject users = getSubscribedUsers();
154153

155154
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "w");
156155

@@ -160,8 +159,7 @@ bool removeSubscribedUser(String chat_id) {
160159
}
161160

162161
users.remove(chat_id);
163-
users.printTo(subscribedUsersFile);
164-
162+
serializeJson(users, subscribedUsersFile);
165163
subscribedUsersFile.close();
166164

167165
return true;
@@ -170,13 +168,13 @@ bool removeSubscribedUser(String chat_id) {
170168
void sendMessageToAllSubscribedUsers(String message) {
171169
int users_processed = 0;
172170

173-
JsonObject& users = getSubscribedUsers();
171+
JsonObject users = getSubscribedUsers();
174172

175173
for (JsonObject::iterator it=users.begin(); it!=users.end(); ++it) {
176174
users_processed++;
177175

178176
if (users_processed < messages_limit_per_second) {
179-
const char* chat_id = it->key;
177+
const char* chat_id = it->key().c_str();
180178
bot.sendMessage(chat_id, message, "");
181179
} else {
182180
delay(bulk_messages_mtbs);

examples/ESP8266/SendPhoto/PhotoFromFileID/PhotoFromFileID.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ void handleNewMessages(int numNewMessages) {
3939
String response = bot.sendPhoto(chat_id, test_photo_url, "This photo was sent using URL");
4040

4141
if (bot.checkForOkResponse(response)) {
42-
DynamicJsonBuffer jsonBuffer;
43-
JsonObject& images = jsonBuffer.parseObject(response);
42+
DynamicJsonDocument images(1500);
43+
DeserializationError error = deserializeJson(images, response);
4444

4545
// There are 3 image sizes after Telegram has process photo
4646
// You may choose what you want, in example was choosed bigger size

examples/ESP8266/SendPhoto/PhotoFromSD/PhotoFromSD.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void loop() {
8080
//Content type for PNG image/png
8181
String sent = bot.sendPhotoByBinary(chat_id, "image/jpeg", myFile.size(),
8282
isMoreDataAvailable,
83-
getNextByte);
83+
getNextByte,nullptr,nullptr);
8484

8585
if (sent) {
8686
Serial.println("was successfully sent");
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
#include <ESP8266WiFi.h>
2+
#include <WiFiClientSecure.h>
3+
#include <UniversalTelegramBot.h>
4+
5+
// Initialize Telegram BOT
6+
#define BOTtoken "XXXXXXXXXXXXX" //Gavrusha_test_bot
7+
8+
// Initialize Wifi connection to the router
9+
char ssid[] = "XXXXXXXXXXXXX"; // your network SSID (name)
10+
char password[] = "XXXXXXXXXXXXX"; // your network key
11+
12+
WiFiClientSecure client;
13+
UniversalTelegramBot bot(BOTtoken, client);
14+
15+
String esp_id;
16+
String chat_id;
17+
18+
int Bot_mtbs = 1; //mean time between scan messages in seconds
19+
long Bot_lasttime; //last time messages' scan has been done
20+
21+
// Initialize Camera settings
22+
#define PIC_PKT_LEN 128 //data length of each read, dont set this too big because ram is limited
23+
#define PIC_FMT_VGA 7
24+
#define PIC_FMT_CIF 5
25+
#define PIC_FMT_OCIF 3
26+
#define CAM_ADDR 0
27+
#define CAM_SERIAL Serial
28+
#define PIC_FMT PIC_FMT_VGA
29+
30+
const byte cameraAddr = (CAM_ADDR << 5); // addr
31+
int picTotalLen = 0; // picture length
32+
unsigned int pktCnt;
33+
unsigned int pktCnt_i = 0;
34+
char pkt[PIC_PKT_LEN];
35+
uint16_t cnt;
36+
int n = 0;
37+
38+
void handleNewMessages(int numNewMessages) {
39+
for (int i = 0; i < numNewMessages; i++) {
40+
chat_id = String(bot.messages[i].chat_id);
41+
String text = bot.messages[i].text;
42+
Serial.print(F("Bot text message: "));
43+
Serial.println(text);
44+
if (text.startsWith("/photo")) sendSnapshot(chat_id);
45+
if (text.startsWith("/start")) {
46+
bot.sendMessage(chat_id, "Hello, " + bot.messages[i].from_name + " " + bot.messages[i].from_id + " from " + chat_id, "");
47+
}
48+
}
49+
}
50+
51+
void setup() {
52+
Serial.begin(115200);
53+
WiFi.mode(WIFI_STA);
54+
WiFi.disconnect();
55+
delay(100);
56+
57+
// Attempt to connect to Wifi network:
58+
Serial.print(F("Connecting Wifi: "));
59+
Serial.println(ssid);
60+
WiFi.begin(ssid, password);
61+
initializeCam();
62+
Bot_lasttime = millis();
63+
}
64+
65+
void loop() {
66+
67+
if (WiFi.status() == WL_CONNECTED) {
68+
if (millis() > Bot_lasttime + Bot_mtbs * 1000) { // checking for
69+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1); // launch API GetUpdates up to xxx message
70+
if (numNewMessages) {
71+
handleNewMessages(numNewMessages); // process incoming messages
72+
}
73+
Bot_lasttime = millis();
74+
}
75+
}
76+
} //конец основного цикла
77+
/*********************************************************************/
78+
void sendSnapshot(String chat_id) { // reading picture from camera serial and sending it directly to telegram
79+
char cmd[] = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
80+
Serial.println(F("\r\nbegin to take picture"));
81+
if (n == 0) preCapture();
82+
Capture();
83+
pktCnt_i = 0;
84+
pktCnt = (picTotalLen) / (PIC_PKT_LEN - 6); // cutting image into PIC_PKT_LEN chunks
85+
if ((picTotalLen % (PIC_PKT_LEN-6)) != 0) pktCnt += 1;
86+
Serial.setTimeout(1000);
87+
String sent = bot.sendPhotoByBinary(chat_id, "image/jpeg", picTotalLen,
88+
isMoreDataAvailable, nullptr,
89+
getNextBuffer, getNextBufferLen);
90+
91+
cmd[4] = 0xf0;
92+
cmd[5] = 0xf0;
93+
sendCmd(cmd, 6);
94+
if (sent) Serial.println(F("\r\nwas successfully sent"));
95+
else Serial.println(F("\r\nwas not sent"));
96+
n++;
97+
}
98+
/*********************************************************************/
99+
bool isMoreDataAvailable() {
100+
return (pktCnt_i < pktCnt);
101+
}
102+
/*********************************************************************/
103+
byte* getNextBuffer(){
104+
char cmd[] = {0xaa, 0x0e | cameraAddr, 0x00, 0x00, pktCnt_i & 0xff,(pktCnt_i >> 8) & 0xff} ;
105+
// cmd[0] = 0xaa;
106+
// cmd[1] = 0x0e | cameraAddr;
107+
// cmd[2] = 0x00;
108+
// cmd[3] = 0x00;
109+
// cmd[4] = pktCnt_i & 0xff;
110+
// cmd[5] = (pktCnt_i >> 8) & 0xff;
111+
int retry_cnt = 0;
112+
retry:
113+
delay(10);
114+
clearRxBuf();
115+
sendCmd(cmd, 6);
116+
cnt = Serial.readBytes((char *)pkt, PIC_PKT_LEN);
117+
unsigned char sum = 0;
118+
for (int y = 0; y < cnt - 2; y++)
119+
sum += pkt[y];
120+
if (sum != pkt[cnt-2])
121+
if (++retry_cnt < 100)
122+
goto retry;
123+
pktCnt_i++;
124+
125+
return ( uint8_t *)&pkt[4];
126+
}
127+
/*********************************************************************/
128+
int getNextBufferLen(){
129+
return cnt-6;
130+
}
131+
/*********************************************************************/
132+
void clearRxBuf()
133+
{
134+
while (Serial.available()) Serial.read();
135+
}
136+
/*********************************************************************/
137+
void sendCmd(char scmd[], int cmd_len)
138+
{
139+
for (char i = 0; i < cmd_len; i++) Serial.write(scmd[i]);
140+
}
141+
/*********************************************************************/
142+
void initializeCam()
143+
{
144+
char cmd[] = {0xaa,0x0d|cameraAddr,0x00,0x00,0x00,0x00} ;
145+
unsigned char resp[6];
146+
147+
Serial.setTimeout(500);
148+
while (1)
149+
{
150+
clearRxBuf();
151+
sendCmd(cmd,6);
152+
Serial.setTimeout(1000);
153+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
154+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
155+
{
156+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
157+
if (resp[0] == 0xaa && resp[1] == (0x0d | cameraAddr) && resp[2] == 0 && resp[3] == 0 && resp[4] == 0 && resp[5] == 0) break;
158+
}
159+
}
160+
cmd[1] = 0x0e | cameraAddr;
161+
cmd[2] = 0x0d;
162+
sendCmd(cmd, 6);
163+
Serial.println(F("\nCamera initialization done."));
164+
}
165+
/*********************************************************************/
166+
void preCapture()
167+
{
168+
char cmd[] = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
169+
unsigned char resp[6];
170+
171+
Serial.setTimeout(100);
172+
while (1)
173+
{
174+
clearRxBuf();
175+
sendCmd(cmd, 6);
176+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
177+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x01 && resp[4] == 0 && resp[5] == 0) break;
178+
}
179+
}
180+
/*********************************************************************/
181+
void Capture()
182+
{
183+
char cmd[] = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN>>8) & 0xff ,0};
184+
unsigned char resp[6];
185+
186+
Serial.setTimeout(100);
187+
while (1)
188+
{
189+
clearRxBuf();
190+
sendCmd(cmd, 6);
191+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
192+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x06 && resp[4] == 0 && resp[5] == 0) break;
193+
}
194+
cmd[1] = 0x05 | cameraAddr;
195+
cmd[2] = 0;
196+
cmd[3] = 0;
197+
cmd[4] = 0;
198+
cmd[5] = 0;
199+
200+
while (1)
201+
{
202+
clearRxBuf();
203+
sendCmd(cmd, 6);
204+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
205+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x05 && resp[4] == 0 && resp[5] == 0) break;
206+
}
207+
cmd[1] = 0x04 | cameraAddr;
208+
cmd[2] = 0x1;
209+
210+
while (1)
211+
{
212+
clearRxBuf();
213+
sendCmd(cmd, 6);
214+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
215+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x04 && resp[4] == 0 && resp[5] == 0)
216+
{
217+
Serial.setTimeout(1000);
218+
if (Serial.readBytes((char *)resp, 6) != 6) continue;
219+
if (resp[0] == 0xaa && resp[1] == (0x0a | cameraAddr) && resp[2] == 0x01)
220+
{
221+
picTotalLen = (resp[3]) | (resp[4] << 8) | (resp[5] << 16);
222+
Serial.print(F("picTotalLen:"));
223+
Serial.println(picTotalLen);
224+
break;
225+
}
226+
}
227+
}
228+
}

0 commit comments

Comments
 (0)