APweb
創建WIFI,接受STA鏈接,串口數據和TCP互傳 oop
#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,串口數據互傳ui
#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/秒,串口打印輸出server
#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); } }