server_tflite.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 "esp_timer.h"
  12. #include "time_sntp.h"
  13. #include "ClassControllCamera.h"
  14. #include "ClassFlowControll.h"
  15. #include "ClassLogFile.h"
  16. #include "server_GPIO.h"
  17. #include "server_file.h"
  18. #include "connect_wlan.h"
  19. #define DEBUG_DETAIL_ON
  20. ClassFlowControll tfliteflow;
  21. TaskHandle_t xHandleblink_task_doFlow = NULL;
  22. TaskHandle_t xHandletask_autodoFlow = NULL;
  23. bool flowisrunning = false;
  24. long auto_intervall = 0;
  25. bool auto_isrunning = false;
  26. int countRounds = 0;
  27. static const char *TAGTFLITE = "server_tflite";
  28. int getCountFlowRounds() {
  29. return countRounds;
  30. }
  31. esp_err_t GetJPG(std::string _filename, httpd_req_t *req)
  32. {
  33. return tfliteflow.GetJPGStream(_filename, req);
  34. }
  35. esp_err_t GetRawJPG(httpd_req_t *req)
  36. {
  37. return tfliteflow.SendRawJPG(req);
  38. }
  39. bool isSetupModusActive() {
  40. return tfliteflow.getStatusSetupModus();
  41. return false;
  42. }
  43. void KillTFliteTasks()
  44. {
  45. #ifdef DEBUG_DETAIL_ON
  46. ESP_LOGD(TAGTFLITE, "Handle: xHandleblink_task_doFlow: %ld", (long) xHandleblink_task_doFlow);
  47. #endif
  48. if (xHandleblink_task_doFlow != NULL)
  49. {
  50. TaskHandle_t xHandleblink_task_doFlowTmp = xHandleblink_task_doFlow;
  51. xHandleblink_task_doFlow = NULL;
  52. vTaskDelete(xHandleblink_task_doFlowTmp);
  53. #ifdef DEBUG_DETAIL_ON
  54. ESP_LOGD(TAGTFLITE, "Killed: xHandleblink_task_doFlow");
  55. #endif
  56. }
  57. #ifdef DEBUG_DETAIL_ON
  58. ESP_LOGD(TAGTFLITE, "Handle: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
  59. #endif
  60. if (xHandletask_autodoFlow != NULL)
  61. {
  62. TaskHandle_t xHandletask_autodoFlowTmp = xHandletask_autodoFlow;
  63. xHandletask_autodoFlow = NULL;
  64. vTaskDelete(xHandletask_autodoFlowTmp);
  65. #ifdef DEBUG_DETAIL_ON
  66. ESP_LOGD(TAGTFLITE, "Killed: xHandletask_autodoFlow");
  67. #endif
  68. }
  69. }
  70. void doInit(void)
  71. {
  72. #ifdef DEBUG_DETAIL_ON
  73. ESP_LOGD(TAGTFLITE, "Start tfliteflow.InitFlow(config);");
  74. #endif
  75. tfliteflow.InitFlow(CONFIG_FILE);
  76. #ifdef DEBUG_DETAIL_ON
  77. ESP_LOGD(TAGTFLITE, "Finished tfliteflow.InitFlow(config);");
  78. #endif
  79. }
  80. bool doflow(void)
  81. {
  82. std::string zw_time = gettimestring(LOGFILE_TIME_FORMAT);
  83. ESP_LOGD(TAGTFLITE, "doflow - start %s", zw_time.c_str());
  84. flowisrunning = true;
  85. tfliteflow.doFlow(zw_time);
  86. flowisrunning = false;
  87. #ifdef DEBUG_DETAIL_ON
  88. ESP_LOGD(TAGTFLITE, "doflow - end %s", zw_time.c_str());
  89. #endif
  90. return true;
  91. }
  92. void blink_task_doFlow(void *pvParameter)
  93. {
  94. #ifdef DEBUG_DETAIL_ON
  95. ESP_LOGD(TAGTFLITE, "blink_task_doFlow");
  96. #endif
  97. if (!flowisrunning)
  98. {
  99. flowisrunning = true;
  100. doflow();
  101. flowisrunning = false;
  102. }
  103. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  104. xHandleblink_task_doFlow = NULL;
  105. }
  106. esp_err_t handler_init(httpd_req_t *req)
  107. {
  108. #ifdef DEBUG_DETAIL_ON
  109. LogFile.WriteHeapInfo("handler_init - Start");
  110. ESP_LOGD(TAGTFLITE, "handler_doinit uri: %s", req->uri);
  111. #endif
  112. const char* resp_str = "Init started<br>";
  113. httpd_resp_send(req, resp_str, strlen(resp_str));
  114. doInit();
  115. resp_str = "Init done<br>";
  116. httpd_resp_send(req, resp_str, strlen(resp_str));
  117. /* Respond with an empty chunk to signal HTTP response completion */
  118. httpd_resp_send_chunk(req, NULL, 0);
  119. #ifdef DEBUG_DETAIL_ON
  120. LogFile.WriteHeapInfo("handler_init - Done");
  121. #endif
  122. return ESP_OK;
  123. };
  124. esp_err_t handler_doflow(httpd_req_t *req)
  125. {
  126. #ifdef DEBUG_DETAIL_ON
  127. LogFile.WriteHeapInfo("handler_doflow - Start");
  128. #endif
  129. ESP_LOGD(TAGTFLITE, "handler_doFlow uri: %s", req->uri);
  130. if (flowisrunning)
  131. {
  132. const char* resp_str = "doFlow is already running and cannot be started again";
  133. httpd_resp_send(req, resp_str, strlen(resp_str));
  134. return 2;
  135. }
  136. else
  137. {
  138. xTaskCreate(&blink_task_doFlow, "blink_doFlow", configMINIMAL_STACK_SIZE * 64, NULL, tskIDLE_PRIORITY+1, &xHandleblink_task_doFlow);
  139. }
  140. const char* resp_str = "doFlow started - takes about 60 seconds";
  141. httpd_resp_send(req, resp_str, strlen(resp_str));
  142. /* Respond with an empty chunk to signal HTTP response completion */
  143. httpd_resp_send_chunk(req, NULL, 0);
  144. #ifdef DEBUG_DETAIL_ON
  145. LogFile.WriteHeapInfo("handler_doflow - Done");
  146. #endif
  147. return ESP_OK;
  148. };
  149. esp_err_t handler_json(httpd_req_t *req)
  150. {
  151. #ifdef DEBUG_DETAIL_ON
  152. LogFile.WriteHeapInfo("handler_json - Start");
  153. #endif
  154. ESP_LOGD(TAGTFLITE, "handler_JSON uri: %s", req->uri);
  155. char _query[100];
  156. // char _size[10];
  157. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  158. httpd_resp_set_type(req, "application/json");
  159. std::string zw = tfliteflow.getJSON();
  160. if (zw.length() > 0)
  161. httpd_resp_sendstr_chunk(req, zw.c_str());
  162. string query = std::string(_query);
  163. /* Respond with an empty chunk to signal HTTP response completion */
  164. httpd_resp_sendstr_chunk(req, NULL);
  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(TAGTFLITE, "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(TAGTFLITE, "Query: %s", _query);
  186. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  187. {
  188. #ifdef DEBUG_DETAIL_ON
  189. ESP_LOGD(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "task is found: %s", _valuechar);
  305. #endif
  306. _task = string(_valuechar);
  307. }
  308. }
  309. if (_task.compare("namenumbers") == 0)
  310. {
  311. ESP_LOGI(TAGTFLITE, "Get NUMBER list");
  312. return get_numbers_file_handler(req);
  313. }
  314. if (_task.compare("data") == 0)
  315. {
  316. ESP_LOGI(TAGTFLITE, "Get data list");
  317. return get_data_file_handler(req);
  318. }
  319. if (_task.compare("tflite") == 0)
  320. {
  321. ESP_LOGD(TAGTFLITE, "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(TAGTFLITE, "in: %s", in.c_str());
  333. ESP_LOGD(TAGTFLITE, "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(TAGTFLITE, "in: %s", in.c_str());
  364. ESP_LOGD(TAGTFLITE, "out: %s", out.c_str());
  365. ESP_LOGD(TAGTFLITE, "x: %s", zw.c_str());
  366. ESP_LOGD(TAGTFLITE, "y: %s", zw.c_str());
  367. ESP_LOGD(TAGTFLITE, "dx: %s", zw.c_str());
  368. ESP_LOGD(TAGTFLITE, "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(TAGTFLITE, "Parameter host: %s", _host.c_str());
  425. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAGTFLITE, zwzw.c_str());
  426. Camera.SetBrightnessContrastSaturation(bri, con, sat);
  427. Camera.SetLEDIntensity(intens);
  428. ESP_LOGD(TAGTFLITE, "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(TAGTFLITE, "Parameter host: %s", _host.c_str());
  440. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAGTFLITE, 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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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(TAGTFLITE, "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. std::string _zw = "task_autodoFlow - next round - Round #" + std::to_string(++countRounds);
  566. LogFile.WriteToFile(ESP_LOG_INFO, _zw);
  567. fr_start = esp_timer_get_time();
  568. if (flowisrunning)
  569. {
  570. #ifdef DEBUG_DETAIL_ON
  571. ESP_LOGD(TAGTFLITE, "Autoflow: doFlow is already running!");
  572. #endif
  573. }
  574. else
  575. {
  576. #ifdef DEBUG_DETAIL_ON
  577. ESP_LOGD(TAGTFLITE, "Autoflow: doFlow is started");
  578. #endif
  579. flowisrunning = true;
  580. doflow();
  581. #ifdef DEBUG_DETAIL_ON
  582. ESP_LOGD(TAGTFLITE, "Remove older log files");
  583. #endif
  584. LogFile.RemoveOld();
  585. }
  586. LogFile.WriteToFile(ESP_LOG_INFO, "task_autodoFlow - round done");
  587. //CPU Temp
  588. float cputmp = temperatureRead();
  589. std::stringstream stream;
  590. stream << std::fixed << std::setprecision(1) << cputmp;
  591. string zwtemp = "CPU Temperature: " + stream.str();
  592. LogFile.WriteToFile(ESP_LOG_INFO, zwtemp);
  593. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  594. if (auto_intervall > fr_delta_ms)
  595. {
  596. const TickType_t xDelay = (auto_intervall - fr_delta_ms) / portTICK_PERIOD_MS;
  597. ESP_LOGD(TAGTFLITE, "Autoflow: sleep for: %ldms", (long) xDelay);
  598. vTaskDelay( xDelay );
  599. }
  600. }
  601. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  602. xHandletask_autodoFlow = NULL;
  603. ESP_LOGD(TAGTFLITE, "task_autodoFlow: end");
  604. }
  605. void TFliteDoAutoStart()
  606. {
  607. BaseType_t xReturned;
  608. int _i = configMINIMAL_STACK_SIZE;
  609. ESP_LOGD(TAGTFLITE, "task_autodoFlow configMINIMAL_STACK_SIZE: %d", _i);
  610. ESP_LOGD(TAGTFLITE, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  611. xReturned = xTaskCreate(&task_autodoFlow, "task_autodoFlow", configMINIMAL_STACK_SIZE * 35, NULL, tskIDLE_PRIORITY+1, &xHandletask_autodoFlow);
  612. if( xReturned != pdPASS )
  613. {
  614. //Memory: 64 --> 48 --> 35 --> 25
  615. ESP_LOGD(TAGTFLITE, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
  616. }
  617. ESP_LOGD(TAGTFLITE, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  618. }
  619. std::string GetMQTTMainTopic()
  620. {
  621. return tfliteflow.GetMQTTMainTopic();
  622. }
  623. void register_server_tflite_uri(httpd_handle_t server)
  624. {
  625. ESP_LOGI(TAGTFLITE, "server_part_camera - Registering URI handlers");
  626. httpd_uri_t camuri = { };
  627. camuri.method = HTTP_GET;
  628. camuri.uri = "/doinit";
  629. camuri.handler = handler_init;
  630. camuri.user_ctx = (void*) "Light On";
  631. httpd_register_uri_handler(server, &camuri);
  632. // Legacy API => New: "/setPreValue"
  633. camuri.uri = "/setPreValue.html";
  634. camuri.handler = handler_prevalue;
  635. camuri.user_ctx = (void*) "Prevalue";
  636. httpd_register_uri_handler(server, &camuri);
  637. camuri.uri = "/setPreValue";
  638. camuri.handler = handler_prevalue;
  639. camuri.user_ctx = (void*) "Prevalue";
  640. httpd_register_uri_handler(server, &camuri);
  641. camuri.uri = "/doflow";
  642. camuri.handler = handler_doflow;
  643. camuri.user_ctx = (void*) "Light Off";
  644. httpd_register_uri_handler(server, &camuri);
  645. camuri.uri = "/statusflow.html";
  646. camuri.handler = handler_statusflow;
  647. camuri.user_ctx = (void*) "Light Off";
  648. httpd_register_uri_handler(server, &camuri);
  649. camuri.uri = "/statusflow";
  650. camuri.handler = handler_statusflow;
  651. camuri.user_ctx = (void*) "Light Off";
  652. httpd_register_uri_handler(server, &camuri);
  653. // Legacy API => New: "/cpu_temperature"
  654. camuri.uri = "/cputemp.html";
  655. camuri.handler = handler_cputemp;
  656. camuri.user_ctx = (void*) "Light Off";
  657. httpd_register_uri_handler(server, &camuri);
  658. camuri.uri = "/cpu_temperature";
  659. camuri.handler = handler_cputemp;
  660. camuri.user_ctx = (void*) "Light Off";
  661. httpd_register_uri_handler(server, &camuri);
  662. // Legacy API => New: "/rssi"
  663. camuri.uri = "/rssi.html";
  664. camuri.handler = handler_rssi;
  665. camuri.user_ctx = (void*) "Light Off";
  666. httpd_register_uri_handler(server, &camuri);
  667. camuri.uri = "/rssi";
  668. camuri.handler = handler_rssi;
  669. camuri.user_ctx = (void*) "Light Off";
  670. httpd_register_uri_handler(server, &camuri);
  671. camuri.uri = "/editflow.html";
  672. camuri.handler = handler_editflow;
  673. camuri.user_ctx = (void*) "EditFlow";
  674. httpd_register_uri_handler(server, &camuri);
  675. // Legacy API => New: "/value"
  676. camuri.uri = "/value.html";
  677. camuri.handler = handler_wasserzaehler;
  678. camuri.user_ctx = (void*) "Value";
  679. httpd_register_uri_handler(server, &camuri);
  680. camuri.uri = "/value";
  681. camuri.handler = handler_wasserzaehler;
  682. camuri.user_ctx = (void*) "Value";
  683. httpd_register_uri_handler(server, &camuri);
  684. // Legacy API => New: "/value"
  685. camuri.uri = "/wasserzaehler.html";
  686. camuri.handler = handler_wasserzaehler;
  687. camuri.user_ctx = (void*) "Wasserzaehler";
  688. httpd_register_uri_handler(server, &camuri);
  689. camuri.uri = "/json";
  690. camuri.handler = handler_json;
  691. camuri.user_ctx = (void*) "JSON";
  692. httpd_register_uri_handler(server, &camuri);
  693. }