Project: IOT Christmas Lights Display

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction

A couple of year’s back I bought one of the Neopixel light strings from Adafruit. I think it was 200 leds. I used them one season and put them away. This Christmas I decided to make an IOT Christmas light display out it. 

To do this I used the code explained in  the Tutorial:Storing WiFi Configuration On Your ESP8266 Using The EEPROM Library Made Simple Part 1 and Tutorial part 2 as my base code. I also used code from the Adafruit Neopixel example code and integrated it with my base code. The complete IOT Christmas Light Display code can be downloaded from this link. 

I won’t explain the base code again as I have a great tutorial that already does this. I will just explain the steps I took adding the Neopixel code and how to control them

I split this project into the steps I tackled to integrate the  Neopickel code into the IOT code. You can download the complete sketch here.

INDEX

Here is a list of the hardware I chose to use:

  • Huzzah ESP8266 Breakout
  • 8-channel Bi-directional Logic Level Converter - TXB0108 from Adafruit
  • On/Off toggle switch
  • 10K Resistor
  • 5V FTDI cable
  • WS2812 Addressable Light String (200 LED)
  • 5V  2A power supply (depending on how many lights the Amperage will vary ) 

 

Step 1 Create the colourset() function

This function sets the light string colour. I started with copying this code from the Adafruit example sketch. For an explanation of this code go to the Adafruit tutorial that explains it in detail. But here are the highlights.

When calling the colourset() function you pass it the RGB value of the colour you want the led’s to turn. In the for() loop you see the NUMPIXELS variable. It has been set on line 61 and contains the number of LED’s your lightstring contains.

void colourset(int r,int g,int b){
  for(int i=0;i<NUMPIXELS;i++){
      // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
          pixels.setPixelColor(i, pixels.Color(r,g,b)); // sets the colour
          pixels.show(); // This sends the updated pixel color to the hardware.
          colour=0;
       }
}

Step 2 calling the colourset() function

This is done in a Switch()/case statement on line 217 in the loop() function. It uses a int variable colour (yes this is the correct spelling in Canada) that gets set through a web form. If you look at option 4 in the case statement you see a bunch of code. This code randomly changes the colours of the string creating a rainbow affect.

  switch(colour){
    case 0:
    
    break;
    case 1:
    colourset(255,0,0);//Red
    break;
    case 2:
    colourset(0,255,0);//Green
    break;
    case 3:
    colourset(244,229,66);//yellow
    break;
    case 4:
     uint16_t i, j;
      breakon=0;
      Serial.println("rainbow");
      for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
        for(i=0; i< pixels.numPixels(); i++) {
          pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
          server.handleClient();
          if(breakon==1){
            Serial.println("breakon");
            Serial.println("Colour: "+ String(colour));
            breakon=0;
            j=256*5+1;
            i=pixels.numPixels()+1;
            
          }
        }
        server.handleClient();
        if(breakon==1){
            Serial.println("breakon");
            Serial.println("Colour: "+ String(colour));
            breakon=0;
            j=256*5+1;
            i=pixels.numPixels()+1;
            
          }
        pixels.show();
        delay(50);
      }
      //colour=0;
    
    break;
    case 5:
    colourset(32,58,229);//blue
    break;
    case 6:
    colourset(204,11,229);//purple
    break;
    case 7:
    colourset(r_Arg,g_Arg,b_Arg);
    break;
    case 8:
    colourset(0,0,0);//off
    break;
  }
}

 

For a better understanding of this rainbow code, look in the Adafruit tutorial for the Neopixels. I modified the code a bit. The reason for this is that it uses 2 nested for() loops that make it hard to communicate with the microcontroller. If you remember that the web server uses this function server.handleClient() to read request from the server.

As long as your sketch is in a for() loop the request won’t be honored. This is why I created a variable called breakon. If it has a value of 1 the if() statements I have added to the code will terminate the for() loops and will allow the microcontroller to fulfill  the web requests. The value of the breakon variable gets set to 1  in the handleroot() function on line 309 when a server request is made. It gets set back to 0 in the switch()/case statement in the loop() function

Screenshot_20171213-124148.jpg

Step 3 Creating the web input screen

The web input screen html code is setup in function colour_form() starting on line 371 . It contains several forms nested in a table structure. It is a normal form that when you press the button makes a request to the root of your server and sending a hidden field with an integer that corresponds with the colour you want to set.

<input type=\"hidden\" name=\"colour\" value=\"1\">"

The value of this hidden field gets used to set the colour variable.

        colour=server.arg("colour").toInt(); 

The colour variable then gets used to operate the Switch()/case statement in the loop() function.

Step 4 The handleroot() function

In this function we use the server.Arg(“colour”) value to set the colour variable. It also sets the breakon variable to 1 so we can exit out of the for() loops in the rainbow mode.  You can find the code on line 319

      }else if (server.hasArg("colour")){
        Serial.println("Colour is set to: "+server.arg("colour"));
        colour=server.arg("colour").toInt(); 
        breakon=1;
      }

It also reads the RGB value from the web form option to create your own colour. The code can be found on line 311

      if(server.hasArg("RED") && server.hasArg("GREEN")&& server.hasArg("BLUE")){
         Serial.println("RGB");
        colour=server.arg("ssid").toInt();
        r_Arg=server.arg("RED").toInt();
        g_Arg=server.arg("GREEN").toInt();
        b_Arg=server.arg("BLUE").toInt();
       breakon=1;
      
      }else if (server.hasArg("colour")){

Step 5 make sure that the libraries are installed

The final steps are to make sure you copy the #include <Adafruit_NeoPixel.h> library and the appropriate variables needed to run this library

  •  #define PIN            12// sets the digital pin to send the data to
  •   #define NUMPIXELS      200 //number of led’s on your light strings
  • Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //sets up the light string

Step 6 The hardware setup

Click To Enlarge

 

Setup switch

It’s explained in the IOT tutorials. We have connected it to Digital pin 13.

LED Light string

The LED light string  is addressable, using the WS2812 LED. It uses a 5v logic and the ESP8266 uses 3.3v logic. This is why we used the  Adafruit TXB0108 logic level. I used this one because it is easy to use, and I had one laying around. But what the logic level does is convert the 3.3v logic info send from the Huzzah and converts it to 5v so the LED string can operate. We use digital pin 12 on the Huzzah for this. Look at the schematic above how it has been connected.

Another thing to think about is an appropriate power supply. Each of those led’s can draw up to 55mA. All the details about this can be found in the Adafruit tutorial. 

 

If you have any questions leave them in the comments and I get back to you as soon as possible. If you like this project and would like to see more of it you can subscribe to my news letter using the form below.

Subscribe to our mailing list

* indicates required
Email Format