server_tflite.cpp 32 KB

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