server_tflite.cpp 29 KB

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