結合無線AP跟WS2812LED環形燈
可以透過網頁簡易操作
燈光漸變的效果參考WS2812燈條
/*
* Copyright (c) 2015, Majenko Technologies
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* * Neither the name of Majenko Technologies nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Adafruit_NeoPixel.h>
// Which pin on the ESP8266 is connected to the NeoPixels?
#define PIN 2
// How many NeoPixels are attached to the ESP8266?
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
int r=0,g=0,b=0;
/* Set these to your desired credentials. */
const char *apid = "Led8266Ap";
const char *passwd = "esp8266";
ESP8266WebServer server(80);
boolean ledOn = false;
int casePos = 0;
/* Just a little test message. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
*/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1><br><a href=\"./ledOn\">LedOn</a><br><a href=\"./ledOff\">LedOff</a><br><a href=\"./ledRand\">LedRand</a>");
}
void handleOn() {
server.send(200, "text/html", "<h1>The led is on</h1><br><a href=\"./ledOff\">LedOff</a><br><a href=\"./ledRand\">LedRand</a>");
casePos = 1;
}
void handleOff() {
server.send(200, "text/html", "<h1>The led is off</h1><br><a href=\"./ledOn\">LedOn</a><br><a href=\"./ledRand\">LedRand</a>");
casePos = 0;
}
void handleRand() {
r=random(255);g=random(255);b=random(255);
server.send(200, "text/html", "<h1>The led is random Led color(r,g,b):"+ String(r) +","+ String(g) +","+ String(b) +"</h1><br><a href=\"./ledOn\">LedOn</a><br><a href=\"./ledOff\">LedOff</a>");
casePos = 2;
}
void setup() {
delay(1000);
Serial.begin(115200);
pixels.begin(); // This initializes the NeoPixel library.
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(apid);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/ledOn", handleOn);
server.on("/ledOff", handleOff);
server.on("/ledRand", handleRand);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
switch(casePos){
case 0: turnOff(); break;
case 1: turnOn(); break;
case 2:
case 3: turnRand(); break;
}
}
void turnOff(){
for(int i=1;i<=NUMPIXELS;i++){
pixels.setPixelColor(i%NUMPIXELS, pixels.Color(0,0,0)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
}
void turnOn(){
for(int i=1;i<=NUMPIXELS;i++){
pixels.setPixelColor((i-1)%NUMPIXELS, pixels.Color(0,0,0)); // Moderately bright green color.
pixels.setPixelColor(i%NUMPIXELS, pixels.Color(0,150,0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval); // Delay for a period of time (in milliseconds).
}
}
void turnRand(){
for(int i=1;i<=NUMPIXELS;i++){
pixels.setPixelColor(i%NUMPIXELS, pixels.Color(r,g,b)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(delayval*8); // Delay for a period of time (in milliseconds).
}
void handleNotFound(){
String uri=server.uri();
int pos1 = 0;int pos2 = 0;int pos3 = 0;
pos1=uri.indexOf("/", 1);
String rval = uri.substring(1,pos1);
pos2=uri.indexOf("/", 2);
String gval = uri.substring(pos1+1,pos2);
pos3=uri.indexOf("/", 3);
String bval = uri.substring(pos2+1);
r= rval.toInt();
g= gval.toInt();
b= bval.toInt();
casePos = 3;
server.send(200, "text/html", "<h1>Led color(r,g,b):"+ rval +","+ gval +","+ bval +"</h1><br><a href=\"./ledOn\">LedOn</a><br><a href=\"./ledOff\">LedOff</a><br><a href=\"./ledRand\">LedRand</a>");
}