Hi
Arduino Nano + Ethernet shield ENC28J60 + 10 pixel WS2812B LED Strip
The following sketch is working
// working code for TR Visual - led strip 10 pixels
// webpage at http://IP
// March 6 2020
//002
#include <SPI.h>
#include <UIPEthernet.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800);
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 1, 10 };
EthernetServer server(80);
String readString, newString;
void setup(){
// server
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
Serial.print("Server: ");
Serial.println(Ethernet.localIP());
// neopixel
strip.begin();
strip.setBrightness(20);
strip.show(); // Initialize all pixels to 'off'
rainbowStart(60);
colorWipe(strip.Color(0, 0, 0), 30);
reverseRainbowStart(60);
reverseColorWipe(strip.Color(0, 0, 0), 30);
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (c == '\n') {
//http response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// webpage
client.println("<HTML>");
client.println("<TITLE>Test Response Visual</TITLE>");
client.println("<BODY>");
client.println("
<H4>Test Response Visual</H4>");
client.println("<a href=\"/?color=red\">red</a>
");
client.println("<a href=\"/?color=sample1\">Sample1</a>
");
client.println("<a href=\"/?color=sample2\">Sample2</a>
");
client.println("<a href=\"/?color=sample3\">Sample3</a>
");
client.println("<a href=\"/?color=orgswing\">Orange Swing</a>
");
client.println("<a href=\"/?color=off\">Off</a>
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
if (readString.length() >0) {
if (readString.indexOf("?") > 0) {
int pos1 = readString.indexOf('/');
int pos2 = readString.lastIndexOf('/');
newString = readString.substring(pos1+1, pos2-5);
Serial.print("URL: ");
Serial.println(newString);
}
if (newString == "?color=red") {
chase(strip.Color(255, 0, 0));
}
if (newString == "?color=yellow") {
chase(strip.Color(155, 155, 0));
}
if (newString == "?color=off") {
chase(strip.Color(0, 0, 0));
}
if (newString == "?color=sample1") { // 3 blue, 5 green
strip.fill(strip.Color(0, 255, 0), 1, 8);
strip.setPixelColor(6, 0, 0, 255);
strip.setPixelColor(2, 0, 0, 255);
strip.setPixelColor(7, 0, 0, 255);
strip.show();
strip.setBrightness(10);
delay(10);
}
if (newString == "?color=sample2") { // 7 green, 1 red, priority order
strip.setPixelColor(0, 255, 255, 255);
strip.setPixelColor(9, 255, 255, 255);
strip.fill(strip.Color(0, 255, 0), 1, 8);
strip.setPixelColor(6, 255, 0, 0);
strip.show();
strip.setBrightness(10);
delay(10);
}
if (newString == "?color=sample3") { // 7 green, 1 red, priority order
strip.fill(strip.Color(255, 0, 0), 1, 8);
strip.show();
strip.setBrightness(10);
delay(10);
}
if (newString == "?color=orgswing") {
reverseColorWipe(strip.Color(0, 0, 0), 40); // off
colorWipe(strip.Color(255, 69, 0), 100); // orange
reverseColorWipe(strip.Color(0, 0, 0), 40); // off
}
readString="";
newString="";
}
}
}
}
}
}
void chase(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels()+4; i++) {
strip.setPixelColor(i , c); // Draw new pixel
//strip.setPixelColor(i-4, 0); // Erase pixel a few steps back
strip.show();
strip.setBrightness(50);
delay(10);
}
}
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);
}
}
void reverseColorWipe(uint32_t c, uint8_t wait) {
for(int16_t i=(strip.numPixels()-1); i>=0; i--) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void reverseRainbowStart(uint8_t wait) {
uint16_t i, j;
for(i=strip.numPixels(); i>0; i--) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + 256*5-1) & 255));
strip.show();
delay(wait);
}
}
void rainbowStart(uint8_t wait) {
uint16_t i, j;
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + 256*5-1) & 255));
strip.show();
delay(wait);
}
}
// cycle the rainbow
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=256*5-1; j>=0; j--) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
I do get a memory warning but the sketch compiles, uploads and executes okay
Sketch uses 22618 bytes (73%) of program storage space. Maximum is 30720 bytes.
Global variables use 1744 bytes (85%) of dynamic memory, leaving 304 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.
but.. If I add one extra line to the ''webpage" such as below (I added green option) the http request stop being processed and in the serial output the url string is not displayed
// webpage
client.println("<HTML>");
client.println("<TITLE>Test Response Visual</TITLE>");
client.println("<BODY>");
client.println("
<H4>Test Response Visual</H4>");
client.println("<a href=\"/?color=red\">red</a>
");
client.println("<a href=\"/?color=red\">green</a>
");
client.println("<a href=\"/?color=sample1\">Sample1</a>
");
client.println("<a href=\"/?color=sample2\">Sample2</a>
");
client.println("<a href=\"/?color=sample3\">Sample3</a>
");
client.println("<a href=\"/?color=orgswing\">Orange Swing</a>
");
client.println("<a href=\"/?color=off\">Off</a>
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
Has anybody else come across this before or have any suggestions
Thanks