News

moodlite - Coming soon


NodeMCU Software... Help required

CdRsKuLL · 2354

CdRsKuLL

  • The Fat Dude
  • Administrator
  • Full Member
  • *****
    • Posts: 113
    • Likes: +15/-0
  • Lighting the way...
    • View Profile
on: December 28, 2018, 10:52:06 AM
So, I'm the first to admit that I'm not very good at software or coding... I'm more of a copy and paste coder, taking bits from things I've found and spend hours upon hours getting it to work :-)


The controller I decided on is the NodeMCU. This is because its cheap (£4) and also includes wifi as well. You can program this through the Arduino IDE which is what I've been doing.  I've attached the code below but I would love to expand it so you can actually select colours etc..


Now the code on this controller tells the LED's what colour they need to be but also runs a webserver.  This enables any web browser from mobile / PC / tablet etc to go to it's IP address and change the current pattern.  At the moment (version v1.0) its very simple and just displays the following;





This allows you to select 8 different patterns and also alter the brightness and speed. Very simple, just a proof of concept really.


You can set it to have a static IP address which is handy if you want to link it to Google home etc via IFTTT :-)


So, if you can help lend a hand to improve this then please do..


Code: [Select]
// Downloaded from http://moodlite.co.uk  - Open source
// Written by Steve Wagg aka CdRsKuLL
// Release v1.0 27th Dec 2018


// Load librarys
#include <FastLED.h>
#include <ESP8266WiFi.h>


// Replace with your network credentials
const char* ssid     = "NetworkName";
const char* password = "NetworkPassword";


// If you want to set a static IP, then uncommment the three below lines AND the line WiFi.config(ip, gateway, subnet); in the void setup() below. Otherwise it will be allocated one via your DCHP server
//IPAddress ip(192, 168, 1, 40); //set static ip
//IPAddress gateway(192, 168, 1, 1); //set getteway
//IPAddress subnet(255, 255, 255, 0);//set subnet


// Set the pin you are outputting on and the number of LEDs (5 tiles = 15 LEDs).
#define LED_PIN     6
#define NUM_LEDS    48


String header;
String pat = "1";
int brightpos1 = 0;
int brightpos2 = 0;
String brightString = String(5);
int speedpos1 = 0;
int speedpos2 = 0;
String speedString = String(5);
#define BRIGHTNESS  250
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
// Set web server port number to 80
WiFiServer server(80);
#define UPDATES_PER_SECOND 30
CRGBPalette16 currentPalette;
TBlendType    currentBlending;
extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;


void setup() {
  Serial.begin(115200);
  delay( 3000 ); // power-up safety delay
  Serial.print("Connecting to ");
  Serial.println(ssid);
  //WiFi.config(ip, gateway, subnet);  //uncomment if you want to use a static IP
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();


  delay(500);
  Serial.print(".");
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );


  currentPalette = RainbowColors_p;
  currentBlending = LINEARBLEND;
}




