Advertisement
hms11

July20thESP32CAMCODE

Jul 20th, 2021
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. // Fill-in information for your Blynk connection here
  2.  
  3.  
  4. #define BLYNK_TEMPLATE_ID "TMPL-flCABCq"
  5. #define BLYNK_DEVICE_NAME "CoopCommand"
  6. #define BLYNK_HEARTBEAT 30
  7. #define BLYNK_FIRMWARE_VERSION "0.2.2"
  8. #define BLYNK_PRINT Serial
  9. //#define BLYNK_DEBUG
  10. #define APP_DEBUG
  11.  
  12. // Pin Defines
  13.  
  14. #define PHOTOCLICK V5
  15. #define LED 4
  16. #define DOORUP V7
  17. #define DOORDOWN V6
  18. #define DOORLOCK V9
  19.  
  20.  
  21. // Uncomment your board, or configure a custom board in Settings.h
  22. //define USE_WROVER_BOARD
  23.  
  24. // Libraries
  25.  
  26. #include "BlynkEdgent.h"
  27. #include "esp_camera.h"
  28. #include "Arduino.h"
  29. #include <ESP32_FTPClient.h>
  30. #include "soc/soc.h" // Disable brownout problems
  31. #include "soc/rtc_cntl_reg.h" // Disable brownout problems
  32.  
  33. // Blynkv LED setup
  34.  
  35. WidgetLED led1(V1);
  36. WidgetLED led2(V2);
  37. WidgetLED led3(V3);
  38. WidgetLED led4(V4);
  39. WidgetLCD lcd(V8);
  40.  
  41. // Pin definition for CAMERA_MODEL_AI_THINKER
  42.  
  43. #define PWDN_GPIO_NUM 32
  44. #define RESET_GPIO_NUM -1
  45. #define XCLK_GPIO_NUM 0
  46. #define SIOD_GPIO_NUM 26
  47. #define SIOC_GPIO_NUM 27
  48.  
  49. #define Y9_GPIO_NUM 35
  50. #define Y8_GPIO_NUM 34
  51. #define Y7_GPIO_NUM 39
  52. #define Y6_GPIO_NUM 36
  53. #define Y5_GPIO_NUM 21
  54. #define Y4_GPIO_NUM 19
  55. #define Y3_GPIO_NUM 18
  56. #define Y2_GPIO_NUM 5
  57. #define VSYNC_GPIO_NUM 25
  58. #define HREF_GPIO_NUM 23
  59. #define PCLK_GPIO_NUM 22
  60.  
  61. //FTP server information and setup
  62.  
  63. char ftp_server[] = "files.000webhost.com";
  64. char ftp_user[] = "coopcommandimages";
  65. char ftp_pass[] ="Speedway*1";
  66. ESP32_FTPClient ftp (ftp_server,ftp_user,ftp_pass, 5000, 2); // you can pass a FTP timeout and debbug mode on the last 2 arguments
  67.  
  68. // Variables
  69.  
  70. bool takePhoto = false;
  71. bool loading = false;
  72. bool newImage = false;
  73. bool newDisplay = false;
  74. bool doorLock = false;
  75. String coopImageURL; //= "https://coopcommandimages.000webhostapp.com/uploads/1.png";
  76. int i = 1;
  77. int ic = 1;
  78. int imageFlip = 1;
  79. char coopRx; // Info received from CoopCommand
  80. bool newDataRx = false; //has CoopCam received new data from CoopCommand
  81.  
  82.  
  83. void setup() {
  84.  
  85. // Startup Serial, Blynk, Blynk Timers and initial imamge. Disable brownout detector
  86. Serial.begin(115200);
  87. delay(50);
  88. BlynkEdgent.begin();
  89. pinMode(LED, OUTPUT);
  90. timer.setInterval(500,coopCom);
  91. timer.setInterval(150,loadingImage);
  92. timer.setInterval(500,doorLocked);
  93. timer.setInterval(500,lcdDisplay);
  94. Blynk.virtualWrite(V0, 3);
  95.  
  96. //disable brownout detector
  97. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  98.  
  99. // Configure the Camera
  100.  
  101. camera_config_t config;
  102. config.ledc_channel = LEDC_CHANNEL_0;
  103. config.ledc_timer = LEDC_TIMER_0;
  104. config.pin_d0 = Y2_GPIO_NUM;
  105. config.pin_d1 = Y3_GPIO_NUM;
  106. config.pin_d2 = Y4_GPIO_NUM;
  107. config.pin_d3 = Y5_GPIO_NUM;
  108. config.pin_d4 = Y6_GPIO_NUM;
  109. config.pin_d5 = Y7_GPIO_NUM;
  110. config.pin_d6 = Y8_GPIO_NUM;
  111. config.pin_d7 = Y9_GPIO_NUM;
  112. config.pin_xclk = XCLK_GPIO_NUM;
  113. config.pin_pclk = PCLK_GPIO_NUM;
  114. config.pin_vsync = VSYNC_GPIO_NUM;
  115. config.pin_href = HREF_GPIO_NUM;
  116. config.pin_sscb_sda = SIOD_GPIO_NUM;
  117. config.pin_sscb_scl = SIOC_GPIO_NUM;
  118. config.pin_pwdn = PWDN_GPIO_NUM;
  119. config.pin_reset = RESET_GPIO_NUM;
  120. config.xclk_freq_hz = 20000000;
  121. config.pixel_format = PIXFORMAT_JPEG;
  122.  
  123. // Image settings depending on PSRAM availability
  124.  
  125. if(psramFound()){
  126. config.frame_size = FRAMESIZE_SVGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  127. config.jpeg_quality = 20;
  128. config.fb_count = 2;
  129. }
  130. else {
  131. config.frame_size = FRAMESIZE_VGA;
  132. config.jpeg_quality = 20;
  133. config.fb_count = 1;
  134. }
  135. // Initialize Camera
  136. esp_err_t err = esp_camera_init(&config);
  137. if (err != ESP_OK) {
  138. return;
  139. }
  140.  
  141. }
  142.  
  143.  
  144. // Changes image in Blynk app depending on camera state
  145. void loadingImage( void ) {
  146. if (loading) {
  147. Blynk.virtualWrite(V0, (i+2));
  148. if (i <= 9) {
  149. i ++;
  150. }
  151. else if (i > 9) {
  152. i = 1;
  153. }
  154. }
  155. else if (!loading) {
  156. if (ic == 1) {
  157. coopImageURL = "https://coopcommandimages.000webhostapp.com/uploads/coopPic5.jpg";
  158. }
  159. else if (ic == 2) {
  160. coopImageURL = "https://coopcommandimages.000webhostapp.com/uploads/coopPic6.jpg";
  161. }
  162. else if (ic == 3) {
  163. coopImageURL = "https://coopcommandimages.000webhostapp.com/uploads/coopPic7.jpg";
  164. }
  165. if (newImage) {
  166. Blynk.setProperty(V0, "url", (ic-1), coopImageURL);
  167. Blynk.virtualWrite(V0, (ic-1));
  168. newImage = false;
  169. }
  170. i = 1;
  171. }
  172. }
  173.  
  174. // Sends a new image to the FTP server
  175.  
  176. void sendPhoto ( void ) {
  177. if (takePhoto) {
  178.  
  179. camera_fb_t * fb = NULL;
  180.  
  181. // Take Picture with Camera
  182. fb = esp_camera_fb_get();
  183. digitalWrite(LED, LOW);
  184. delay (50);
  185. if(!fb) {
  186. ESP.restart();
  187. return;
  188. }
  189.  
  190. if (imageFlip == 1) {
  191. ftp.OpenConnection();
  192. // Create the new file and send the image
  193. ftp.ChangeWorkDir("/public_html/uploads/");
  194. ftp.InitFile("Type I");
  195. ftp.NewFile("coopPic5.jpg");
  196. ftp.WriteData( fb->buf, fb->len );
  197. ftp.CloseFile();
  198. ftp.CloseConnection();
  199. takePhoto = false;
  200. imageFlip = 2;
  201. ic = 1;
  202. esp_camera_fb_return(fb);
  203. Serial.print('N');
  204. }
  205. else if (imageFlip == 2) {
  206. ftp.OpenConnection();
  207. // Create the new file and send the image
  208. ftp.ChangeWorkDir("/public_html/uploads/");
  209. ftp.InitFile("Type I");
  210. ftp.NewFile("coopPic6.jpg");
  211. ftp.WriteData( fb->buf, fb->len );
  212. ftp.CloseFile();
  213. ftp.CloseConnection();
  214. takePhoto = false;
  215. imageFlip = 3;
  216. ic = 2;
  217. esp_camera_fb_return(fb);
  218. Serial.print('N');
  219. }
  220. else if (imageFlip == 3) {
  221. ftp.OpenConnection();
  222. // Create the new file and send the image
  223. ftp.ChangeWorkDir("/public_html/uploads/");
  224. ftp.InitFile("Type I");
  225. ftp.NewFile("coopPic7.jpg");
  226. ftp.WriteData( fb->buf, fb->len );
  227. ftp.CloseFile();
  228. ftp.CloseConnection();
  229. takePhoto = false;
  230. imageFlip = 1;
  231. ic = 3;
  232. esp_camera_fb_return(fb);
  233. Serial.print('N');
  234. }
  235. }
  236. }
  237.  
  238. // Serial communication with CoopCommand board
  239.  
  240. void coopCom ( void ) {
  241.  
  242. if (Serial.available() > 0) {
  243. coopRx = Serial.read();
  244. newDataRx = true;
  245. }
  246. if (newDataRx == true) {
  247. if (coopRx == 'O') { //If CoopCommand says the door is up
  248. led1.on();
  249. led2.off();
  250. led3.off();
  251. led4.off();
  252. newDataRx = false;
  253. }
  254. if (coopRx == 'S') { //If CoopCommand says the door is down
  255.  
  256. led1.off();
  257. led2.on();
  258. led3.off();
  259. led4.off();
  260. newDataRx = false;
  261. }
  262. if (coopRx == 'U') { //If CoopCommand says the door is opening
  263. led1.off();
  264. led2.off();
  265. led3.on();
  266. led4.off();
  267. newDataRx = false;
  268. }
  269. if (coopRx == 'D') { //If CoopCommand says the door is closing
  270. led1.off();
  271. led2.off();
  272. led3.off();
  273. led4.on();
  274. newDataRx = false;
  275. }
  276. }
  277. }
  278.  
  279. // Put the door up
  280.  
  281. BLYNK_WRITE(DOORUP) {
  282. Serial.print('U');
  283. }
  284.  
  285. // Put the door down
  286.  
  287. BLYNK_WRITE(DOORDOWN) {
  288. Serial.print('D');
  289. }
  290.  
  291. BLYNK_WRITE(DOORLOCK) {
  292. doorLock = true;
  293. }
  294.  
  295. void lcdDisplay (void) {
  296. if (coopRx == 'O') { //If CoopCommand says the door is up
  297. lcd.clear(); //Use it to clear the LCD Widget
  298. lcd.print(4, 0, "COOP DOOR:"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  299. lcd.print(4, 1, "OPEN"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  300. }
  301. if (coopRx == 'S') { //If CoopCommand says the door is down
  302. lcd.clear(); //Use it to clear the LCD Widget
  303. lcd.print(4, 0, "COOP DOOR:"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  304. lcd.print(4, 1, "CLOSED"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  305. }
  306. if (coopRx == 'U') { //If CoopCommand says the door is opening
  307. lcd.clear(); //Use it to clear the LCD Widget
  308. lcd.print(4, 0, "COOP DOOR:"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  309. lcd.print(4, 1, "OPENING"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  310. }
  311. if (coopRx == 'D') { //If CoopCommand says the door is closing
  312. lcd.clear(); //Use it to clear the LCD Widget
  313. lcd.print(4, 0, "COOP DOOR:"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  314. lcd.print(4, 1, "CLOSING"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  315. }
  316. }
  317.  
  318. void doorLocked (void) {
  319. if (doorLock) {
  320. Serial.print('D');
  321. doorLock = false;
  322. }
  323. }
  324. // Take a photo and display it in the app
  325.  
  326. BLYNK_WRITE(V5) {
  327. digitalWrite(LED, HIGH);
  328. Serial.print('L');
  329. takePhoto = true;
  330. loading = true;
  331. newImage = true;
  332. loadingImage();
  333. timer.setTimeout(250, sendPhoto);
  334. timer.setTimeout(3000, []()
  335. {
  336. loading = false;
  337. loadingImage();
  338. });
  339. }
  340.  
  341.  
  342. void loop() {
  343. BlynkEdgent.run();
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement