Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By emvee
#24573 Has anyone been able to break out of a animation loop? I can only get static colors to change but any animations that are looped infinitely the server hangs and doesn't respond to any requests... trying to get the rainbow function to switch from the web front end when in a while loop but can't.. :(

Also I'm using the adafruit neopixel library seems much better than the neopixel bus library....
User avatar
By emvee
#24632 Got animation loops working on demand!Needed to stop the wifi client and start it back up within the loop (made a function for this), create a status variable, create an if statement that breaks the loop if its not equal to the status for any animation. This also now lets me support multiple connections somewhat in ESP8266 Web Server works fine with simple GET requests..
User avatar
By axius
#27251 Hello, I've been trying to run the code I found here but I'm getting an error about not finding NeoPixelBus.h

Where could I find this ?

I have a NeoPixel Stick with 8 leds :)

** Nevermind, I have found the NeopixelBus library :)
** Now it flashes correctly and appears to connect to my wifi network but it doesn't respond to ping and I can't connect to it's web server... still debugging
User avatar
By JohnnyBlaze
#27391
SwiCago wrote:Hi Chris,

Here is a working sample...The only thing I could not test is the actual ws2812 update, but the color from web and converting it to numbers that the NeoPixelBus understands works.

I had to make my own hex string to int converter, because of strtol causing the esp to crash.

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <NeoPixelBus.h>

#define pixelCount 4            //number of pixels in RGB strip

NeoPixelBus strip = NeoPixelBus(pixelCount, 0);  //GPIO 0

const char* ssid     = "MY_ROUTER";
const char* password = "MY_PASSWD";

IPAddress apIP(192, 168, 1, 1);        //FOR AP mode
IPAddress netMsk(255,255,255,0);         //FOR AP mode

const char* html = "<html><head><style></style></head>"
                   "<body><form action='/' method='GET'><input type='color' name='color' value='#000000'/>"
                   "<input type='submit' name='submit' value='Update RGB Strip'/></form></body></html>";

ESP8266WebServer server(80);

void setup() {
  Serial.begin(115200);
  strip.Begin();   
  strip.Show();

//***************WIFI client************************//
  WiFi.begin(ssid, password);
  Serial.print("\n\r \n\rWorking to connect");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
//***************WIFI ACCESS POINT******************//
/*  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP,apIP,netMsk);
  WiFi.softAP(ssid);//,password);  //leave password away for open AP
  Serial.print("SSID: ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());   */
//**************************************************//

 
  server.on("/", handle_root);             //root page
  server.onNotFound(handleNotFound);       //page if not found
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void handleNotFound() {
  Serial.print("\t\t\t\t URI Not Found: ");
  Serial.println(server.uri());
  server.send ( 200,"text/plain","URI Not Found" );
}

void handle_root() {
  String toSend = html;
  if (server.hasArg("color")) {
    String rgbStr = server.arg("color");  //get value from html5 color element
    rgbStr.replace("%23","#"); //%23 = # in URI
    toSend.replace("#000000",rgbStr);     //replace our default black with new color, so when page loads our new color shows
    int rgb[3];                           //define rgb pointer for ws2812
    getRGB(rgbStr,rgb);                   //convert RGB string to rgb ints
    updateEntireStrip(rgb[0], rgb[1], rgb[2]);           //update strip
  }
  server.send(200, "text/html", toSend);
  delay(100);
}

void getRGB(String hexRGB, int *rgb) {
  hexRGB.toUpperCase();
  char c[7];
  hexRGB.toCharArray(c,8);
  rgb[0] = convertToInt(c[1],c[2]); //red
  rgb[1] = convertToInt(c[3],c[4]); //green
  rgb[2] = convertToInt(c[5],c[6]); //blue 
}

int convertToInt(char upper,char lower)
{
  int uVal = (int)upper;
  int lVal = (int)lower;
  uVal = uVal >64 ? uVal - 55 : uVal - 48;
  uVal = uVal << 4;
  lVal = lVal >64 ? lVal - 55 : lVal - 48;
  return uVal + lVal;
}

void updateEntireStrip(int red, int blue, int green) {
  RgbColor rgb = RgbColor(red, blue, green);     
  for(int i=0; i < pixelCount; i++) {
    strip.SetPixelColor(i,rgb);
  }
  strip.Show();
}

 


Thanks for this sample code! It works without a problem on newest lib and Arduino 1.6.5