OTA_OLED.ino 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * HelTec Automation(TM) WIFI_LoRa_32 factory test code, witch includ
  3. * follow functions:
  4. *
  5. * - Basic OLED function test;
  6. *
  7. * - Basic serial port test(in baud rate 115200);
  8. *
  9. * - Basic LED test;
  10. *
  11. * - WIFI join and scan test;
  12. *
  13. * - ArduinoOTA By Wifi;
  14. *
  15. * - Timer test and some other Arduino basic functions.
  16. *
  17. * by lxyzn from HelTec AutoMation, ChengDu, China
  18. * 成都惠利特自动化科技有限公司
  19. * www.heltec.cn
  20. *
  21. * this project also realess in GitHub:
  22. * https://github.com/HelTecAutomation/Heltec_ESP32
  23. */
  24. #include <ArduinoOTA.h>
  25. #include <WiFi.h>
  26. #include <Wire.h>
  27. #include "heltec.h"
  28. /********************************************** WIFI Client 注意编译时要设置此值 *********************************
  29. * wifi client
  30. */
  31. const char* ssid = "xxxxxx"; //replace "xxxxxx" with your WIFI's ssid
  32. const char* password = "xxxxxx"; //replace "xxxxxx" with your WIFI's password
  33. //WiFi&OTA 参数
  34. //#define HOSTNAME "HelTec_OTA_OLED"
  35. #define PASSWORD "HT.123456" //the password for OTA upgrade, can set it in any char you want
  36. /************************************************ 注意编译时要设置此值 *********************************
  37. * 是否使用静态IP
  38. */
  39. #define USE_STATIC_IP false
  40. #if USE_STATIC_IP
  41. IPAddress staticIP(192,168,1,22);
  42. IPAddress gateway(192,168,1,9);
  43. IPAddress subnet(255,255,255,0);
  44. IPAddress dns1(8, 8, 8, 8);
  45. IPAddress dns2(114,114,114,114);
  46. #endif
  47. /*******************************************************************
  48. * OLED Arguments
  49. */
  50. //#define RST_OLED 16 //OLED Reset引脚,需要手动Reset,否则不显示
  51. #define OLED_UPDATE_INTERVAL 500 //OLED屏幕刷新间隔ms
  52. //SSD1306 display(0x3C, 4, 15); //引脚4,15是绑定在Kit 32的主板上的,不能做其它用
  53. /********************************************************************
  54. * OTA升级配置
  55. */
  56. void setupOTA()
  57. {
  58. //Port defaults to 8266
  59. //ArduinoOTA.setPort(8266);
  60. //Hostname defaults to esp8266-[ChipID]
  61. // ArduinoOTA.setHostname(HOSTNAME);
  62. //No authentication by default
  63. ArduinoOTA.setPassword(PASSWORD);
  64. //Password can be set with it's md5 value as well
  65. //MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  66. //ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  67. ArduinoOTA.onStart([]()
  68. {
  69. Heltec.display->clear();
  70. Heltec.display->setFont(ArialMT_Plain_10); //设置字体大小
  71. Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);//设置字体对齐方式
  72. Heltec.display->drawString(0, 0, "Start Updating....");
  73. Serial.printf("Start Updating....Type:%s\n", (ArduinoOTA.getCommand() == U_FLASH) ? "sketch" : "filesystem");
  74. });
  75. ArduinoOTA.onEnd([]()
  76. {
  77. Heltec.display->clear();
  78. Heltec.display->drawString(0, 0, "Update Complete!");
  79. Serial.println("Update Complete!");
  80. ESP.restart();
  81. });
  82. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
  83. {
  84. String pro = String(progress / (total / 100)) + "%";
  85. int progressbar = (progress / (total / 100));
  86. //int progressbar = (progress / 5) % 100;
  87. //int pro = progress / (total / 100);
  88. Heltec.display->clear();
  89. Heltec.display->drawProgressBar(0, 32, 120, 10, progressbar); // draw the progress bar
  90. Heltec.display->setTextAlignment(TEXT_ALIGN_CENTER); // draw the percentage as String
  91. Heltec.display->drawString(64, 15, pro);
  92. Heltec.display->display();
  93. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  94. });
  95. ArduinoOTA.onError([](ota_error_t error)
  96. {
  97. Serial.printf("Error[%u]: ", error);
  98. String info = "Error Info:";
  99. switch(error)
  100. {
  101. case OTA_AUTH_ERROR:
  102. info += "Auth Failed";
  103. Serial.println("Auth Failed");
  104. break;
  105. case OTA_BEGIN_ERROR:
  106. info += "Begin Failed";
  107. Serial.println("Begin Failed");
  108. break;
  109. case OTA_CONNECT_ERROR:
  110. info += "Connect Failed";
  111. Serial.println("Connect Failed");
  112. break;
  113. case OTA_RECEIVE_ERROR:
  114. info += "Receive Failed";
  115. Serial.println("Receive Failed");
  116. break;
  117. case OTA_END_ERROR:
  118. info += "End Failed";
  119. Serial.println("End Failed");
  120. break;
  121. }
  122. Heltec.display->clear();
  123. Heltec.display->drawString(0, 0, info);
  124. ESP.restart();
  125. });
  126. ArduinoOTA.begin();
  127. }
  128. /********************************************************************
  129. * setup oled
  130. */
  131. void setupOLED()
  132. {
  133. pinMode(RST_OLED, OUTPUT);
  134. //复位OLED电路
  135. digitalWrite(RST_OLED, LOW); // turn D16 low to reset OLED
  136. delay(50);
  137. digitalWrite(RST_OLED, HIGH); // while OLED is running, must set D16 in high
  138. Heltec.display->init();
  139. Heltec.display->flipScreenVertically(); //倒过来显示内容
  140. Heltec.display->setFont(ArialMT_Plain_10); //设置字体大小
  141. Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);//设置字体对齐方式
  142. Heltec.display->clear();
  143. Heltec.display->drawString(0, 0, "Initialize...");
  144. }
  145. /*********************************************************************
  146. * setup wifi
  147. */
  148. void setupWIFI()
  149. {
  150. Heltec.display->clear();
  151. Heltec.display->drawString(0, 0, "Connecting...");
  152. Heltec.display->drawString(0, 10, String(ssid));
  153. Heltec.display->display();
  154. //连接WiFi,删除旧的配置,关闭WIFI,准备重新配置
  155. WiFi.disconnect(true);
  156. delay(1000);
  157. WiFi.mode(WIFI_STA);
  158. //WiFi.onEvent(WiFiEvent);
  159. WiFi.setAutoConnect(true);
  160. WiFi.setAutoReconnect(true); //断开WiFi后自动重新连接,ESP32不可用
  161. //WiFi.setHostname(HOSTNAME);
  162. WiFi.begin(ssid, password);
  163. #if USE_STATIC_IP
  164. WiFi.config(staticIP, gateway, subnet);
  165. #endif
  166. //等待5000ms,如果没有连接上,就继续往下
  167. //不然基本功能不可用
  168. byte count = 0;
  169. while(WiFi.status() != WL_CONNECTED && count < 10)
  170. {
  171. count ++;
  172. delay(500);
  173. Serial.print(".");
  174. }
  175. Heltec.display->clear();
  176. if(WiFi.status() == WL_CONNECTED)
  177. Heltec.display->drawString(0, 0, "Connecting...OK.");
  178. else
  179. Heltec.display->drawString(0, 0, "Connecting...Failed");
  180. Heltec.display->display();
  181. }
  182. /******************************************************
  183. * arduino setup
  184. */
  185. void setup()
  186. {
  187. Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, true /*Serial Enable*/);
  188. pinMode(25, OUTPUT);
  189. digitalWrite(25,HIGH);
  190. while (!Serial) {
  191. ; // wait for serial port to connect. Needed for native USB port only
  192. }
  193. Serial.println("Initialize...");
  194. setupOLED();
  195. setupWIFI();
  196. setupOTA();
  197. }
  198. /******************************************************
  199. * arduino loop
  200. */
  201. void loop()
  202. {
  203. ArduinoOTA.handle();
  204. unsigned long ms = millis();
  205. if(ms % 1000 == 0)
  206. {
  207. Serial.println("hello,OTA now");
  208. }
  209. }
  210. /****************************************************
  211. * [通用函数]ESP32 WiFi Kit 32事件处理
  212. */
  213. void WiFiEvent(WiFiEvent_t event)
  214. {
  215. Serial.printf("[WiFi-event] event: %d\n", event);
  216. switch(event)
  217. {
  218. case SYSTEM_EVENT_WIFI_READY: /**< ESP32 WiFi ready */
  219. break;
  220. case SYSTEM_EVENT_SCAN_DONE: /**< ESP32 finish scanning AP */
  221. break;
  222. case SYSTEM_EVENT_STA_START: /**< ESP32 station start */
  223. break;
  224. case SYSTEM_EVENT_STA_STOP: /**< ESP32 station stop */
  225. break;
  226. case SYSTEM_EVENT_STA_CONNECTED: /**< ESP32 station connected to AP */
  227. break;
  228. case SYSTEM_EVENT_STA_DISCONNECTED: /**< ESP32 station disconnected from AP */
  229. break;
  230. case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: /**< the auth mode of AP connected by ESP32 station changed */
  231. break;
  232. case SYSTEM_EVENT_STA_GOT_IP: /**< ESP32 station got IP from connected AP */
  233. case SYSTEM_EVENT_STA_LOST_IP: /**< ESP32 station lost IP and the IP is reset to 0 */
  234. break;
  235. case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: /**< ESP32 station wps succeeds in enrollee mode */
  236. case SYSTEM_EVENT_STA_WPS_ER_FAILED: /**< ESP32 station wps fails in enrollee mode */
  237. case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: /**< ESP32 station wps timeout in enrollee mode */
  238. case SYSTEM_EVENT_STA_WPS_ER_PIN: /**< ESP32 station wps pin code in enrollee mode */
  239. break;
  240. case SYSTEM_EVENT_AP_START: /**< ESP32 soft-AP start */
  241. case SYSTEM_EVENT_AP_STOP: /**< ESP32 soft-AP stop */
  242. case SYSTEM_EVENT_AP_STACONNECTED: /**< a station connected to ESP32 soft-AP */
  243. case SYSTEM_EVENT_AP_STADISCONNECTED: /**< a station disconnected from ESP32 soft-AP */
  244. case SYSTEM_EVENT_AP_PROBEREQRECVED: /**< Receive probe request packet in soft-AP interface */
  245. case SYSTEM_EVENT_AP_STA_GOT_IP6: /**< ESP32 station or ap interface v6IP addr is preferred */
  246. break;
  247. case SYSTEM_EVENT_ETH_START: /**< ESP32 ethernet start */
  248. case SYSTEM_EVENT_ETH_STOP: /**< ESP32 ethernet stop */
  249. case SYSTEM_EVENT_ETH_CONNECTED: /**< ESP32 ethernet phy link up */
  250. case SYSTEM_EVENT_ETH_DISCONNECTED: /**< ESP32 ethernet phy link down */
  251. case SYSTEM_EVENT_ETH_GOT_IP: /**< ESP32 ethernet got IP from connected AP */
  252. case SYSTEM_EVENT_MAX:
  253. break;
  254. }
  255. }