Advertisement
hms11

CoopCamEdgentRev1

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