#include <Adafruit_NeoPixel.h> #include <SPI.h> #include <Ethernet.h> #include <EEPROM.h> #define PIN 6 // Parameter 1 = number of pixels in strip // Parameter 2 = pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800); byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x95, 0x3F}; IPAddress ip(192,168,0,99); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); int startValue = 255; int red = startValue; int green = startValue; int blue = startValue; int interval = startValue; int brightness = 255; String readString = ""; String lastValue = ""; const String RED = "red"; const String GREEN = "green"; const String BLUE = "blue"; const int RED_ADDRESS = 0; const int GREEN_ADDRESS = 1; const int BLUE_ADDRESS = 2; void setup() { Serial.begin(9600); strip.begin(); strip.show(); // Initialize all pixels to 'off' strip.setBrightness(brightness); int tmpRed = EEPROM.read(RED_ADDRESS); int tmpGreen = EEPROM.read(GREEN_ADDRESS); int tmpBlue = EEPROM.read(BLUE_ADDRESS); if(tmpRed > 0 || tmpGreen > 0 || tmpBlue > 0){ red = tmpRed; green = tmpGreen; blue = tmpBlue; colorWipe(strip.Color(red, green, blue)); }else{ colorWipe(strip.Color(red, green, blue)); } Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); //read char by char HTTP request if (readString.length() < 100) { //store characters to string readString += c; } Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println("Connection: close"); client.println(); client.println(prepareHtml()); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disonnected"); String queryString = getQueryString(readString); if(getValue(queryString, RED).length() > 0) { red = getIntValue(getValue(queryString, RED)); EEPROM.write(RED_ADDRESS, red); Serial.print("red:"); Serial.println(red); } if(getValue(queryString, GREEN).length() > 0) { green = getIntValue(getValue(queryString, GREEN)); EEPROM.write(GREEN_ADDRESS, green); Serial.print("green:"); Serial.println(green); } if(getValue(queryString, BLUE).length() > 0) { blue = getIntValue(getValue(queryString, BLUE)); EEPROM.write(BLUE_ADDRESS, blue); Serial.print("blue:"); Serial.println(blue); } //clearing string for next read readString=""; queryString=""; colorWipe(strip.Color(red, green, blue)); setBrightness(brightness); Serial.print(lastValue); } // client } void setBrightness(int brightness){ strip.setBrightness(brightness); } String prepareHtml(){ String html = "{\"status\":\"ok\"}"; int tmpRed = EEPROM.read(RED_ADDRESS); int tmpGreen = EEPROM.read(GREEN_ADDRESS); int tmpBlue = EEPROM.read(BLUE_ADDRESS); if(tmpRed > 0 || tmpGreen > 0 || tmpBlue > 0){ html = "{\"status\":\"ok\", \"rgb\":["+String(tmpRed)+","+String(tmpGreen)+","+String(tmpBlue)+"]}"; }else{ html = "{\"status\":\"ok\", \"rgb\":["+String(startValue)+","+String(startValue)+","+String(startValue)+"]}"; } return html; } // Fill the dots one after the other with a color void colorWipe(uint32_t c) { for(uint16_t i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); } } int getIntValue(String value){ int tmpValue = value.toInt(); if(tmpValue > 255){ return 255; } if(tmpValue < 0){ return 0; } return tmpValue; } String getValue(String queryString, String param){ if(queryString.indexOf("?") > -1 && queryString.indexOf(param) > -1){ //Serial.print(param);Serial.println(queryString.indexOf('?') > -1 && queryString.indexOf(param) > -1); int paramPos = queryString.indexOf(param) + 1 + param.length(); int AndCharPosOrLength = queryString.indexOf("&",paramPos); if(paramPos > 0){ if(AndCharPosOrLength == -1){ AndCharPosOrLength = queryString.length(); } return queryString.substring(paramPos, AndCharPosOrLength); } } return ""; } String getQueryString(String httpHeader){ String startString = "GET /"; String endString = " HTTP/1.1"; int startPos = httpHeader.indexOf(startString) + startString.length(); int endPos = httpHeader.indexOf(endString, startPos); if(startPos > 0 && endPos > 0){ Serial.println(httpHeader.substring(startPos, endPos)); return httpHeader.substring(startPos, endPos); } return ""; }
Kategorier