server_tflite.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #include "server_tflite.h"
  2. #include <string>
  3. #include <vector>
  4. #include "string.h"
  5. #include "esp_log.h"
  6. #include <iomanip>
  7. #include <sstream>
  8. #include "defines.h"
  9. #include "Helper.h"
  10. #include "esp_camera.h"
  11. #include "time_sntp.h"
  12. #include "ClassControllCamera.h"
  13. #include "ClassFlowControll.h"
  14. #include "ClassLogFile.h"
  15. #include "server_GPIO.h"
  16. #include "server_file.h"
  17. #include "connect_wlan.h"
  18. #define DEBUG_DETAIL_ON
  19. ClassFlowControll tfliteflow;
  20. TaskHandle_t xHandleblink_task_doFlow = NULL;
  21. TaskHandle_t xHandletask_autodoFlow = NULL;
  22. bool flowisrunning = false;
  23. long auto_intervall = 0;
  24. bool auto_isrunning = false;
  25. int countRounds = 0;
  26. static const char *TAGTFLITE = "server_tflite";
  27. int getCountFlowRounds() {
  28. return countRounds;
  29. }
  30. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  31. {
  32. return tfliteflow.GetJPGStream(_filename, req);
  33. }
  34. esp_err_t GetRawJPG(httpd_req_t *req)
  35. {
  36. return tfliteflow.SendRawJPG(req);
  37. }
  38. bool isSetupModusActive() {
  39. return tfliteflow.getStatusSetupModus();
  40. return false;
  41. }
  42. void KillTFliteTasks()
  43. {
  44. #ifdef DEBUG_DETAIL_ON
  45. ESP_LOGD(TAGTFLITE, "Handle: xHandleblink_task_doFlow: %ld", (long) xHandleblink_task_doFlow);
  46. #endif
  47. if (xHandleblink_task_doFlow != NULL)
  48. {
  49. TaskHandle_t xHandleblink_task_doFlowTmp = xHandleblink_task_doFlow;
  50. xHandleblink_task_doFlow = NULL;
  51. vTaskDelete(xHandleblink_task_doFlowTmp);
  52. #ifdef DEBUG_DETAIL_ON
  53. ESP_LOGD(TAGTFLITE, "Killed: xHandleblink_task_doFlow");
  54. #endif
  55. }
  56. #ifdef DEBUG_DETAIL_ON
  57. ESP_LOGD(TAGTFLITE, "Handle: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
  58. #endif
  59. if (xHandletask_autodoFlow != NULL)
  60. {
  61. TaskHandle_t xHandletask_autodoFlowTmp = xHandletask_autodoFlow;
  62. xHandletask_autodoFlow = NULL;
  63. vTaskDelete(xHandletask_autodoFlowTmp);
  64. #ifdef DEBUG_DETAIL_ON
  65. ESP_LOGD(TAGTFLITE, "Killed: xHandletask_autodoFlow");
  66. #endif
  67. }
  68. }
  69. void doInit(void)
  70. {
  71. #ifdef DEBUG_DETAIL_ON
  72. ESP_LOGD(TAGTFLITE, "Start tfliteflow.InitFlow(config);");
  73. #endif
  74. tfliteflow.InitFlow(CONFIG_FILE);
  75. #ifdef DEBUG_DETAIL_ON
  76. ESP_LOGD(TAGTFLITE, "Finished tfliteflow.InitFlow(config);");
  77. #endif
  78. }
  79. bool doflow(void)
  80. {
  81. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  82. ESP_LOGD(TAGTFLITE, "doflow - start %s", zw_time.c_str());
  83. flowisrunning = true;
  84. tfliteflow.doFlow(zw_time);
  85. flowisrunning = false;
  86. #ifdef DEBUG_DETAIL_ON
  87. ESP_LOGD(TAGTFLITE, "doflow - end %s", zw_time.c_str());
  88. #endif
  89. return true;
  90. }
  91. void blink_task_doFlow(void *pvParameter)
  92. {
  93. #ifdef DEBUG_DETAIL_ON
  94. ESP_LOGD(TAGTFLITE, "blink_task_doFlow");
  95. #endif
  96. if (!flowisrunning)
  97. {
  98. flowisrunning = true;
  99. doflow();
  100. flowisrunning = false;
  101. }
  102. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  103. xHandleblink_task_doFlow = NULL;
  104. }
  105. esp_err_t handler_init(httpd_req_t *req)
  106. {
  107. #ifdef DEBUG_DETAIL_ON
  108. LogFile.WriteHeapInfo("handler_init - Start");
  109. ESP_LOGD(TAGTFLITE, "handler_doinit uri: %s", req->uri);
  110. #endif
  111. const char* resp_str = "Init started<br>";
  112. httpd_resp_send(req, resp_str, strlen(resp_str));
  113. doInit();
  114. resp_str = "Init done<br>";
  115. httpd_resp_send(req, resp_str, strlen(resp_str));
  116. /* Respond with an empty chunk to signal HTTP response completion */
  117. httpd_resp_send_chunk(req, NULL, 0);
  118. #ifdef DEBUG_DETAIL_ON
  119. LogFile.WriteHeapInfo("handler_init - Done");
  120. #endif
  121. return ESP_OK;
  122. };
  123. esp_err_t handler_doflow(httpd_req_t *req)
  124. {
  125. #ifdef DEBUG_DETAIL_ON
  126. LogFile.WriteHeapInfo("handler_doflow - Start");
  127. #endif
  128. ESP_LOGD(TAGTFLITE, "handler_doFlow uri: %s", req->uri);
  129. if (flowisrunning)
  130. {
  131. const char* resp_str = "doFlow is already running and cannot be started again";
  132. httpd_resp_send(req, resp_str, strlen(resp_str));
  133. return 2;
  134. }
  135. else
  136. {
  137. xTaskCreate(&blink_task_doFlow, "blink_doFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandleblink_task_doFlow);
  138. }
  139. const char* resp_str = "doFlow started - takes about 60 seconds";
  140. httpd_resp_send(req, resp_str, strlen(resp_str));
  141. /* Respond with an empty chunk to signal HTTP response completion */
  142. httpd_resp_send_chunk(req, NULL, 0);
  143. #ifdef DEBUG_DETAIL_ON
  144. LogFile.WriteHeapInfo("handler_doflow - Done");
  145. #endif
  146. return ESP_OK;
  147. };
  148. esp_err_t handler_json(httpd_req_t *req)
  149. {
  150. #ifdef DEBUG_DETAIL_ON
  151. LogFile.WriteHeapInfo("handler_json - Start");
  152. #endif
  153. ESP_LOGD(TAGTFLITE, "handler_JSON uri: %s", req->uri);
  154. char _query[100];
  155. // char _size[10];
  156. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  157. httpd_resp_set_type(req, "application/json");
  158. std::string zw = tfliteflow.getJSON();
  159. if (zw.length() > 0)
  160. httpd_resp_sendstr_chunk(req, zw.c_str());
  161. string query = std::string(_query);
  162. /* Respond with an empty chunk to signal HTTP response completion */
  163. httpd_resp_sendstr_chunk(req, NULL);
  164. #ifdef DEBUG_DETAIL_ON
  165. LogFile.WriteHeapInfo("handler_JSON - Done");
  166. #endif
  167. return ESP_OK;
  168. };
  169. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  170. {
  171. #ifdef DEBUG_DETAIL_ON
  172. LogFile.WriteHeapInfo("handler_wasserzaehler - Start");
  173. #endif
  174. bool _rawValue = false;
  175. bool _noerror = false;
  176. bool _all = false;
  177. std::string _type = "value";
  178. string zw;
  179. ESP_LOGD(TAGTFLITE, "handler_wasserzaehler uri: %s", req->uri);
  180. char _query[100];
  181. char _size[10];
  182. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  183. {
  184. // ESP_LOGD(TAGTFLITE, "Query: %s", _query);
  185. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  186. {
  187. #ifdef DEBUG_DETAIL_ON
  188. ESP_LOGD(TAGTFLITE, "all is found%s", _size);
  189. #endif
  190. _all = true;
  191. }
  192. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  193. {
  194. #ifdef DEBUG_DETAIL_ON
  195. ESP_LOGD(TAGTFLITE, "all is found: %s", _size);
  196. #endif
  197. _type = std::string(_size);
  198. }
  199. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  200. {
  201. #ifdef DEBUG_DETAIL_ON
  202. ESP_LOGD(TAGTFLITE, "rawvalue is found: %s", _size);
  203. #endif
  204. _rawValue = true;
  205. }
  206. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  207. {
  208. #ifdef DEBUG_DETAIL_ON
  209. ESP_LOGD(TAGTFLITE, "noerror is found: %s", _size);
  210. #endif
  211. _noerror = true;
  212. }
  213. }
  214. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  215. if (_all)
  216. {
  217. httpd_resp_set_type(req, "text/plain");
  218. ESP_LOGD(TAGTFLITE, "TYPE: %s", _type.c_str());
  219. int _intype = READOUT_TYPE_VALUE;
  220. if (_type == "prevalue")
  221. _intype = READOUT_TYPE_PREVALUE;
  222. if (_type == "raw")
  223. _intype = READOUT_TYPE_RAWVALUE;
  224. if (_type == "error")
  225. _intype = READOUT_TYPE_ERROR;
  226. zw = tfliteflow.getReadoutAll(_intype);
  227. ESP_LOGD(TAGTFLITE, "ZW: %s", zw.c_str());
  228. if (zw.length() > 0)
  229. httpd_resp_sendstr_chunk(req, zw.c_str());
  230. httpd_resp_sendstr_chunk(req, NULL);
  231. return ESP_OK;
  232. }
  233. zw = tfliteflow.getReadout(_rawValue, _noerror);
  234. if (zw.length() > 0)
  235. httpd_resp_sendstr_chunk(req, zw.c_str());
  236. string query = std::string(_query);
  237. // ESP_LOGD(TAGTFLITE, "Query: %s, query.c_str());
  238. if (query.find("full") != std::string::npos)
  239. {
  240. string txt, zw;
  241. txt = "<p>Aligned Image: <p><img src=\"/img_tmp/alg_roi.jpg\"> <p>\n";
  242. txt = txt + "Digital Counter: <p> ";
  243. httpd_resp_sendstr_chunk(req, txt.c_str());
  244. std::vector<HTMLInfo*> htmlinfodig;
  245. htmlinfodig = tfliteflow.GetAllDigital();
  246. for (int i = 0; i < htmlinfodig.size(); ++i)
  247. {
  248. if (tfliteflow.GetTypeDigital() == Digital)
  249. {
  250. if (htmlinfodig[i]->val == 10)
  251. zw = "NaN";
  252. else
  253. zw = to_string((int) htmlinfodig[i]->val);
  254. txt = "<img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"> " + zw;
  255. }
  256. else
  257. {
  258. std::stringstream stream;
  259. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  260. zw = stream.str();
  261. txt = "<img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"> " + zw;
  262. }
  263. httpd_resp_sendstr_chunk(req, txt.c_str());
  264. delete htmlinfodig[i];
  265. }
  266. htmlinfodig.clear();
  267. txt = " <p> Analog Meter: <p> ";
  268. httpd_resp_sendstr_chunk(req, txt.c_str());
  269. std::vector<HTMLInfo*> htmlinfoana;
  270. htmlinfoana = tfliteflow.GetAllAnalog();
  271. for (int i = 0; i < htmlinfoana.size(); ++i)
  272. {
  273. std::stringstream stream;
  274. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  275. zw = stream.str();
  276. txt = "<img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"> " + zw;
  277. httpd_resp_sendstr_chunk(req, txt.c_str());
  278. delete htmlinfoana[i];
  279. }
  280. htmlinfoana.clear();
  281. }
  282. /* Respond with an empty chunk to signal HTTP response completion */
  283. httpd_resp_sendstr_chunk(req, NULL);
  284. #ifdef DEBUG_DETAIL_ON
  285. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  286. #endif
  287. return ESP_OK;
  288. };
  289. esp_err_t handler_editflow(httpd_req_t *req)
  290. {
  291. #ifdef DEBUG_DETAIL_ON
  292. LogFile.WriteHeapInfo("handler_editflow - Start");
  293. #endif
  294. ESP_LOGD(TAGTFLITE, "handler_editflow uri: %s", req->uri);
  295. char _query[200];
  296. char _valuechar[30];
  297. string _task;
  298. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  299. {
  300. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  301. {
  302. #ifdef DEBUG_DETAIL_ON
  303. ESP_LOGD(TAGTFLITE, "task is found: %s", _valuechar);
  304. #endif
  305. _task = string(_valuechar);
  306. }
  307. }
  308. if (_task.compare("namenumbers") == 0)
  309. {
  310. ESP_LOGI(TAGTFLITE, "Get NUMBER list");
  311. return get_numbers_file_handler(req);
  312. }
  313. if (_task.compare("data") == 0)
  314. {
  315. ESP_LOGI(TAGTFLITE, "Get data list");
  316. return get_data_file_handler(req);
  317. }
  318. if (_task.compare("tflite") == 0)
  319. {
  320. ESP_LOGD(TAGTFLITE, "Get tflite list");
  321. return get_tflite_file_handler(req);
  322. }
  323. if (_task.compare("copy") == 0)
  324. {
  325. string in, out, zw;
  326. httpd_query_key_value(_query, "in", _valuechar, 30);
  327. in = string(_valuechar);
  328. httpd_query_key_value(_query, "out", _valuechar, 30);
  329. out = string(_valuechar);
  330. #ifdef DEBUG_DETAIL_ON
  331. ESP_LOGD(TAGTFLITE, "in: %s", in.c_str());
  332. ESP_LOGD(TAGTFLITE, "out: %s", out.c_str());
  333. #endif
  334. in = "/sdcard" + in;
  335. out = "/sdcard" + out;
  336. CopyFile(in, out);
  337. zw = "Copy Done";
  338. httpd_resp_sendstr_chunk(req, zw.c_str());
  339. }
  340. if (_task.compare("cutref") == 0)
  341. {
  342. string in, out, zw;
  343. int x, y, dx, dy;
  344. bool enhance = false;
  345. httpd_query_key_value(_query, "in", _valuechar, 30);
  346. in = string(_valuechar);
  347. httpd_query_key_value(_query, "out", _valuechar, 30);
  348. out = string(_valuechar);
  349. httpd_query_key_value(_query, "x", _valuechar, 30);
  350. zw = string(_valuechar);
  351. x = stoi(zw);
  352. httpd_query_key_value(_query, "y", _valuechar, 30);
  353. zw = string(_valuechar);
  354. y = stoi(zw);
  355. httpd_query_key_value(_query, "dx", _valuechar, 30);
  356. zw = string(_valuechar);
  357. dx = stoi(zw);
  358. httpd_query_key_value(_query, "dy", _valuechar, 30);
  359. zw = string(_valuechar);
  360. dy = stoi(zw);
  361. #ifdef DEBUG_DETAIL_ON
  362. ESP_LOGD(TAGTFLITE, "in: %s", in.c_str());
  363. ESP_LOGD(TAGTFLITE, "out: %s", out.c_str());
  364. ESP_LOGD(TAGTFLITE, "x: %s", zw.c_str());
  365. ESP_LOGD(TAGTFLITE, "y: %s", zw.c_str());
  366. ESP_LOGD(TAGTFLITE, "dx: %s", zw.c_str());
  367. ESP_LOGD(TAGTFLITE, "dy: %s", zw.c_str());
  368. #endif
  369. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  370. {
  371. zw = string(_valuechar);
  372. if (zw.compare("true") == 0)
  373. {
  374. enhance = true;
  375. }
  376. }
  377. in = "/sdcard" + in;
  378. out = "/sdcard" + out;
  379. string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  380. CAlignAndCutImage *caic = new CAlignAndCutImage(in);
  381. caic->CutAndSave(out2, x, y, dx, dy);
  382. delete caic;
  383. CImageBasis *cim = new CImageBasis(out2);
  384. if (enhance)
  385. {
  386. cim->Contrast(90);
  387. }
  388. cim->SaveToFile(out);
  389. delete cim;
  390. zw = "CutImage Done";
  391. httpd_resp_sendstr_chunk(req, zw.c_str());
  392. }
  393. if (_task.compare("test_take") == 0)
  394. {
  395. std::string _host = "";
  396. std::string _bri = "";
  397. std::string _con = "";
  398. std::string _sat = "";
  399. std::string _int = "";
  400. int bri = -100;
  401. int sat = -100;
  402. int con = -100;
  403. int intens = -100;
  404. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  405. _host = std::string(_valuechar);
  406. }
  407. if (httpd_query_key_value(_query, "int", _valuechar, 30) == ESP_OK) {
  408. _int = std::string(_valuechar);
  409. intens = stoi(_int);
  410. }
  411. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
  412. _bri = std::string(_valuechar);
  413. bri = stoi(_bri);
  414. }
  415. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
  416. _con = std::string(_valuechar);
  417. con = stoi(_con);
  418. }
  419. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
  420. _sat = std::string(_valuechar);
  421. sat = stoi(_sat);
  422. }
  423. // ESP_LOGD(TAGTFLITE, "Parameter host: %s", _host.c_str());
  424. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAGTFLITE, zwzw.c_str());
  425. Camera.SetBrightnessContrastSaturation(bri, con, sat);
  426. Camera.SetLEDIntensity(intens);
  427. ESP_LOGD(TAGTFLITE, "test_take - vor MakeImage");
  428. std::string zw = tfliteflow.doSingleStep("[MakeImage]", _host);
  429. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  430. httpd_resp_sendstr_chunk(req, zw.c_str());
  431. }
  432. if (_task.compare("test_align") == 0)
  433. {
  434. std::string _host = "";
  435. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  436. _host = std::string(_valuechar);
  437. }
  438. // ESP_LOGD(TAGTFLITE, "Parameter host: %s", _host.c_str());
  439. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAGTFLITE, zwzw.c_str());
  440. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  441. httpd_resp_sendstr_chunk(req, zw.c_str());
  442. }
  443. /* Respond with an empty chunk to signal HTTP response completion */
  444. httpd_resp_sendstr_chunk(req, NULL);
  445. #ifdef DEBUG_DETAIL_ON
  446. LogFile.WriteHeapInfo("handler_editflow - Done");
  447. #endif
  448. return ESP_OK;
  449. };
  450. esp_err_t handler_statusflow(httpd_req_t *req)
  451. {
  452. #ifdef DEBUG_DETAIL_ON
  453. LogFile.WriteHeapInfo("handler_prevalue - Start");
  454. #endif
  455. const char* resp_str;
  456. #ifdef DEBUG_DETAIL_ON
  457. ESP_LOGD(TAGTFLITE, "handler_prevalue: %s", req->uri);
  458. #endif
  459. string* zw = tfliteflow.getActStatus();
  460. resp_str = zw->c_str();
  461. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  462. httpd_resp_send(req, resp_str, strlen(resp_str));
  463. /* Respond with an empty chunk to signal HTTP response completion */
  464. httpd_resp_send_chunk(req, NULL, 0);
  465. #ifdef DEBUG_DETAIL_ON
  466. LogFile.WriteHeapInfo("handler_prevalue - Start");
  467. #endif
  468. return ESP_OK;
  469. };
  470. esp_err_t handler_cputemp(httpd_req_t *req)
  471. {
  472. #ifdef DEBUG_DETAIL_ON
  473. LogFile.WriteHeapInfo("handler_cputemp - Start");
  474. #endif
  475. const char* resp_str;
  476. char cputemp[20];
  477. sprintf(cputemp, "CPU Temp: %4.1f°C", temperatureRead());
  478. resp_str = cputemp;
  479. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  480. httpd_resp_send(req, resp_str, strlen(resp_str));
  481. /* Respond with an empty chunk to signal HTTP response completion */
  482. httpd_resp_send_chunk(req, NULL, 0);
  483. #ifdef DEBUG_DETAIL_ON
  484. LogFile.WriteHeapInfo("handler_cputemp - End");
  485. #endif
  486. return ESP_OK;
  487. };
  488. esp_err_t handler_rssi(httpd_req_t *req)
  489. {
  490. #ifdef DEBUG_DETAIL_ON
  491. LogFile.WriteHeapInfo("handler_rssi - Start");
  492. #endif
  493. const char* resp_str;
  494. char rssi[20];
  495. sprintf(rssi, "RSSI: %idBm", get_WIFI_RSSI());
  496. resp_str = rssi;
  497. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  498. httpd_resp_send(req, resp_str, strlen(resp_str));
  499. /* Respond with an empty chunk to signal HTTP response completion */
  500. httpd_resp_send_chunk(req, NULL, 0);
  501. #ifdef DEBUG_DETAIL_ON
  502. LogFile.WriteHeapInfo("handler_rssi - End");
  503. #endif
  504. return ESP_OK;
  505. };
  506. esp_err_t handler_prevalue(httpd_req_t *req)
  507. {
  508. #ifdef DEBUG_DETAIL_ON
  509. LogFile.WriteHeapInfo("handler_prevalue - Start");
  510. #endif
  511. const char* resp_str;
  512. string zw;
  513. #ifdef DEBUG_DETAIL_ON
  514. ESP_LOGD(TAGTFLITE, "handler_prevalue: %s", req->uri);
  515. #endif
  516. char _query[100];
  517. char _size[10] = "";
  518. char _numbers[50] = "default";
  519. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  520. {
  521. #ifdef DEBUG_DETAIL_ON
  522. ESP_LOGD(TAGTFLITE, "Query: %s", _query);
  523. #endif
  524. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  525. {
  526. #ifdef DEBUG_DETAIL_ON
  527. ESP_LOGD(TAGTFLITE, "Value: %s", _size);
  528. #endif
  529. }
  530. httpd_query_key_value(_query, "numbers", _numbers, 50);
  531. }
  532. if (strlen(_size) == 0)
  533. {
  534. zw = tfliteflow.GetPrevalue(std::string(_numbers));
  535. }
  536. else
  537. {
  538. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size, _numbers, true);
  539. }
  540. resp_str = zw.c_str();
  541. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  542. httpd_resp_send(req, resp_str, strlen(resp_str));
  543. /* Respond with an empty chunk to signal HTTP response completion */
  544. httpd_resp_send_chunk(req, NULL, 0);
  545. #ifdef DEBUG_DETAIL_ON
  546. LogFile.WriteHeapInfo("handler_prevalue - End");
  547. #endif
  548. return ESP_OK;
  549. };
  550. void task_autodoFlow(void *pvParameter)
  551. {
  552. int64_t fr_start, fr_delta_ms;
  553. ESP_LOGD(TAGTFLITE, "task_autodoFlow: start");
  554. doInit();
  555. gpio_handler_init();
  556. auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
  557. if (isSetupModusActive()) {
  558. auto_isrunning = false;
  559. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  560. tfliteflow.doFlowMakeImageOnly(zw_time);
  561. }
  562. while (auto_isrunning)
  563. {
  564. std::string _zw = "task_autodoFlow - next round - Round #" + std::to_string(++countRounds);
  565. LogFile.WriteToFile(ESP_LOG_INFO, _zw);
  566. fr_start = esp_timer_get_time();
  567. if (flowisrunning)
  568. {
  569. #ifdef DEBUG_DETAIL_ON
  570. ESP_LOGD(TAGTFLITE, "Autoflow: doFlow is already running!");
  571. #endif
  572. }
  573. else
  574. {
  575. #ifdef DEBUG_DETAIL_ON
  576. ESP_LOGD(TAGTFLITE, "Autoflow: doFlow is started");
  577. #endif
  578. flowisrunning = true;
  579. doflow();
  580. #ifdef DEBUG_DETAIL_ON
  581. ESP_LOGD(TAGTFLITE, "Remove older log files");
  582. #endif
  583. LogFile.RemoveOld();
  584. }
  585. LogFile.WriteToFile(ESP_LOG_INFO, "task_autodoFlow - round done");
  586. //CPU Temp
  587. float cputmp = temperatureRead();
  588. std::stringstream stream;
  589. stream << std::fixed << std::setprecision(1) << cputmp;
  590. string zwtemp = "CPU Temperature: " + stream.str();
  591. LogFile.WriteToFile(ESP_LOG_INFO, zwtemp);
  592. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  593. if (auto_intervall > fr_delta_ms)
  594. {
  595. const TickType_t xDelay = (auto_intervall - fr_delta_ms) / portTICK_PERIOD_MS;
  596. ESP_LOGD(TAGTFLITE, "Autoflow: sleep for: %ldms", (long) xDelay);
  597. vTaskDelay( xDelay );
  598. }
  599. }
  600. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  601. xHandletask_autodoFlow = NULL;
  602. ESP_LOGD(TAGTFLITE, "task_autodoFlow: end");
  603. }
  604. void TFliteDoAutoStart()
  605. {
  606. BaseType_t xReturned;
  607. int _i = configMINIMAL_STACK_SIZE;
  608. ESP_LOGD(TAGTFLITE, "task_autodoFlow configMINIMAL_STACK_SIZE: %d", _i);
  609. ESP_LOGD(TAGTFLITE, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  610. xReturned = xTaskCreate(&task_autodoFlow, "task_autodoFlow", configMINIMAL_STACK_SIZE * 35, NULL, tskIDLE_PRIORITY+1, &xHandletask_autodoFlow);
  611. if( xReturned != pdPASS )
  612. {
  613. //Memory: 64 --> 48 --> 35 --> 25
  614. ESP_LOGD(TAGTFLITE, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
  615. }
  616. ESP_LOGD(TAGTFLITE, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  617. }
  618. std::string GetMQTTMainTopic()
  619. {
  620. return tfliteflow.GetMQTTMainTopic();
  621. }
  622. void register_server_tflite_uri(httpd_handle_t server)
  623. {
  624. ESP_LOGI(TAGTFLITE, "server_part_camera - Registering URI handlers");
  625. httpd_uri_t camuri = { };
  626. camuri.method = HTTP_GET;
  627. camuri.uri = "/doinit";
  628. camuri.handler = handler_init;
  629. camuri.user_ctx = (void*) "Light On";
  630. httpd_register_uri_handler(server, &camuri);
  631. camuri.uri = "/setPreValue.html";
  632. camuri.handler = handler_prevalue;
  633. camuri.user_ctx = (void*) "Prevalue";
  634. httpd_register_uri_handler(server, &camuri);
  635. camuri.uri = "/doflow";
  636. camuri.handler = handler_doflow;
  637. camuri.user_ctx = (void*) "Light Off";
  638. httpd_register_uri_handler(server, &camuri);
  639. camuri.uri = "/statusflow.html";
  640. camuri.handler = handler_statusflow;
  641. camuri.user_ctx = (void*) "Light Off";
  642. httpd_register_uri_handler(server, &camuri);
  643. camuri.uri = "/cputemp.html";
  644. camuri.handler = handler_cputemp;
  645. camuri.user_ctx = (void*) "Light Off";
  646. httpd_register_uri_handler(server, &camuri);
  647. camuri.uri = "/rssi.html";
  648. camuri.handler = handler_rssi;
  649. camuri.user_ctx = (void*) "Light Off";
  650. httpd_register_uri_handler(server, &camuri);
  651. camuri.uri = "/editflow.html";
  652. camuri.handler = handler_editflow;
  653. camuri.user_ctx = (void*) "EditFlow";
  654. httpd_register_uri_handler(server, &camuri);
  655. camuri.uri = "/value.html";
  656. camuri.handler = handler_wasserzaehler;
  657. camuri.user_ctx = (void*) "Value";
  658. httpd_register_uri_handler(server, &camuri);
  659. camuri.uri = "/wasserzaehler.html";
  660. camuri.handler = handler_wasserzaehler;
  661. camuri.user_ctx = (void*) "Wasserzaehler";
  662. httpd_register_uri_handler(server, &camuri);
  663. camuri.uri = "/json";
  664. camuri.handler = handler_json;
  665. camuri.user_ctx = (void*) "JSON";
  666. httpd_register_uri_handler(server, &camuri);
  667. }