当前位置:   article > 正文

ESP32使用+Arduino编程:WIFI连接_esp32 arduino wifi服务端客户端通信

esp32 arduino wifi服务端客户端通信

一、首先烧录一个代码 

1.没有可以烧录以下示例

2.也可以CV以下代码

  1. /*
  2. WiFi Web Server LED Blink
  3. A simple web server that lets you blink an LED via the web.
  4. This sketch will print the IP address of your WiFi Shield (once connected)
  5. to the Serial monitor. From there, you can open that address in a web browser
  6. to turn on and off the LED on pin 5.
  7. If the IP address of your shield is yourAddress:
  8. http://yourAddress/H turns the LED on
  9. http://yourAddress/L turns it off
  10. This example is written for a network using WPA2 encryption. For insecure
  11. WEP or WPA, change the Wifi.begin() call and use Wifi.setMinSecurity() accordingly.
  12. Circuit:
  13. * WiFi shield attached
  14. * LED attached to pin 5
  15. created for arduino 25 Nov 2012
  16. by Tom Igoe
  17. ported for sparkfun esp32
  18. 31.01.2017 by Jan Hendrik Berlin
  19. */
  20. #include <WiFi.h>
  21. const char* ssid = "yourssid";
  22. const char* password = "yourpasswd";
  23. WiFiServer server(80);
  24. void setup()
  25. {
  26. Serial.begin(115200);
  27. pinMode(5, OUTPUT); // set the LED pin mode
  28. delay(10);
  29. // We start by connecting to a WiFi network
  30. Serial.println();
  31. Serial.println();
  32. Serial.print("Connecting to ");
  33. Serial.println(ssid);
  34. WiFi.begin(ssid, password);
  35. while (WiFi.status() != WL_CONNECTED) {
  36. delay(500);
  37. Serial.print(".");
  38. }
  39. Serial.println("");
  40. Serial.println("WiFi connected.");
  41. Serial.println("IP address: ");
  42. Serial.println(WiFi.localIP());
  43. server.begin();
  44. }
  45. void loop(){
  46. WiFiClient client = server.available(); // listen for incoming clients
  47. if (client) { // if you get a client,
  48. Serial.println("New Client."); // print a message out the serial port
  49. String currentLine = ""; // make a String to hold incoming data from the client
  50. while (client.connected()) { // loop while the client's connected
  51. if (client.available()) { // if there's bytes to read from the client,
  52. char c = client.read(); // read a byte, then
  53. Serial.write(c); // print it out the serial monitor
  54. if (c == '\n') { // if the byte is a newline character
  55. // if the current line is blank, you got two newline characters in a row.
  56. // that's the end of the client HTTP request, so send a response:
  57. if (currentLine.length() == 0) {
  58. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  59. // and a content-type so the client knows what's coming, then a blank line:
  60. client.println("HTTP/1.1 200 OK");
  61. client.println("Content-type:text/html");
  62. client.println();
  63. // the content of the HTTP response follows the header:
  64. client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");
  65. client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");
  66. // The HTTP response ends with another blank line:
  67. client.println();
  68. // break out of the while loop:
  69. break;
  70. } else { // if you got a newline, then clear currentLine:
  71. currentLine = "";
  72. }
  73. } else if (c != '\r') { // if you got anything else but a carriage return character,
  74. currentLine += c; // add it to the end of the currentLine
  75. }
  76. // Check to see if the client request was "GET /H" or "GET /L":
  77. if (currentLine.endsWith("GET /H")) {
  78. digitalWrite(5, HIGH); // GET /H turns the LED on
  79. }
  80. if (currentLine.endsWith("GET /L")) {
  81. digitalWrite(5, LOW); // GET /L turns the LED off
  82. }
  83. }
  84. }
  85. // close the connection:
  86. client.stop();
  87. Serial.println("Client Disconnected.");
  88. }
  89. }

3、 改WiFi名和密码

二、如何WiFi连接成功:

1.选择好串口号

2.编译和烧录

3.烧录成功后打开串口监视器

4.按下esp32的复位键

这里一开始连接不上

原因是:设备被我用手机禁用

还有的可能原因:热点应该设置为2.4不然连不上 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号