server_tflite.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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, "KillTFliteTasks: xHandletask_autodoFlow: %ld", (long) xHandletask_autodoFlow);
  46. #endif
  47. if( xHandletask_autodoFlow != NULL )
  48. {
  49. vTaskDelete(xHandletask_autodoFlow);
  50. xHandletask_autodoFlow = NULL;
  51. }
  52. #ifdef DEBUG_DETAIL_ON
  53. ESP_LOGD(TAG, "Killed: xHandletask_autodoFlow");
  54. #endif
  55. }
  56. void doInit(void)
  57. {
  58. #ifdef DEBUG_DETAIL_ON
  59. ESP_LOGD(TAG, "Start tfliteflow.InitFlow(config);");
  60. #endif
  61. tfliteflow.InitFlow(CONFIG_FILE);
  62. FlowInitDone = true;
  63. #ifdef DEBUG_DETAIL_ON
  64. ESP_LOGD(TAG, "Finished tfliteflow.InitFlow(config);");
  65. #endif
  66. /* GPIO handler has to be initialized before MQTT init to ensure proper topic subscription */
  67. gpio_handler_init();
  68. #ifdef ENABLE_MQTT
  69. tfliteflow.StartMQTTService();
  70. #endif //ENABLE_MQTT
  71. }
  72. bool doflow(void)
  73. {
  74. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  75. ESP_LOGD(TAG, "doflow - start %s", zw_time.c_str());
  76. flowisrunning = true;
  77. tfliteflow.doFlow(zw_time);
  78. flowisrunning = false;
  79. #ifdef DEBUG_DETAIL_ON
  80. ESP_LOGD(TAG, "doflow - end %s", zw_time.c_str());
  81. #endif
  82. return true;
  83. }
  84. esp_err_t handler_init(httpd_req_t *req)
  85. {
  86. #ifdef DEBUG_DETAIL_ON
  87. LogFile.WriteHeapInfo("handler_init - Start");
  88. ESP_LOGD(TAG, "handler_doinit uri: %s", req->uri);
  89. #endif
  90. const char* resp_str = "Init started<br>";
  91. httpd_resp_send(req, resp_str, strlen(resp_str));
  92. doInit();
  93. resp_str = "Init done<br>";
  94. httpd_resp_send(req, resp_str, strlen(resp_str));
  95. /* Respond with an empty chunk to signal HTTP response completion */
  96. httpd_resp_send_chunk(req, NULL, 0);
  97. #ifdef DEBUG_DETAIL_ON
  98. LogFile.WriteHeapInfo("handler_init - Done");
  99. #endif
  100. return ESP_OK;
  101. }
  102. esp_err_t handler_flow_start(httpd_req_t *req) {
  103. #ifdef DEBUG_DETAIL_ON
  104. LogFile.WriteHeapInfo("handler_flow_start - Start");
  105. #endif
  106. ESP_LOGD(TAG, "handler_flow_start uri: %s", req->uri);
  107. if (auto_isrunning) {
  108. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  109. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by REST API /flow_start");
  110. const char* resp_str = "The flow is going to be started immediately or is already running";
  111. httpd_resp_send(req, resp_str, strlen(resp_str));
  112. }
  113. else {
  114. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by REST API, but flow is not active!");
  115. const char* resp_str = "WARNING: Flow start triggered by REST API, but flow is not active";
  116. httpd_resp_send(req, resp_str, strlen(resp_str));
  117. }
  118. /* Respond with an empty chunk to signal HTTP response completion */
  119. httpd_resp_send_chunk(req, NULL, 0);
  120. #ifdef DEBUG_DETAIL_ON
  121. LogFile.WriteHeapInfo("handler_flow_start - Done");
  122. #endif
  123. return ESP_OK;
  124. }
  125. #ifdef ENABLE_MQTT
  126. esp_err_t MQTTCtrlFlowStart(std::string _topic) {
  127. #ifdef DEBUG_DETAIL_ON
  128. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Start");
  129. #endif
  130. ESP_LOGD(TAG, "MQTTCtrlFlowStart: topic %s", _topic.c_str());
  131. if (auto_isrunning)
  132. {
  133. xTaskAbortDelay(xHandletask_autodoFlow); // Delay will be aborted if task is in blocked (waiting) state. If task is already running, no action
  134. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Flow start triggered by MQTT topic " + _topic);
  135. }
  136. else
  137. {
  138. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Flow start triggered by MQTT topic " + _topic + ", but flow is not active!");
  139. }
  140. #ifdef DEBUG_DETAIL_ON
  141. LogFile.WriteHeapInfo("MQTTCtrlFlowStart - Done");
  142. #endif
  143. return ESP_OK;
  144. }
  145. #endif //ENABLE_MQTT
  146. esp_err_t handler_json(httpd_req_t *req)
  147. {
  148. #ifdef DEBUG_DETAIL_ON
  149. LogFile.WriteHeapInfo("handler_json - Start");
  150. #endif
  151. ESP_LOGD(TAG, "handler_JSON uri: %s", req->uri);
  152. if (FlowInitDone)
  153. {
  154. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  155. httpd_resp_set_type(req, "application/json");
  156. std::string zw = tfliteflow.getJSON();
  157. if (zw.length() > 0)
  158. {
  159. httpd_resp_send(req, zw.c_str(), zw.length());
  160. }
  161. else
  162. {
  163. httpd_resp_send(req, NULL, 0);
  164. }
  165. }
  166. else
  167. {
  168. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "JSON API not yet initialized. Please retry later...");
  169. return ESP_ERR_NOT_FOUND;
  170. }
  171. #ifdef DEBUG_DETAIL_ON
  172. LogFile.WriteHeapInfo("handler_JSON - Done");
  173. #endif
  174. return ESP_OK;
  175. }
  176. esp_err_t handler_wasserzaehler(httpd_req_t *req)
  177. {
  178. #ifdef DEBUG_DETAIL_ON
  179. LogFile.WriteHeapInfo("handler water counter - Start");
  180. #endif
  181. if (FlowInitDone)
  182. {
  183. bool _rawValue = false;
  184. bool _noerror = false;
  185. bool _all = false;
  186. std::string _type = "value";
  187. string zw;
  188. ESP_LOGD(TAG, "handler water counter uri: %s", req->uri);
  189. char _query[100];
  190. char _size[10];
  191. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  192. {
  193. // ESP_LOGD(TAG, "Query: %s", _query);
  194. if (httpd_query_key_value(_query, "all", _size, 10) == ESP_OK)
  195. {
  196. #ifdef DEBUG_DETAIL_ON
  197. ESP_LOGD(TAG, "all is found%s", _size);
  198. #endif
  199. _all = true;
  200. }
  201. if (httpd_query_key_value(_query, "type", _size, 10) == ESP_OK)
  202. {
  203. #ifdef DEBUG_DETAIL_ON
  204. ESP_LOGD(TAG, "all is found: %s", _size);
  205. #endif
  206. _type = std::string(_size);
  207. }
  208. if (httpd_query_key_value(_query, "rawvalue", _size, 10) == ESP_OK)
  209. {
  210. #ifdef DEBUG_DETAIL_ON
  211. ESP_LOGD(TAG, "rawvalue is found: %s", _size);
  212. #endif
  213. _rawValue = true;
  214. }
  215. if (httpd_query_key_value(_query, "noerror", _size, 10) == ESP_OK)
  216. {
  217. #ifdef DEBUG_DETAIL_ON
  218. ESP_LOGD(TAG, "noerror is found: %s", _size);
  219. #endif
  220. _noerror = true;
  221. }
  222. }
  223. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  224. if (_all)
  225. {
  226. httpd_resp_set_type(req, "text/plain");
  227. ESP_LOGD(TAG, "TYPE: %s", _type.c_str());
  228. int _intype = READOUT_TYPE_VALUE;
  229. if (_type == "prevalue")
  230. _intype = READOUT_TYPE_PREVALUE;
  231. if (_type == "raw")
  232. _intype = READOUT_TYPE_RAWVALUE;
  233. if (_type == "error")
  234. _intype = READOUT_TYPE_ERROR;
  235. zw = tfliteflow.getReadoutAll(_intype);
  236. ESP_LOGD(TAG, "ZW: %s", zw.c_str());
  237. if (zw.length() > 0)
  238. httpd_resp_sendstr_chunk(req, zw.c_str());
  239. httpd_resp_sendstr_chunk(req, NULL);
  240. return ESP_OK;
  241. }
  242. zw = tfliteflow.getReadout(_rawValue, _noerror);
  243. if (zw.length() > 0)
  244. httpd_resp_sendstr_chunk(req, zw.c_str());
  245. string query = std::string(_query);
  246. // ESP_LOGD(TAG, "Query: %s, query.c_str());
  247. if (query.find("full") != std::string::npos)
  248. {
  249. string txt, zw;
  250. txt = "<p>Aligned Image: <p><img src=\"/img_tmp/alg_roi.jpg\"> <p>\n";
  251. txt = txt + "Digital Counter: <p> ";
  252. httpd_resp_sendstr_chunk(req, txt.c_str());
  253. std::vector<HTMLInfo*> htmlinfodig;
  254. htmlinfodig = tfliteflow.GetAllDigital();
  255. for (int i = 0; i < htmlinfodig.size(); ++i)
  256. {
  257. if (tfliteflow.GetTypeDigital() == Digital)
  258. {
  259. if (htmlinfodig[i]->val == 10)
  260. zw = "NaN";
  261. else
  262. zw = to_string((int) htmlinfodig[i]->val);
  263. txt = "<img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"> " + zw;
  264. }
  265. else
  266. {
  267. std::stringstream stream;
  268. stream << std::fixed << std::setprecision(1) << htmlinfodig[i]->val;
  269. zw = stream.str();
  270. txt = "<img src=\"/img_tmp/" + htmlinfodig[i]->filename + "\"> " + zw;
  271. }
  272. httpd_resp_sendstr_chunk(req, txt.c_str());
  273. delete htmlinfodig[i];
  274. }
  275. htmlinfodig.clear();
  276. txt = " <p> Analog Meter: <p> ";
  277. httpd_resp_sendstr_chunk(req, txt.c_str());
  278. std::vector<HTMLInfo*> htmlinfoana;
  279. htmlinfoana = tfliteflow.GetAllAnalog();
  280. for (int i = 0; i < htmlinfoana.size(); ++i)
  281. {
  282. std::stringstream stream;
  283. stream << std::fixed << std::setprecision(1) << htmlinfoana[i]->val;
  284. zw = stream.str();
  285. txt = "<img src=\"/img_tmp/" + htmlinfoana[i]->filename + "\"> " + zw;
  286. httpd_resp_sendstr_chunk(req, txt.c_str());
  287. delete htmlinfoana[i];
  288. }
  289. htmlinfoana.clear();
  290. }
  291. /* Respond with an empty chunk to signal HTTP response completion */
  292. httpd_resp_sendstr_chunk(req, NULL);
  293. }
  294. else
  295. {
  296. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Value API not yet initialized. Please retry later...");
  297. return ESP_ERR_NOT_FOUND;
  298. }
  299. #ifdef DEBUG_DETAIL_ON
  300. LogFile.WriteHeapInfo("handler_wasserzaehler - Done");
  301. #endif
  302. return ESP_OK;
  303. }
  304. esp_err_t handler_editflow(httpd_req_t *req)
  305. {
  306. #ifdef DEBUG_DETAIL_ON
  307. LogFile.WriteHeapInfo("handler_editflow - Start");
  308. #endif
  309. ESP_LOGD(TAG, "handler_editflow uri: %s", req->uri);
  310. char _query[200];
  311. char _valuechar[30];
  312. string _task;
  313. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  314. {
  315. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  316. {
  317. #ifdef DEBUG_DETAIL_ON
  318. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  319. #endif
  320. _task = string(_valuechar);
  321. }
  322. }
  323. if (_task.compare("namenumbers") == 0)
  324. {
  325. ESP_LOGI(TAG, "Get NUMBER list");
  326. return get_numbers_file_handler(req);
  327. }
  328. if (_task.compare("data") == 0)
  329. {
  330. ESP_LOGI(TAG, "Get data list");
  331. return get_data_file_handler(req);
  332. }
  333. if (_task.compare("tflite") == 0)
  334. {
  335. ESP_LOGD(TAG, "Get tflite list");
  336. return get_tflite_file_handler(req);
  337. }
  338. if (_task.compare("copy") == 0)
  339. {
  340. string in, out, zw;
  341. httpd_query_key_value(_query, "in", _valuechar, 30);
  342. in = string(_valuechar);
  343. httpd_query_key_value(_query, "out", _valuechar, 30);
  344. out = string(_valuechar);
  345. #ifdef DEBUG_DETAIL_ON
  346. ESP_LOGD(TAG, "in: %s", in.c_str());
  347. ESP_LOGD(TAG, "out: %s", out.c_str());
  348. #endif
  349. in = "/sdcard" + in;
  350. out = "/sdcard" + out;
  351. CopyFile(in, out);
  352. zw = "Copy Done";
  353. httpd_resp_sendstr_chunk(req, zw.c_str());
  354. }
  355. if (_task.compare("cutref") == 0)
  356. {
  357. string in, out, zw;
  358. int x, y, dx, dy;
  359. bool enhance = false;
  360. httpd_query_key_value(_query, "in", _valuechar, 30);
  361. in = string(_valuechar);
  362. httpd_query_key_value(_query, "out", _valuechar, 30);
  363. out = string(_valuechar);
  364. httpd_query_key_value(_query, "x", _valuechar, 30);
  365. zw = string(_valuechar);
  366. x = stoi(zw);
  367. httpd_query_key_value(_query, "y", _valuechar, 30);
  368. zw = string(_valuechar);
  369. y = stoi(zw);
  370. httpd_query_key_value(_query, "dx", _valuechar, 30);
  371. zw = string(_valuechar);
  372. dx = stoi(zw);
  373. httpd_query_key_value(_query, "dy", _valuechar, 30);
  374. zw = string(_valuechar);
  375. dy = stoi(zw);
  376. #ifdef DEBUG_DETAIL_ON
  377. ESP_LOGD(TAG, "in: %s", in.c_str());
  378. ESP_LOGD(TAG, "out: %s", out.c_str());
  379. ESP_LOGD(TAG, "x: %s", zw.c_str());
  380. ESP_LOGD(TAG, "y: %s", zw.c_str());
  381. ESP_LOGD(TAG, "dx: %s", zw.c_str());
  382. ESP_LOGD(TAG, "dy: %s", zw.c_str());
  383. #endif
  384. if (httpd_query_key_value(_query, "enhance", _valuechar, 10) == ESP_OK)
  385. {
  386. zw = string(_valuechar);
  387. if (zw.compare("true") == 0)
  388. {
  389. enhance = true;
  390. }
  391. }
  392. in = "/sdcard" + in;
  393. out = "/sdcard" + out;
  394. string out2 = out.substr(0, out.length() - 4) + "_org.jpg";
  395. CAlignAndCutImage *caic = new CAlignAndCutImage(in);
  396. caic->CutAndSave(out2, x, y, dx, dy);
  397. delete caic;
  398. CImageBasis *cim = new CImageBasis(out2);
  399. if (enhance)
  400. {
  401. cim->Contrast(90);
  402. }
  403. cim->SaveToFile(out);
  404. delete cim;
  405. zw = "CutImage Done";
  406. httpd_resp_sendstr_chunk(req, zw.c_str());
  407. }
  408. if (_task.compare("test_take") == 0)
  409. {
  410. std::string _host = "";
  411. std::string _bri = "";
  412. std::string _con = "";
  413. std::string _sat = "";
  414. std::string _int = "";
  415. int bri = -100;
  416. int sat = -100;
  417. int con = -100;
  418. int intens = -100;
  419. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  420. _host = std::string(_valuechar);
  421. }
  422. if (httpd_query_key_value(_query, "int", _valuechar, 30) == ESP_OK) {
  423. _int = std::string(_valuechar);
  424. intens = stoi(_int);
  425. }
  426. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
  427. _bri = std::string(_valuechar);
  428. bri = stoi(_bri);
  429. }
  430. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
  431. _con = std::string(_valuechar);
  432. con = stoi(_con);
  433. }
  434. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
  435. _sat = std::string(_valuechar);
  436. sat = stoi(_sat);
  437. }
  438. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  439. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  440. Camera.SetBrightnessContrastSaturation(bri, con, sat);
  441. Camera.SetLEDIntensity(intens);
  442. ESP_LOGD(TAG, "test_take - vor MakeImage");
  443. std::string zw = tfliteflow.doSingleStep("[MakeImage]", _host);
  444. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  445. httpd_resp_sendstr_chunk(req, zw.c_str());
  446. }
  447. if (_task.compare("test_align") == 0)
  448. {
  449. std::string _host = "";
  450. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  451. _host = std::string(_valuechar);
  452. }
  453. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  454. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  455. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  456. httpd_resp_sendstr_chunk(req, zw.c_str());
  457. }
  458. /* Respond with an empty chunk to signal HTTP response completion */
  459. httpd_resp_sendstr_chunk(req, NULL);
  460. #ifdef DEBUG_DETAIL_ON
  461. LogFile.WriteHeapInfo("handler_editflow - Done");
  462. #endif
  463. return ESP_OK;
  464. }
  465. esp_err_t handler_statusflow(httpd_req_t *req)
  466. {
  467. #ifdef DEBUG_DETAIL_ON
  468. LogFile.WriteHeapInfo("handler_prevalue - Start");
  469. #endif
  470. if (FlowInitDone)
  471. {
  472. const char* resp_str;
  473. #ifdef DEBUG_DETAIL_ON
  474. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  475. #endif
  476. string* zw = tfliteflow.getActStatus();
  477. resp_str = zw->c_str();
  478. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  479. httpd_resp_send(req, resp_str, strlen(resp_str));
  480. /* Respond with an empty chunk to signal HTTP response completion */
  481. httpd_resp_send_chunk(req, NULL, 0);
  482. }
  483. else
  484. {
  485. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flowstatus API not yet initialized. Please retry later...");
  486. return ESP_ERR_NOT_FOUND;
  487. }
  488. #ifdef DEBUG_DETAIL_ON
  489. LogFile.WriteHeapInfo("handler_prevalue - Done");
  490. #endif
  491. return ESP_OK;
  492. }
  493. esp_err_t handler_cputemp(httpd_req_t *req)
  494. {
  495. #ifdef DEBUG_DETAIL_ON
  496. LogFile.WriteHeapInfo("handler_cputemp - Start");
  497. #endif
  498. const char* resp_str;
  499. char cputemp[20];
  500. sprintf(cputemp, "%4.1f°C", temperatureRead());
  501. resp_str = cputemp;
  502. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  503. httpd_resp_send(req, resp_str, strlen(resp_str));
  504. /* Respond with an empty chunk to signal HTTP response completion */
  505. httpd_resp_send_chunk(req, NULL, 0);
  506. #ifdef DEBUG_DETAIL_ON
  507. LogFile.WriteHeapInfo("handler_cputemp - End");
  508. #endif
  509. return ESP_OK;
  510. }
  511. esp_err_t handler_rssi(httpd_req_t *req)
  512. {
  513. #ifdef DEBUG_DETAIL_ON
  514. LogFile.WriteHeapInfo("handler_rssi - Start");
  515. #endif
  516. if (getWIFIisConnected())
  517. {
  518. const char* resp_str;
  519. char rssi[20];
  520. sprintf(rssi, "%idBm", get_WIFI_RSSI());
  521. resp_str = rssi;
  522. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  523. httpd_resp_send(req, resp_str, strlen(resp_str));
  524. /* Respond with an empty chunk to signal HTTP response completion */
  525. httpd_resp_send_chunk(req, NULL, 0);
  526. }
  527. else
  528. {
  529. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "RSSI API not yet initialized. Please retry later...");
  530. return ESP_ERR_NOT_FOUND;
  531. }
  532. #ifdef DEBUG_DETAIL_ON
  533. LogFile.WriteHeapInfo("handler_rssi - End");
  534. #endif
  535. return ESP_OK;
  536. }
  537. esp_err_t handler_uptime(httpd_req_t *req)
  538. {
  539. #ifdef DEBUG_DETAIL_ON
  540. LogFile.WriteHeapInfo("handler_uptime - Start");
  541. #endif
  542. std::string formatedUptime = getFormatedUptime(false);
  543. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  544. httpd_resp_send(req, formatedUptime.c_str(), strlen(formatedUptime.c_str()));
  545. /* Respond with an empty chunk to signal HTTP response completion */
  546. httpd_resp_send_chunk(req, NULL, 0);
  547. #ifdef DEBUG_DETAIL_ON
  548. LogFile.WriteHeapInfo("handler_uptime - End");
  549. #endif
  550. return ESP_OK;
  551. }
  552. esp_err_t handler_prevalue(httpd_req_t *req)
  553. {
  554. #ifdef DEBUG_DETAIL_ON
  555. LogFile.WriteHeapInfo("handler_prevalue - Start");
  556. #endif
  557. const char* resp_str;
  558. string zw;
  559. #ifdef DEBUG_DETAIL_ON
  560. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  561. #endif
  562. char _query[100];
  563. char _size[10] = "";
  564. char _numbers[50] = "default";
  565. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  566. {
  567. #ifdef DEBUG_DETAIL_ON
  568. ESP_LOGD(TAG, "Query: %s", _query);
  569. #endif
  570. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  571. {
  572. #ifdef DEBUG_DETAIL_ON
  573. ESP_LOGD(TAG, "Value: %s", _size);
  574. #endif
  575. }
  576. httpd_query_key_value(_query, "numbers", _numbers, 50);
  577. }
  578. if (strlen(_size) == 0)
  579. {
  580. zw = tfliteflow.GetPrevalue(std::string(_numbers));
  581. }
  582. else
  583. {
  584. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size, _numbers, true);
  585. }
  586. resp_str = zw.c_str();
  587. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  588. httpd_resp_send(req, resp_str, strlen(resp_str));
  589. /* Respond with an empty chunk to signal HTTP response completion */
  590. httpd_resp_send_chunk(req, NULL, 0);
  591. #ifdef DEBUG_DETAIL_ON
  592. LogFile.WriteHeapInfo("handler_prevalue - End");
  593. #endif
  594. return ESP_OK;
  595. }
  596. void task_autodoFlow(void *pvParameter)
  597. {
  598. int64_t fr_start, fr_delta_ms;
  599. if (esp_reset_reason() == ESP_RST_PANIC) {
  600. 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!");
  601. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!");
  602. LogFile.setLogLevel(ESP_LOG_DEBUG);
  603. //MQTTPublish(GetMQTTMainTopic() + "/" + "status", "Postponing first round", false);
  604. vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA or fetch the log
  605. }
  606. ESP_LOGD(TAG, "task_autodoFlow: start");
  607. doInit();
  608. auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
  609. if (isSetupModusActive()) {
  610. auto_isrunning = false;
  611. std::string zw_time = getCurrentTimeString(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. }