So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Bruce Markey
#59779 I'm using the esp8266 as a lighting controller. I ran into a problem and I'm not sure how to solve it.

Here's what I'm confused about. Esp8266 running an embedded webserver and the neopixel library. Everythign works just fine.

In the main loop I check for a connection like so:

Code: Select all    Serial.println("new client");
    while(!client.available()){
    delay(1);
    }

So I can send a command like http://esp_ip_address/something/something and parse that. Ok easy enough. Works great for turning on and off the lights.

Wrote a "breath" function so the lights will slowly dim and brighten. But once I send that command to start that function i'm stuck in that loop and cannot send another web command to turn it off.

Is there any way around this other than setting a timer? So I could call the function and say "breath" for 5 minutes then stop? My guess is no but I thought I'd check .
User avatar
By Barnabybear
#59780 Hi, without seeing the code I can only guess, but set a variable for the postion in the fade and a variable to show the fade is active and you can drop out of the fade loop then check the fade variables to jump back into it.
User avatar
By Bruce Markey
#59781 That makes sense. I was thinking about how to do that. Here's the test code, nothing special. I can jump out at the delay section since there's a large pause.

Code: Select all#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
#define PIN 2

const char* ssid = "xxxxx";
const char* password = "xxxxx";
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
 
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    colorWipe(strip.Color(0, 0, 0) ,50);
  else if (req.indexOf("/gpio/1") != -1)
    colorWipe(strip.Color(255, 0, 0) ,50);
  else if (req.indexOf("/gpio/2") != -1)
    colorWipe(strip.Color(127, 0, 0), 50);
  else if (req.indexOf("/gpio/3") != -1)
    colorWipe(strip.Color(63, 0, 0), 50);
  else if (req.indexOf("/gpio/4") != -1) {
    colorWipe(strip.Color(0, 0, 255), 50);
    colorBreath();
  }
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

 
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nCommand Accepted";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
  Serial.println("Color changed");
}

void colorBreath() {
  for (int x=1; x<5000; x++) {
    for (int i=50; i<200; i++) { strip.setBrightness(i); strip.show(); delay(1); }
    for (int i=200; i>50; i--) { strip.setBrightness(i); strip.show(); delay(1); }
    delay(300);
  }
}