ESP8266的一些杂乱记录

以下的代码不能直接用

#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
void handleRoot() {
  server.send(200, "text/html", "<h1>You are connected</h1>");
}
WiFi.softAP(ssid, password);
server.on("/", handleRoot);
server.begin();
server.handleClient();

ESP.restart();


关于UDP

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP udp;

IPAddress xip(192, 168,2, 2);//下位远程IP
IPAddress Serverip(192, 168, 1, 4);//上位机远程IP
unsigned int localPort = 9999;//本地端口
unsigned int remoteport = 9999;//远程端口
const char *ssid = "Netcore_wsn";
const char *password = "99325408322";
const char *ssid1 = "Netcore_wsn1";//AP
const char *password1 = "99325408322";//AP

char packetBuffer[255];//收发缓冲区
void setup() {Serial.begin(115200);//初始化串口波特率
       delay(5000);//延时5S
       WiFi.mode(WIFI_AP_STA);//设置模式为AP+STA
       WiFi.softAPConfig("192.168.2.1","192.168.2.1","255.255.255.0");//设置AP网络参数,: AP端IP,AP端网关,AP端子网掩码
       WiFi.softAP("AAA","00000000",1);//设置AP账号密码
       WiFi.begin("BBB","00000000");//连接指定路由
       WiFi.config("192.168.100.2","192.168.100.1","255.255.255.0");//设置本地网络参数
       Serial.print("Is connection routing, please wait");
       while(WiFi.status()!=WL_CONNECTED)//等待路由连接
       {  
           delay(500);  Serial.print(".");
       }
       Serial.println(" ");

       udp.begin(localPort);//监听指定端口
       Serial.print("ip:");  Serial.println(WiFi.localIP());
       Serial.print("apip:"); Serial.println(WiFi.softAPIP());
}
void loop() {
   if(udp.parsePacket()) {
         udp.read(packetBuffer,255);//读取数据
         udp.beginPacket(Serverip,remoteport);
         udp.write(packetBuffer,255);
         udp.endPacket();
         Serial.println(packetBuffer);

         udp.beginPacket(xip,remoteport);
         udp.write(packetBuffer,255);
         udp.endPacket();
         memset(packetBuffer, 0, 255);//清除缓冲器数值
    }
}

TCP

WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
          Serial.println("connection failed");
          return;
  }
 
  // We now create a URI for the request
  String url = "/input/";
  url += streamId;
  url += "?private_key=";
  url += privateKey;
  url += "&value=";
  url += value;
 
  Serial.print("Requesting URL: ");  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
}

void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for Smartconfig");
  WiFi.beginSmartConfig();
  while (1)
  {
    Serial.print(".");
    digitalWrite(LED, 0);  delay(500);
    digitalWrite(LED, 1);  delay(500);
    if (WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;
    }
  }
}

相关文章