MeshHdl.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. //////////////////////////////////////////////////////////////////////////////////////
  3. //////////////////////////////////////////
  4. /////////////////
  5. //
  6. // Global Include Files
  7. //
  8. //
  9. //
  10. #if 0
  11. #include <WiFiUdp.h>
  12. #include <ArduinoOTA.h>
  13. #include <ESP8266WiFi.h>
  14. //////////////////////////////////////////////////////////////////////////////////////
  15. //////////////////////////////////////////
  16. /////////////////
  17. //
  18. // Include Files from local modules
  19. //
  20. //
  21. #include "globalDefs.h"
  22. #include "DebugHdl.h"
  23. #include "views/cViewHdl.h"
  24. #include "DisplayHdl.h"
  25. //////////////////////////////////////////////////////////////////////////////////////
  26. //////////////////////////////////////////
  27. /////////////////
  28. //
  29. // Class instanciation, external declarations and defines
  30. //
  31. //
  32. const char *ssid = "WLAN-LX"; // <- SSID here
  33. const char *password = "SP-RAXX749x7"; // <- Password here
  34. const char *server = "192.168.1.10"; // the IP address of your HA server here
  35. const char *hostName = "ESP8266-Touch"; // HostName for mDNS resolution
  36. uint16_t port = 8123; // the port of your HA server here
  37. // Place your Long Lived Access Token from HA here:
  38. const char *authtoken = " ";
  39. /** OTA progress */
  40. int otaStatus = 0;
  41. //////////////////////////////////////////////////////////////////////////////////////
  42. //////////////////////////////////////////
  43. /////////////////
  44. //
  45. // Capture Button actions
  46. //
  47. //
  48. class cMeshHdl
  49. {
  50. public:
  51. cMeshHdl(){};
  52. ~cMeshHdl(){};
  53. bool init()
  54. {
  55. LOGMSG(INFO, "[INFO]: Connecting to WLAN-LX");
  56. WiFi.mode(WIFI_STA);
  57. LOGMSG(INFO, "[WIFI]: Board MAC-Address: ", WiFi.macAddress().c_str());
  58. WiFi.begin(ssid, password);
  59. while (WiFi.status() != WL_CONNECTED)
  60. {
  61. delay(500);
  62. Serial.print('.');
  63. }
  64. Serial.println("");
  65. LOGMSG(INFO, "[WIFI]: Connected! IP address: ", WiFi.localIP().toString().c_str());
  66. // LOGMSG(INFO | SRC,WiFi.localIP().toString().c_str());
  67. // std::cout << WiFi.localIP().toString().c_str() << endl;
  68. // Port defaults to 8266
  69. // ArduinoOTA.setPort(8266);
  70. // Hostname defaults to esp8266-[ChipID]
  71. // ArduinoOTA.setHostname("myesp8266");
  72. // No authentication by default
  73. // ArduinoOTA.setPassword((const char *)"123");
  74. ArduinoOTA.setHostname(hostName);
  75. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  76. *
  77. */
  78. ArduinoOTA.onStart([]()
  79. {
  80. Serial.println("Start");
  81. tft.fillScreen(TFT_NAVY);
  82. delay(10);
  83. tft.fillScreen(TFT_NAVY);
  84. tft.setTextDatum(TL_DATUM);
  85. tft.setTextColor(TFT_WHITE);
  86. tft.setFreeFont(&FreeSansBold12pt7b); // Choose a nicefont that fits box
  87. tft.drawString("OTA",10,30);
  88. tft.drawString("Progress:",10,60); });
  89. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  90. *
  91. */
  92. ArduinoOTA.onEnd([]()
  93. {
  94. Serial.println("\nEnd");
  95. // Clear the screen and print OTA finished text
  96. tft.fillScreen(TFT_GREEN);
  97. delay(10);
  98. tft.fillScreen(TFT_GREEN);
  99. tft.setTextDatum(TL_DATUM);
  100. tft.setTextColor(TFT_BLACK);
  101. tft.setFreeFont(&FreeSansBold12pt7b); // Choose a nicefont that fits box
  102. tft.drawString("OTA",10,30);
  103. tft.drawString("FINISHED!",10,60);
  104. delay(1000); });
  105. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  106. *
  107. */
  108. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
  109. {
  110. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  111. // Calculate progress
  112. // unsigned int achieved = progress / (total / 100); // caused unsigned / signed warning message
  113. int achieved = progress / (total / 100);
  114. // Update progress every 1 %
  115. if (otaStatus == 0 || achieved == otaStatus + 1) {
  116. // Toggle the LED
  117. // digitalWrite(16, !digitalRead(16));
  118. otaStatus = achieved;
  119. // Print the progress
  120. // tft.setFreeFont(&FreeSansBold12pt7b); // Choose a nicefont that fits box
  121. tft.setTextDatum(TL_DATUM);
  122. // tft.setTextSize(1);
  123. tft.fillRect(120,100,80,30,TFT_NAVY);
  124. delay(10);
  125. String progVal = String(achieved) + "%";
  126. tft.drawString(progVal,125,100);
  127. } });
  128. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  129. *
  130. */
  131. ArduinoOTA.onError([](ota_error_t error)
  132. {
  133. Serial.printf("Error[%u]: ", error);
  134. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  135. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  136. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  137. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  138. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  139. // Clear the screen for error message
  140. tft.fillScreen(TFT_RED);
  141. tft.setTextDatum(TL_DATUM);
  142. tft.setTextColor(TFT_WHITE);
  143. // Print error message
  144. tft.setTextSize(1);
  145. tft.drawString("OTA",10,30);
  146. tft.drawString("ERROR:",10,60);
  147. // Get detailed error and print error reason
  148. if (error == OTA_AUTH_ERROR) {
  149. tft.drawString("Auth Failed",10,120);
  150. }
  151. else if (error == OTA_BEGIN_ERROR) {
  152. tft.drawString("Begin Failed",10,120);
  153. }
  154. else if (error == OTA_CONNECT_ERROR) {
  155. tft.drawString("Connect Failed",10,120);
  156. }
  157. else if (error == OTA_RECEIVE_ERROR) {
  158. tft.drawString("Receive Failed",10,120);
  159. }
  160. else if (error == OTA_END_ERROR) {
  161. tft.drawString("End Failed",10,120);
  162. }
  163. otaStatus = 0; });
  164. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  165. *
  166. */
  167. ArduinoOTA.begin();
  168. return FNC_SUCCESS;
  169. }
  170. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  171. *
  172. */
  173. void showConfiguration()
  174. {
  175. LOGMSG(INFO, "[MESH]: MESH initialized");
  176. }
  177. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  178. *
  179. */
  180. void run()
  181. {
  182. ArduinoOTA.handle();
  183. }
  184. }; // end of class cMeshHdl
  185. #endif