-->
Page 1 of 2

request help: esp client to esp server slow

PostPosted: Tue Nov 17, 2015 7:53 pm
by aortiz557
I have 2 esp-01 modules communicating with each other. one module controls a string of ws2812 leds, the other module for now just comes up with an rgb value and sends it to the other. The issue I'm having is speed, the only way I can get it to work is to open a new connection each time i send a value, and this seems to be slowing things down. I'm having a decent amount of trouble understanding the client/server documentation so this whole thing could be horribly wrong. really all i need is a fast way for one esp module to send 4 bytes to another one, the rest i can handle. Right now i can send somewhere between 4 and 6 values per second, any improvement to that would help enormously.

client code:
Code: Select all//seems to work for now
#include <ESP8266WiFi.h>
byte ip[] ={192,168,0,5};
WiFiClient client;
void padnumber(int number);


  int reddegrees = 0;
  int bluedegrees = 120;
  int greendegrees = 240;
  int counter = 0;


void setup() {
  Serial.begin(115200);
  WiFi.begin("Router? I hardly know her!","********");
  Serial.println("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
       while(!client.connect(ip,80))
  {
    delay(100);
  }
  Serial.println("succesfully connected to server");


 
 
  // put your setup code here, to run once:

}



void loop() {
         while(!client.connect(ip,80)) //as far as i can tell this is the part tripping me up, Is there a way to open a connection         //and keep it open?
  {
    yield();
  }
   if(reddegrees == 360)
  {
    reddegrees = 0;
  }
    if(greendegrees == 360)
  {
    greendegrees = 0;
  }
    if(bluedegrees == 360)
  {
    bluedegrees = 0;
  }
  float redradians = reddegrees*0.0174532925;
  float greenradians = greendegrees*0.0174532925;
  float blueradians = bluedegrees*0.0174532925;
 
  byte red = ((sin(redradians) + 1)/2)*255;
  byte green = ((sin(greenradians) + 1)/2)*255;
  byte blue = ((sin(blueradians) + 1)/2)*255;


 
  //padnumber(led);
  padnumber(0);
  padnumber(red);
  padnumber(green);
  padnumber(blue);
  client.print("\r");

  reddegrees = reddegrees + 12;
  greendegrees = greendegrees + 12;
  bluedegrees = bluedegrees + 12;
  counter++;
 


}

void padnumber(int number)
{
  String temporary = "";
  if(number < 100)
  {
    temporary += "0";
  }
  if(number < 10)
  {
    temporary += "0";
  }
  temporary += number;
  client.print(temporary);
}



and here is the server code:

Code: Select all#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60,2); //60 leds, pin 2
WiFiServer server(80);

void setup() {
 
  // put your setup code here, to run once:
  Serial.begin(115200);
  strip.begin();
  strip.show();
WiFi.begin("Router? I hardly know her!","**********");
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");


server.begin();
Serial.println("server started");
Serial.println(WiFi.localIP());

}

void loop() {
  // put your main code here, to run repeatedly:

WiFiClient client = server.available();


if(client == true)
{
 String temp = client.readStringUntil('\r'); //i tried reading out data using while(client.available()) but i could never get //any data
 String temp1 = temp.substring(0,3);
 String temp2 = temp.substring(3,6);
 String temp3 = temp.substring(6,9);
 String temp4 = temp.substring(9,12);
 int led = temp1.toInt();
 int red = temp2.toInt();
 int green = temp3.toInt();
 int blue = temp4.toInt();
 strip.setPixelColor(led,red,green,blue);
 client.flush();
 strip.show();
 yield();
}
 
}

 

Re: request help: esp client to esp server slow

PostPosted: Wed Nov 18, 2015 2:42 am
by schufti
I'm no expert on web things, so just some general hint:
first you should try to figure if its the server or client code that limits the troughput.
Then you can try localizing the relevant part e.g. comment out complex calculation etc.
There was a thread on speeding up serving webpages ... use forum search.

Re: request help: esp client to esp server slow

PostPosted: Wed Nov 18, 2015 6:42 am
by inx
using
Code: Select allclient.connect(ip,80)
always opens a new connection to the server, closing an exisiting one.
you could check with client.connected() if the client is connected, re-establishing a connection only if the client is disconnected.

Re: request help: esp client to esp server slow

PostPosted: Wed Nov 18, 2015 7:34 am
by aortiz557
so perhaps something like this on the client side?
Code: Select all  if(!client.connected())
  {
    Serial.println("client not connected, reconnecting");
   client.connect(ip,80);
  }