Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By SwiCago
#23282 I let my code run for over an hour. .No hangs what so ever. I ran it as wifi AP not client and I have it hooked up to a bench supply with no actual Pixels connected.
I changed the code to update 300 pixels (that aren't really there) ... Did you change it or run it as I posted?

Here is my sketch I used for debugging, running on a ESP-07
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <NeoPixelBus.h>

#define DEBUG
#define DEBUG_OUTPUT Serial

#define pixelCount 300            //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");
}
int cnt=0;
void loop() {
  server.handleClient();
  if(cnt == 100)
    {Serial.println("Still alive"); cnt = 0;}
  delay(100);
  cnt ++;
 
}

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() {
  Serial.println("Parse web data");
  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);
  Serial.println("Web data sent");
  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();
}

 
User avatar
By Mario Mikočević
#23312 Heya,

tried debug defines while still running as AP client and still experiencing http silence after aprox 5min. Debug log does not have anything interesting other than 'New client'.
Tried after that /staging/ version of IDE, still unresponsive http.

Finally tried softAP mode and it's been running now for several hours still happily answering http requests for NeoPixel strip color change.

So, problem is somewhere in ESP WiFi client mode and WebServer combination,
will do some more debugging later ..

--
Mozz
User avatar
By SwiCago
#23353
Mario Mikočević wrote:Heya,

tried debug defines while still running as AP client and still experiencing http silence after aprox 5min. Debug log does not have anything interesting other than 'New client'.
Tried after that /staging/ version of IDE, still unresponsive http.

Finally tried softAP mode and it's been running now for several hours still happily answering http requests for NeoPixel strip color change.

So, problem is somewhere in ESP WiFi client mode and WebServer combination,
will do some more debugging later ..

--
Mozz

No we are getting somewhere...so it looks like webserver may be hanging when esp in is client mode. I'll try tonight as well. Maybe the ESP loses connection with wifi and upon reconnection, the handle for tcp80 is no longer valid. Maybe requiring some code to detect this and restart the webserver ???
I should be able to see on my wifi router if and when the connection is dropped and re-established.

if ( WiFi.status() == WL_DISCONNECTED )
{ Serial.println("Disconnected"); }

function WiFi.status() returns WL_CONNECTED if it has an IP address, which might still be the case whilst disconnected

Extra debugging could also be done via
WiFi.printDiag(Serial);
User avatar
By SwiCago
#23376 :idea: Guys, I cannot get mine to disconnect while running as a client. Is it possible you guys are using an older wificlient than I am? I recently updated mine directly from the github sources, as the the package installer kept installing older sources.
This is where I got the latest of each package
https://github.com/esp8266/Arduino/tree ... /libraries

Here is my test client code
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <NeoPixelBus.h>

#define DEBUG
#define DEBUG_OUTPUT Serial

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

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

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

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");
}
int cnt=0;
void loop() {
  server.handleClient();
 
  if(cnt == 100) {
    Serial.println("Still alive");
    WiFi.printDiag(Serial);
    if ( WiFi.status() == WL_DISCONNECTED ) {
      Serial.println("Disconnected");
    }
    cnt = 0;
  }
  delay(100);
  cnt ++;
}

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() {
  Serial.println("Parse web data");
  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);
  Serial.println("Web data sent");
  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();
}

 


Update, an hour later via cell phone VPN still working!!!!
Update...5hrs later and still working correctly. ....

So either you guys are using older sources or have a very poor wifi AP at home and somehow losing connection...I think updating your sources will fix the issue.