问题
I am trying to communicate between two ESP8266 12 E modules, one is set up in access point mode and the other as a station. My aim is to establish communication between the two.
- How can I make the data transfer faster?
- Is this what is called TCP/IP connection?
The code for the access point:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
WiFiServer server(80);
void setup() {
WiFi.mode(WIFI_AP);
WiFi.softAP("esp", "lol123");
server.begin();
Serial.begin(9600);
IPAddress IP = WiFi.softAPIP();
//Serial.flush();
Serial.println();
Serial.print("Server IP is: ");
Serial.println(IP);
}
void loop() {
char ID, R, G, B, anim_ID;
WiFiClient client = server.available();
int data_outgoing[5] = {10, 128, 128, 123, 123};
int mapFun[5];
Serial.print("Sent data: ");
Serial.print(ESP.getChipId());
Serial.println();
for(int i = 0; i < 5; i++){
mapFun[i] =data_outgoing[i];
//mapFun[i] = map(mapFun[i], 0, 255, 0, 128);
client.print(mapFun[i]);
Serial.print(mapFun[i]);
}
delay(10);
}
and for the receiving end, the station.
const char* ssid = "esp";
const char* password = "lol123";
const char* host = "192.168.4.1";
WiFiServer server(80);
void setup(){
int count = 0;
Serial.begin(9600);
delay(10);
//Serial.println();
//Serial.println();
//Serial.print("Connecting to: ");
//Serial.print(ssid);
//Serial.println();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.print(".");
count++;
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Time for connection(s): ");
Serial.print(count);
Serial.println();
Serial.println("IP address: ");
Serial.print(WiFi.localIP());
Serial.println();
}
void loop(){
WiFiClient client;
if((client.connect(host, 80))){
// Serial.println("Connected");
// Serial.print(host);
// Serial.println();
}
String data;
data = client.readStringUntil('\n');
//Serial.println(data);
for(int i = 0; i< 100; i++){
Serial.write(data[i]);
Serial.print(data[i]);
}
}
The problem with this set up is the data is transferred at a good speed, but while receiving the data is very slow. It takes quite a while to receive the data at the station side, what can be done to make this process faster, are there any other protocols to use to make this faster? The output is like this..
WiFi connected
Time for connection(s): 3
IP address:
192.168.4.2
10128128123123
The long length of data is the string I received, how do i convert it into integers? I tried ATOI but failed.
I'm kinda new to networking, any suggestion much appreciated.
回答1:
Try following.
Server loop
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
if (client.connected()) {
if (client.available()) {
int data_outgoing[5] = {10, 128, 128, 123, 123};
for(int i = 0; i < 5; i++){
client.print(data_outgoing[i]);
//Edit
client.print('\n');
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Client loop
void loop(){
WiFiClient client;
WiFiClient client = client.connect(host, 80);
while (client.connected()) {
char c = client.read();
Serial.print(c);
}
}
回答2:
The code in the original post won't establish a network unless the password length is at least 8 characters. Using "lol12345" will work. Hope it helps - this missed detail cost me 4 days. It could be a manefestation of my IDE version (arduino 1.8.1).
来源:https://stackoverflow.com/questions/45031974/faster-communication-between-two-esp8266-in-client-server-setup