|
| 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