Neopixel color control with esp32 or esp8266 in kodular

Hello friends
I want neopixel(WS2812) color control with esp32 or esp8266 in kodular
Someone can help me

What is kodular ?

It is like Mit App Inventor

1 Like

hi,kodular is a site for building Android applications.

Can it send commands via Bluetooth ?

You give us too little information. With what kind of communication system? WiFi, BLE?
What are your skills?
What is the aspect that you are unable to implement?

my arduino cod

// Neopixel
#include <Adafruit_NeoPixel.h>

// ESP8266
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

// Webserver1 Config
const char *ssid = "amir1380"; //put your WIFI ssid in between " "
const char *password = "123456789"; // and password too
WebServer server1 ( 80 );

// Neopixel Config
#define NeoPIN 21 //D4 is 2
#define NUM_LEDS 8 //define number of leds in your strip, mine is 6 
int brightness = 150;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800);


const int led = 13;

void setup ( void ) {

  Serial.begin ( 115200 );

  // ##############
  // NeoPixel start
  Serial.println();
  strip.setBrightness(brightness);
  strip.begin();
  strip.show(); 
  delay(50);
  Serial.println("NeoPixel started");
  
  // #########
  // Webserver1
  pinMode ( led, OUTPUT );
  digitalWrite ( led, 0 );
  
  WiFi.begin ( ssid, password );
  Serial.println ( "" );

  // Wait for connection
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  Serial.println ( "" );
  Serial.print ( "Connected to " );
  Serial.println ( ssid );
  Serial.print ( "IP address: " );
  Serial.println ( WiFi.localIP() );

  if ( MDNS.begin ( "esp" ) ) {
    Serial.println ( "MDNS responder started" );
  }

  // what to do with requests
  server1.on ( "/", handleRoot );
  server1.onNotFound ( handleNotFound );
  server1.begin();

  
  
  Serial.println ( "HTTP server1 started" );
}

void loop ( void ) {
  // waiting fo a client
  server1.handleClient();
}


void handleRoot() {
  Serial.println("Client connected");
  digitalWrite ( led, 1 );
  
  // data from the colorpicker (e.g. #FF00FF)
  String color = server1.arg("c");
  Serial.println("Color: " + color);
  // setting the color to the strip 
  setNeoColor(color);

  // building a website
  char temp[5000];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  char clr [7];
  color.toCharArray(clr, 7);
  snprintf ( temp, 5000,

"<!DOCTYPE html>\n<html>\n\
  <head>\n\
    <title>Esp8266 NeoPixel Controler</title>\n\
    <style>\
      body { background-color: #cccccc; font-family: Arial; Color: #008; }\
    </style>\n\
    <meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0\" />\n\
  </head>\n\
  <body>\n\
    <h1>Amir_Eshaqy</h1>\n\
    <p>Uptime: %02d:%02d:%02d</p>\n\
    \n\
    <form action=\"\" name=\"pick\" method=\"post\">\n\
    <input type=\"color\" name=\"c\" value=\"%02d\" onchange=\"document.forms['pick'].submit();\" />\n\
    &nbsp;<span onclick=\"document.forms['pick'].submit();\" style=\"font-size:16pt;\"> CHANGE </span>\n\
    </form>\n\
    \n\
  </body>\
</html>",

    hr, min % 60, sec % 60, clr
  );
  server1.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
}

