WiFiTCP.ino 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 TCP.
  10. * Date: 2021/7/29
  11. *******************************************************************************
  12. M5Core will sends a message to a TCP server
  13. M5Core 将向TCP服务器发送一条数据
  14. */
  15. #include <M5Stack.h>
  16. #include <WiFi.h>
  17. #include <WiFiMulti.h>
  18. // Set the name and password of the wifi to be connected.
  19. // 配置所连接wifi的名称和密码
  20. const char *ssid = "cam";
  21. const char *password = "12345678";
  22. WiFiMulti WiFiMulti;
  23. void setup() {
  24. int sum = 0;
  25. M5.begin(); // Init M5Core. 初始化M5Core
  26. M5.Power.begin(); // Init power 初始化电源模块
  27. WiFiMulti.addAP(
  28. ssid,
  29. password); // Add wifi configuration information. 添加wifi配置信息
  30. M5.lcd.printf(
  31. "Waiting connect to WiFi: %s ...",
  32. ssid); // Serial port output format string. 串口输出格式化字符串
  33. while (WiFiMulti.run() !=
  34. WL_CONNECTED) { // If the connection to wifi is not established
  35. // successfully. 如果没有与wifi成功建立连接
  36. M5.lcd.print(".");
  37. delay(1000);
  38. sum += 1;
  39. if (sum == 8) M5.lcd.print("Conncet failed!");
  40. }
  41. M5.lcd.println("\nWiFi connected");
  42. M5.lcd.print("IP address: ");
  43. M5.lcd.println(WiFi.localIP()); // The serial port outputs the IP address
  44. // of the M5Core. 串口输出M5Core的IP地址
  45. delay(500);
  46. }
  47. void loop() {
  48. M5.lcd.setCursor(0, 40);
  49. const char *host = "www.baidu.com"; // Set the IP address or DNS of the TCP
  50. // server. 设置TCP服务器的ip或dns
  51. const uint16_t port =
  52. 80; // The port of the TCP server is specified. 设置TCP服务器的端口
  53. M5.lcd.printf("Connecting to: %s\n", host);
  54. WiFiClient client;
  55. if (!client.connect(
  56. host,
  57. port)) { // Connect to the server. 0 is returned if the
  58. // connection fails. 连接服务器,若连接失败返回0
  59. M5.lcd.print(
  60. "Connection failed.\nWaiting 5 seconds before retrying...\n");
  61. delay(5000);
  62. return;
  63. }
  64. // send an arbitrary string to the server. 发送一个字符串到上边连接的服务器
  65. client.print("Send this data to the server");
  66. // send a basic document request to the server.
  67. // 向服务器发送一个基本的文档请求.
  68. client.print("GET /index.html HTTP/1.1\n\n");
  69. int maxloops = 0;
  70. // wait for the server's reply to become available
  71. //等待服务器的回复
  72. while (!client.available() && maxloops < 1000) {
  73. maxloops++;
  74. delay(1); // delay 1 msec
  75. }
  76. if (client.available() >
  77. 0) { // Detects whether data is received. 检测是否接收到数据
  78. String line = client.readStringUntil(
  79. '\r'); // Read information from data received by the device until
  80. // \r is read. 从设备接收到的数据中读取信息,直至读取到\r时
  81. M5.lcd.println(line); // String received by serial port output.
  82. // 串口输出接收到的字符串
  83. } else {
  84. M5.lcd.println("client.available() timed out ");
  85. }
  86. M5.lcd.println("Closing connection.");
  87. client.stop();
  88. M5.lcd.println("Waiting 5 seconds before restarting...");
  89. delay(5000);
  90. M5.lcd.fillRect(0, 40, 320, 220, BLACK);
  91. }