Advertisement
hms11

July21stCoopCam

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