AP
建立WIFI,接受STA连接,串口数据和TCP互传
#include <ESP8266WiFi.h>
const char *ssid = "esp8266_666";
const char *password = "12345678";
WiFiServer server(8266);
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Setting soft-AP ... ");
IPAddress softLocal(192,168,1,1);
IPAddress softGateway(192,168,1,1);
IPAddress softSubnet(255,255,255,0);
WiFi.softAPConfig(softLocal, softGateway, softSubnet);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
float roll, pitch, heading;
void loop()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// 将串口数据打印给TCP
if(Serial.available()){
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
client.write(sbuf, len);
delay(1);
}
roll=roll+10; pitch=pitch+10; heading=heading+10;
Serial.print("Orientation: ");
Serial.print(heading);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);
delay(1000);
// 将TCP数据打印给串口
if (client.available())
{
// String line = client.readStringUntil(13);// arduino换行符号 ascll码 13
String line = client.readStringUntil('\r');
Serial.println(line);
}
}
delay(1);
// client.stop();
Serial.println("[Client disonnected]");
}
}
STA
连接WIFI,连接AP,串口数据互传
#include <ESP8266WiFi.h>
const char* ssid = "esp8266_666";
const char* password = "12345678";
const char* host = "192.168.1.1";
const int httpPort = 8266;
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WiFiClient client;
void setup(void)
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s\n", ssid);
WiFi.config(staticIP, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
}
void loop() {
// client.print("abc\r");
// delay(1000);
while(client.available()){
//int newLine = 13;
// String line = client.readStringUntil(13);
String line = client.readStringUntil('\r');
Serial.println(line);
}
}
测试实例 发送 abc/秒,串口打印输出
#include <ESP8266WiFi.h>
const char* ssid = "esp8266_666";
const char* password = "12345678";
const char* host = "192.168.1.1";
const int httpPort = 8266;
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WiFiClient client;
void setup(void)
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s\n", ssid);
WiFi.config(staticIP, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
}
void loop() {
client.print("abc\r");
delay(1000);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
}