void handleNotFound() {
  digitalWrite ( led, 1 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server1.uri();
  message += "\nMethod: ";
  message += ( server1.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server1.args();
  message += "\n";

  for ( uint8_t i = 0; i < server1.args(); i++ ) {
    message += " " + server1.argName ( i ) + ": " + server1.arg ( i ) + "\n";
  }

  server1.send ( 404, "text/plain", message );
  digitalWrite ( led, 0 );
}



void setNeoColor(String value){
  Serial.print("Setting Neopixel...");
  // converting Hex to Int
  int number = (int) strtol( &value[1], NULL, 16);
  
  // splitting into three parts
  int r = number >> 16;
  int g = number >> 8 & 0xFF;
  int b = number & 0xFF;
  
  // DEBUG
  Serial.print("RGB: ");
  Serial.print(r, DEC);
  Serial.print(" ");
  Serial.print(g, DEC);
  Serial.print(" ");
  Serial.print(b, DEC);
  Serial.println(" ");
  
  // setting whole strip to the given color
  for(int i=0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color( g, r, b ) );
  }
  // init
  strip.show();
  
  Serial.println("on.");
}

I want to do this using a web server

Yes, it can

Then you can just use the webviewer component as you do with browser, calling webpages served from ESP mcu.

1 Like

It is true,
But there is a problem . Along with the rest of the code. Does not run

https://forum.arduino.cc/t/neopixel-color-control-code-problem-in-web-server/938355

What do you mean exactly?
I've just tested your code and it serve webpage as expected parsing the HTTP request correctly

I can't test Neopixel strip, but I assume that the instructions are correct

You are serving the page as root, not as /rgb

Yes the commands are correct and this code is completely correct.
And runs correctly alone.
Please try this code too. I have a problem with this code.
__
The name of the color control page is (192.168.27.50/rgb)

#include <WebServer.h>
#include <WiFi.h>
#include <DallasTemperature.h>
#include <ESPAsyncWebServer.h>
#include <OneWire.h>
#include <Adafruit_NeoPixel.h>
#include <WiFiClient.h>
#ifndef APSSID
#define APSSID "amir1380"
#define APPSK  "123456789"
#endif
 
float dama;
const char *ssid = APSSID;
const char *password = APPSK;

// Neopixel Config
#define NeoPIN 21 //D4 is 2
#define NUM_LEDS 8 //define number of leds in your strip, mine is 6 
int brightness = 150;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800);
const int led = 13;



 
// Data wire is connected to GPIO 5
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

// Variables to store temperature values
String temperatureF = "";
String temperatureC = "";

// Timer variables
unsigned long lastTime = 0;  
unsigned long timerDelay = 30000;


// Static IP 
IPAddress local_IP(192, 168,27,50);
// Gateway IP
IPAddress gateway(192, 168, 27, 1);

IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8);  
IPAddress secondaryDNS(8, 8, 4, 4); 

String header; 

const int relay1 = 15;                                     
const int relay2 = 2;
const int relay3 = 4;                                     
const int relay4 = 16;
const int relay5 = 17;
const int relay6 = 18;     
const int relay7 = 23;  
void handleNotFound(void);
void setNeoColor(void);
void handleRoot(void);
 ///////////////////////////////
WiFiServer server(80);  
WebServer server1 ( 81 );
////////////////////////////////////////
String readDSTemperatureC() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  float tempC = sensors.getTempCByIndex(0);

  if(tempC == -127.00) {
    Serial.println("Failed to read from DS18B20 sensor");
    return "--";
  } else {
    Serial.print("Temperature Celsius: ");
    Serial.println(tempC); 
  }
  return String(tempC);
}

String readDSTemperatureF() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  float tempF = sensors.getTempFByIndex(0);

  if(int(tempF) == -196){
    Serial.println("Failed to read from DS18B20 sensor");
    return "--";
  } else {
    Serial.print("Temperature Fahrenheit: ");
    Serial.println(tempF);
  }
  return String(tempF);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// Replaces placeholder with DS18B20 values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATUREC"){
    return temperatureC;
  }
  else if(var == "TEMPERATUREF"){
    return temperatureF;
  }
  return String();
}



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

  
  // ##############
  // NeoPixel start
  Serial.println();
  strip.setBrightness(brightness);
  strip.begin();
  strip.show(); 
  delay(50);
  Serial.println("NeoPixel started");
  
  // Start up the DS18B20 library
  sensors.begin();
  temperatureC = readDSTemperatureC();

  pinMode(relay1, OUTPUT);   
  pinMode(relay2, OUTPUT);                                           
  pinMode(relay3, OUTPUT);                                       
  pinMode(relay4, OUTPUT);                                  
  pinMode(relay5, OUTPUT);     
  pinMode(relay6, OUTPUT);                                
  pinMode(relay7, OUTPUT);
  digitalWrite(relay7, HIGH);
  digitalWrite(relay6, HIGH);
  digitalWrite(relay5, HIGH);
  digitalWrite(relay4, HIGH);  
  digitalWrite(relay3, HIGH);  
  digitalWrite(relay2, HIGH);  
  digitalWrite(relay1, HIGH);  
  // tatic IP 
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
   Serial.println("STA Failed to configure");
  }
  
   Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
   delay(500);
  Serial.print(".");}
  /*if (!WiFi.softAPConfig(local_IP, gateway, subnet)) {
    Serial.println("STA Failed to configure");
  }
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(local_IP);
  server.begin();*/

  

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());






