server_tflite.cpp 28 KB

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