server_tflite.cpp 27 KB

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