These are chat archives for Makuna/NeoPixelBus

9th
Jun 2017
Michael Miller
@Makuna
Jun 09 2017 00:04
@joefly888 do you have some sample code to review? Its sort of hard to understand what you are trying to do.
joefly888
@joefly888
Jun 09 2017 00:29
@Makuna undertandably my question is poorly written. I just left the lab. Will post a better question and code tomorrow. Thanks
utybo
@utybo
Jun 09 2017 08:26
Hello, i am currently struggling with the animator api, for some reason the progress in AnimationParam is always 0.0
utybo
@utybo
Jun 09 2017 09:33
Solved my problem by putting AnimationParam& as the parameter type
joefly888
@joefly888
Jun 09 2017 19:03
@Makuna here is the code I am dealing with

include <ESP8266WiFi.h>

include <NeoPixelBrightnessBus.h>

const uint16_t PixelCount = 6; // this example assumes 4 pixels, making it smaller will cause a failure
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266

define colorSaturation 128

float sinVal;
int ledVal;
NeoPixelBrightnessBus<NeoRgbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
RgbColor color;

const char ssid = "SETC";
const char
password = "";

int ledPin = 13; //D13
WiFiServer server(80);

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

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

// 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(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
server.begin();
Serial.println("Server started");

// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
strip.Begin();
strip.Show();

}

void testled() {
do {
Serial.println("hello");
color.R = 0;
color.G = 254;
color.B = 0;

for (int x=0; x<180; x++) {
  for (int y=0; y<PixelCount; y++) {
     strip.SetPixelColor(y, color);
  }
  sinVal = (sin(x*(3.1412/180)));
  ledVal = int(sinVal*255); 
  strip.SetBrightness(ledVal);
  strip.Show();
  Serial.println("2222");
  checkweb();
  delay(20);
}

} while(1);
}

void checkweb() {

Serial.println("checkweb");
// 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
Serial.println("new request");
String request = client.readStringUntil('\r');
//Serial.println(request);
client.flush();

// Match the request

int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
Serial.println ("LED button on");
digitalWrite(ledPin, HIGH);
delay(100);
testled();
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");

client.print("Led pin is now: ");

if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>"); client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");
client.println("</html>");

delay(1);
Serial.println("Client disonnected");
Serial.println("");
if (value == LOW) {
loop();
}

}

void loop() {
Serial.print("loop");
checkweb();
delay(50);
}

void testled is the function that does the heart beat which has to stay in an endless loop due to the fading cycle of led, but during the cycle it must check for web commands from function checkweb. It is kinda working, but once I enter the testled function, and issue a turn off LED, it does turn off the LED on D13 but for some reason it goes right back to the testled function. I think due to this command
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
joefly888
@joefly888
Jun 09 2017 19:08
however, I have no idea the return why it is being returned to the testled loop, if instead of using return, I call loop(); it crashes the program or it spits out garbage on serial. Hopefully this explains my problem better
is there a better way of posting the code?
joefly888
@joefly888
Jun 09 2017 19:20
OK figured out how to properly post code
#include <ESP8266WiFi.h>
#include <NeoPixelBrightnessBus.h>
const uint16_t PixelCount = 6; // this example assumes 4 pixels, making it smaller will cause a failure
const uint8_t PixelPin = 2;  // make sure to set this to the correct pin, ignored for Esp8266
#define colorSaturation 128
float sinVal;
int ledVal;
NeoPixelBrightnessBus<NeoRgbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
RgbColor color;


const char* ssid = "SETC";
const char* password = "";

int ledPin = 13; //D13
WiFiServer server(80);

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

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // 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(100);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  strip.Begin();
  strip.Show();


}

void testled() {
  do {
    Serial.println("hello");
    color.R = 0; 
    color.G = 254;
    color.B = 0;

    for (int x=0; x<180; x++) {
      for (int y=0; y<PixelCount; y++) {
         strip.SetPixelColor(y, color);
      }
      sinVal = (sin(x*(3.1412/180)));
      ledVal = int(sinVal*255); 
      strip.SetBrightness(ledVal);
      strip.Show();
      Serial.println("2222");
      checkweb();
      delay(20);
    }
  } while(1);
}

void checkweb() {

  Serial.println("checkweb");
  // 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
  Serial.println("new request");
  String request = client.readStringUntil('\r');
  //Serial.println(request);
  client.flush();

  // Match the request

  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    Serial.println ("LED button on");
    digitalWrite(ledPin, HIGH);
    delay(100);
    testled();
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");

  client.print("Led pin is now: ");

  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
  client.println("</html>");

  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
  if (value == LOW) { 
    loop();
  }

}

void loop() {
  Serial.print("loop");
  checkweb();
  delay(50);
}
joefly888
@joefly888
Jun 09 2017 20:11
in this example https://gist.github.com/bbx10/667e3d4f5f2c0831d00b/forks does the websocket even work in the background? so in my case, I can have program in the loop to control the hearbeat and exit when there is a web event without calling for it each time? Does the websocket event just monitor in the background?
Michael Miller
@Makuna
Jun 09 2017 23:05
NEVER CALL LOOP()!
Further, you are calling TestLed from checkWeb and checkWeb from testLed; that can cause an infinite loop.
Michael Miller
@Makuna
Jun 09 2017 23:12
The first thing you want to do is rethink how you are doing your fade; this is what the animation class is good for and this is a perfect example of where it will help your code.
Look at some of the animation examples, there are demonstrations of diming and fading, so instead of calling testLED, just start the animation with the new color and just let the code run. The animation class will update the colors, and in your loop you will need to just call strip.show() (which is smart and will only do work if something changed).
Again, spend a little time looking at the animation examples.
joefly888
@joefly888
Jun 09 2017 23:15
Yes. As you guessed the code is pretty convoluted. The key which I am trying to figuring out is that I am trying so simultaneous things as the same time. Check for web commands while continuing the Leds. Is this even possible, if so any hints. Looking at websocket events. Do any events types work in the background. I vaguely remember similar issue to always avoid using delay and use timer event or something like that. Sorry I simply know enough about programming to get into trouble and make a mess :-)
Michael Miller
@Makuna
Jun 09 2017 23:21
Again, look at my animation class, it updates the values async to animate the changes so you can continue to check for web stuffs.
For esp wifi, look for the ASYNC libraries; I forget their names, but they all start with "async" for almost all things.
Even if you don't use the async, you can accomplish it. The main thing to do is never call delay, NEVER. Never call a blocking call without checking if there is something there. always call methods that check if something is available before calling one that will block to get it; this is called "polling". Then also never have a wait for ever code, like "for(;;)" or "while(0);"
joefly888
@joefly888
Jun 09 2017 23:28
@Makuna thanks for the guidance. I see that you are guiding me in good direction and important principles. I will study your comments and examples. And hopefully come back with good news. Thanks. I gotta obviously approach my problem from a different angle.