// what to do with requests
 
  server1.begin();
  
  server.begin();

}

void loop() {
  server1.handleClient();

  //neopixelwebpaige
  server1.on ( "GET/rgb", handleRoot );
 server1.onNotFound ( handleNotFound );




 
 WiFiClient client = server.available(); 

 if ((millis() - lastTime) > timerDelay) {
    temperatureC = readDSTemperatureC();
     dama = temperatureC.toInt();
     Serial.println(dama);
     if (dama>30 ) { 
      digitalWrite(relay6, LOW);
    }
    if (dama<25 ) { 
      digitalWrite(relay6, HIGH);
    }
    if (dama<10 ) { 
      digitalWrite(relay7, LOW);
    }
    if (dama>25 ) { 
      digitalWrite(relay7, HIGH);
    }
    lastTime = millis();
    }
  
  if (client) 
  {                             
    Serial.println("New Client.");          
    String currentLine = "";               
    while (client.connected()) 
    {            
      if (client.available()) 
      {             
        char c = client.read();             
        Serial.write(c);                    
        header += c;
        if (c == '\n') 
        {                    

          if (currentLine.length() == 0) 
          {
       
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
          
            if (header.indexOf("GET /1") >= 0) 
            {
              if(digitalRead(relay1)== LOW)
              {
                Serial.println("Relay 1 OFF");
                digitalWrite(relay1, HIGH);
                client.print("Relay 1 OFF");
              }
              else
              {
                Serial.println("Relay 1 ON");
                digitalWrite(relay1, LOW);
                client.print("Relay 1 ON");
              }
            }
             
              else if (header.indexOf("GET /2") >= 0) 
            {
              if(digitalRead(relay2)== LOW)
              {
                Serial.println("Relay 2 OFF");
                digitalWrite(relay2, HIGH);
                client.print("Relay 2 OFF");
              }
              else
              {
                Serial.println("Relay 2 ON");
                
                digitalWrite(relay2, LOW);
                client.print("Relay 2 ON");
              }
            }
              
             else if (header.indexOf("GET /3") >= 0) 
            {
              if(digitalRead(relay3)== LOW)
              {
                Serial.println("Relay 3 OFF");
                digitalWrite(relay3, HIGH);
                client.print("Relay 3 OFF");
              }
              else
              {
                Serial.println("Relay 3 ON");
                digitalWrite(relay3, LOW);
                client.print("Relay 3 ON");
              }
            }
              // Relay 4 GPIO control
              else if (header.indexOf("GET /4") >= 0) 
            {
              if(digitalRead(relay4)== LOW)
              {
                Serial.println("Relay 4 OFF");
                digitalWrite(relay4, HIGH);
                client.print("Relay 4 OFF");
              }
              else
              {
                Serial.println("Relay 4 ON");
                digitalWrite(relay4, LOW);
                client.print("Relay 4 ON");
              } 
            }
              // Relay 5 GPIO control
              else if (header.indexOf("GET /5") >= 0) 
            {
              
                Serial.println("dar baz shod");
                client.print("dar baz shod");
                //client.print(temperatureC);
                digitalWrite(relay5, LOW);
                delay(200);
                digitalWrite(relay5, HIGH);
                

            }
            else if (header.indexOf("GET /6") >= 0) 
            {   
                client.print("<!DOCTYPE html><html><h1>");
                client.print(temperatureC);
                client.print("&deg;C");
                client.print("</h1></body></html>");
                Serial.println(dama);

           
            }else if (header.indexOf("GET /rgb41") >= 0) {
             
              }
            }

            
            client.stop();
            header = "";
          }
            
          
        }
      }    
   
    
    }
    
  }
  