void loop()
{
  WiFiClient client = server.available();   // Listen for incoming clients


  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();








            // reads the url for pattern change
            if (header.indexOf("GET /1/on") >= 0) {
              Serial.println("LED 1");
              pat = "1";
              currentPalette = RainbowColors_p; currentBlending = LINEARBLEND;
            } else if (header.indexOf("GET /2/on") >= 0) {
              Serial.println("LED 2");
              pat = "2";
              currentPalette = RainbowStripeColors_p;  currentBlending = NOBLEND;
            } else if (header.indexOf("GET /3/on") >= 0) {
              Serial.println("LED 3");
              pat = "3";;
              currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND;
            } else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("LED 4");
              pat = "4";
              SetupTotallyRandomPalette(); currentBlending = LINEARBLEND;
            } else if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("LED 5");
              pat = "5";
              SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND;
            } else if (header.indexOf("GET /6/on") >= 0) {
              Serial.println("LED 6");
              pat = "6";
              currentPalette = currentPalette = CloudColors_p; currentBlending = LINEARBLEND;
            } else if (header.indexOf("GET /7/on") >= 0) {
              Serial.println("LED 7");
              pat = "7";
              currentPalette = currentPalette = PartyColors_p; currentBlending = LINEARBLEND;
            } else if (header.indexOf("GET /8/on") >= 0) {
              Serial.println("LED 8");
              pat = "8";
              currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND;
            }


            if (header.indexOf("GET /?bvalue=") >= 0) {
              brightpos1 = header.indexOf('=');
              brightpos2 = header.indexOf('&');
              brightString = header.substring(brightpos1 + 1, brightpos2);
              FastLED.setBrightness( brightString.toInt() );
              Serial.println(brightString);
            }


            if (header.indexOf("GET /?svalue=") >= 0) {
              speedpos1 = header.indexOf('=');
              speedpos2 = header.indexOf('&');
              speedString = header.substring(speedpos1 + 1, speedpos2); //this variable is updated at the end of the void sub
              Serial.println(speedString);
            }


            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 25px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".textstyle { color: #FFFFFF;}");
            client.println(".slider { width: 300px; }</style></head>");
            client.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>");


            // Web Page Heading
            client.println("<body style =\"background-color: #000000; color: #FFFFFF;\"><h1>moodlite</h1>");
            if (pat == "1") {
              client.println("<hr><p class=\"textstyle\">PATTERN 1<br><br>");
            }
            if (pat == "2") {
              client.println("<hr><p class=\"textstyle\">PATTERN 2<br><br>");
            }
            if (pat == "3") {
              client.println("<hr><p class=\"textstyle\">PATTERN 3<br><br>");
            }
            if (pat == "4") {
              client.println("<hr><p class=\"textstyle\">PATTERN 4<br><br>");
            }
            if (pat == "5") {
              client.println("<hr><p class=\"textstyle\">PATTERN 5<br><br>");
            }
            if (pat == "6") {
              client.println("<hr><p class=\"textstyle\">PATTERN 6<br><br>");
            }
            if (pat == "7") {
              client.println("<hr><p class=\"textstyle\">PATTERN 7<br><br>");
            }
            if (pat == "8") {
              client.println("<hr><p class=\"textstyle\">PATTERN 8<br><br>");
            }


            //Web Page buttons
            client.println("<a href=\"/1/on\"><button class=\"button\">P1</button></a>");
            client.println("<a href=\"/2/on\"><button class=\"button\">P2</button></a>");
            client.println("<a href=\"/3/on\"><button class=\"button\">P3</button></a>");
            client.println("<a href=\"/4/on\"><button class=\"button\">P4</button></a>");
            client.println("</p>");
            client.println("<p>");
            client.println("<a href=\"/5/on\"><button class=\"button\">P5</button></a>");
            client.println("<a href=\"/6/on\"><button class=\"button\">P6</button></a>");
            client.println("<a href=\"/7/on\"><button class=\"button\">P7</button></a>");
            client.println("<a href=\"/8/on\"><button class=\"button\">P8</button></a>");


            //Web page sliders
            client.println("</p><hr><p class=\"textstyle\">BRIGHTNESS</p>");
            client.println("<input type=\"range\" min=\"1\" max=\"255\" class=\"slider\" id=\"brightSlider\" onchange=\"bright(this.value)\" value=\"" + brightString + "\"/>");
            client.println("<script>var slider = document.getElementById(\"brightSlider\");");
            client.println("var brightP = document.getElementById(\"brightPos\"); brightP.innerHTML = slider.value;");
            client.println("slider.oninput = function() { slider.value = this.value; brightP.innerHTML = this.value; }");
            client.println("$.ajaxSetup({timeout:1000}); function bright(pos) { ");
            client.println("$.get(\"/?bvalue=\" + pos + \"&\"); {Connection: close};}</script>");


            client.println("</p><p class=\"textstyle\">SPEED</p>");
            client.println("<input type=\"range\" min=\"1\" max=\"499\" class=\"slider\" id=\"speedSlider\" onchange=\"speed(this.value)\" value=\"" + speedString + "\"/>");
            client.println("<script>var slider = document.getElementById(\"speedSlider\");");
            client.println("var speedP = document.getElementById(\"speedPos\"); speedP.innerHTML = slider.value;");
            client.println("slider.oninput = function() { slider.value = this.value; speedP.innerHTML = this.value; }");
            client.println("$.ajaxSetup({timeout:1000}); function speed(pos) { ");
            client.println("$.get(\"/?svalue=\" + pos + \"&\"); {Connection: close};}</script>");


            client.println("</body></html>");


            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }


  static uint8_t startIndex = 0;
  startIndex = startIndex + 1; /* motion speed */
  FillLEDsFromPaletteColors( startIndex);
  FastLED.show();
  FastLED.delay(1000 / speedString.toInt());
}


