Examples
-
#include <SoftSerial.h>
#include <DigisparkRGB.h>
#include <Arduino.h>
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 64
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 5
#define CLOCK_PIN 13
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
// Uncomment/edit one of the following lines for your leds arrangement.
// FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2801, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
Serial.begin(9600);
Serial.println("Waiting for ESP-01");
}
int realpixel[8][8] = {
{ 0,1,2,3,16,17,18,19 },
{ 7,6,5,4,23,22,21,20 },
{ 8,9,10,11,24,25,26,27 },
{ 15,14,13,12,31,30,29,28 },
{ 32,33,34,35,48,49,50,51 },
{ 39,38,37,36,55,54,53,52 },
{ 40,41,42,43,56,57,58,59 },
{ 47,46,45,44,63,62,61,60 }
};
void postDOT(int x, int y, uint16_t color, int delayTime) {
leds[realpixel[x][y]] = CRGB( random(0, 254), random(0, 254), random(0, 254));
FastLED.show();
leds[realpixel[x][y]] = CRGB::Black;
delay(delayTime);
}
String inputString = "";
void loop() {
/*
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
leds[realpixel[random(0, 8)][random(0, 8)]] = CRGB( random(0, 254), random(0, 254), random(0, 254));
FastLED.show();
leds[realpixel[random(0, 8)][random(0, 8)]] = CRGB::Black;
delay(30);
}
}
//FastLED.show();
*/
if(Serial.available() > 0)
{
String str = Serial.readStringUntil('\n');
int x = Serial.parseInt();
Serial.println(str.toInt());
//Serial.println(x);
leds[str.toInt()] = CRGB( random(0, 254), random(0, 254), random(0, 254));
FastLED.show();
}
//while (Serial.available() > 0) {
//char inByte = Serial.read();
//Serial.print(inByte);
//inputString +=
// }
//leds[realpixel[j][7]] = CRGB( random(0, 254), random(0, 254), random(0, 254));
// FastLED.show();
/*
for (int j=0;j<8;j++) {
leds[realpixel[j][7]] = CRGB( random(0, 254), random(0, 254), random(0, 254));
FastLED.show();
leds[realpixel[j][7]] = CRGB::Black;
delay(30);
}
*/
/*
for(int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB::Blue;
FastLED.show();
// clear this led for the next time around the loop
leds[dot] = CRGB::Black;
delay(30);
}
*/
}
ESP-01
-
/*
* SANKI 2016 03 31
*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio0 will set the GPIO0 high/low,
* http://server_ip/gpio2 will set the GPIO2 high/low
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SoftwareSerial.h>
//SoftwareSerial mySerial(0, 2); // RX, TX
const char* ssid = "<SSID>";
const char* password = "<Password>";
ESP8266WebServer server(1104);
const int led = 0;
String sMessage = "";
int realpixel[8][8] = {
{ 0,1,2,3,16,17,18,19 },
{ 7,6,5,4,23,22,21,20 },
{ 8,9,10,11,24,25,26,27 },
{ 15,14,13,12,31,30,29,28 },
{ 32,33,34,35,48,49,50,51 },
{ 39,38,37,36,55,54,53,52 },
{ 40,41,42,43,56,57,58,59 },
{ 47,46,45,44,63,62,61,60 } };
void handleNotFound(){
digitalWrite(LED_BUILTIN , 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(LED_BUILTIN , 0);
}
void handleMessage(int gpio0, int gpio2){
digitalWrite(LED_BUILTIN , 1);
// Prepare the response
//String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html";
String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
//s += "<body style=\"background-color:lightgrey;\">"; // Color Only
s += "<head>";
s += "<style>";
//------------------------------- Select
s += " select{";
s += " width: 20%;";
s += " height: 100%;";
s += " padding: 5px;";
s += " font-size:24px;";
s += " }";
s += " select option { color: black; }";
s += " select option:first-child";
s += " {";
s += " color: green;";
s += " // font-size:25px;";
s += " }";
// ------------------------------ Button
s += ".button {";
//s += " background-color: #4CAF50; /* Green */";
s += " border: none;";
s += " color: white;";
s += " padding: 15px 32px;";
s += " text-align: center;";
s += " text-decoration: none;";
s += " display: inline-block;";
s += " font-size: 16px;";
s += " margin: 4px 2px;";
s += " cursor: pointer;";
s += " -webkit-transition-duration: 0.4s; /* Safari */";
s += " transition-duration: 0.4s;";
s += "}";
s += "";
s += ".button1 {";
s += " background-color: #4CAF50; font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";
s += "}";
s += "";
s += ".button2 {";
s += " background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";
s += "}";
s += ".reseton {";
s += " background-color: #4CAF50; border-radius: 12px;";
s += "}";
s += ".resetoff {";
s += " background-color: #f44336; border-radius: 12px;";
s += "}";
s += " #wrap{";
s += "width:1000px;";
s += "margin:0 auto;";
s += "}";
s += "body {";
s += " background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");";
s += " background-repeat: no-repeat;";
s += " background-position: center top;";
s += " background-color: lightgrey";
s += " margin-right: 20px;";
s += " margin-left: 20px;";
s += " margin-top: 180px;";
s += "}";
s += "h1 {";
s += " background-color: green;";
s += "}";
s += "";
s += "div {";
s += " background-color: lightblue;";
s += "}";
s += "";
s += "p {";
s += " background-color: yellow;";
s += " font-size: 48px;";
s += "}";
s += " p.ridge {border-style: ridge;}";
s += "</style>";
s += "</head>";
s += "<body align=center>";
s += "";
// s += " <div class=\"wrap\">";
/*
s += "<h1>GPIO2 is now ";
s += (val)?"high":"low";
s += "</h1>";
*/
s += "RGB WS2182B 8x8 Matrix Controller : " + sMessage + "";
if (gpio0 == 0)
s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button2\">OFF</button></a></p>";
else
s += "<p class=\"ridge\">GPIO 0 : <a href=\"\\gpio0\"><button class=\"button button1\">ON</button></a></p>";
s += "<form action=\"\\analogWrite\" method=\"get\">";
//s += "First name: <input type="text" name="FirstName" value="Mickey"><br>";
//s += "Last name: <input type="text" name="LastName" value="Mouse"><br>";
s += "<p>Analog Write to RGB LEN Number : <select id=\"rgblen\" name=\"rgblen\">";
for (int i=0;i<64;i++)
s += " <option value=\"" + String(i) + "\">RGB + " + String(i) + "</option>";
s += "</select>";
s += "</p>";
//------------------------------------------------
s += "<p>";
s += "<table align=center width=20px>";
s += "<tr align=center>";
s += " <th></th>";
for (int i=0;i<8;i++)
s += "<th>" + String(i) + "</th>";
s += " </tr>";
for (int j=0;j<8;j++) {
s += "<tr align=center>";
s += "<th>" + String(j) + "</th>";
for (int i=0;i<8;i++) {
s += "<td><a href=\"\\analogWrite?rgblen=" + String(realpixel[j][i]) + "\">" + String(realpixel[j][i]) + "</a></td>";
}
s += " </tr>";
}
s += "</table>";
s += "</p>";
s += "<input type=\"submit\" value=\"Submit\">";
s += "</form>";
/*
if (gpio2 == 0)
s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button2\">OFF</button></a></p>";
else
s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button1\">ON</button></a></p>";
*/
// ----------------------------- Reset All gpio
s += "<p class=\"ridge\">Reset : <a href=\"\\reseton\"><button class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button class=\"button resetoff\">All OFF</button></a></p>";
// --------------------------- PWM Display by Forms
s += "<p>";
s += "<table align=center width=20px>";
s += "<tr align=center>";
s += " <th></th>";
for (int i=0;i<8;i++)
s += "<th>" + String(i) + "</th>";
s += " </tr>";
for (int j=0;j<8;j++) {
s += "<tr align=center>";
s += "<th>" + String(j) + "</th>";
for (int i=0;i<8;i++) {
s += "<td>" + String(realpixel[j][i]) + "</td>";
}
s += " </tr>";
}
s += "</table>";
s += "</p>";
//s += m + "\n";
s += "</body></html>\n";
server.send(200, "text/html", s);
digitalWrite(LED_BUILTIN , 0);
}
String ipToString(IPAddress ip){
String s="";
for (int i=0; i<4; i++)
s += i ? "." + String(ip[i]) : String(ip[i]);
return s;
} //- See more at: http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf
void handleRoot() {
digitalWrite(LED_BUILTIN , 1);
//server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio");
handleMessage(digitalRead(0), digitalRead(2));
digitalWrite(LED_BUILTIN , 0);
}
void handleReset(int onoff) {
digitalWrite(LED_BUILTIN , 1);
//server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio");
if (onoff == 0) {
digitalWrite(0, 0);
digitalWrite(2, 0);
handleMessage(0 ,0);
}
if (onoff == 1) {
digitalWrite(0, 1);
digitalWrite(2, 1);
handleMessage(1, 1);
}
digitalWrite(LED_BUILTIN , 0);
}
void setup(void){
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
// USART MUst be 9600 and control by USART Command!
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.begin();
Serial.println("HTTP server started");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
uint8_t MAC_array[6];
char MAC_char[18];
WiFi.macAddress(MAC_array);
sprintf(MAC_char, "%02x:%02x:%02x:%02x:%02x:%02x", MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]);
Serial.print("MAC Address : ");
Serial.println(MAC_char);
sMessage += "[SSID : " + String(ssid) + "] [Server IP : " + ipToString(WiFi.localIP()) + "] [MAC : " + MAC_char + "]";
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
pinMode(0, OUTPUT);
digitalWrite(0, 0);
server.on("/", handleRoot);
// ---------------------------- Control USART Display
server.on("/analogWrite", []() {
String state= "";
state = server.arg("rgblen");
//analogWrite(2, state.toInt());
//Serial.print();
// Length (with one extra character for the null terminator)
int str_len = state.length() + 1;
// Prepare the character array (the buffer)
char char_array[str_len];
// Copy it over
state.toCharArray(char_array, str_len);
Serial.write(char_array);
//if (state == "1") USARTsend("page 1");
//if (state == "2") USARTsend("page 2");
//server.send(200, "text/plain", "Led is now " + state);
handleRoot();
});
server.on("/reseton", [](){
//server.send(200, "text/plain", "this works as well");
handleReset(1);
});
server.on("/resetoff", [](){
//server.send(200, "text/plain", "this works as well");
handleReset(0);
});
server.on("/gpio2", [](){
String m = "GPIO 2 : ";
int val = digitalRead(2);
if (val == 0) {
val = 1;
m += "ON";
} else {
val = 0;
m += "OFF";
}
digitalWrite(2, val);
//server.send(200, "text/plain", m);
handleMessage(digitalRead(0), val);
});
server.on("/gpio0", [](){
String m = "GPIO 0 : ";
int val = digitalRead(0);
if (val == 0) {
val = 1;
digitalWrite(0, 1);
m += "ON";
} else {
val = 0;
digitalWrite(0, 0);
m += "OFF";
}
//digitalWrite(led, val);
//server.send(200, "text/plain", m);
handleMessage(val, digitalRead(2));
});
/*
server.on("/gpio2/0", [](){
server.send(200, "text/plain", "Turn OFF GPIO 2");
digitalWrite(2, 0);
});
*/
server.onNotFound(handleNotFound);
}
void loop(void){
server.handleClient();
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Digispark + ESP-01 +RGBLEN WS21811
This example is ..........
-
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#include <SoftSerial.h>
const int rx=3;
const int tx=2;
SoftSerial mySerial(rx,tx);
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
int delayval = 100; // delay for half a second
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
mySerial.begin(9600);
mySerial.println("Digispark Control WS2811");
}
boolean onoff = true;
int i=0;
char buf[12];
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel((i+j) & 255));
}
pixels.show();
delay(wait);
}
}
void loop() {
/* ------------------------------ Testing Running RGB
// For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
for(int i=0;i<NUMPIXELS;i++){
if (i == 0) onoff = onoff?true:false;
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
if (onoff)
pixels.setPixelColor(i, pixels.Color(random(0, 254),random(0, 254),random(0, 254))); // Moderately bright green color.
else
pixels.setPixelColor(i, pixels.Color(0, 0, 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).
}
//------------------------------ Testing END */
pixels.setPixelColor(0, pixels.Color(random(0, 254),random(0, 254),random(0, 254))); // 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).
pixels.setPixelColor(0, pixels.Color(0, 0, 0)); // Moderately bright green color.
delay(delayval); // Delay for a period of time (in milliseconds).
if (mySerial.available()>0){
buf[i]= mySerial.read();
// When Reach char * then process
if (int(buf[i])==13 || int(buf[i])==10 || int(buf[i])==42 ){ //If Carriage return has been reached
//mySerial.println(buf);
int n = atoi(buf);
if (n == 0) rainbow(20);
pixels.setPixelColor(n, pixels.Color(random(0, 254),random(0, 254),random(0, 254))); // 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).
for(int x=0;x<=10;x++){
buf[x]=' ';
}
i=0; //start over again
} //if enter
i++;
} //If mySerial.available
}
ESP-01
This example is ..........
-
/*
* SANKI 2016 03 31
*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio0 will set the GPIO0 high/low,
* http://server_ip/gpio2 will set the GPIO2 high/low
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SoftwareSerial.h>
//SoftwareSerial mySerial(0, 2); // RX, TX
const char* ssid = "<SSID>";
const char* password = "<Password>";
ESP8266WebServer server(1104);
const int led = 0;
String sMessage = "";
int realpixel[8][8] = {
{ 0,1,2,3,16,17,18,19 },
{ 7,6,5,4,23,22,21,20 },
{ 8,9,10,11,24,25,26,27 },
{ 15,14,13,12,31,30,29,28 },
{ 32,33,34,35,48,49,50,51 },
{ 39,38,37,36,55,54,53,52 },
{ 40,41,42,43,56,57,58,59 },
{ 47,46,45,44,63,62,61,60 } };
void handleNotFound(){
digitalWrite(LED_BUILTIN , 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(LED_BUILTIN , 0);
}
void handleMessage(int gpio0, int gpio2){
digitalWrite(LED_BUILTIN , 1);
// Prepare the response
//String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html";
String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
//s += "<body style=\"background-color:lightgrey;\">"; // Color Only
s += "<head>";
s += "<style>";
//------------------------------- Select
s += " select{";
s += " width: 20%;";
s += " height: 100%;";
s += " padding: 5px;";
s += " font-size:24px;";
s += " }";
s += " select option { color: black; }";
s += " select option:first-child";
s += " {";
s += " color: green;";
s += " // font-size:25px;";
s += " }";
// ------------------------------ Button
s += ".button {";
//s += " background-color: #4CAF50; /* Green */";
s += " border: none;";
s += " color: white;";
s += " padding: 15px 32px;";
s += " text-align: center;";
s += " text-decoration: none;";
s += " display: inline-block;";
s += " font-size: 16px;";
s += " margin: 4px 2px;";
s += " cursor: pointer;";
s += " -webkit-transition-duration: 0.4s; /* Safari */";
s += " transition-duration: 0.4s;";
s += "}";
s += "";
s += ".button1 {";
s += " background-color: #4CAF50; font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";
s += "}";
s += "";
s += ".button2 {";
s += " background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";
s += "}";
s += ".reseton {";
s += " background-color: #4CAF50; border-radius: 12px;";
s += "}";
s += ".resetoff {";
s += " background-color: #f44336; border-radius: 12px;";
s += "}";
s += " #wrap{";
s += "width:1000px;";
s += "margin:0 auto;";
s += "}";
s += "body {";
s += " background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");";
s += " background-repeat: no-repeat;";
s += " background-position: center top;";
s += " background-color: lightgrey";
s += " margin-right: 20px;";
s += " margin-left: 20px;";
s += " margin-top: 180px;";
s += "}";
s += "h1 {";
s += " background-color: green;";
s += "}";
s += "";
s += "div {";
s += " background-color: lightblue;";
s += "}";
s += "";
s += "p {";
s += " background-color: yellow;";
s += " font-size: 48px;";
s += "}";
s += " p.ridge {border-style: ridge;}";
s += "</style>";
s += "</head>";
s += "<body align=center>";
s += "";
// s += " <div class=\"wrap\">";
/*
s += "<h1>GPIO2 is now ";
s += (val)?"high":"low";
s += "</h1>";
*/
s += "Digispark with RGB LEN : " + sMessage + "";
if (gpio0 == 0)
s += "<p class=\"ridge\">Rainbow Show : <a href=\"\\analogWrite?rgblen=999*\"><button class=\"button button2\">Rainbow Off</button></a></p>";
else
s += "<p class=\"ridge\">Rainbow Show : <a href=\"\\analogWrite?rgblen=999*\"><button class=\"button button2\">Rainbow ON</button></a></p>";
s += "<form action=\"\\analogWrite\" method=\"get\">";
//s += "First name: <input type="text" name="FirstName" value="Mickey"><br>";
//s += "Last name: <input type="text" name="LastName" value="Mouse"><br>";
s += "<p>Analog Write to RGB LEN Number : <select id=\"rgblen\" name=\"rgblen\">";
for (int i=0;i<64;i++)
s += " <option value=\"" + String(i) + "*\">RGB + " + String(i) + "</option>";
s += "</select>";
s += "</p>";
//------------------------------------------------
s += "<p>";
s += "<table align=center width=20px>";
s += "<tr align=center>";
s += " <th></th>";
for (int i=0;i<8;i++)
s += "<th>" + String(i) + "</th>";
s += " </tr>";
for (int j=0;j<8;j++) {
s += "<tr align=center>";
s += "<th>" + String(j) + "</th>";
for (int i=0;i<8;i++) {
s += "<td><a href=\"\\analogWrite?rgblen=" + String(realpixel[j][i]) + "*\">" + String(realpixel[j][i]) + "</a></td>";
}
s += " </tr>";
}
s += "</table>";
s += "</p>";
s += "<input type=\"submit\" value=\"Submit\">";
s += "</form>";
/*
if (gpio2 == 0)
s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button2\">OFF</button></a></p>";
else
s += "<p class=\"ridge\">GPIO 2 : <a href=\"\\gpio2\"><button class=\"button button1\">ON</button></a></p>";
*/
// ----------------------------- Reset All gpio
s += "<p class=\"ridge\">Reset : <a href=\"\\reseton\"><button class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button class=\"button resetoff\">All OFF</button></a></p>";
// --------------------------- PWM Display by Forms
s += "<p>";
s += "<table align=center width=20px>";
s += "<tr align=center>";
s += " <th></th>";
for (int i=0;i<8;i++)
s += "<th>" + String(i) + "</th>";
s += " </tr>";
for (int j=0;j<8;j++) {
s += "<tr align=center>";
s += "<th>" + String(j) + "</th>";
for (int i=0;i<8;i++) {
s += "<td>" + String(realpixel[j][i]) + "</td>";
}
s += " </tr>";
}
s += "</table>";
s += "</p>";
//s += m + "\n";
s += "</body></html>\n";
server.send(200, "text/html", s);
digitalWrite(LED_BUILTIN , 0);
}
String ipToString(IPAddress ip){
String s="";
for (int i=0; i<4; i++)
s += i ? "." + String(ip[i]) : String(ip[i]);
return s;
} //- See more at: http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf
void handleRoot() {
digitalWrite(LED_BUILTIN , 1);
//server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio");
handleMessage(digitalRead(0), digitalRead(2));
digitalWrite(LED_BUILTIN , 0);
}
void handleReset(int onoff) {
digitalWrite(LED_BUILTIN , 1);
//server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio");
if (onoff == 0) {
digitalWrite(0, 0);
digitalWrite(2, 0);
handleMessage(0 ,0);
}
if (onoff == 1) {
digitalWrite(0, 1);
digitalWrite(2, 1);
handleMessage(1, 1);
}
digitalWrite(LED_BUILTIN , 0);
}
int val = 0;
void setup(void){
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
// USART MUst be 9600 and control by USART Command!
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.begin();
Serial.println("HTTP server started");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
uint8_t MAC_array[6];
char MAC_char[18];
WiFi.macAddress(MAC_array);
sprintf(MAC_char, "%02x:%02x:%02x:%02x:%02x:%02x", MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]);
Serial.print("MAC Address : ");
Serial.println(MAC_char);
sMessage += "[SSID : " + String(ssid) + "] [Server IP : " + ipToString(WiFi.localIP()) + "] [MAC : " + MAC_char + "]";
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
pinMode(0, OUTPUT);
digitalWrite(0, 0);
server.on("/", handleRoot);
// ---------------------------- Control RGBLEN Display
server.on("/analogWrite", []() {
String state= "";
state = server.arg("rgblen");
int str_len = state.length() + 1;
char char_array[str_len];
state.toCharArray(char_array, str_len);
Serial.write(char_array);
handleRoot();
});
//-----------------------------------------------------------------------
server.on("/reseton", [](){
//server.send(200, "text/plain", "this works as well");
handleReset(1);
});
server.on("/resetoff", [](){
//server.send(200, "text/plain", "this works as well");
handleReset(0);
});
/*
server.on("/gpio2", [](){
String m = "GPIO 2 : ";
int val = digitalRead(2);
if (val == 0) {
val = 1;
m += "ON";
} else {
val = 0;
m += "OFF";
}
digitalWrite(2, val);
//server.send(200, "text/plain", m);
handleMessage(digitalRead(0), val);
});
*/
server.on("asdasd", [](){
//String m = "GPIO 0 : ";
//int val = digitalRead(0);
if (val == 0) {
val = 1;
//digitalWrite(0, 1);
//m += "ON";
} else {
val = 0;
//digitalWrite(0, 0);
//m += "OFF";
}
String state = "analogWrite?rgblen=2*";
int str_len = state.length() + 1;
char char_array[str_len];
state.toCharArray(char_array, str_len);
Serial.write(char_array);
//digitalWrite(led, val);
//server.send(200, "text/plain", m);
handleMessage(val, digitalRead(2));
});
/*
server.on("/gpio2/0", [](){
server.send(200, "text/plain", "Turn OFF GPIO 2");
digitalWrite(2, 0);
});
*/
server.onNotFound(handleNotFound);
}
void loop(void){
server.handleClient();
}
|