Slow data transfer between 2 esp8266-12
Posted: Sun Apr 01, 2018 7:03 pm
I am using 2 esp8266-12 nodemcu as a wireless joystick and motor control for a remote control tank.
After combining several different examples and tweaking them to suit my needs i got the wireless joystick working and i am able to drive the tank from 50 feet away and through 5 or 6 walls.
The problem that i am having is that the joystick, which is acting like a station, processes the data from the buttons and joysticks and sends the information off but the the module in the tank lags and there is a noticeable delay of around 1 second between the wireless controller and the module in the tank.
I added the bit of code to show if the station is remaining connected, and it is saying that after every iteration of the void loop that it is having to reconnect.
Long story short i want to speed up the response time of the tank.
This is the station "the wireless controller"
This is the Access point " the module in the RC tank"
Please let me know if there is a way to speed up the data transfer between the two esp8266-12 nodemcu.
Thanks
After combining several different examples and tweaking them to suit my needs i got the wireless joystick working and i am able to drive the tank from 50 feet away and through 5 or 6 walls.
The problem that i am having is that the joystick, which is acting like a station, processes the data from the buttons and joysticks and sends the information off but the the module in the tank lags and there is a noticeable delay of around 1 second between the wireless controller and the module in the tank.
I added the bit of code to show if the station is remaining connected, and it is saying that after every iteration of the void loop that it is having to reconnect.
Long story short i want to speed up the response time of the tank.
This is the station "the wireless controller"
Code: Select all
#include <ESP8266WiFi.h>
#include <Mux.h>
int s0 = D5;
int s1 = D6;
int s2 = D7;
int s3 = D8;
//Mux in "SIG" pin
int SIG_pin = A0;
//int Dir_A;
//int Dir_B;
//int Dir[2];
//int readMux_1;
//int readMux_2;
byte ledPin = 2;
char ssid[] = "Wemos_AP"; // SSID of your AP
char pass[] = "Wemos_comm"; // password of your AP
IPAddress server(192,168,4,15); // IP address of the AP
WiFiClient client;
void setup() {
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
Serial.begin(57600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass); // connects to the WiFi AP
Serial.println();
Serial.println("Connection to the AP");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("Connected");
Serial.println("station_bare_01.ino");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
pinMode(ledPin, OUTPUT);
client.connect(server, 80);
}
void loop() {
if(!client.connected())
{
Serial.println("client not connected, reconnecting");
client.connect(server,80);
}
//client.connect(server, 80);
digitalWrite(ledPin, LOW);
//Loop through and read all 16 values
//Reports back Value at channel 6 is: 346
for(int i = 0; i < 4; i ++){
Serial.print("Value at channel ");
Serial.print(i);
Serial.print(" is : ");
Serial.println(readMux(i));
client.print(readMux(i));
client.print(',');
delay(10);
}
// readMux_1=readMux(0);
// readMux_2=readMux(2);
// if (readMux_1>530){Dir_A=1;}
// else if (readMux_1<480){Dir_A=2;}
// else {Dir_A=3;}
// if (readMux_2>530){Dir_B=1;}
// else if (readMux_2<480){Dir_B=2;}
// else {Dir_B=3;}
// int Dir[]={Dir_A,Dir_B};
// client.print(Dir_A);
// client.print(',');
// client.print(Dir_B);
// client.print(',');
client.flush();
digitalWrite(ledPin, HIGH);
client.stop();
delay(200);
}
int readMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}
This is the Access point " the module in the RC tank"
Code: Select all
#include <ESP8266WiFi.h>
WiFiServer server(80);
IPAddress IP(192,168,4,15);
IPAddress mask = (255, 255, 255, 0);
byte ledPin = 2;
long joystick[4];
long joystick_1;
long joystick_2;
#define MOTOR_PIN_1 D0
#define MOTOR_PIN_2 D1
#define MOTOR_PIN_3 D2
#define MOTOR_PIN_4 D3
//#define MOTOR_PWM_PIN_1 D4
//#define MOTOR_PWM_PIN_2 D5
void setup() {
Serial.begin(57600);
WiFi.mode(WIFI_AP);
WiFi.softAP("Wemos_AP", "Wemos_comm");
WiFi.softAPConfig(IP, IP, mask);
server.begin();
pinMode(ledPin, OUTPUT);
pinMode(MOTOR_PIN_1, OUTPUT);
pinMode(MOTOR_PIN_2, OUTPUT);
pinMode(MOTOR_PIN_3, OUTPUT);
pinMode(MOTOR_PIN_4, OUTPUT);
// pinMode(MOTOR_PWM_PIN_1, OUTPUT);
// pinMode(MOTOR_PWM_PIN_2, OUTPUT);
Serial.println();
Serial.println("accesspoint_bare_01.ino");
Serial.println("Server started.");
Serial.print("IP: "); Serial.println(WiFi.softAPIP());
Serial.print("MAC:"); Serial.println(WiFi.softAPmacAddress());
}
void loop() {
WiFiClient client = server.available();
if (!client) {return;}
if (client == true){
server.write(client.read());
}
digitalWrite(ledPin, LOW);
long joystick[]={client.parseInt(), client.parseInt(), client.parseInt(), client.parseInt()};
for(int i = 0; i < 4; i ++){
Serial.print(joystick[i]);
Serial.print('\n');
}
// long SPEED_A=joystick[0];
// long SPEED_B=joystick[2];
// analogWrite(MOTOR_PWM_PIN_1,SPEED_A);
// analogWrite(MOTOR_PWM_PIN_2,SPEED_B);
//joystick[]={client.parseInt(), client.parseInt()};
// joystick_1=client.parseInt();
// joystick_2=client.parseInt();
// if (joystick_1==1)
// {
// digitalWrite(MOTOR_PIN_1, HIGH);
// digitalWrite(MOTOR_PIN_2, LOW);
// }
// else if (joystick_1==2)
// {
// digitalWrite(MOTOR_PIN_1, LOW);
// digitalWrite(MOTOR_PIN_2, HIGH);
// }
// else if (joystick_1==3)
// {
// digitalWrite(MOTOR_PIN_1, LOW);
// digitalWrite(MOTOR_PIN_2, LOW);
// }
//
// if (joystick_2==1)
// {
// digitalWrite(MOTOR_PIN_3, HIGH);
// digitalWrite(MOTOR_PIN_4, LOW);
// }
// else if (joystick_2==2)
// {
// digitalWrite(MOTOR_PIN_3, LOW);
// digitalWrite(MOTOR_PIN_4, HIGH);
// }
// else if (joystick_2==3)
// {
// digitalWrite(MOTOR_PIN_3, LOW);
// digitalWrite(MOTOR_PIN_4, LOW);
// }
if (joystick[0]>530)
{
digitalWrite(MOTOR_PIN_1, HIGH);
digitalWrite(MOTOR_PIN_2, LOW);
}
else if (joystick[0]<480)
{
digitalWrite(MOTOR_PIN_1, LOW);
digitalWrite(MOTOR_PIN_2, HIGH);
}
else
{
digitalWrite(MOTOR_PIN_1, LOW);
digitalWrite(MOTOR_PIN_2, LOW);
}
if (joystick[2]>530)
{
digitalWrite(MOTOR_PIN_3, HIGH);
digitalWrite(MOTOR_PIN_4, LOW);
}
else if (joystick[2]<480)
{
digitalWrite(MOTOR_PIN_3, LOW);
digitalWrite(MOTOR_PIN_4, HIGH);
}
else
{
digitalWrite(MOTOR_PIN_3, LOW);
digitalWrite(MOTOR_PIN_4, LOW);
}
// Serial.print(joystick_1);
Serial.print(joystick[0]);
Serial.print('\n');
// Serial.print(joystick_2);
Serial.print(joystick[2]);
Serial.print('\n');
client.flush();
digitalWrite(ledPin, HIGH);
}
Please let me know if there is a way to speed up the data transfer between the two esp8266-12 nodemcu.
Thanks