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