void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
  uint8_t brightness = 255;


  for ( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    colorIndex += 3;
  }
}


void SetupTotallyRandomPalette()
{
  for ( int i = 0; i < 16; i++) {
    currentPalette[i] = CHSV( random8(), 255, random8());
  }
}


// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
  // 'black out' all 16 palette entries...
  fill_solid( currentPalette, 16, CRGB::Black);
  // and set every fourth one to white.
  currentPalette[0] = CRGB::White;
  currentPalette[4] = CRGB::White;
  currentPalette[8] = CRGB::White;
  currentPalette[12] = CRGB::White;


}


// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
  CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  CRGB green  = CHSV( HUE_GREEN, 255, 255);
  CRGB black  = CRGB::Black;


  currentPalette = CRGBPalette16(
                     green,  green,  black,  black,
                     purple, purple, black,  black,
                     green,  green,  black,  black,
                     purple, purple, black,  black );
}




// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM.  A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
  CRGB::Red,
  CRGB::Gray, // 'white' is too bright compared to red and blue
  CRGB::Blue,
  CRGB::Black,


  CRGB::Red,
  CRGB::Gray,
  CRGB::Blue,
  CRGB::Black,


  CRGB::Red,
  CRGB::Red,
  CRGB::Gray,
  CRGB::Gray,
  CRGB::Blue,
  CRGB::Blue,
  CRGB::Black,
  CRGB::Black
};


I'm currently playing with adding a mic to the power base, this will hopefully allow it to react to sound, so maybe beat to the music or something.. that's the idea anyway.   


Hoping others can lend a hand :-)


Thanks,


Steve
« Last Edit: December 28, 2018, 10:55:19 AM by CdRsKuLL »

Moodlite is a completely free open source project.
If however you would like to say thanks and help contribute to this site, or just buy me a beer then please donate below. :-)



spigot

  • Global Moderator
  • Full Member
  • *****
    • Posts: 112
    • Likes: +24/-0
  • There's no place like 127.0.0.1
    • View Profile
Reply #1 on: December 28, 2018, 04:59:59 PM
Hi Steve.

   I'm planning to make nanoleaf myself for some time now.  ;D

   For the Web server part, I would suggest to use ESPAsyncWebServer and ESPAsyncTCP from me-no-dev (https://github.com/me-no-dev/ESPAsyncWebServer, https://github.com/me-no-dev/ESPAsyncTCP). I'm using it with few of my projects (See the picturea attached). :D
All the data for web server is stored in Arduino SPIFFS, and it's not hard to modify it.

I'm planing to use Wemos D1 mini (NodeMCU seems like a overkill  ;) )

Maybe I could help you with project if I find some spare time (I'm also not a professional software developer, it's my hobby).



ikirt

  • Newbie
  • *
    • Posts: 2
    • Likes: +0/-0
    • View Profile
Reply #2 on: December 28, 2018, 07:50:49 PM
Awesome project, love what you have accomplished here !!
For adding audio to the project, check this out, https://github.com/scottlawsonbc/audio-reactive-led-strip



spigot

  • Global Moderator
  • Full Member
  • *****
    • Posts: 112
    • Likes: +24/-0
  • There's no place like 127.0.0.1
    • View Profile
Reply #3 on: December 28, 2018, 11:24:56 PM
Would you consider create a github repository? Then you could track changes, etc.
Thanks.



CdRsKuLL

  • The Fat Dude
  • Administrator
  • Full Member
  • *****
    • Posts: 113
    • Likes: +15/-0
  • Lighting the way...
    • View Profile
Reply #4 on: December 29, 2018, 10:54:53 AM
Hiya,


I don't know much about GitHub, Gitlab etc.. What I need is someone who knows what there doing and to take the code and do what's right with an open source project like this :-)


