logo elektroda
logo elektroda
X
logo elektroda

ESP8266 NodeMCU V3 + WS2812B 5V LED Strip Control via WiFi

BusyBoy 861 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20524335
    BusyBoy
    Level 8  
    Hello,

    Some time ago using esp8266 nodemcu v3 plus transmitter id control TV via phone. Now I still wanted to add the ws2812b 5v led strip control.

    I added new buttons in the code and added the effect I would like when the button is triggered (code all)

    
    #include <FastLED.h>
    #include <i18n.h>
    #include <IRac.h>
    #include <IRrecv.h>
    #include <IRremoteESP8266.h>
    #include <IRsend.h>
    #include <IRtext.h>
    #include <IRtimer.h>
    #include <IRutils.h>
    #include <ir_NEC.h>
    
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>
    
    // Add this library: https://github.com/markszabo/IRremoteESP8266
    #include <IRremoteESP8266.h>
    
    #define IR_SEND_PIN D2
    
    #define DELAY_BETWEEN_COMMANDS 1000
    
    #define LED_PIN     D3
    #define NUM_LEDS    20
    
    CRGB leds[NUM_LEDS];
    IRsend irsend(IR_SEND_PIN);
    
    const char* ssid = "*****";
    const char* password = "******";
    
    ESP8266WebServer server(80);
    
    const int led = BUILTIN_LED;
    
    String rowDiv = "    <div class="row" style="padding-bottom:1em">n";
    String endDiv = "    </div>n";
    
    // Buttons are using the bootstrap grid for sizing - http://getbootstrap.com/css/#grid
    String generateButton(String colSize, String id, String text, String url) {
    
      return  "<div class="" + colSize + "" style="text-align: center">n" +
              "    <button id="" + id + "" type="button" class="btn btn-default" style="width: 100%" onclick='makeAjaxCall("" + url + "")'>" + text + "</button>n" +
              "</div>n";
    }
    
    void handleRoot() {
      digitalWrite(led, 0);
      String website = "<!DOCTYPE html>n";
      website = website + "<html>n";
      website = website + "  <head>n";
      website = website + "    <meta charset="utf-8">n";
      website = website + "    <meta http-equiv="X-UA-Compatible" content="IE=edge">n";
      website = website + "    <meta name="viewport" content="width=device-width, initial-scale=1">n";
      website = website + "    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">n";
      website = website + "  </head>n";
      website = website + "  <body>n";
      website = website + "    <div class="container-fluid">n";
    
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-6", "tvpower","TV POWER", "tvpower");
      website = website + generateButton("col-xs-6", "tvmute","MUTE", "tvmute");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-6", "pilotinfower","PILOT/INFO", "pilotinfo");
      website = website + generateButton("col-xs-6", "cofnij","COFINIJ", "cofnij");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "channel1","1", "channel1");
      website = website + generateButton("col-xs-4", "channel2","2", "channel2");
      website = website + generateButton("col-xs-4", "channel3","3", "channel3");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "channel4","4", "channel4");
      website = website + generateButton("col-xs-4", "channel5","5", "channel5");
      website = website + generateButton("col-xs-4", "channel6","6", "channel6");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "channel7","7", "channel7");
      website = website + generateButton("col-xs-4", "channel8","8", "channel8");
      website = website + generateButton("col-xs-4", "channel9","9", "channel9");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
     website = website + generateButton("col-xs-12", "channel0","0", "channel0");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
    
      website = website + generateButton("col-xs-6", "volplus","VOL+", "volplus");
      website = website + generateButton("col-xs-6", "volminus","VOL-", "volminus");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-6", "progplus","PROG+", "progplus");
      website = website + generateButton("col-xs-6", "progminus","PROG-", "progminus");
    
    
      website = website + endDiv;
    [b]  website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "off","OFF", "off");
      website = website + generateButton("col-xs-4", "czerowny","CZERWONY", "czerowny");
      website = website + generateButton("col-xs-4", "niebieski","NIEBIESKI", "niebieski");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "zielony","ZIELONY", "zielony");
      website = website + generateButton("col-xs-4", "zolty","ŻÓŁTY", "zolty");
      website = website + generateButton("col-xs-4", "bialy","BIAŁY", "bialy");[/b]
    
      website = website + endDiv;
      website = website + "    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>n";
      website = website + "    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>n";
      website = website + "    <script> function makeAjaxCall(url){$.ajax({"url": url})}</script>n";
      website = website + "  </body>n";
      website = website + "</html>n";
    
      server.send(200, "text/html", website);
      digitalWrite(led, 1);
    }
    
    void handleNotFound(){
      digitalWrite(led, 1);
      String message = "File Not Foundnn";
      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, 1);
    }
    
    void setup(void){
    
      FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
    
      irsend.begin();
      pinMode(led, OUTPUT);
      digitalWrite(led, 1);
      Serial.begin(115200);
      WiFi.begin(ssid, password);
    
    // IP
    
    IPAddress local_IP(192, 168, 1, 11);
    IPAddress gateway(192, 168, 1, 1);
    IPAddress subnet(255, 255, 255, 0);
    WiFi.config(local_IP, gateway, subnet);
    
      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("esp8266")) {
        Serial.println("MDNS Responder Started");
      }
    
      server.on("/", handleRoot);
    
      server.on("/tvpower", [](){
        Serial.println("TV POWER");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A48B7, 32);
        delay(50);
      }
        server.send(200, "text/plain", "TV POWER");
      });
    
      server.on("/tvmute", [](){
        Serial.println("MUTE");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A9C63, 32);
        delay(50);
      }
        server.send(200, "text/plain", "MUTE");
      });
    
      server.on("/pilotinfo", [](){
        Serial.println("PILOT/INFO");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17ADA25, 32);
        delay(50);
      }
        server.send(200, "text/plain", "PILOT/INFO");
      });
    
      server.on("/cofnij", [](){
        Serial.println("COFINIJ");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A3AC5, 32);
        delay(50);
      }
        server.send(200, "text/plain", "COFINIJ");
      });
    
      server.on("/channel1", [](){
        Serial.println("CH 1");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A807F, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 1");
      });
    
      server.on("/channel2", [](){
        Serial.println("CH 2");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A40BF, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 2");
      });
    
      server.on("/channel3", [](){
        Serial.println("CH 3");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17AC03F, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 3");
      });
    
      server.on("/channel4", [](){
        Serial.println("CH 4");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A20DF, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 4");
      });
    
      server.on("/channel5", [](){
        Serial.println("CH 5");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17AA05F, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 5");
      });
    
      server.on("/channel6", [](){
        Serial.println("CH 6");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A609F, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 6");
      });
    
      server.on("/channel7", [](){
        Serial.println("CH 7");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17AE01F, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 7");
      });
    
    
      server.on("/channel8", [](){
        Serial.println("CH 8");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A10EF, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 8");
      });
    
      server.on("/channel9", [](){
        Serial.println("CH 9");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A906F, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 9");
      });
    
      server.on("/channel0", [](){
        Serial.println("CH 0");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A00FF, 32);
        delay(50);
      }
        server.send(200, "text/plain", "CH 0");
      });
    
      server.on("/volplus", [](){
        Serial.println("VOL+");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A4CB3, 32);
        delay(50);
      }
        server.send(200, "text/plain", "VOL+");
      });
    
      server.on("/volminus", [](){
        Serial.println("VOL-");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17ACC33, 32);
        delay(50);
      }
        server.send(200, "text/plain", "VOL-");
      });
    
      server.on("/progplus", [](){
        Serial.println("PROG+");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A0CF3, 32);
        delay(50);
      }
        server.send(200, "text/plain", "PROG+");
      });
    
      server.on("/progminus", [](){
        Serial.println("PROG-");
      for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xE17A8C73, 32);
        delay(50);
      }
        server.send(200, "text/plain", "PROG-");
      });
    
    [b]
        server.on("/zielony", [](){
        for (int i = 0; i < 3; i++) {
      for (int j = 0; j <= 19; j++) {
        leds[i] = CRGB ( 255, 0, 0);
        FastLED.show();
        delay(50);
      }
      }
      });
    
    
      server.on("/czerowny", [](){
        for (int i = 0; i < 3; i++) {
      for (int j = 0; j <= 19; j++) {
        leds[j] = CRGB ( 0, 0, 255);
        FastLED.show();
        delay(50);
      }
      }
      });
    
        server.on("/niebieski", [](){
        for (int i = 0; i < 3; i++) {
      for (int j = 0; j <= 19; j++) {
        leds[i] = CRGB ( 0, 255, 0);
        FastLED.show();
        delay(50);
      }
      }
      });
    
    
    
        server.on("/off", [](){
        for (int i = 0; i < 3; i++) {
      FastLED.clear();  // clear all pixel data
      FastLED.show();
      }
      }
      );[/b]
    
    
    
      server.onNotFound(handleNotFound);
    
      server.begin();
      Serial.println("HTTP Server Started");
    }
    
    void loop(void){
      server.handleClient();
    }


    Fragment which I have added

      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "off","OFF", "off");
      website = website + generateButton("col-xs-4", "czerowny","CZERWONY", "czerowny");
      website = website + generateButton("col-xs-4", "niebieski","NIEBIESKI", "niebieski");
      website = website + endDiv;
      website = website + rowDiv;
      website = website + endDiv;
      website = website + rowDiv;
      website = website + generateButton("col-xs-4", "zielony","ZIELONY", "zielony");
      website = website + generateButton("col-xs-4", "zolty","ŻÓŁTY", "zolty");
      website = website + generateButton("col-xs-4", "bialy","BIAŁY", "bialy");


    and

        server.on("/zielony", [](){
        for (int i = 0; i < 3; i++) {
      for (int j = 0; j <= 19; j++) {
        leds[i] = CRGB ( 255, 0, 0);
        FastLED.show();
        delay(50);
      }
      }
      });
    
    
      server.on("/czerowny", [](){
        for (int i = 0; i < 3; i++) {
      for (int j = 0; j <= 19; j++) {
        leds[j] = CRGB ( 0, 0, 255);
        FastLED.show();
        delay(50);
      }
      }
      });
    
        server.on("/niebieski", [](){
        for (int i = 0; i < 3; i++) {
      for (int j = 0; j <= 19; j++) {
        leds[i] = CRGB ( 0, 255, 0);
        FastLED.show();
        delay(50);
      }
      }
      });
    
    
    
        server.on("/off", [](){
        for (int i = 0; i < 3; i++) {
      FastLED.clear();  // clear all pixel data
      FastLED.show();
      }
      }
      );


    And here the problem arises. Because, for example, when I press the green button, the led bar works correctly, but when I press e.g. the next red button, the led bar "goes stupid" and starts green, red, blue so not the green colour itself. The buttons work with a delay. Unfortunately I do not know what this is caused by ☹

    Maybe someone can help me with this topic....
  • ADVERTISEMENT
  • #2 20525681
    p.kaczmarek2
    Moderator Smart Home
    Why do you use i and j once?
    ESP8266 NodeMCU V3 + WS2812B 5V LED Strip Control via WiFi
    By the way, I don't know why you have these extra loops so strangely done. Aren't they by any chance just copied without further analysis from:
    Code: C / C++
    Log in, to see the code
    ?

    By the way.... if you have a constant in the code, why do you rigidly type 19...?
    Code: C / C++
    Log in, to see the code
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 20526325
    BusyBoy
    Level 8  
    By the way, I don't know why you have these extra loops done so strangely. Aren't they by any chance just copied without further analysis from:

    Yes you're right - I'm trying by trial and error somehow to solve this teamt - that's why I copied the part responsible for infrared transmission (ira) and converted it to led bar.

    Why are there two variables ? hmm as I mentioned - I copied the code - and since there was also a loop there, I used it plus a loop for the led bar.
  • #4 20526333
    p.kaczmarek2
    Moderator Smart Home
    It seems to me that it would be better if you analysed what these loops were doing, rather than copying. I don't know the libraries by heart, but that loop up to 3 is a leftover from copying the loop that sent the IR codes three times. In addition, in one of the loops you use j and in another you use only i, with the result that you only change the first 3 colours, the others are unchanged.

    Remove the unnecessary loop counting up to 3 and change the colours only with one loop, for the stop condition use NUM_LEDS.

    You have underlined here what causes the problem:
    ESP8266 NodeMCU V3 + WS2812B 5V LED Strip Control via WiFi


    Also these delays are superfluous - they also confuse you and make it hard to keep track of what is happening. For this reason the bar can seemingly run with a delay.

    Another thing is that in the loop you have to set the colours - and the show is enough once outside the loop. You at this point in the loop do these 20 times delay and show....
    Helpful post? Buy me a coffee.
ADVERTISEMENT