This is the 12th day of my participation in the genwen Challenge

introduce

The working principle of AP+STA is that when the module acts as an AP, it can also exist as an STA mode. For example, WiFi module acts as an AP, allowing customers’ mobile phones or computers to access. At the same time, the module acts as an STA to access routers or upper servers for data uploading. It should be noted that the MAC address of the module is different from that of the module when it is used as a STA. Therefore, the MAC address of the module when it is used as an AP in the module is different from that when it is used as a STA in the router.

Procedural steps

  1. Set the local IP address, gateway, subnet mask, and remote IP address

  2. Set the IP address, gateway, subnet mask, and upper remote IP address of the AP

  3. Set the local port and remote port

  4. Set the name and password for connecting to the remote AP

  5. This is the 10th day of my participation in the challenge. Check out the details of the challenge

  6. Setting the send/receive Buffer

  7. Initialize baud rate

  8. Disconnect (to prevent already connected) wifi.disconnect ()

  9. Wifi. mode(WiFi_AP_STA);

  10. Set AP network parameters

WiFi.softAPConfig()
Copy the code
  1. Set the AP account password
WiFi.softAP()
Copy the code
  1. Connect the specified route
WiFi.begin()
Copy the code
  1. Set local network parameters
WiFi.config()
Copy the code
  1. Waiting for routing connection
Serial. Println (please wait ") WiFi. The status (). =WL\_CONNECTEDCopy the code
  1. Establishes a UDP Server to listen on the specified port
udp.begin(localPort)
Copy the code
  1. Display IP address
WiFi.localIP()  
WiFi.softAPIP()
Copy the code
  1. Read the data
udp.read(buff,255)  
udp.beginPacket(serverip,remoteport)  
udp.write(buff,255)  
udp.endPacket()
Copy the code
  1. Clear buffer data
Memset (buff, 0255);Copy the code

The program

#include<ESP8266WiFi.h> #include<WiFiUdp.h> WiFiUDP udp; IPAddress sip,1,29 (192168); // local IP address IPAddress sip1(192,168,1,1); IPAddress sip2(255,255,255,0); // local subnet mask IPAddress xip(192,168,2,2); IPAddress lxip(192,168,2,1); //AP IP IPAddress lxip1(192,168,2,1); //AP end gateway IPAddress lxip2(255,255,255,0); IPAddress serverip(192,168,1,4); // Host remote IP unsigned int localPort=9999; // Unsigned int remotePort =9999; Const char* ssid="WIFI"; // The name of the remote AP to connect to const char* password="123456789"; // Password to connect to remote AP const char* ssid1="WIFI1"; // create AP name const char* password="987654321"; // create AP password char buff[255]; // Send/receive buffer void setup() {serial.begin (115200); delay(5000); WiFi.disconnect(); WiFi.mode(WIFI_AP_STA); WiFi.softAPConfig(lxip,lxip1.lxip2); SoftAP (ssid1,password1,1); // Set AP network parameters wifi. softAP(ssid1,password1,1); Wifi. begin(ssid,password); // Connect to the specified route wifi. config(sip,sip1,sip2); Serial.println("please wait"); // Set the local network parameter serial. println("please wait"); while(WiFi.status()! =WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); udp.begin(WiFi.localPort); Serial.print(" IP :"); Serial.print(WiFi.localIP()); Serial. Print (apip ":"); Serial.print(WiFi.softAPIP()); } void loop() { if(udp.parsePacket()) { udp.read(buff,255); BeginPacket (serverIP, remotePort); udp.write(buff,255); udp.endPacket(); Serial.print(buff); udp.beginPacket(xip,remoteport); udp.write(buff,255); udp.endPacket(); Memset (buff, 0255); // Clear buffer data}}Copy the code

subsequent

Look at it bit by bit when you’re done.

If you want to know more about the Internet of things and smart home projects, you can pay attention to my project practice column.

Writing is not easy, thank you for your support.

This article uses the article synchronization assistant to synchronize