I could develop the code a little more myself but it could take me days what someone else could do in minutes.


I'm happy to lend a hand with guidance etc.. on where I think it should go but other than this if you would to start something that would be great :-)


Steve

Moodlite is a completely free open source project.
If however you would like to say thanks and help contribute to this site, or just buy me a beer then please donate below. :-)



CdRsKuLL

  • The Fat Dude
  • Administrator
  • Full Member
  • *****
    • Posts: 113
    • Likes: +15/-0
  • Lighting the way...
    • View Profile
Reply #5 on: December 29, 2018, 10:55:16 AM
Awesome project, love what you have accomplished here !!
For adding audio to the project, check this out, https://github.com/scottlawsonbc/audio-reactive-led-strip


Great I will :-)  Thanks for the link.


Steve

Moodlite is a completely free open source project.
If however you would like to say thanks and help contribute to this site, or just buy me a beer then please donate below. :-)



wywywywy

  • Newbie
  • *
    • Posts: 2
    • Likes: +1/-0
    • View Profile
Reply #6 on: December 31, 2018, 02:43:49 PM
I registered to vote for getting it on GitHub but looks like it's already done!

Any plans on implementing home automation control, e.g. MQTT?



shfaxx

  • Jr. Member
  • **
    • Posts: 95
    • Likes: +11/-0
    • View Profile
Reply #7 on: January 05, 2019, 01:58:54 AM
Total newb to the ESP8266 boards (really any Arduino boards).  Is there a quick guide somewhere detailing how to connect and program the .ino file?  I'm starting from scratch as I have no software or anything installed to program the board, etc.  I'm assuming I need some application on my PC and then simply connect to the board via MicroUSB and flash the board.

If nothing exists, I'm happy to document the process for future folks coming across this project.  Just don't want to re-invent the wheel as I stumble through all this.

tnx



Expecto30

  • Newbie
  • *
    • Posts: 1
    • Likes: +0/-0
    • View Profile
Reply #8 on: January 05, 2019, 06:20:37 AM
Total newb to the ESP8266 boards (really any Arduino boards).  Is there a quick guide somewhere detailing how to connect and program the .ino file?  I'm starting from scratch as I have no software or anything installed to program the board, etc.  I'm assuming I need some application on my PC and then simply connect to the board via MicroUSB and flash the board.

If nothing exists, I'm happy to document the process for future folks coming across this project.  Just don't want to re-invent the wheel as I stumble through all this.

tnx

You use the same arduino IDE to program esp8266 chips. I recommend you watch a yt video/follow an article on blinking led using nodemcu dev board. This will follow you through the whole development process from simple coding to compiling and uploading of code from arduino IDE



shfaxx

  • Jr. Member
  • **
    • Posts: 95
    • Likes: +11/-0
    • View Profile
Reply #9 on: January 05, 2019, 05:22:28 PM
Looks like I'm good to go.  I threw this little outline of the steps I followed.   May help someone else.

Reference Links Used:
http://www.whatimade.today/esp8266-easiest-way-to-program-so-far/
https://www.letscontrolit.com/wiki/index.php/Tutorial_Arduino_Firmware_Upload
https://randomnerdtutorials.com/esp8266-web-server-with-arduino-ide/



Board Purchased:  HiLetgo 2pcs ESP8266 NodeMCU LUA CP2102 ESP-12E Internet WiFi Development Board Open Source Serial Wireless Module Works Great with Arduino IDE/Micropython (Pack of 2PCS)
https://www.amazon.com/gp/product/B010N1SPRK/ref=ppx_yo_dt_b_detailpage_o00_s03?ie=UTF8&psc=1