void handleRoot() {
  Serial.println("Client connected");
  digitalWrite ( led, 1 );
  
  // data from the colorpicker (e.g. #FF00FF)
  String color = server1.arg("c");
  Serial.println("Color: " + color);
  // setting the color to the strip 
  setNeoColor(color);

  // building a website
  char temp[5000];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  char clr [7];
  color.toCharArray(clr, 7);
 snprintf ( temp, 5000,

"<!DOCTYPE html>\n<html>\n\
  <head>\n\
    <title>Esp8266 NeoPixel Controler</title>\n\
    <style>\
      body { background-color: #cccccc; font-family: Arial; Color: #008; }\
    </style>\n\
    <meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0\" />\n\
  </head>\n\
  <body>\n\
    <h1>Amir_Eshaqy</h1>\n\
    <p>Uptime: %02d:%02d:%02d</p>\n\
    \n\
    <form action=\"\" name=\"pick\" method=\"post\">\n\
    <input type=\"color\" name=\"c\" value=\"%02d\" onchange=\"document.forms['pick'].submit();\" />\n\
    &nbsp;<span onclick=\"document.forms['pick'].submit();\" style=\"font-size:16pt;\"> CHANGE </span>\n\
    </form>\n\
    \n\
  </body>\
</html>",

    hr, min % 60, sec % 60, clr);
  server1.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
}

void handleNotFound() {
  digitalWrite ( led, 1 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server1.uri();
  message += "\nMethod: ";
  message += ( server1.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server1.args();
  message += "\n";

  for ( uint8_t i = 0; i < server1.args(); i++ ) {
    message += " " + server1.argName ( i ) + ": " + server1.arg ( i ) + "\n";
  }

  server1.send ( 404, "text/plain", message );
  digitalWrite ( led, 0 );
}



void setNeoColor(String value){
  Serial.print("Setting Neopixel...");
  // converting Hex to Int
  int number = (int) strtol( &value[1], NULL, 16);
  
  // splitting into three parts
  int r = number >> 16;
  int g = number >> 8 & 0xFF;
  int b = number & 0xFF;
  
  // DEBUG
  Serial.print("RGB: ");
  Serial.print(r, DEC);
  Serial.print(" ");
  Serial.print(g, DEC);
  Serial.print(" ");
  Serial.print(b, DEC);
  Serial.println(" ");
  
  // setting whole strip to the given color
  for(int i=0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color( g, r, b ) );
  }
  // init
  strip.show();
  
  Serial.println("on.");
}

I think I made a mistake in the code
The web page does not pop up in this code(192.168.27.50/rgb)
Thank you.

You're doing a weird confusing mix of code ...
Include the ESPAsyncWebServer.h file and then use the server1 object (which has nothing to do with this library) on port 81 which you must then explicit in the HTTP request

The WiFiServer object on port 80 is completely useless. It is a server that makes "raw" TCP sockets available, and is not capable of processing HTTP requests.

Also this instructions (which goes in the setup and not in the loop) is wrong.

  • GET is the type of request handled in handleRoot () function,
  • /rgb is the address of resourse requested (http://192.168.27.50:81/rgb)
server1.on ( "GET/rgb", handleRoot );

1 Like

Sorry, I could not correct this code.
You can help me
If possible
Thank you very much.

Yes, no problem at all.
But I'm not sure this will help you increasing your programming skills :wink:

Anyway, try with this code.

N.B.
I've commented out things relative to temperature because I haven't the sensor and without ESP get's crash. Also static IP handling is commented out.

#include <WiFi.h>
#include <WebServer.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Adafruit_NeoPixel.h>

#ifndef APSSID
#define APSSID "xxxxxxxxx"
#define APPSK  "xxxxxxxxx"
#endif

float dama;
const char *ssid = APSSID;
const char *password = APPSK;

// Neopixel Config
#define NeoPIN 21 //D4 is 2
#define NUM_LEDS 8 //define number of leds in your strip, mine is 6 
int brightness = 150;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800);
const int led = 13;

// Data wire is connected to GPIO 5
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

// Variables to store temperature values
String temperatureF = "";
String temperatureC = "";

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;

/* Don't match with my network, uncomment to restore static IP

  // Static IP
  IPAddress local_IP(192, 168,27,50);
  // Gateway IP
  IPAddress gateway(192, 168, 27, 1);

  IPAddress subnet(255, 255, 0, 0);
  IPAddress primaryDNS(8, 8, 8, 8);
  IPAddress secondaryDNS(8, 8, 4, 4);
*/

const byte relayPins[] = {15, 2, 4, 16, 17, 18, 23} ;

// Functions prototypes
void handleNotFound(void);
void handleRoot(void);
void handleColot(void);

void setNeoColor(void);

///////////////////////////////

WebServer webServer(80);

////////////////////////////////////////
String readDSTemperatureC() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  if (tempC == -127.00) {
    Serial.println("Failed to read from DS18B20 sensor");
    return "--";
  } else {
    Serial.print("Temperature Celsius: ");
    Serial.println(tempC);
  }
  return String(tempC);
}

