ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由

(3)、esp8266+UNO建立路由模式

(1)准备材料

esp8266、UNO、NodeMcu Lua、led、电阻 、导线、NodeMcu lua数据线和UNO数据线

(2)思路

在上一篇博文中提到WiFi控制led,我手里有两块ESP8266,如下图
所以我考虑将WiFi也换成自己搭建的。

(3)搭建WiFi

ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由_第1张图片

  1. 接线:

ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由_第2张图片
注意:这个esp8266要求的供电很精细!

  1. 代码

    #include
    SoftwareSerial mySerial(3, 2); // RX, TX

    void setup() {
    Serial.begin(9600);
    while (!Serial) {;}
    Serial.println(“hardware serial!”);
    mySerial.begin(115200);
    mySerial.println(“software seria”);
    }

    void loop() {
    if(mySerial.available())
    Serial.write(mySerial.read());

    if(Serial.available())
    mySerial.write(Serial.read());
    }

    注意:因为这个实验涉及两个开发板,所以在烧录代码时要确定好开发板选项和端口选项

烧录成功后,打开串口监听器,结束符设置为NL和CR,波特率设为9600 然后发送

AT+CWMODE=2 //设置esp8266为路由器模式

接着重启esp8266,发送

AT+RST //重启使路由模式生效

设置路由器的ssid和密码,发送

AT+CWSAP=“ESP8266”,“12345678”,11,2 //这里ESP8266是SSID 12345678是密码

OK,路由器完成了

(4)控制led(具体查看上篇博客)

ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由_第3张图片

  1. 连线:mcu通过数据线连接到电脑一个端口,D4脚位连接一个LED灯,需要一个保护电阻,注意要接地。
  2. 代码:

/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/?gpio=0 will set the GPIO2 low,
* http://server_ip/?gpio=1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/

#include 

//输入esp8266的ssid 和密码
const char* ssid = "ESP8266";
const char* password = "12345678";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/?gpio=0") != -1)
    val = 0;
  else if (req.indexOf("/?gpio=1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

烧录之前,确定好IDE的开发板选项选择NodeMCU 1.0 端口选择mcu连接的端口,不要烧录到你刚刚做好的路由器里面了。
烧录代码后,即可通过WIFI控制led。
打开串口监听器,要是未显示连接成功,可以按下板子上的reset键,记录好IP地址(我的是192.168.4.3)
浏览器输入192.168.4.3/?gpio=1 点亮,
输入192.168.4.3/?gpio=0 熄灭

ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由_第4张图片

ESP8266(NodeMcu Lua)学习 (3)建立路由(ESP8266-01学习)并测试该路由_第5张图片

你可能感兴趣的:(arduino学习)