#include #include #include #ifndef STASSID #define STASSID "esp8266" //WIFI名 #define STAPSK "12345678" //WIFI密码 #endif #define BAUD_SERIAL 115200 // 串口波特率115200 #define RXBUFFERSIZE 1024 //设置串口接收的字节大小 //最多的连接个数 #define MAX_SRV_CLIENTS 4 const char* ssid = STASSID; //wifi名字 const char* password = STAPSK;//wifi密码 const int port = 30; //服务器端口30 WiFiServer server(port); //创建服务器对象 WiFiClient serverClients[MAX_SRV_CLIENTS]; //连接对象数组 ESP8266WiFiMulti WiFiMulti; WiFiClient client; // 初始化函数 void setup() { Serial.begin(BAUD_SERIAL); // 串口初始化 Serial.setRxBufferSize(RXBUFFERSIZE); //串口接收的初始化 pinMode(LED_BUILTIN, OUTPUT); //配置LED灯 digitalWrite(LED_BUILTIN, HIGH); //LED置高 //设置客户端模式 WiFi.mode(WIFI_STA); //连接网络 WiFiMulti.addAP(ssid, password); Serial.println(ssid); Serial.println(password); // 等待连接WIFI while (WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //获取自身IP地址 delay(500); if (!client.connect("101.35.200.152", 7400)) { Serial.println("connection failed"); delay(5000); return; } // 登录 client.write("{\"usr\":\"admin4\", \"pwd\":\"741852963\"}"); } // loop函数 void loop() { // 如果串口接收到数据 if(Serial.available()) { delay(10); size_t counti = Serial.available(); uint8_t sbuf[counti]; Serial.readBytes(sbuf, counti); if(client.connected()){ client.write(sbuf, counti); } } while(client.available()) { uint8_t c = client.read(); Serial.write(c); } }