String readDSTemperatureF() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures();
  float tempF = sensors.getTempFByIndex(0);

  if (int(tempF) == -196) {
    Serial.println("Failed to read from DS18B20 sensor");
    return "--";
  } else {
    Serial.print("Temperature Fahrenheit: ");
    Serial.println(tempF);
  }
  return String(tempF);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// Replaces placeholder with DS18B20 values
String processor(const String& var) {
  //Serial.println(var);
  if (var == "TEMPERATUREC") {
    return temperatureC;
  }
  else if (var == "TEMPERATUREF") {
    return temperatureF;
  }
  return String();
}



void setup() {
  Serial.begin(115200);
  // ##############
  // NeoPixel start
  Serial.println();
  strip.setBrightness(brightness);
  strip.begin();
  strip.show();
  delay(50);
  Serial.println("NeoPixel started");

  // set relay pin with a for cycle
  
  for (int i = 0; i < sizeof(relayPins) / sizeof(byte); i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], HIGH);
  }
  

  // Start up the DS18B20 library
  //sensors.begin();                      // I need to comment this because I haven't
  //temperatureC = readDSTemperatureC();  // I need to comment this because I haven't

  /* Don't match with my network, uncomment to restore static IP
    // Static IP
    if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
    }
  */

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // what to do with HTTP requests 
  webServer.on ( "/", handleRoot );
  webServer.on ( "/rgb", handleColor );     // HTTP request mu be something like http://192.168.27.50/rgb?c=#7866FF
  webServer.on ( "/relay", handleRelays );  // HTTP request mu be something like http://192.168.27.50/relay?number=1&value=0
  webServer.onNotFound ( handleNotFound );
  webServer.begin();
}

void loop() {
  webServer.handleClient();

  if ((millis() - lastTime) > timerDelay) {
    //temperatureC = readDSTemperatureC();    // I need to comment this because I haven't
    //dama = temperatureC.toInt();            // I need to comment this because I haven't
    Serial.println(dama);
    if (dama > 30 ) {
      digitalWrite(relayPins[5], LOW);
    }
    if (dama < 25 ) {
      digitalWrite(relayPins[5], HIGH);
    }
    if (dama < 10 ) {
      digitalWrite(relayPins[6], LOW);
    }
    if (dama > 25 ) {
      digitalWrite(relayPins[6], HIGH);
    }
    lastTime = millis();
  }
}


