Advertisement
hms11

CoopCommandCamFTPtest

Jun 4th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. // #define BLYNK_PRINT Serial
  2. #define BUTTON V5
  3. #define LED 4
  4. #define DOORUP V7
  5. #define DOORDOWN V6
  6.  
  7. // #define BLYNK_TEMPLATE_ID "TMPLQjB7UkPG" //Clone or comment out for other deployments
  8. // #define BLYNK_DEVICE_NAME "CoopCommand"
  9.  
  10. #include "esp_camera.h"
  11. #include "SPI.h"
  12. #include "driver/rtc_io.h"
  13. #include "soc/soc.h"
  14. #include "soc/rtc_cntl_reg.h"
  15. #include <FS.h>
  16. #include <WiFiUdp.h>
  17. #include <ESPmDNS.h>
  18. #include <WiFi.h>
  19. #include <WiFiClient.h>
  20. #include <BlynkSimpleEsp32.h>
  21. #include <ESP32_FTPClient.h>
  22.  
  23. // #include <ArduinoOTA.h>
  24.  
  25. char ftp_server[] = "files.000webhost.com";
  26. char ftp_user[] = "xxxxxxx";
  27. char ftp_pass[] ="xxxxxxx";
  28. String pic_url = "xxxxxxxx/";
  29. WiFiClient client;
  30. ESP32_FTPClient ftp (ftp_server,ftp_user,ftp_pass, 5000, 2);
  31. // BLYNK Widgets
  32.  
  33. WidgetLED led1(V1);
  34. WidgetLED led2(V2);
  35. WidgetLED led3(V3);
  36. WidgetLED led4(V4);
  37.  
  38.  
  39. // Your WiFi credentials & Authentication Code from Blynk
  40. // Set password to "" for open networks.
  41. const char* ssid = "xxxxxxx";
  42. const char* password = "xxxxxxx";
  43. char auth[] = "xxxxxx"; //sent by Blynk
  44.  
  45. // Serial Communication Variables
  46.  
  47. char coopRx; // Info received from CoopCommand
  48. bool newDataRx = false; //has CoopCam received new data from CoopCommand
  49. bool takePhoto = false; //Did the Blynk App request a photo?
  50.  
  51. // Timers
  52. unsigned long photoTimer = 500; // Time to run flash before taking photo
  53. unsigned long lastPhotoTimer = 0; // Last time the flash timer was checked
  54. unsigned long wifiTimer = 30000; // How often to try re-connecting if WIFI lost
  55. unsigned long lastWifiTimer = 0; // Last time the WIFI re-connect timer was checked
  56. BlynkTimer timer;
  57.  
  58.  
  59.  
  60. #define CAMERA_MODEL_AI_THINKER
  61.  
  62. #if defined(CAMERA_MODEL_AI_THINKER)
  63. #define PWDN_GPIO_NUM 32
  64. #define RESET_GPIO_NUM -1
  65. #define XCLK_GPIO_NUM 0
  66. #define SIOD_GPIO_NUM 26
  67. #define SIOC_GPIO_NUM 27
  68.  
  69. #define Y9_GPIO_NUM 35
  70. #define Y8_GPIO_NUM 34
  71. #define Y7_GPIO_NUM 39
  72. #define Y6_GPIO_NUM 36
  73. #define Y5_GPIO_NUM 21
  74. #define Y4_GPIO_NUM 19
  75. #define Y3_GPIO_NUM 18
  76. #define Y2_GPIO_NUM 5
  77. #define VSYNC_GPIO_NUM 25
  78. #define HREF_GPIO_NUM 23
  79. #define PCLK_GPIO_NUM 22
  80. #else
  81. #error "Camera model not selected"
  82. #endif
  83.  
  84.  
  85. void setup() {
  86.  
  87. // ArduinoOTA.begin();
  88.  
  89. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  90. pinMode(LED, OUTPUT);
  91. // Serial, WI-FI & Blynk Communication
  92. Serial.begin(115200);
  93. Blynk.begin(auth, ssid, password);
  94. WiFi.begin( ssid, password );
  95. Blynk.setProperty(V8, "url", 1, pic_url);
  96.  
  97. // You can also specify server:
  98. //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  99. //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  100.  
  101.  
  102.  
  103. camera_config_t config;
  104. config.ledc_channel = LEDC_CHANNEL_0;
  105. config.ledc_timer = LEDC_TIMER_0;
  106. config.pin_d0 = Y2_GPIO_NUM;
  107. config.pin_d1 = Y3_GPIO_NUM;
  108. config.pin_d2 = Y4_GPIO_NUM;
  109. config.pin_d3 = Y5_GPIO_NUM;
  110. config.pin_d4 = Y6_GPIO_NUM;
  111. config.pin_d5 = Y7_GPIO_NUM;
  112. config.pin_d6 = Y8_GPIO_NUM;
  113. config.pin_d7 = Y9_GPIO_NUM;
  114. config.pin_xclk = XCLK_GPIO_NUM;
  115. config.pin_pclk = PCLK_GPIO_NUM;
  116. config.pin_vsync = VSYNC_GPIO_NUM;
  117. config.pin_href = HREF_GPIO_NUM;
  118. config.pin_sscb_sda = SIOD_GPIO_NUM;
  119. config.pin_sscb_scl = SIOC_GPIO_NUM;
  120. config.pin_pwdn = PWDN_GPIO_NUM;
  121. config.pin_reset = RESET_GPIO_NUM;
  122. config.xclk_freq_hz = 20000000;
  123. config.pixel_format = PIXFORMAT_JPEG;
  124.  
  125. if (psramFound()) {
  126. config.frame_size = FRAMESIZE_SVGA;
  127. config.jpeg_quality = 10;
  128. config.fb_count = 2;
  129. } else {
  130. config.frame_size = FRAMESIZE_CIF;
  131. config.jpeg_quality = 12;
  132. config.fb_count = 1;
  133. }
  134.  
  135. // Initialize camera
  136. esp_err_t err = esp_camera_init(&config);
  137. if (err != ESP_OK) {
  138. ESP.restart();
  139. }
  140.  
  141. }
  142.  
  143.  
  144. String sendPhoto() {
  145.  
  146. camera_fb_t * fb = NULL;
  147. fb = esp_camera_fb_get();
  148.  
  149. delay(100);
  150.  
  151. if(!fb) {
  152. Serial.println("Camera capture failed");
  153. // delay(1000);
  154. ESP.restart();
  155. }
  156. ftp.OpenConnection();
  157. // Create a file and write the image data to it;
  158. ftp.ChangeWorkDir("/public_html/uploads/"); // change it to reflect your directory
  159. ftp.InitFile("Type I");
  160. ftp.NewFile("1.jpg");
  161. ftp.WriteData(fb->buf, fb->len);
  162. ftp.CloseFile();
  163. ftp.CloseConnection();
  164.  
  165. }
  166.  
  167.  
  168. void coopCom ( void ) {
  169. if (WiFi.status() == WL_CONNECTED) {
  170. lastWifiTimer = millis();
  171. }
  172. else if (WiFi.status() != WL_CONNECTED) {
  173. if ((unsigned long)(millis() - lastWifiTimer) >= wifiTimer) {
  174. ESP.restart();
  175. }
  176. }
  177. if (Serial.available() > 0) {
  178. coopRx = Serial.read();
  179. newDataRx = true;
  180. }
  181. if (newDataRx == true) {
  182. if (coopRx == 'O') { //If CoopCommand says the door is up
  183. led1.on();
  184. led2.off();
  185. led3.off();
  186. led4.off();
  187. newDataRx = false;
  188. }
  189. if (coopRx == 'S') { //If CoopCommand says the door is down
  190. led1.off();
  191. led2.on();
  192. led3.off();
  193. led4.off();
  194. newDataRx = false;
  195. }
  196. if (coopRx == 'U') { //If CoopCommand says the door is opening
  197. led1.off();
  198. led2.off();
  199. led3.on();
  200. led4.off();
  201. newDataRx = false;
  202. }
  203. if (coopRx == 'D') { //If CoopCommand says the door is closing
  204. led1.off();
  205. led2.off();
  206. led3.off();
  207. led4.on();
  208. newDataRx = false;
  209. }
  210. }
  211. }
  212.  
  213.  
  214. BLYNK_WRITE(DOORUP) {
  215. Serial.print('U');
  216. }
  217.  
  218. BLYNK_WRITE(DOORDOWN) {
  219. Serial.print('D');
  220. }
  221.  
  222. void flashOff ( void ) {
  223. digitalWrite(LED, LOW);
  224. sendPhoto();
  225. takePhoto = false;
  226. Serial.print('N');
  227. delay(100);
  228. Blynk.setProperty(V5, "urls", 1, pic_url);
  229. }
  230.  
  231. void flashOn (void) {
  232. digitalWrite(LED, HIGH);
  233. Serial.print('L');
  234. }
  235.  
  236.  
  237. BLYNK_WRITE(V5) {
  238. flashOn();
  239. timer.setTimeout(500L, flashOff);
  240. }
  241.  
  242.  
  243. void loop() {
  244. // ArduinoOTA.handle();
  245. coopCom();
  246. Blynk.run();
  247. timer.run();
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement