server_tflite.cpp 28 KB

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