You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.9 KiB
83 lines
1.9 KiB
3 years ago
|
#include <ESP8266WiFi.h>
|
||
|
#include <algorithm>
|
||
|
#include <ESP8266WiFiMulti.h>
|
||
|
|
||
|
#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\":\"admin1\", \"pwd\":\"147258369\"}");
|
||
|
}
|
||
|
|
||
|
// 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);
|
||
|
}
|
||
|
}
|