server_tflite.cpp 25 KB

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