request help: esp client to esp server slow
Posted: Tue Nov 17, 2015 7:53 pm
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:
and here is the server code:
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();
}
}