The simplest DIY intelligent color lamp ⑤ based on ESP8266 (use C# palette UI to control WS2812 color lamp through serial port)

ESP8266 and ESP32 intelligent lantern development series article directory

Part 1: the simplest DIY intelligent color lamp ① based on ESP8266 (use the button to light up the ordinary RGB lamp on the web page)
Part 2: the simplest DIY intelligent color lamp based on ESP8266 ② (RGB lamp is controlled by buttons and sliding bars on Web pages)
Part 3: the simplest DIY intelligent color lamp ③ based on ESP8266 (RGB lamp is controlled by button + slider + mobile APP on Web page)
Part IV: the simplest DIY domestic WS2812 intelligent color lamp ④ based on ESP8266 (lit with a button on the web page)
Part 5: the simplest DIY intelligent lantern ⑤ based on ESP8266 (use C# palette UI to control WS2812 lantern through serial port)

preface

The simplest original core technology of the Internet of things DIY intelligent lantern ⑤ based on ESP8266 (use the C# palette UI to control the WS2812 lantern through the serial port).
There are various open-source intelligent color lamp host computer programs on the market, but some are complex and some are simple. If you want to get started quickly, use the C# graphical interface to light up the domestic WS2812RGB lamp through the serial port, this scheme will give you a fast and efficient scheme.

1, What is the simplest DIY intelligent color lamp ⑤ based on ESP8266 (using C# palette UI to control WS2812 color lamp through serial port)?

The lighting procedures previously written are just to directly light the LED. For example, the first, second and third articles of this column do not involve complex PWM or timing problems. This time, an intelligent color lamp scheme is changed. Domestic chips WS2812 and WS2811 are used to realize the color matching of intelligent color lamps. The main control chip adopts domestic ESP8266, which supports domestic start from me, opposes hegemonism, technology blockade and chip monopoly. Now, the color lights arranged on the walls of many buildings are this series of color lights. Each color light contains WS2812 chip. When the number of color lights reaches a certain level, the cool effect will come out. In order to learn this lantern, daodanjishui bought 33 pieces of 8X8=64 WS2812 matrix lanterns at a treasure. A matrix costs about 20 pieces, so the total value is 20 * 33 = 660 pieces. Later, these matrix lanterns will be spliced together and added to image processing, intelligent app client and computer client to control the lanterns, In 2021, when the new coronavirus was isolated at home for the new year, a batch of codes were written. At present, there is no open source tutorial on the omni-directional breaking of various upper and lower computers and servers of WS2812 color lights in China, so I hope to be the first one here. The family photo is shown below:

Function Description: the simplest DIY intelligent color lamp ① based on ESP8266 (use the button to light up the ordinary RGB lamp on the web page) was an ordinary RGB LED lamp without a chip. PWM wave was used to control the brightness, and the color was controlled by controlling the PWM wave ratio of three color filaments. This time, the ESP8266 library uses its IO port to control the LED indirectly through the WS2812 chip, and the IO port is controlled through the single line zeroing protocol. As for how to use this protocol, we don't need to go deep into it, because the WS2812 Library of Arduino helps us do a good job of driving, we can directly call the function driver. Here we use the FastLED library. If you really want to go deep into it, Wait until I release the WS2812 project of STM32 version. The final effect is that ESP8266 creates a hotspot, and then the user controls the change of color lights on its embedded home page. This time, click a light, WS2811 and WS2812 respectively. In fact, the code is universal. More lights can be lit only after the first light is lit. The following is a screenshot of the original open source code written by Arduino, in which 64 colored lights are used:

The software UI design adopts C# language. The VS2013 integrated development environment is used to develop the host computer, and the Arduino IDE is used to develop the ESP8266 program. The ESP8266 uses an IO port to communicate with WS2812 to control the color of 8 * 8 = 64 lights, while retaining the function of Web lighting, which can be controlled simultaneously by web lighting and computer C# UI lighting, Ordinary people really can't write such an effect. Because there may be conflict. The design is: the simplest DIY is an upgraded version of the domestic WS2812 intelligent color lamp ④ (lit with a button on the web page) based on ESP8266, which retains the original function and adds new functions.

The hardware uses: ESP-12F D1 development board and WS2812 lantern matrix, which are sold online. After the phone is connected to the ESP6266 hotspot, open the embedded home page in the browser:

The operation effect is as follows:

The video of station B is as follows:

The simplest DIY intelligent color lamp ⑤ based on ESP8266 (use C# palette UI to control WS2812 color lamp through serial port)

The video address is: https://www.bilibili.com/video/BV1af4y1M7QC?t=0.0

2, Use steps

1. Prepare hardware

(1) Purchase ESP8266 development board: mini D1 wifi ESP-12F N ESP8266.

The chip packaging is as follows:

(2) Buy WS2812 domestic RGB lamp board. Note that 8 * 8 = 64 lamps.

2. Build Arduino development environment

(1) The first step is to download the Arduino IDE installation. Double click Next until it is completed. It is recommended not to use the Chinese path.

(2) Download the ESP8266 installation package plug-in, which is the most convenient. If you search and install in Arduino, you will find it very slow, especially in this critical period of intense chip competition, download it according to this website: https://www.arduino.cn/thread-76029-1-1.html

(3) Double click one of the three packages downloaded in (2). I double-click to install the second package. Restart Arduino to install it.

After installation, you will find that there are more development boards such as ESP8266. If you can't, just try other tutorials. There's no need to teach this development environment step by step.
(3.1) import the FASTLED library, or install the FASTLED Library (you can install it yourself)

Can open a red arrow official sample code to play
Mainly modify the following parameters:
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 64
The first is the model of RGB lamp, which can be 2812 or 2811
The third is the number of RGB lights. My setting is 64, which is different from the previous issue

#include <FastLED.h>

FASTLED_USING_NAMESPACE

// FastLED "100-lines-of-code" demo reel, showing just a few 
// of the kinds of animation patterns you can quickly and easily 
// compose using FastLED.  
//
// This example also shows one easy way to define multiple 
// animations patterns and have them automatically rotate.
//
// -Mark Kriegsman, December 2014

#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif

#define DATA_PIN    3
//#define CLK_PIN   4
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    64
CRGB leds[NUM_LEDS];

#define BRIGHTNESS          96
#define FRAMES_PER_SECOND  120

void setup() {
  delay(3000); // 3 second delay for recovery
  
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
}


// List of patterns to cycle through.  Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };

uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  
void loop()
{
  // Call the current pattern function once, updating the 'leds' array
  gPatterns[gCurrentPatternNumber]();

  // send the 'leds' array out to the actual LED strip
  FastLED.show();  
  // insert a delay to keep the framerate modest
  FastLED.delay(1000/FRAMES_PER_SECOND); 

  // do some periodic updates
  EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
}

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

void nextPattern()
{
  // add one to the current pattern number, and wrap around at the end
  gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}

void rainbow() 
{
  // FastLED's built-in rainbow generator
  fill_rainbow( leds, NUM_LEDS, gHue, 7);
}

void rainbowWithGlitter() 
{
  // built-in FastLED rainbow, plus some random sparkly glitter
  rainbow();
  addGlitter(80);
}

void addGlitter( fract8 chanceOfGlitter) 
{
  if( random8() < chanceOfGlitter) {
    leds[ random16(NUM_LEDS) ] += CRGB::White;
  }
}

void confetti() 
{
  // random colored speckles that blink in and fade smoothly
  fadeToBlackBy( leds, NUM_LEDS, 10);
  int pos = random16(NUM_LEDS);
  leds[pos] += CHSV( gHue + random8(64), 200, 255);
}

void sinelon()
{
  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy( leds, NUM_LEDS, 20);
  int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  leds[pos] += CHSV( gHue, 255, 192);
}

void bpm()
{
  // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  uint8_t BeatsPerMinute = 62;
  CRGBPalette16 palette = PartyColors_p;
  uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  for( int i = 0; i < NUM_LEDS; i++) { //9948
    leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  }
}

void juggle() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 20);
  byte dothue = 0;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
    dothue += 32;
  }
}

(4) Open my project: double click to open it,

At first glance, you can see that ESP8266 creates an AP hotspot for notebook or mobile phone links. There is no password.
(5) Configure development board and serial port information

Pay attention to what type of development board you choose, and what serial port number your development board occupies when connected to the computer

(6) Connect the IO port according to the source code
Observe the source pin definition:
//Domestic open source ESP8266 Internet of things intelligent color lamp 5
//Author: daodan Jishui
//Time: November 2021
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <FastLED.h>
#define DATA_PIN 4 / / light with data pin – D2-GPIO4
#define LED_TYPE WS2812 / / light strip type
#define COLOR_ORDER GRB / / color order
#define NUM_LEDS 64 / / number of lights
CRGB leds[NUM_LEDS]; // Create color object
#define BRIGHTNESS 96 / / brightness 0 ~ 255
It can be seen that the lamp has data pin – D2-GPIO4
After downloading, you can run the program.

3. Modify the source code of (3.1) to become my code

In general, there are still a lot of changes. It has been changed beyond recognition, and a lot of content has been added in order to improve its function. All source code (readers can copy and use):

//Domestic open source ESP8266 Internet of things intelligent color lamp 4
//Author: daodan Jishui
//Time: June 3, 2021
//QQ:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

#include <FastLED.h>
#define DATA_ Pin 4 / / light with data pin -- D2 -- gpio4
#define LED_ Type ws2812 / / light strip type
#define COLOR_ORDER GRB / / color order
#define NUM_ LEDs 1 / / number of lights
CRGB leds[NUM_LEDS];      //Create color object
#Define brightness 96 / / brightness 0 ~ 255



const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 4, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80); 

//Web page function, the incoming parameter is the server IP, and the returned is the web page string
//http://192.168.4.1/HandleVal?ssid=r&password=168
String  index_html(String WiFiAddr){
return  String("")+"<html> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /"+
"<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"+
"</head>"+
"<body>"+
"<h1>daodanjishui Classic Internet of things intelligent lantern open source project 4</h1>"+
"<p></p> <form action=\"HandleVal\" method=\"\" name=\"\" >The first parameter is the color initial, and the second is the brightness value 0~255<br>"+
"<input type=\"text\" value=\"r\" name=\"ssid\" id=\"cmd\" size=\"10\" maxlength=\"20\">"+
"<input type=\"text\" value=\"168\" name=\"password\" id=\"cmd1\" size=\"10\" maxlength=\"20\">"+
"<input type=\"submit\" value=\"send\" ><br>"+
"<h2>daodanjishui Original classics are worth looking forward to!</h2>"+
" </form>"+
"</body>"+
"</html>";
}

void handleRoot() {
  webServer.send(200, "text/html", index_html("192.168.4.1")); //!!! Note that you need to use "text/html" to return to the web page!!!
  Serial.println("The user visited the home page");
}


#define LAMP_PIN 16
#define LAMP_PIN1 14  //R--D5
#define LAMP_PIN2 12  //G--D6
#define LAMP_PIN3 13  //B--D7


void controlLamp(bool lampVal) {//Lighting function, gpio16 ----- D0
  pinMode(LAMP_PIN, OUTPUT);//Set to output mode
  //digitalWrite(LAMP_PIN, lampVal);// Control IO port
  Serial.printf("Turn lamp %s\n", lampVal ? "On" : "Off");
if(lampVal){
    for (int i = 0; i < 1024; i++) {//The level rises from dark to bright 
      analogWrite(LAMP_PIN, i); 
      delay(2);
    }   
  }else{
     digitalWrite(LAMP_PIN, false);//Control IO port
     /*
    for (int i = 1024; i >= 0; i--){//The level decreases from light to dark
      analogWrite(LAMP_PIN, i);
      delay(2); 
    } */
  } 

}





/*****************************************************
 * Function name: HandleVal()
 * Function Description: process the return value of client request
 * Parameter Description: None
******************************************************/
void HandleVal()
{
    String wifis = webServer.arg("ssid"); //Find the value of ssid from the data sent by JavaScript
    String wifip = webServer.arg("password"); //Find the value of password from the data sent by JavaScript
    Serial.println(wifis);
    Serial.println(wifip);  //The received data is processed here

    if(wifis.equals("pwm")){     
      int i=wifip.toInt();//Here, convert WiFi into INT value for subsequent use
       Serial.println(i);    
       analogWrite(LAMP_PIN, i); 
    }

     if(wifis.equals("r")){     
      int i=wifip.toInt();//Here, convert WiFi into INT value for subsequent use
       Serial.println(wifis+" "+i);         
      //analogWrite(LAMP_PIN1, i); 
      FastLED.setBrightness(i);//Set the corresponding brightness
      leds[0]=CRGB::Red;//Set the color of the first lamp to red
      FastLED.show();//Display color
      
     }else  if(wifis.equals("g")){     
      int i=wifip.toInt();//Here, convert WiFi into INT value for subsequent use
       Serial.println(wifis+" "+i);    
      //analogWrite(LAMP_PIN2, i); 
      
      FastLED.setBrightness(i);//Set the corresponding brightness
      leds[0]=CRGB::Green;//Set the color of the first lamp to green
       FastLED.show();//Display color

          
     }else if(wifis.equals("b")){     
      int i=wifip.toInt();//Here, convert WiFi into INT value for subsequent use
       Serial.println(wifis+" "+i);    
       //analogWrite(LAMP_PIN3, i); 
       FastLED.setBrightness(i);//Set the corresponding brightness
       leds[0]=CRGB::Blue;//Set the color of the first lamp to blue
         FastLED.show();//Display color
     }
   
    
    if(wifip.equals("on")){
         Serial.println("turn on the light");
         controlLamp(true);
      }else if(wifip.equals("off")){
         Serial.println("Turn off the lights");
          controlLamp(false);
      }else if(wifip.equals("data")){
         Serial.println("getdata");
          //Here, the data is returned directly to the client
      }
    String  cmd="ssid="+wifis+" password="+wifip+" is OK";  
    webServer.send(200, "text/plain", cmd);//Data is returned to the client here
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("DNSServer example");


    pinMode(LAMP_PIN1, OUTPUT);//Set to output mode
     pinMode(LAMP_PIN2, OUTPUT);//Set to output mode
       pinMode(LAMP_PIN3, OUTPUT);//Set to output mode
  //initialize
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);//Set the corresponding brightness

  // modify TTL associated  with the domain name (in seconds)
  // default is 60 seconds
  dnsServer.setTTL(300);
  // set which return code will be used for all other domains (e.g. sending
  // ServerFailure instead of NonExistentDomain will reduce number of queries
  // sent by clients)
  // default is DNSReplyCode::NonExistentDomain
  dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);

  // start DNS server for a specific domain name
  dnsServer.start(DNS_PORT, "www.daodanjishui.com", apIP);

  // simple HTTP server to see that DNS server is working

  webServer.on("/HandleVal", HTTP_GET, HandleVal);//Receive the parameters and link routers submitted by the distribution network,


  webServer.on("/", HTTP_GET,handleRoot);//Show home page

  webServer.onNotFound([]() {
    String message = "Hello daodanjishui!\n\n";
    message += "URI: ";
    message += webServer.uri();

    webServer.send(200, "text/plain", message);
  });
  webServer.begin();
   Serial.println("webServer is ok");   
}

void loop() {
  dnsServer.processNextRequest();
  webServer.handleClient(); 
  
}


//Because it involves serial communication, you also need to add the program of serial receiving.
   //Method 1: the wifi function is blocked
    byte r, g, b; //Serial port assistant input example: ffffff be careful not to add carriage return and line feed. It must be sent in hexadecimal and displayed in white 
                  //ff0000 red; 00ff00 green; 0000ff blue. You must not send FF, a two digit hexadecimal number
    //arduino receives three times in total, and takes out 1 byte of data to r component for the first time  
    while(!Serial.available());
    r = Serial.read();
    while(!Serial.available());
    g = Serial.read();
    while(!Serial.available());
    b = Serial.read();  
     myRGBcolor.r=r;//Set component
     myRGBcolor.g=g;//Set component
     myRGBcolor.b=b;//Set component
     Serial.println("RGB:");
     fill_solid(leds,NUM_LEDS,myRGBcolor);//Set blue component
     FastLED.setBrightness(168);//Set the corresponding brightness   
     FastLED.show();//Display color

//The codes for controlling brightness are as follows:
if(wifis.equals("Brightness")){     
      int i=wifip.toInt();//Here, convert WiFi into INT value for subsequent use
       Serial.println(wifis+" "+i);    
       FastLED.setBrightness(i);//Set the corresponding brightness    
       FastLED.show();//Display color
     }

Please download the source code of my project at the back of the source code project. Respect originality and labor achievements.
The screenshot of C# code is as follows:

3, Operation and commissioning

Please see the video of station B for details. The main idea is: C# collect the color clicked by the mouse on the software UI and transmit it to the single chip microcomputer ESP8266 through the serial port. After receiving the information, the single chip microcomputer analyzes the color, and then controls the light board to display the response color. At the same time, the embedded web page can also accept the request of the web page to control the color change of the lantern.

summary

Through the above operation and debugging, the program and hardware meet the requirements put forward in the blog. That's it: the browser web page wirelessly lights up the domestic ws2812 RGB lamp and the domestic ws2811 RGB lamp. Making the intelligent color lamp of the Internet of things requires persistent efforts and innovative imagination. It supports originality. Please download the code I charge, Free code is not necessarily the best.

If there is a problem with the installation and development environment, please search the Internet carefully for a solution to the problem; If I suspect that my code is wrong, it is impossible. I write the code myself and test the hardware myself. If you have more needs, please talk to me privately. What I lack is not technology, but a strange idea or creativity······

Finally, the download address of this blog post code is attached: https://www.cirmall.com/circuit/28121/
Direct jump

Tags: network

Posted on Sun, 05 Dec 2021 23:57:03 -0500 by j_miguel_y