WiFiAccessPoint.ino 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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: WIFI AP. wifi热点
  10. * Date: 2021/7/29
  11. *******************************************************************************
  12. WiFiAccessPoint.ino creates a WiFi access point and provides a web server on
  13. it. 创建一个WiFi接入点,并在其上提供一个网络服务器 And can send requests to
  14. M5Core through the web page 并可通过网页向M5Core发送请求
  15. */
  16. #include <M5Stack.h>
  17. #include <WiFi.h>
  18. #include <WiFiAP.h>
  19. #include <WiFiClient.h>
  20. // Set these to your desired credentials. 设置你的热点名称和密码
  21. const char *ssid = "M5Stack_Ap";
  22. const char *password = "66666666";
  23. WiFiServer server(80);
  24. void setup() {
  25. M5.begin(); // Init M5Stack. 初始化M5Stack
  26. M5.Power.begin(); // Init power 初始化电源模块
  27. M5.lcd.setTextSize(2); // Set text size to 2. 设置字号大小为2
  28. M5.lcd.println(
  29. "WIFI ACCESS POINT"); // Screen print string. 屏幕打印字符串.
  30. M5.lcd.printf("Please connect:%s \nThen access to:", ssid);
  31. WiFi.softAP(
  32. ssid,
  33. password); // You can remove the password parameter if you want the AP
  34. // to be open. 如果你想建立开放式热点,可以删除密码
  35. IPAddress myIP = WiFi.softAPIP(); // Get the softAP interface IP address.
  36. // 获取AP接口IP地址
  37. M5.lcd.println(myIP);
  38. server.begin(); // Start the established Internet of Things network server.
  39. // 启动建立的物联网网络服务器
  40. }
  41. void loop() {
  42. WiFiClient client =
  43. server
  44. .available(); // listen for incoming clients.
  45. // 检查有没有设备通过网络向M5Stack网络服务器发送请求
  46. if (client) { // if you get a client. 如果收到请求
  47. M5.lcd.print("New Client:");
  48. String currentLine =
  49. ""; // make a String to hold incoming data from the client.
  50. // 创建一个String来保存来自客户端的传入数据
  51. while (client.connected()) { // loop while the client's
  52. // connected,continuously receiving data.
  53. // 在客户端连接时进行循环,不断接收数据
  54. if (client.available()) { // if there's bytes to read from the
  55. // client. 如果有数据可读取
  56. char c =
  57. client.read(); // store the read a byte. 存储读取到的数据
  58. Serial.write(c);
  59. if (c == '\n') { // if the byte is a newline character.
  60. // 如果读取到的字节为换行符
  61. // \n is the end of the client'S HTTP request, indicating
  62. // that the client has sent a new request \n
  63. // 是客户端HTTP请求的结尾,说明客户端发来新请求:
  64. if (currentLine.length() == 0) {
  65. // Here are the instructions to create a page.
  66. // 下面是创建一个页面的指令
  67. // HTTP headers always start with a response code (e.g.
  68. // HTTP/1.1 200 OK) HTTP的开头总是以响应代码开始(例如
  69. // HTTP/1.1 200 OK) and a content-type so the client
  70. // knows what's coming, then a blank line:
  71. //然后是content-type,这样客户端就知道接下来会发生什么,然后是空行:
  72. client.println("HTTP/1.1 200 OK");
  73. client.println("Content-type:text/html");
  74. client.println();
  75. // the content of the HTTP response follows the header:
  76. // HTTP页面显示的内容跟在开头后面:
  77. // /High and /Low are the data received when clicking
  78. // the corresponding connection, which can be replaced.
  79. // /High和/Low 为点击对应连接时接收到的数据,可更换
  80. client.print(
  81. "Click <a href=\"/High\">here</a> to turn ON the "
  82. "LED.<br>");
  83. client.print(
  84. "Click <a href=\"/Low\">here</a> to turn OFF the "
  85. "LED.<br>");
  86. // The HTTP response ends with another blank line:
  87. // HTTP响应以空行结束:
  88. client.println();
  89. // break out of the while loop:退出循环
  90. break;
  91. } else { // if you got a newline, then clear
  92. // currentLine:如果得到新的一行,那么清除当前行
  93. currentLine = "";
  94. }
  95. } else if (c !=
  96. '\r') { // if you got anything else but a carriage
  97. // return character.
  98. // 如果你得到了除了回车符以外的其他字符,
  99. currentLine += c; // add it to the end of the currentLine.
  100. // 将它添加到currentLine的末尾
  101. }
  102. // Check to see if the client request was "GET /H" or "GET /L":
  103. // 检查客户端请求是“GET /High”还是“GET /Low”:
  104. if (currentLine.endsWith("GET /High")) {
  105. M5.Lcd.print("ON\n");
  106. } else if (currentLine.endsWith("GET /Low")) {
  107. M5.Lcd.print("OFF\n");
  108. }
  109. }
  110. }
  111. client.stop(); // close the connection. 关闭连接
  112. }
  113. }