Advertisement
hms11

CoopCommandCamPhpTest

Jun 2nd, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1.  
  2. // #define BLYNK_PRINT Serial
  3. #define BUTTON V5
  4. #define LED 4
  5. #define DOORUP V7
  6. #define DOORDOWN V6
  7.  
  8. // #define BLYNK_TEMPLATE_ID "TMPLQjB7UkPG" //Clone or comment out for other deployments
  9. // #define BLYNK_DEVICE_NAME "CoopCommand"
  10.  
  11. #include "esp_camera.h"
  12. #include "SPI.h"
  13. #include "driver/rtc_io.h"
  14. #include "soc/soc.h"
  15. #include "soc/rtc_cntl_reg.h"
  16. #include <FS.h>
  17. #include <WiFiUdp.h>
  18. #include <ESPmDNS.h>
  19. #include <WiFi.h>
  20. #include <WiFiClient.h>
  21. #include <BlynkSimpleEsp32.h>
  22. // #include <ArduinoOTA.h>
  23.  
  24. String serverName = "coopcommandimages.000webhostapp.com"; // OR REPLACE WITH YOUR DOMAIN NAME
  25. String serverPath = "/upload.php"; // The default serverPath should be upload.php
  26. const int serverPort = 21;
  27. WiFiClient client;
  28.  
  29. // BLYNK Widgets
  30.  
  31. WidgetLED led1(V1);
  32. WidgetLED led2(V2);
  33. WidgetLED led3(V3);
  34. WidgetLED led4(V4);
  35. Blynk.setProperty(V8, "url", 1, pic_url);
  36.  
  37.  
  38. // Your WiFi credentials & Authentication Code from Blynk
  39. // Set password to "" for open networks.
  40. const char* ssid = "1243fry";
  41. const char* password = "Speedway*1";
  42. char auth[] = "c68pAfXJJOqSxdNe2L9IzxDJrjxYZhbA"; //sent by Blynk
  43.  
  44. // Serial Communication Variables
  45.  
  46. char coopRx; // Info received from CoopCommand
  47. bool newDataRx = false; //has CoopCam received new data from CoopCommand
  48. bool takePhoto = false; //Did the Blynk App request a photo?
  49.  
  50. // Timers
  51. unsigned long photoTimer = 500; // Time to run flash before taking photo
  52. unsigned long lastPhotoTimer = 0; // Last time the flash timer was checked
  53. unsigned long wifiTimer = 30000; // How often to try re-connecting if WIFI lost
  54. unsigned long lastWifiTimer = 0; // Last time the WIFI re-connect timer was checked
  55. BlynkTimer timer;
  56.  
  57.  
  58.  
  59. #define CAMERA_MODEL_AI_THINKER
  60.  
  61. #if defined(CAMERA_MODEL_AI_THINKER)
  62. #define PWDN_GPIO_NUM 32
  63. #define RESET_GPIO_NUM -1
  64. #define XCLK_GPIO_NUM 0
  65. #define SIOD_GPIO_NUM 26
  66. #define SIOC_GPIO_NUM 27
  67.  
  68. #define Y9_GPIO_NUM 35
  69. #define Y8_GPIO_NUM 34
  70. #define Y7_GPIO_NUM 39
  71. #define Y6_GPIO_NUM 36
  72. #define Y5_GPIO_NUM 21
  73. #define Y4_GPIO_NUM 19
  74. #define Y3_GPIO_NUM 18
  75. #define Y2_GPIO_NUM 5
  76. #define VSYNC_GPIO_NUM 25
  77. #define HREF_GPIO_NUM 23
  78. #define PCLK_GPIO_NUM 22
  79. #else
  80. #error "Camera model not selected"
  81. #endif
  82.  
  83.  
  84. void setup() {
  85.  
  86. // ArduinoOTA.begin();
  87.  
  88. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  89. pinMode(LED, OUTPUT);
  90. // Serial, WI-FI & Blynk Communication
  91. Serial.begin(115200);
  92. Blynk.begin(auth, ssid, password);
  93.  
  94.  
  95. // You can also specify server:
  96. //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  97. //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  98.  
  99.  
  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. if (psramFound()) {
  124. config.frame_size = FRAMESIZE_SVGA;
  125. config.jpeg_quality = 10;
  126. config.fb_count = 2;
  127. } else {
  128. config.frame_size = FRAMESIZE_CIF;
  129. config.jpeg_quality = 12;
  130. config.fb_count = 1;
  131. }
  132.  
  133. // Initialize camera
  134. esp_err_t err = esp_camera_init(&config);
  135. if (err != ESP_OK) {
  136. ESP.restart();
  137. }
  138.  
  139. }
  140.  
  141.  
  142. String sendPhoto() {
  143.  
  144. String getAll;
  145. String getBody;
  146.  
  147. camera_fb_t * fb = NULL;
  148. fb = esp_camera_fb_get();
  149. if(!fb) {
  150. // Serial.println("Camera capture failed");
  151. // delay(1000);
  152. ESP.restart();
  153. }
  154.  
  155. // Serial.println("Connecting to server: " + serverName);
  156.  
  157. if (client.connect(serverName.c_str(), serverPort)) {
  158. // Serial.println("Connection successful!");
  159. String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"esp32-cam.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
  160. String tail = "\r\n--RandomNerdTutorials--\r\n";
  161.  
  162. uint32_t imageLen = fb->len;
  163. uint32_t extraLen = head.length() + tail.length();
  164. uint32_t totalLen = imageLen + extraLen;
  165.  
  166. client.println("POST " + serverPath + " HTTP/1.1");
  167. client.println("Host: " + serverName);
  168. client.println("Content-Length: " + String(totalLen));
  169. client.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
  170. client.println();
  171. client.print(head);
  172.  
  173. uint8_t *fbBuf = fb->buf;
  174. size_t fbLen = fb->len;
  175. for (size_t n=0; n<fbLen; n=n+1024) {
  176. if (n+1024 < fbLen) {
  177. client.write(fbBuf, 1024);
  178. fbBuf += 1024;
  179. }
  180. else if (fbLen%1024>0) {
  181. size_t remainder = fbLen%1024;
  182. client.write(fbBuf, remainder);
  183. }
  184. }
  185. client.print(tail);
  186.  
  187. esp_camera_fb_return(fb);
  188.  
  189. int timoutTimer = 10000;
  190. long startTimer = millis();
  191. boolean state = false;
  192.  
  193. while ((startTimer + timoutTimer) > millis()) {
  194. // Serial.print(".");
  195. delay(100);
  196. while (client.available()) {
  197. char c = client.read();
  198. if (c == '\n') {
  199. if (getAll.length()==0) { state=true; }
  200. getAll = "";
  201. }
  202. else if (c != '\r') { getAll += String(c); }
  203. if (state==true) { getBody += String(c); }
  204. startTimer = millis();
  205. }
  206. if (getBody.length()>0) { break; }
  207. }
  208. // Serial.println();
  209. client.stop();
  210. // Serial.println(getBody);
  211. }
  212. else {
  213. getBody = "Connection to " + serverName + " failed.";
  214. // Serial.println(getBody);
  215. }
  216. return getBody;
  217. }
  218.  
  219.  
  220. void coopCom ( void ) {
  221. if (WiFi.status() == WL_CONNECTED) {
  222. lastWifiTimer = millis();
  223. }
  224. else if (WiFi.status() != WL_CONNECTED) {
  225. if ((unsigned long)(millis() - lastWifiTimer) >= wifiTimer) {
  226. ESP.restart();
  227. }
  228. }
  229. if (Serial.available() > 0) {
  230. coopRx = Serial.read();
  231. newDataRx = true;
  232. }
  233. if (newDataRx == true) {
  234. if (coopRx == 'O') { //If CoopCommand says the door is up
  235. led1.on();
  236. led2.off();
  237. led3.off();
  238. led4.off();
  239. newDataRx = false;
  240. }
  241. if (coopRx == 'S') { //If CoopCommand says the door is down
  242. led1.off();
  243. led2.on();
  244. led3.off();
  245. led4.off();
  246. newDataRx = false;
  247. }
  248. if (coopRx == 'U') { //If CoopCommand says the door is opening
  249. led1.off();
  250. led2.off();
  251. led3.on();
  252. led4.off();
  253. newDataRx = false;
  254. }
  255. if (coopRx == 'D') { //If CoopCommand says the door is closing
  256. led1.off();
  257. led2.off();
  258. led3.off();
  259. led4.on();
  260. newDataRx = false;
  261. }
  262. }
  263. }
  264.  
  265.  
  266. BLYNK_WRITE(DOORUP) {
  267. Serial.print('U');
  268. }
  269.  
  270. BLYNK_WRITE(DOORDOWN) {
  271. Serial.print('D');
  272. }
  273.  
  274. void flashOff ( void ) {
  275. sendPhoto();
  276. digitalWrite(LED, LOW);
  277. Serial.print('N');
  278. takePhoto = false;
  279. }
  280.  
  281. void flashOn (void) {
  282. digitalWrite(LED, HIGH);
  283. Serial.print('L');
  284. }
  285. BLYNK_WRITE(V5) {
  286. flashOn();
  287. timer.setTimeout(1000L, flashOff);
  288. }
  289.  
  290.  
  291. void loop() {
  292. // ArduinoOTA.handle();
  293. coopCom();
  294. Blynk.run();
  295. timer.run();
  296. }
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement