server_tflite.cpp 24 KB

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