|
| 1 | +/* |
| 2 | + Name: echoBot.ino |
| 3 | + Created: 20/06/2020 |
| 4 | + Author: Tolentino Cotesta <cotestatnt@yahoo.com> |
| 5 | + Description: an example that show how is possible send an image captured from a ESP32-CAM board |
| 6 | +*/ |
| 7 | + |
| 8 | +// WARNING!!! |
| 9 | +// Make sure that you have selected ESP32 Wrover Module, or another board which has PSRAM enabled |
| 10 | + |
| 11 | +#include "esp_camera.h" |
| 12 | + |
| 13 | +// Select camera model |
| 14 | +//#define CAMERA_MODEL_WROVER_KIT |
| 15 | +//#define CAMERA_MODEL_ESP_EYE |
| 16 | +//#define CAMERA_MODEL_M5STACK_PSRAM |
| 17 | +//#define CAMERA_MODEL_M5STACK_WIDE |
| 18 | +#define CAMERA_MODEL_AI_THINKER |
| 19 | +#include "camera.h" |
| 20 | + |
| 21 | +#include "soc/soc.h" // Brownout error fix |
| 22 | +#include "soc/rtc_cntl_reg.h" // Brownout error fix |
| 23 | + |
| 24 | +// Define where store images (on board SD card reader or internal flash memory) |
| 25 | +#define USE_MMC true |
| 26 | +#ifdef USE_MMC |
| 27 | + #include <SD_MMC.h> // Use onboard SD Card reader |
| 28 | + fs::FS &filesystem = SD_MMC; |
| 29 | +#else |
| 30 | + #include <FFat.h> // Use internal flash memory |
| 31 | + fs::FS &filesystem = FFat; // Is necessary select the proper partition scheme (ex. "Default 4MB with ffta..") |
| 32 | +#endif |
| 33 | + |
| 34 | +// You only need to format FFat when error on mount (don't work with MMC SD card) |
| 35 | +#define FORMAT_FS_IF_FAILED true |
| 36 | +#define FILENAME_SIZE 20 |
| 37 | +#define KEEP_IMAGE true |
| 38 | + |
| 39 | +#include <WiFi.h> |
| 40 | +#include <AsyncTelegram.h> |
| 41 | +AsyncTelegram myBot; |
| 42 | + |
| 43 | +const char* ssid = "XXXXXXXX"; // REPLACE mySSID WITH YOUR WIFI SSID |
| 44 | +const char* pass = "XXXXXXXX"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY |
| 45 | +const char* token = "XXXXXXXXXXXXXXXXXXXX"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN |
| 46 | + |
| 47 | + |
| 48 | +// Struct for saving time datas (needed for time-naming the image files) |
| 49 | +struct tm timeinfo; |
| 50 | + |
| 51 | + |
| 52 | +// List all files saved in the selected filesystem |
| 53 | +void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ |
| 54 | + Serial.printf("Listing directory: %s\r\n", dirname); |
| 55 | + |
| 56 | + File root = fs.open(dirname); |
| 57 | + if(!root){ |
| 58 | + Serial.println("- failed to open directory"); |
| 59 | + return; |
| 60 | + } |
| 61 | + if(!root.isDirectory()){ |
| 62 | + Serial.println(" - not a directory"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + File file = root.openNextFile(); |
| 67 | + while(file){ |
| 68 | + if(file.isDirectory()){ |
| 69 | + Serial.printf(" DIR : %s", file.name()); |
| 70 | + if(levels) |
| 71 | + listDir(fs, file.name(), levels -1); |
| 72 | + } |
| 73 | + else |
| 74 | + Serial.printf(" FILE: %s\tSIZE: %d", file.name(), file.size()); |
| 75 | + |
| 76 | + file = root.openNextFile(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +String takePicture(fs::FS &fs){ |
| 81 | + |
| 82 | + // Set filename with current timestamp "YYYYMMDD_HHMMSS.jpg" |
| 83 | + char pictureName[FILENAME_SIZE]; |
| 84 | + getLocalTime(&timeinfo); |
| 85 | + snprintf(pictureName, FILENAME_SIZE, "%02d%02d%02d_%02d%02d%02d.jpg", timeinfo.tm_year +1900, |
| 86 | + timeinfo.tm_mon +1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); |
| 87 | + |
| 88 | + // Path where new picture will be saved |
| 89 | + String path = "/"; |
| 90 | + path += String(pictureName); |
| 91 | + |
| 92 | + File file = fs.open(path.c_str(), FILE_WRITE); |
| 93 | + if(!file){ |
| 94 | + Serial.println("Failed to open file in writing mode"); |
| 95 | + return ""; |
| 96 | + } |
| 97 | + |
| 98 | + // Take Picture with Camera |
| 99 | + ledcWrite(15, 255); // Flash led ON |
| 100 | + camera_fb_t * fb = esp_camera_fb_get(); |
| 101 | + if(!fb) { |
| 102 | + Serial.println("Camera capture failed"); |
| 103 | + return ""; |
| 104 | + } |
| 105 | + ledcWrite(15, 0); // Flash led OFF |
| 106 | + |
| 107 | + // Save picture to memory |
| 108 | +#ifdef USE_MMC |
| 109 | + uint64_t freeBytes = SD_MMC.totalBytes() - SD_MMC.usedBytes(); |
| 110 | +#else |
| 111 | + uint64_t freeBytes = filesystem.freeBytes(); |
| 112 | +#endif |
| 113 | + |
| 114 | + if(freeBytes> file.size() ){ |
| 115 | + file.write(fb->buf, fb->len); // payload (image), payload length |
| 116 | + Serial.printf("Saved file to path: %s\n", path.c_str()); |
| 117 | + file.close(); |
| 118 | + } |
| 119 | + else |
| 120 | + Serial.println("Not enough space avalaible"); |
| 121 | + |
| 122 | + esp_camera_fb_return(fb); |
| 123 | + return path; |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +void setup() { |
| 128 | + Serial.begin(115200); |
| 129 | + Serial.setDebugOutput(true); |
| 130 | + Serial.println(); |
| 131 | + |
| 132 | + // Init the camera |
| 133 | + cameraSetup(FRAMESIZE_UXGA); |
| 134 | + |
| 135 | + // Init WiFi connections |
| 136 | + WiFi.begin(ssid, pass); |
| 137 | + while (WiFi.status() != WL_CONNECTED) { |
| 138 | + delay(500); |
| 139 | + Serial.print("."); |
| 140 | + } |
| 141 | + Serial.print("\nWiFi connected: "); |
| 142 | + Serial.print(WiFi.localIP()); |
| 143 | + |
| 144 | + // Init filesystem |
| 145 | +#ifdef USE_MMC |
| 146 | + if(!SD_MMC.begin()) |
| 147 | + Serial.println("SD Card Mount Failed"); |
| 148 | + if(SD_MMC.cardType() == CARD_NONE) |
| 149 | + Serial.println("No SD Card attached"); |
| 150 | + Serial.printf("\nTotal space: %10llu\n", SD_MMC.totalBytes()); |
| 151 | + Serial.printf("Free space: %10llu\n", SD_MMC.totalBytes() - SD_MMC.usedBytes()); |
| 152 | +#else |
| 153 | + // Init filesystem (format if necessary) |
| 154 | + if(!filesystem.begin(FORMAT_FS_IF_FAILED)) |
| 155 | + Serial.println("\nFS Mount Failed.\nFilesystem will be formatted, please wait."); |
| 156 | + Serial.printf("\nTotal space: %10lu\n", filesystem.totalBytes()); |
| 157 | + Serial.printf("Free space: %10lu\n", filesystem.freeBytes()); |
| 158 | +#endif |
| 159 | + |
| 160 | + listDir(filesystem, "/", 0); |
| 161 | + |
| 162 | + // Set the Telegram bot properies |
| 163 | + myBot.setUpdateTime(1000); |
| 164 | + myBot.setTelegramToken(token); |
| 165 | + |
| 166 | + // Check if all things are ok |
| 167 | + Serial.print("\nTest Telegram connection... "); |
| 168 | + myBot.begin() ? Serial.println("OK") : Serial.println("NOK"); |
| 169 | + |
| 170 | + // Init and get the system time |
| 171 | + configTime(3600, 3600, "pool.ntp.org"); |
| 172 | + getLocalTime(&timeinfo); |
| 173 | + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | +void loop() { |
| 178 | + |
| 179 | + // A variable to store telegram message data |
| 180 | + TBMessage msg; |
| 181 | + |
| 182 | + // if there is an incoming message... |
| 183 | + if (myBot.getNewMessage(msg)) { |
| 184 | + Serial.print("New message from chat_id: "); |
| 185 | + Serial.println(msg.sender.id); |
| 186 | + MessageType msgType = msg.messageType; |
| 187 | + |
| 188 | + if (msgType == MessageText){ |
| 189 | + // Received a text message |
| 190 | + if (msg.text.equalsIgnoreCase("/takePhoto")) { |
| 191 | + Serial.println("\nSending Photo from CAM"); |
| 192 | + |
| 193 | + // Take picture and save to file |
| 194 | + String myFile = takePicture(filesystem); |
| 195 | + if(myFile != "") { |
| 196 | + myBot.sendPhotoByFile(msg.sender.id, myFile, filesystem); |
| 197 | + |
| 198 | + //If you don't need to keep image in memory, delete it |
| 199 | + if(KEEP_IMAGE == false){ |
| 200 | + filesystem.remove("/" + myFile); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + else { |
| 205 | + Serial.print("\nText message received: "); |
| 206 | + Serial.println(msg.text); |
| 207 | + String replyStr = "Message received:\n"; |
| 208 | + replyStr += msg.text; |
| 209 | + replyStr += "\nTry with /takePhoto"; |
| 210 | + myBot.sendMessage(msg, replyStr); |
| 211 | + } |
| 212 | + |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments