Hi, sorry for inconvenience. Yesterday I used mobile to create this topic, I could not find how to post code with mobile phone. Below is the full code. Please take a look at lines 175, 184 and 187 for the show( ) calling.
The circuit is simple as the picture. Even the logs show the correct for loop with number of leds, there is only 1 first led lighted up
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include "configs.h"
#if defined (EEPROM_ENABLED)
#include <EEPROM.h>
#endif
#include "html.h"
#include "js.h"
const byte DNS_PORT = 53;
const char WiFiPassword[] = "12345678";
const char AP_NameChar[] = "LEDControl" ;
DNSServer dnsServer;
#define DATA_PIN D6 // The DATA PIN for the assignable LEDs
CRGB leds[MAX_NUM_LEDS]; // Define the array of leds
#include <Adafruit_GFX.h> // Include core graphics library for the display
#include <Adafruit_SSD1306.h> // Include Adafruit_SSD1306 library to drive the display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//WiFiServer server(80);
ESP8266WebServer webserver(80);
String request = "";
ledSettings_st ledSettings;
String oled_color_hex = "";
#if defined (WIFI_AP_CONFIG_MANUAL)
IPAddress WIFI_AP_IP (192,168,4,23); /* NOTE: The seperator should be a comma (,) */
IPAddress WIFI_AP_GATEWAY (192,168,4,1);
IPAddress WIFI_AP_SUBNET (255,255,255,0);
#else
IPAddress WIFI_AP_IP (192,168,4,1);
#endif
void handleWebRoot( void );
void handleLED();
void LocalWriteDatabase( byte h, byte s, byte v, uint16_t numled, byte state )
{
#if defined (EEPROM_ENABLED)
EEPROM.write( DB_LED_COLOR_ADDR_HUE, h );
EEPROM.write( DB_LED_COLOR_ADDR_SAT, s );
EEPROM.write( DB_LED_COLOR_ADDR_BRI, v );
byte byte1 = numled >> 8;
byte byte2 = numled & 0x00FF;
EEPROM.write( DB_LED_COLOR_ADDR_NUM, byte1 );
EEPROM.write( DB_LED_COLOR_ADDR_NUM + 1, byte2 );
EEPROM.write( DB_LED_STATE_ADDR, state );
if ( EEPROM.commit() )
{
Serial.println("EEPROM successfully committed");
} else {
Serial.println("ERROR! EEPROM commit failed");
}
#endif
}
bool LocalReadEEPROM( ledSettings_st &ledSettings )
{
bool ret = false;
#if defined (EEPROM_ENABLED)
byte indicator = EEPROM.read(0);
Serial.print("EEPROM indicator: "); Serial.println(indicator, HEX);
if ( DATABASE_INDICATOR == indicator )
{
ledSettings.hue = EEPROM.read(DB_LED_COLOR_ADDR_HUE);
ledSettings.saturation = EEPROM.read(DB_LED_COLOR_ADDR_SAT);
ledSettings.bright = EEPROM.read(DB_LED_COLOR_ADDR_BRI);
byte byte1 = EEPROM.read(DB_LED_COLOR_ADDR_NUM);
byte byte2 = EEPROM.read(DB_LED_COLOR_ADDR_NUM + 1);
ledSettings.numled = ((uint16_t)byte1 << 8) + (uint16_t)byte2;
ledSettings.state = EEPROM.read(DB_LED_STATE_ADDR);
ret = true;
}
else
{
//Wrong database
EEPROM.write( 0, DATABASE_INDICATOR );
ledSettings.hue = 0;
ledSettings.saturation = 0;
ledSettings.bright = 0;
ledSettings.numled = MAX_NUM_LEDS;
ledSettings.state = LED_OFF;
LocalWriteDatabase( ledSettings.hue, ledSettings.saturation,
ledSettings.bright, ledSettings.numled, ledSettings.state );
}
#endif
return ret;
}
void setup()
{
#if defined (EEPROM_ENABLED)
EEPROM.begin(512);
#endif
Serial.begin(115200);
Serial.println(WiFi.macAddress());
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize display with the I2C address of 0x3C
display.clearDisplay(); // Clear the buffer
display.setTextColor(WHITE); // Set color of the text
display.setRotation(0); // Set orientation. Goes from 0, 1, 2 or 3
display.setTextWrap(true); // By default, long lines of text are set to automatically βwrapβ back to the leftmost column.
display.dim(0); //Set brightness (0 is maximun and 1 is a little dim)
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, MAX_NUM_LEDS);
WiFi.mode(WIFI_AP);
#if defined (WIFI_AP_CONFIG_MANUAL)
WiFi.softAPConfig(WIFI_AP_IP, WIFI_AP_GATEWAY, WIFI_AP_SUBNET);
#endif
String AP_NameMAC = String("LED ") + String(WiFi.macAddress());
boolean conn = WiFi.softAP(AP_NameMAC);
dnsServer.setTTL(300);
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);
#if defined (WIFI_AP_CONFIG_MANUAL)
dnsServer.start(DNS_PORT, LED_CONTROL_DOMAIN_NAME, WIFI_AP_IP);
#else
dnsServer.start(DNS_PORT, LED_CONTROL_DOMAIN_NAME, WIFI_AP_IP);
#endif
webserver.on( "/", handleWebRoot );
webserver.on( "/led", HTTP_POST, handleLED );
webserver.begin();
if ( LocalReadEEPROM( ledSettings ) == true )
{
Serial.printf("Database - hue(%d) - saturation(%d) - bright(%d) - numled(%d) - state(%d)\n",
ledSettings.hue, ledSettings.saturation, ledSettings.bright,
ledSettings.numled, ledSettings.state);
}
else
{
Serial.println("No database found!!!");
}
FastLED.setMaxRefreshRate(0);
/* Turn off */
for( int i = 0; i < MAX_NUM_LEDS; i++ )
{
leds[i] = CHSV(0, 0, 0);
// FastLED.show();
}
/* Update last settings */
if ( LED_ON == ledSettings.state )
{
for( int i = 0; i < ledSettings.numled; i++ )
{
leds[i] = CHSV(ledSettings.hue, ledSettings.saturation, ledSettings.bright);
// FastLED.show();
}
}
FastLED.show();
char buff[10];
sprintf( buff, "#%02X%02X%02X", ledSettings.hue, ledSettings.saturation, ledSettings.bright );
oled_color_hex = String( buff );
display.setTextSize(2); // Set text size. We are using a custom font so you should always use the text size of 0
display.setCursor(44, 20); // (x,y)
if ( LED_ON == ledSettings.state )
{
Serial.println(String("OLED: ")+oled_color_hex);
display.print(oled_color_hex);
}
else
{
Serial.println(String("OLED: OFF"));
display.print(String("OFF"));
}
display.display();
Serial.println("Server started");
// pinMode(LED_BUILTIN, OUTPUT);
} // void setup()
String log_debug;
void loop()
{
// display.clearDisplay(); // Clear the display so we can refresh
display.setTextSize(1);
display.setCursor(0, 45);
display.println(" WiFi MAC Address:");
display.setCursor(0, 55);
display.print(" ");
display.println(WiFi.macAddress());
display.display();
dnsServer.processNextRequest();
webserver.handleClient();
display.setTextSize(2); // Set text size. We are using a custom font so you should always use the text size of 0
display.setCursor(44, 20); // (x,y)
delay(5);
// The client will actually be disconnected when the function returns and 'client' object is detroyed
} // void loop()
void handleLED()
{
bool requestValid = false;
String onoff_str = webserver.arg("onoff");
if ( ! onoff_str.isEmpty() )
{
ledSettings.state = LED_ON;
}
else
{
Serial.println("LED OFF");
ledSettings.state = LED_OFF;
}
String hue_str = webserver.arg("hue");
if ( ! hue_str.isEmpty() )
{
ledSettings.hue = hue_str.toInt();
}
String sat_str = webserver.arg("sat");
if ( ! sat_str.isEmpty() )
{
ledSettings.saturation = sat_str.toInt();
}
String bright_str = webserver.arg("bright");
if ( ! bright_str.isEmpty() )
{
ledSettings.bright = bright_str.toInt();
}
String lednum_str = webserver.arg("lednumber");
if ( ! lednum_str.isEmpty() )
{
ledSettings.numled = lednum_str.toInt();
requestValid = true;
}
if ( requestValid )
{
Serial.print("Update LEDS #");
Serial.print(ledSettings.hue, HEX);
Serial.print(ledSettings.saturation, HEX);
Serial.print(ledSettings.bright, HEX);
Serial.print(" - Num: ");
Serial.print(ledSettings.numled, DEC);
Serial.print(" - State: ");
Serial.println(ledSettings.state, DEC);
for( int i = 0; i < MAX_NUM_LEDS; i++ )
{
leds[i] = CHSV( 0, 0, 0 ); //Turn off all LEDs
// FastLED.show();
}
//Update color
if ( LED_ON == ledSettings.state )
{
Serial.println("Update LEDs color");
for( int i = 0; i < ledSettings.numled; i++ )
{
leds[i] = CHSV( ledSettings.hue, ledSettings.saturation, ledSettings.bright );
// FastLED.show();
}
}
delay(100);
FastLED.show();
LocalWriteDatabase( ledSettings.hue, ledSettings.saturation,
ledSettings.bright, ledSettings.numled, ledSettings.state );
char buff[10];
sprintf( buff, "#%02X%02X%02X", ledSettings.hue, ledSettings.saturation, ledSettings.bright );
oled_color_hex = String( buff );
display.setTextSize(2); // Set text size. We are using a custom font so you should always use the text size of 0
display.setCursor(44, 20); // (x,y)
display.clearDisplay();
if ( LED_ON == ledSettings.state )
{
Serial.println(String("OLED: ")+oled_color_hex);
display.print(oled_color_hex);
}
else
{
Serial.println(String("OLED: OFF"));
display.print(String("OFF"));
}
display.display();
}
else
{
Serial.println("Client has no request!!!");
}
// digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
webserver.sendHeader("Location","/");
webserver.send(303);
}
void handleWebRoot( void )
{
webserver.sendContent( header );
webserver.sendContent( html_style );
// webserver.sendContent( log_debug );
webserver.sendContent("<form id='F1' action='/led' method='POST'>");
String html_switch_preview = html_onoff;
String str_preview = String("hsl(") + String((ledSettings.hue*100/255)*360/100)
+ String(", ") + String((ledSettings.saturation*100/255)*100/100)
+ String("%, ") + String((ledSettings.bright*100/255)*50/100)
+ String("%)");
html_switch_preview.replace("%state", (LED_ON==ledSettings.state)?"checked":"");
html_switch_preview.replace("%s", String(str_preview));
webserver.sendContent( html_switch_preview );
String html_preview = html_preview_hex;
html_preview.replace("%s", oled_color_hex);
webserver.sendContent( html_preview );
String html_hue = html_sb_hue;
html_hue.replace("%s", String(ledSettings.hue));
webserver.sendContent( html_hue );
String html_sat = html_sb_sat;
html_sat.replace("%s", String(ledSettings.saturation));
webserver.sendContent( html_sat );
String html_val = html_sb_bri;
html_val.replace("%s", String(ledSettings.bright));
webserver.sendContent( html_val );
String html_5 = html_numled;
html_5.replace("%numled", String(ledSettings.numled));
html_5.replace("%maxled", String(MAX_NUM_LEDS));
webserver.sendContent( html_5 );
webserver.sendContent( html_save );
webserver.sendContent( String("</form>") );
webserver.sendContent( html_js );
webserver.sendContent( html_tail );
webserver.sendContent(WiFi.macAddress());
}