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. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  361. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
  362. {
  363. if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
  364. {
  365. #ifdef DEBUG_DETAIL_ON
  366. ESP_LOGD(TAG, "task is found: %s", _valuechar);
  367. #endif
  368. _task = string(_valuechar);
  369. }
  370. }
  371. if (_task.compare("namenumbers") == 0)
  372. {
  373. ESP_LOGI(TAG, "Get NUMBER list");
  374. return get_numbers_file_handler(req);
  375. }
  376. if (_task.compare("data") == 0)
  377. {
  378. ESP_LOGI(TAG, "Get data list");
  379. return get_data_file_handler(req);
  380. }
  381. if (_task.compare("tflite") == 0)
  382. {
  383. ESP_LOGD(TAG, "Get tflite list");
  384. return get_tflite_file_handler(req);
  385. }
  386. if (_task.compare("copy") == 0)
  387. {
  388. string in, out, zw;
  389. httpd_query_key_value(_query, "in", _valuechar, 30);
  390. in = string(_valuechar);
  391. httpd_query_key_value(_query, "out", _valuechar, 30);
  392. out = string(_valuechar);
  393. #ifdef DEBUG_DETAIL_ON
  394. ESP_LOGD(TAG, "in: %s", in.c_str());
  395. ESP_LOGD(TAG, "out: %s", out.c_str());
  396. #endif
  397. in = "/sdcard" + in;
  398. out = "/sdcard" + out;
  399. CopyFile(in, out);
  400. zw = "Copy Done";
  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_send(req, zw.c_str(), zw.length());
  455. }
  456. if (_task.compare("test_take") == 0)
  457. {
  458. std::string _host = "";
  459. std::string _bri = "";
  460. std::string _con = "";
  461. std::string _sat = "";
  462. std::string _int = "";
  463. int bri = -100;
  464. int sat = -100;
  465. int con = -100;
  466. int intens = -100;
  467. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  468. _host = std::string(_valuechar);
  469. }
  470. if (httpd_query_key_value(_query, "int", _valuechar, 30) == ESP_OK) {
  471. _int = std::string(_valuechar);
  472. intens = stoi(_int);
  473. }
  474. if (httpd_query_key_value(_query, "bri", _valuechar, 30) == ESP_OK) {
  475. _bri = std::string(_valuechar);
  476. bri = stoi(_bri);
  477. }
  478. if (httpd_query_key_value(_query, "con", _valuechar, 30) == ESP_OK) {
  479. _con = std::string(_valuechar);
  480. con = stoi(_con);
  481. }
  482. if (httpd_query_key_value(_query, "sat", _valuechar, 30) == ESP_OK) {
  483. _sat = std::string(_valuechar);
  484. sat = stoi(_sat);
  485. }
  486. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  487. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  488. Camera.SetBrightnessContrastSaturation(bri, con, sat);
  489. Camera.SetLEDIntensity(intens);
  490. ESP_LOGD(TAG, "test_take - vor MakeImage");
  491. std::string zw = tfliteflow.doSingleStep("[MakeImage]", _host);
  492. httpd_resp_send(req, zw.c_str(), zw.length());
  493. }
  494. if (_task.compare("test_align") == 0)
  495. {
  496. std::string _host = "";
  497. if (httpd_query_key_value(_query, "host", _valuechar, 30) == ESP_OK) {
  498. _host = std::string(_valuechar);
  499. }
  500. // ESP_LOGD(TAG, "Parameter host: %s", _host.c_str());
  501. // string zwzw = "Do " + _task + " start\n"; ESP_LOGD(TAG, zwzw.c_str());
  502. std::string zw = tfliteflow.doSingleStep("[Alignment]", _host);
  503. httpd_resp_send(req, zw.c_str(), zw.length());
  504. }
  505. #ifdef DEBUG_DETAIL_ON
  506. LogFile.WriteHeapInfo("handler_editflow - Done");
  507. #endif
  508. return ESP_OK;
  509. }
  510. esp_err_t handler_statusflow(httpd_req_t *req)
  511. {
  512. #ifdef DEBUG_DETAIL_ON
  513. LogFile.WriteHeapInfo("handler_prevalue - Start");
  514. #endif
  515. if (FlowInitDone)
  516. {
  517. const char* resp_str;
  518. #ifdef DEBUG_DETAIL_ON
  519. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  520. #endif
  521. string* zw = tfliteflow.getActStatus();
  522. resp_str = zw->c_str();
  523. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  524. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  525. }
  526. else
  527. {
  528. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "Flow not (yet) started: REST API /flowstatus not available!");
  529. return ESP_ERR_NOT_FOUND;
  530. }
  531. #ifdef DEBUG_DETAIL_ON
  532. LogFile.WriteHeapInfo("handler_prevalue - Done");
  533. #endif
  534. return ESP_OK;
  535. }
  536. esp_err_t handler_cputemp(httpd_req_t *req)
  537. {
  538. #ifdef DEBUG_DETAIL_ON
  539. LogFile.WriteHeapInfo("handler_cputemp - Start");
  540. #endif
  541. const char* resp_str;
  542. char cputemp[20];
  543. sprintf(cputemp, "%4.1f°C", temperatureRead());
  544. resp_str = cputemp;
  545. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  546. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  547. #ifdef DEBUG_DETAIL_ON
  548. LogFile.WriteHeapInfo("handler_cputemp - End");
  549. #endif
  550. return ESP_OK;
  551. }
  552. esp_err_t handler_rssi(httpd_req_t *req)
  553. {
  554. #ifdef DEBUG_DETAIL_ON
  555. LogFile.WriteHeapInfo("handler_rssi - Start");
  556. #endif
  557. if (getWIFIisConnected())
  558. {
  559. const char* resp_str;
  560. char rssi[20];
  561. sprintf(rssi, "%idBm", get_WIFI_RSSI());
  562. resp_str = rssi;
  563. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  564. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  565. }
  566. else
  567. {
  568. httpd_resp_send_err(req, HTTPD_403_FORBIDDEN, "WIFI not (yet) connected: REST API /rssi not available!");
  569. return ESP_ERR_NOT_FOUND;
  570. }
  571. #ifdef DEBUG_DETAIL_ON
  572. LogFile.WriteHeapInfo("handler_rssi - End");
  573. #endif
  574. return ESP_OK;
  575. }
  576. esp_err_t handler_uptime(httpd_req_t *req)
  577. {
  578. #ifdef DEBUG_DETAIL_ON
  579. LogFile.WriteHeapInfo("handler_uptime - Start");
  580. #endif
  581. std::string formatedUptime = getFormatedUptime(false);
  582. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  583. httpd_resp_send(req, formatedUptime.c_str(), formatedUptime.length());
  584. #ifdef DEBUG_DETAIL_ON
  585. LogFile.WriteHeapInfo("handler_uptime - End");
  586. #endif
  587. return ESP_OK;
  588. }
  589. esp_err_t handler_prevalue(httpd_req_t *req)
  590. {
  591. #ifdef DEBUG_DETAIL_ON
  592. LogFile.WriteHeapInfo("handler_prevalue - Start");
  593. #endif
  594. const char* resp_str;
  595. string zw;
  596. #ifdef DEBUG_DETAIL_ON
  597. ESP_LOGD(TAG, "handler_prevalue: %s", req->uri);
  598. #endif
  599. char _query[100];
  600. char _size[10] = "";
  601. char _numbers[50] = "default";
  602. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  603. {
  604. #ifdef DEBUG_DETAIL_ON
  605. ESP_LOGD(TAG, "Query: %s", _query);
  606. #endif
  607. if (httpd_query_key_value(_query, "value", _size, 10) == ESP_OK)
  608. {
  609. #ifdef DEBUG_DETAIL_ON
  610. ESP_LOGD(TAG, "Value: %s", _size);
  611. #endif
  612. }
  613. httpd_query_key_value(_query, "numbers", _numbers, 50);
  614. }
  615. if (strlen(_size) == 0)
  616. {
  617. zw = tfliteflow.GetPrevalue(std::string(_numbers));
  618. }
  619. else
  620. {
  621. zw = "SetPrevalue to " + tfliteflow.UpdatePrevalue(_size, _numbers, true);
  622. }
  623. resp_str = zw.c_str();
  624. httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  625. httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
  626. #ifdef DEBUG_DETAIL_ON
  627. LogFile.WriteHeapInfo("handler_prevalue - End");
  628. #endif
  629. return ESP_OK;
  630. }
  631. void task_autodoFlow(void *pvParameter)
  632. {
  633. int64_t fr_start, fr_delta_ms;
  634. if (!isPlannedReboot)
  635. {
  636. if (esp_reset_reason() == ESP_RST_PANIC) {
  637. 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!");
  638. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Setting logfile level to DEBUG until the next reboot!");
  639. LogFile.setLogLevel(ESP_LOG_DEBUG);
  640. //MQTTPublish(GetMQTTMainTopic() + "/" + "status", "Postponing first round", false);
  641. vTaskDelay(60*5000 / portTICK_RATE_MS); // Wait 5 minutes to give time to do an OTA Update or fetch the log
  642. }
  643. }
  644. ESP_LOGD(TAG, "task_autodoFlow: start");
  645. doInit();
  646. auto_isrunning = tfliteflow.isAutoStart(auto_intervall);
  647. if (isSetupModusActive()) {
  648. auto_isrunning = false;
  649. std::string zw_time = getCurrentTimeString(LOGFILE_TIME_FORMAT);
  650. tfliteflow.doFlowMakeImageOnly(zw_time);
  651. }
  652. while (auto_isrunning)
  653. {
  654. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "----------------------------------------------------------------"); // Clear separation between runs
  655. std::string _zw = "Round #" + std::to_string(++countRounds) + " started";
  656. time_t roundStartTime = getUpTime();
  657. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _zw);
  658. fr_start = esp_timer_get_time();
  659. if (flowisrunning)
  660. {
  661. #ifdef DEBUG_DETAIL_ON
  662. ESP_LOGD(TAG, "Autoflow: doFlow is already running!");
  663. #endif
  664. }
  665. else
  666. {
  667. #ifdef DEBUG_DETAIL_ON
  668. ESP_LOGD(TAG, "Autoflow: doFlow is started");
  669. #endif
  670. flowisrunning = true;
  671. doflow();
  672. #ifdef DEBUG_DETAIL_ON
  673. ESP_LOGD(TAG, "Remove older log files");
  674. #endif
  675. LogFile.RemoveOldLogFile();
  676. LogFile.RemoveOldDataLog();
  677. }
  678. //CPU Temp
  679. float cputmp = temperatureRead();
  680. std::stringstream stream;
  681. stream << std::fixed << std::setprecision(1) << cputmp;
  682. string zwtemp = "CPU Temperature: " + stream.str();
  683. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, zwtemp);
  684. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Round #" + std::to_string(countRounds) +
  685. " completed (" + std::to_string(getUpTime() - roundStartTime) + " seconds)");
  686. fr_delta_ms = (esp_timer_get_time() - fr_start) / 1000;
  687. if (auto_intervall > fr_delta_ms)
  688. {
  689. const TickType_t xDelay = (auto_intervall - fr_delta_ms) / portTICK_PERIOD_MS;
  690. ESP_LOGD(TAG, "Autoflow: sleep for: %ldms", (long) xDelay);
  691. vTaskDelay( xDelay );
  692. }
  693. }
  694. vTaskDelete(NULL); //Delete this task if it exits from the loop above
  695. xHandletask_autodoFlow = NULL;
  696. ESP_LOGD(TAG, "task_autodoFlow: end");
  697. }
  698. void TFliteDoAutoStart()
  699. {
  700. BaseType_t xReturned;
  701. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  702. xReturned = xTaskCreatePinnedToCore(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow, 0);
  703. //xReturned = xTaskCreate(&task_autodoFlow, "task_autodoFlow", 16 * 1024, NULL, tskIDLE_PRIORITY+2, &xHandletask_autodoFlow);
  704. if( xReturned != pdPASS )
  705. {
  706. ESP_LOGD(TAG, "ERROR task_autodoFlow konnte nicht erzeugt werden!");
  707. }
  708. ESP_LOGD(TAG, "getESPHeapInfo: %s", getESPHeapInfo().c_str());
  709. }
  710. #ifdef ENABLE_MQTT
  711. std::string GetMQTTMainTopic()
  712. {
  713. return tfliteflow.GetMQTTMainTopic();
  714. }
  715. #endif//ENABLE_MQTT
  716. void register_server_tflite_uri(httpd_handle_t server)
  717. {
  718. ESP_LOGI(TAG, "server_part_camera - Registering URI handlers");
  719. httpd_uri_t camuri = { };
  720. camuri.method = HTTP_GET;
  721. camuri.uri = "/doinit";
  722. camuri.handler = handler_init;
  723. camuri.user_ctx = (void*) "Light On";
  724. httpd_register_uri_handler(server, &camuri);
  725. // Legacy API => New: "/setPreValue"
  726. camuri.uri = "/setPreValue.html";
  727. camuri.handler = handler_prevalue;
  728. camuri.user_ctx = (void*) "Prevalue";
  729. httpd_register_uri_handler(server, &camuri);
  730. camuri.uri = "/setPreValue";
  731. camuri.handler = handler_prevalue;
  732. camuri.user_ctx = (void*) "Prevalue";
  733. httpd_register_uri_handler(server, &camuri);
  734. camuri.uri = "/flow_start";
  735. camuri.handler = handler_flow_start;
  736. camuri.user_ctx = (void*) "Flow Start";
  737. httpd_register_uri_handler(server, &camuri);
  738. camuri.uri = "/statusflow.html";
  739. camuri.handler = handler_statusflow;
  740. camuri.user_ctx = (void*) "Light Off";
  741. httpd_register_uri_handler(server, &camuri);
  742. camuri.uri = "/statusflow";
  743. camuri.handler = handler_statusflow;
  744. camuri.user_ctx = (void*) "Light Off";
  745. httpd_register_uri_handler(server, &camuri);
  746. // Legacy API => New: "/cpu_temperature"
  747. camuri.uri = "/cputemp.html";
  748. camuri.handler = handler_cputemp;
  749. camuri.user_ctx = (void*) "Light Off";
  750. httpd_register_uri_handler(server, &camuri);
  751. camuri.uri = "/cpu_temperature";
  752. camuri.handler = handler_cputemp;
  753. camuri.user_ctx = (void*) "Light Off";
  754. httpd_register_uri_handler(server, &camuri);
  755. // Legacy API => New: "/rssi"
  756. camuri.uri = "/rssi.html";
  757. camuri.handler = handler_rssi;
  758. camuri.user_ctx = (void*) "Light Off";
  759. httpd_register_uri_handler(server, &camuri);
  760. camuri.uri = "/rssi";
  761. camuri.handler = handler_rssi;
  762. camuri.user_ctx = (void*) "Light Off";
  763. httpd_register_uri_handler(server, &camuri);
  764. camuri.uri = "/uptime";
  765. camuri.handler = handler_uptime;
  766. camuri.user_ctx = (void*) "Light Off";
  767. httpd_register_uri_handler(server, &camuri);
  768. camuri.uri = "/editflow";
  769. camuri.handler = handler_editflow;
  770. camuri.user_ctx = (void*) "EditFlow";
  771. httpd_register_uri_handler(server, &camuri);
  772. // Legacy API => New: "/value"
  773. camuri.uri = "/value.html";
  774. camuri.handler = handler_wasserzaehler;
  775. camuri.user_ctx = (void*) "Value";
  776. httpd_register_uri_handler(server, &camuri);
  777. camuri.uri = "/value";
  778. camuri.handler = handler_wasserzaehler;
  779. camuri.user_ctx = (void*) "Value";
  780. httpd_register_uri_handler(server, &camuri);
  781. // Legacy API => New: "/value"
  782. camuri.uri = "/wasserzaehler.html";
  783. camuri.handler = handler_wasserzaehler;
  784. camuri.user_ctx = (void*) "Wasserzaehler";
  785. httpd_register_uri_handler(server, &camuri);
  786. camuri.uri = "/json";
  787. camuri.handler = handler_json;
  788. camuri.user_ctx = (void*) "JSON";
  789. httpd_register_uri_handler(server, &camuri);
  790. camuri.uri = "/heap";
  791. camuri.handler = handler_get_heap;
  792. camuri.user_ctx = (void*) "Heap";
  793. httpd_register_uri_handler(server, &camuri);
  794. }