I'm not able to keep the placeholder update and send to my global string

My serial always returns ""No message sent", apparently my function does not work, if I try to force send a function through "else" ESP crash, could someone help me?

example code. It contains a lot of other information that I don't think is going into question.

#include <ESPAsyncWebServer.h>

const char* input_color = "value";

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP32</title>  
</head>
<body>
  <h2>FAN 125 PRO</h2>

<input type="color" id="valuecolor" onchange="colorled(this)" value="%COLOR%">
 <p><span id="textcolorled">%COLOR%</span></p>
<script>

function corlorled(element) {  
  var colorled = document.getElementById("valuecolor").value;
  document.getElementById("textcolorled").innerHTML = colorled;
  console.log(colorled);
  var xhm = new XMLHttpRequest();
  xhm.open("GET", "/valuecolor"+colorled, true);
  xhm.send();
}

</script>
</body>
</html>
)rawliteral";

String processor(const String& var){
  if (var == "COLOR"){    
    return colorled;
  }
  return String();
}

void setup(){

 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });


  server.on("/valuecolor", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String sms;
    if (request->hasParam(input_color)) {
      sms = request->getParam(input_color)->value();
      colorled = sms;
    }
    else {      
      sms = "No message sent"; // my serial always returns ""No message sent", when I make a color change
/////////////////if I try to force send a function through "else" ESP crash
    }
    Serial.println(sms);
    request->send(200, "text/plain", "OK");
  });

  server.begin();
}

what's colorled ?? show us the full code

1 Like

colorled is a string that stores the value to return the void setNeoColor .


#include <ESPAsyncWebServer.h>
#include <Adafruit_NeoPixel.h>

#include <analogWrite.h>

#define NeoPIN 4 //D4 is 2
#define NUM_LEDS 30 //define number of leds in your strip, mine is 6 
int brightness = 100;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800);

const int led = 21;

AsyncWebServer server(80);
const int led_pin = 13;

String colorled = "#ff0000";

const char* input_color = "value";

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP32</title>
  <style>
     
  </style>
</head>
<body>
  <h2>FAN 125 PRO</h2>
 <input type="color" id="valuecolor" onchange="colorled(this)" value="%COLOR%">
 <p><span id="textcolorled">%COLOR%</span></p>
<script>
<script>

function corlorled(element) {  
  var colorled = document.getElementById("valordacor").value;
  document.getElementById("textcolorled").innerHTML = colorled;
  console.log(colorled);
  var xhm = new XMLHttpRequest();
  xhm.open("GET", "/valuecor"+colorled, true);
  xhm.send();
}


</script>
</body>
</html>
)rawliteral";



String processor(const String& var){
  if (var == "COLOR"){    
    return colorled;
  }
  return String();
}


void setup(){
  Serial.begin(115200);
  strip.setBrightness(brightness);
  strip.begin();
  strip.show(); 
  pinMode (led, OUTPUT );
  digitalWrite (led, 1 );
  setNeoColor(colorled);


 server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  
    request->send_P(200, "text/html", index_html, processor);
  });


  server.on("/valuecolor", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String sms;
    if (request->hasParam(input_color)) {
      sms = request->getParam(input_color)->value();
      colorled = sms;
    }
    else {      
      sms = "No message sent";
    }
    Serial.println(sms);
    request->send(200, "text/plain", "OK");
  });
  server.begin();
}


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.");
}
 
void loop() {

    
}

wifi is not configured, how can this work?
the HTML is full of spelling mistakes for variables

1 Like

I did not look at how the NeoPIN should be defined but I did clean up the HTML and javascript mess, try this

#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <Adafruit_NeoPixel.h>

const char*   ssid = "XXX";
const char*   password = "XXX";

#define NeoPIN 4 //D4 is 2
#define NUM_LEDS 30 //define number of leds in your strip, mine is 6 

int brightness = 100;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NeoPIN, NEO_RGB + NEO_KHZ800);
AsyncWebServer server(80);
const int led_pin = 13;
const int led = 21;
String stripRGBColor = "FF0000"; // full red

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ESP32</title>
  <style></style>
  <script>
    function changeColor() {  
      var choosenColor = document.getElementById("colorPickerID").value;
      console.log("new color chosen : " + choosenColor);
      document.getElementById("colorTextID").innerHTML = choosenColor;
      var httpRequest = new XMLHttpRequest();
      httpRequest.open("GET", "/setColor?color=" + choosenColor.slice(1), true);
      httpRequest.send(null);
    }
</script>
</head>

<body>
  <h2>FAN 125 PRO</h2>
 <input type="color" id="colorPickerID" onchange="changeColor()" value="%COLOR%"> (<span id="colorTextID">%COLOR%</span>)</p>
</body></html>
)rawliteral";


void setNeoColor(String& value) {   // parameter is a color as "RRGGBB"
  unsigned long colorValue = strtoul(value.c_str(), NULL, 16); // we get the color as "RRGGBB" in HEX
  // splitting into three parts
  int r = (colorValue >> 16) & 0xFF;
  int g = (colorValue >> 8 ) & 0xFF;
  int b = colorValue & 0xFF;

  // setting whole strip to the given color
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color( g, r, b ) ); // would likely work passing directly colorValue
  }
  strip.show();
  Serial.print("Set strip color to 0x");
  if (r < 0x10) Serial.write('0');
  Serial.print(r, HEX);
  if (g < 0x10) Serial.write('0');
  Serial.print(g, HEX);
  if (b < 0x10) Serial.write('0');
  Serial.println(b, HEX);
}

String processor(const String& var) {
  if (var == "COLOR") {
    return String("#") + stripRGBColor;
  }
  return var;
}

void notFound(AsyncWebServerRequest *request) {
  request->send(404, "text/plain", "Not found");
}

void setup() {
  pinMode (led, OUTPUT );
  digitalWrite (led, HIGH );

  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    while (true) yield();
  }

  Serial.print("Open URL http://"); Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send_P(200, "text/html", index_html, processor);
  });


  server.on("/setColor", HTTP_GET, [] (AsyncWebServerRequest * request) {
    if (request->hasParam("color")) {
      stripRGBColor = request->getParam("color")->value();
      setNeoColor(stripRGBColor);
    } else {
      Serial.println("URL error");;
    }
    request->send(200, "text/plain", "OK");
  });

  // error
  server.onNotFound(notFound);

  strip.setBrightness(brightness);
  strip.begin();
  setNeoColor(stripRGBColor);

  server.begin();
}

void loop() {}

(note that passing a parameter starting with # does not get decoded correctly so I send the color without the # in the URL)

excuse the ignorance, I ended up not sending the server settings, wifi etc.
the HTML was not complete sorry

it works!!, you are amazing i love you.

:wink: LOL

have fun

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.