server_tflite.cpp 32 KB

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