server_tflite.cpp 28 KB

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