client code:
//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:
#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();
}
}