void handleRoot() {
  String rgbLink = "http://";
  rgbLink += WiFi.localIP().toString();
  rgbLink += "/rgb";

  String relayLink = "http://";
  relayLink += WiFi.localIP().toString();
  relayLink += "/relay?num=1&val=0";
  
  String reply = "This resource is not handled.";
  reply += "Try with: <br><br><a href='" + rgbLink + "'>" + rgbLink + "</a>";
  reply += "<br><br><a href='" + relayLink + "'>" + relayLink + "</a> " ;
  reply += "(relay will be activated with val=0)";


  // Send reply to client
  webServer.send ( 200, "text/html", reply );
}

void handleRelays() {
  int relayNo = webServer.arg("num").toInt();
  int relayVal = webServer.arg("val").toInt();

  // Array start always from 0, so -1 in needed to get the right pin from relayPins
  digitalWrite(relayPins[relayNo-1], relayVal); 
  
  String reply = "Relay ";
  reply += relayNo;
  reply += " (connected on pin ";
  reply += relayPins[relayNo-1];
  reply += ") is now ";
  reply += relayVal ? " OFF" : " ON"; // Relay will be activated on 0
  Serial.println(reply);
  
  // Send reply to client
  webServer.send ( 200, "text/plain", reply );
}


void handleColor() {
  Serial.println("Client connected");
  digitalWrite (led, 1 );

  // data from the colorpicker (e.g. #FF00FF)
  String color = webServer.arg("c");
  Serial.println("Color: " + color);
  // setting the color to the strip
  setNeoColor(color);

  // building a website
  char temp[5000];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  char clr [7];
  color.toCharArray(clr, 7);
  snprintf ( temp, 5000,

             "<!DOCTYPE html>\n<html>\n\
  <head>\n\
    <title>Esp8266 NeoPixel Controler</title>\n\
    <style>\
      body { background-color: #cccccc; font-family: Arial; Color: #008; }\
    </style>\n\
    <meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0\" />\n\
  </head>\n\
  <body>\n\
    <h1>Amir_Eshaqy</h1>\n\
    <p>Uptime: %02d:%02d:%02d</p>\n\
    \n\
    <form action=\"\" name=\"pick\" method=\"post\">\n\
    <input type=\"color\" name=\"c\" value=\"%02d\" onchange=\"document.forms['pick'].submit();\" />\n\
    &nbsp;<span onclick=\"document.forms['pick'].submit();\" style=\"font-size:16pt;\"> CHANGE </span>\n\
    </form>\n\
    \n\
  </body>\
</html>",

         hr, min % 60, sec % 60, clr);
  webServer.send ( 200, "text/html", temp );
  digitalWrite ( led, 0 );
}

void handleNotFound() {
  digitalWrite ( led, 1 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += webServer.uri();
  message += "\nMethod: ";
  message += ( webServer.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += webServer.args();
  message += "\n";

  for ( uint8_t i = 0; i < webServer.args(); i++ ) {
    message += " " + webServer.argName ( i ) + ": " + webServer.arg ( i ) + "\n";
  }

  webServer.send ( 404, "text/plain", message );
  digitalWrite ( led, 0 );
}



void setNeoColor(String value) {
  Serial.print("Setting Neopixel...");
  // converting Hex to Int
  int number = (int) strtol( &value[1], NULL, 16);

  // splitting into three parts
  int r = number >> 16;
  int g = number >> 8 & 0xFF;
  int b = number & 0xFF;

  // DEBUG
  Serial.print("RGB: ");
  Serial.print(r, DEC);
  Serial.print(" ");
  Serial.print(g, DEC);
  Serial.print(" ");
  Serial.print(b, DEC);
  Serial.println(" ");

  // setting whole strip to the given color
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color( g, r, b ) );
  }
  // init
  strip.show();

  Serial.println("on.");
}
1 Like

Wow your work is great
Thank you so much . :white_heart: :white_heart:
Unfortunately, there are very few educational resources in our language.
And I'm weak in English, but I'll do my best anyway :slightly_smiling_face:

You're welcome.
If you need additional info, you just have to ask.

1 Like

"this code is completely correct." != "Please try this code too. I have a problem with this code."

Only one of those statements is true.

I do not catch you?
My code was wrong.

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

1 Like