OTAUpload.ino 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: OTA Upload. 隔空传输程序
  10. * Date: 2021/7/30
  11. *******************************************************************************
  12. PC and M5Core can only be used on the same wifi.
  13. 电脑和M5Core需在同一wifi下才可使用 When the OTA is ready, restart the Arduino
  14. client from Tools > Ports > Network ports to instantly transmit the program
  15. wirelessly. OTA 准备好后重启Arduino客户端在工具->端口->网络端口,即刻无线传输程序
  16. */
  17. #include <ArduinoOTA.h>
  18. #include <M5Stack.h>
  19. #include <WiFi.h>
  20. // Set the name and password of the wifi to be connected.
  21. // 配置所连接wifi的名称和密码
  22. const char* ssid = "M5wifi";
  23. const char* password = "1234";
  24. void setup() {
  25. M5.begin(); // Init M5Core. 初始化 M5Core
  26. M5.Power.begin();
  27. WiFi.begin(ssid, password); // Connect wifi and return connection status.
  28. // 连接wifi并返回连接状态
  29. M5.lcd.print("Waiting Wifi Connect");
  30. while (WiFi.status() !=
  31. WL_CONNECTED) { // If the wifi connection fails. 若wifi未连接成功
  32. delay(1000);
  33. M5.lcd.print(".");
  34. }
  35. M5.lcd.println("\nWiFi Connected!");
  36. M5.lcd.print("WiFi Connect To: ");
  37. M5.lcd.println(WiFi.SSID()); // Output Network name. 输出网络名称
  38. M5.lcd.print("IP address: ");
  39. M5.lcd.println(WiFi.localIP()); // Output IP Address. 输出IP地址
  40. ArduinoOTA.setHostname(
  41. "M5Core"); // Set the network port name. 设置网络端口名称
  42. ArduinoOTA.setPassword("666666"); // Set the network port connection
  43. // password. 设置网络端口连接的密码
  44. ArduinoOTA.begin(); // Initialize the OTA. 初始化OTA
  45. M5.lcd.println("OTA ready!"); // M5.lcd port output format string.
  46. // 串口输出格式化字符串
  47. }
  48. void loop() {
  49. ArduinoOTA.handle(); // Continuously check for update requests.
  50. // 持续检测是否有更新请求
  51. M5.update();
  52. if (M5.BtnA.isPressed()) { // if BtnA is Pressed. 如果按键A按下
  53. ArduinoOTA.end(); // Ends the ArduinoOTA service. 结束OTA服务
  54. M5.lcd.println("OTA End!");
  55. delay(200);
  56. }
  57. }