Install / Configuration Steps:
Download – Arduino IDE v1.8.8 from https://www.arduino.cc/en/main/software
Install with all options selected
When prompted – Install device driver
Open Arduino IDE
Select File – Preferences
Enter this URL in additional boards manager dialog
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Restart Arduino IDE software
Open Tools – Board – Boards Manager
Scroll to the bottom and select esp8266 by esp8266 Community
Click Install
Click Close
Plug in board via Micro USB cable (If running on windows, you should hear the USB connection sound)
Select Tools – Port – COMx
   (COM# port will vary, select each port, then select Tools – Get Board Info.  When Board Info dialog appears, this is your port.)
Select Tools – Board – NodeMCU 1.0 (ESP-12E Module)
Select Sketch - Include Library – Manage Libraries…
Type fastled in the search dialog
Select FastLED library from list and click Install
Click Close
Select File – Open – Locate moddline.ino file which you downloaded from https://github.com/spigotx/Moodlite
Modify ssid / password settings to match your home network
Modify NUM_LEDS to the number of LEDs you are using
Click Sketch – Upload (When completed dialog at the bottom should show 100%)
Programming Complete!

To retrieve IP and validate web server (Leave board connected)
Select Tools – Serial Port monitor
Change connection speed to 115200
Click RST (Reset) button on the ESP8266 board (Next to the USB port)
You may see some garbage character, but after a few seconds you should see:
WiFi connected.
IP address:
10.0.0.XX
Open browser to the IP above http://10.0.0.xx
moodlite web page should be displayed.



CdRsKuLL

  • The Fat Dude
  • Administrator
  • Full Member
  • *****
    • Posts: 113
    • Likes: +15/-0
  • Lighting the way...
    • View Profile
Reply #10 on: January 05, 2019, 05:28:50 PM
Great write up :-) and good details. Will certainly help others. Just a note mind.. the NodeMCU board in the video is not the one I've used and is a different size (it's bigger). This might cause problems if people buy the wrong one. Photo attached


Thanks for the info :-) I might pinch it for my build diary if you dont mind.


Steve
« Last Edit: January 05, 2019, 05:34:10 PM by CdRsKuLL »

Moodlite is a completely free open source project.
If however you would like to say thanks and help contribute to this site, or just buy me a beer then please donate below. :-)



shfaxx

  • Jr. Member
  • **
    • Posts: 95
    • Likes: +11/-0
    • View Profile
Reply #11 on: January 05, 2019, 06:45:03 PM
Great write up :-) and good details. Will certainly help others. Just a note mind.. the NodeMCU board in the video is not the one I've used and is a different size (it's bigger). This might cause problems if people buy the wrong one. Photo attached


Thanks for the info :-) I might pinch it for my build diary if you dont mind.


Steve

Thanks!   
Yes agreed, that is why I added the link to the board I purchased just to hopefully help with any confusion, those vids and links were just used as guidance for me on how to setup the software and flash the code, etc..

Yes, please use what you can in the build diary, this is exactly why I wrote it up, hopefully it would help someone starting from scratch like I was.



kingfisher

  • Newbie
  • *
    • Posts: 5
    • Likes: +0/-0
    • View Profile
Reply #12 on: January 17, 2019, 11:01:44 PM
Quote
To retrieve IP and validate web server (Leave board connected)
Select Tools – Serial Port monitor
Change connection speed to 115200
Click RST (Reset) button on the ESP8266 board (Next to the USB port)
You may see some garbage character, but after a few seconds you should see:
WiFi connected.
IP address:
10.0.0.XX
Open browser to the IP above http://10.0.0.xx
moodlite web page should be displayed.

Thanks for these notes. I've used them and the wiki to try and load the software but I'm struggling when I get to the last section:

"You may see some garbage character, but after a few seconds you should see:
WiFi connected."

I see the garbage characters but never see "WiFi connected"



touko810

  • Newbie
  • *
    • Posts: 1
    • Likes: +0/-0
    • View Profile
Reply #13 on: January 22, 2019, 10:08:58 AM
Hi,

if you just want to find your board IP adress, you could use an android App (ie: FING).
Make a scan without your board then plug your board and rescan, the new IP will be your board.

Hope it'll help.



LordVader

  • moodlite Developer
  • Full Member
  • *****
    • Posts: 113
    • Likes: +5/-0
  • I find your lack of faith disturbing!
    • View Profile
Reply #14 on: January 22, 2019, 02:22:06 PM
I see the garbage characters but never see "WiFi connected"

Try switching the Baud rate of the serial monitor and then press the RST button on your NodeMCU. Do this until your see something useful.
That helped me to find out the correct Baud rate as this was different from the one I am uploading with.


 


SMF spam blocked by CleanTalk