server_tflite.cpp 25 KB

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