Snippets

Pete Matthews H801 (LIXADA) ESP8266 code for RGB control of LED strips

Created by Pete Matthews
#include <ESP8266WiFi.h>
/*
 * This bespoke "hack" code is for the H801 (LIXADA) control module. 
 * The H801 module is based on an ESP8266 and can drive a RGB LED strip of lights. 
 * This code responds to HTTP commands, but it could easily be converted to respond to MQTT etc - up to you
 * 
 * N.B. You have to solder 6 header pins on the H801 - this is so you can connect a cheap FDTI USB for programming.
 * N.B. You have to store your network SSID and password in the code variables *ssid and *pass (see below in code)
 * 
 * When all is done and the module connected and powered up, you will see a new IP (server) appear in your network. 
 * If you access this new IP (server) with a web browser, you can control the lights as follows :
 * To turn all three RGB outputs ON,              use http://192.168.1.xxx/rgb/ffffff
 * To turn all three RGB outputs OFF,             use http://192.168.1.xxx/rgb/000000 
 * To turn on GREEN a little bit,                 use http://192.168.1.xxx/rgb/003300 
 * To turn on RED and BLUE a bit, with max GREEN, use http://192.168.1.xxx/rgb/33ff33 
 */
void LED_RED();
void LED_GREEN();
void LED_BLUE();
void change_LED();
int convertToInt(char upper,char lower);

#define PWM_VALUE 63
int gamma_table[PWM_VALUE+1] = {
    0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10,
    11, 12, 13, 15, 17, 19, 21, 23, 26, 29, 32, 36, 40, 44, 49, 55,
    61, 68, 76, 85, 94, 105, 117, 131, 146, 162, 181, 202, 225, 250,
    279, 311, 346, 386, 430, 479, 534, 595, 663, 739, 824, 918, 1023
};


// RGB FET
#define redPIN    15 //12
#define greenPIN  13 //15
#define bluePIN   12 //13

// onboard green LED D1
#define LEDPIN    5
// onboard red LED D2
#define LED2PIN   1

// note 
// TX GPIO2 @Serial1 (Serial ONE)
// RX GPIO3 @Serial    


#define LEDoff digitalWrite(LEDPIN,HIGH)
#define LEDon digitalWrite(LEDPIN,LOW)

#define LED2off digitalWrite(LED2PIN,HIGH)
#define LED2on digitalWrite(LED2PIN,LOW)

int led_delay_red = 0;
int led_delay_green = 0;
int led_delay_blue = 0;
#define time_at_colour 1300 
unsigned long TIME_LED_RED = 0;
unsigned long TIME_LED_GREEN = 0;
unsigned long TIME_LED_BLUE = 0;
int RED, GREEN, BLUE; 
int RED_A = 0;
int GREEN_A = 0; 
int BLUE_A = 0;

const char *ssid =  "Your network SSID here";
const char *pass =  "Your network password here"; 

// Start WiFi Server
WiFiServer server(80);

void setup()
{
  
  pinMode(LEDPIN, OUTPUT);  
  pinMode(LED2PIN, OUTPUT);  
  
  pinMode(redPIN, OUTPUT);
  pinMode(greenPIN, OUTPUT);
  pinMode(bluePIN, OUTPUT);
  
  // Setup console
  Serial1.begin(115200);
  delay(10);
  Serial1.println();
  Serial1.println();

  //client.set_callback(callback);

  WiFi.begin(ssid, pass);

  LEDon;
  
  while (WiFi.status() != WL_CONNECTED) {

    LED2off;
    delay(500);
    Serial1.print(".");
    LED2on;
  }

  LEDoff;
  Serial1.println("");
  
  Serial1.println("WiFi connected");
  Serial1.println("IP address: ");
  Serial1.println(WiFi.localIP());
  
  Serial1.println("");
  
  server.begin();
  Serial1.println("Webserver started");
  
}

void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
      LEDon;
      // Read the first line of the request
      String req = client.readStringUntil('\r');
      Serial1.println(req);
      client.flush();
    
      // Prepare the response. Start with the common header:
      String s = "HTTP/1.1 200 OK\r\n";
      s += "Content-Type: text/html\r\n\r\n";
      s += "<!DOCTYPE HTML>\r\n<html>\r\n";
      
      if((req.indexOf("/rgb/") != -1)) {
         
        // get RGB value
        int pos = req.length();
        int ind1 = req.indexOf("/rgb/") + 5;
        String teststring = req.substring(ind1, pos);
        int ind2 = teststring.indexOf("/");
        String hexRGB = req.substring(ind1, ind2+ind1);
      
        // convert HEX to RGB
        hexRGB.toUpperCase();
        char c[6];
        hexRGB.toCharArray(c,7);
        long r = convertToInt(c[0],c[1]); //red
        long g = convertToInt(c[2],c[3]); //green
        long b = convertToInt(c[4],c[5]); //blue 
      
        // set value of RGB controller
        int red = map(r,0,255,0,PWM_VALUE); 
        red = constrain(red,0,PWM_VALUE);
        int green = map(g,0,255,0,PWM_VALUE);
        green = constrain(green, 0, PWM_VALUE);
        int blue = map(b,0,255,0,PWM_VALUE);
        blue = constrain(blue,0,PWM_VALUE);
      
        RED = gamma_table[red];
        GREEN = gamma_table[green];
        BLUE = gamma_table[blue];
        
        change_LED();
        
        // confirm to client
        s += "RGB value changed to ";
        s += hexRGB;
             
      }
      
      s += "<br>"; // Go to the next line.
      s += "Request OK!";
      
      s += "</html>\n";
     
      client.flush();
    
      // Send the response to the client
      client.print(s);
      //delay(1);
      Serial1.println("Client disconnected");
    
      // The client will actually be disconnected 
      // when the function returns and 'client' object is destroyed
      
  }
 if ((RED != RED_A) or (GREEN != GREEN_A) or (BLUE != BLUE_A)) {
  if(millis() - TIME_LED_RED >= led_delay_red){
    TIME_LED_RED = millis();
    LED_RED();
//  };
  }else{
  
  if(millis() - TIME_LED_GREEN >= led_delay_green){
    TIME_LED_GREEN = millis();
    LED_GREEN();
//  };
  }else{
  
  if(millis() - TIME_LED_BLUE >= led_delay_blue){
    TIME_LED_BLUE = millis();
    LED_BLUE();
//  };
  }}};
  delayMicroseconds(200);

  }else{
      LEDoff;
// Put normal code here!!
  }
}

void change_LED()
{
  int diff_red = abs(RED-RED_A);
  if(diff_red > 0){
    led_delay_red = time_at_colour / abs(RED-RED_A); 
  }else{
    led_delay_red = time_at_colour / 1023; 
  }
  
  int diff_green = abs(GREEN-GREEN_A);
  if(diff_green > 0){
    led_delay_green = time_at_colour / abs(GREEN-GREEN_A);
  }else{
    led_delay_green = time_at_colour / 1023; 
  }
  
  int diff_blue = abs(BLUE-BLUE_A);
  if(diff_blue > 0){
    led_delay_blue = time_at_colour / abs(BLUE-BLUE_A); 
  }else{
    led_delay_blue = time_at_colour / 1023; 
  }
  
}

void LED_RED()
{
  if(RED != RED_A){
    if(RED_A > RED) RED_A = RED_A - 1;
    if(RED_A < RED) RED_A++;
    analogWrite(redPIN, RED_A);
  }
}

void LED_GREEN()
{
  if(GREEN != GREEN_A){
    if(GREEN_A > GREEN) GREEN_A = GREEN_A - 1;
    if(GREEN_A < GREEN) GREEN_A++;
    analogWrite(greenPIN, GREEN_A);
  }
}
  
void LED_BLUE()
{
  if(BLUE != BLUE_A){
    if(BLUE_A > BLUE) BLUE_A = BLUE_A - 1;
    if(BLUE_A < BLUE) BLUE_A++;
    analogWrite(bluePIN, BLUE_A);
  }
}

int convertToInt(char upper,char lower)
{
  int uVal = (int)upper;
  int lVal = (int)lower;
  uVal = uVal >64 ? uVal - 55 : uVal - 48;
  uVal = uVal << 4;
  lVal = lVal >64 ? lVal - 55 : lVal - 48;
  return uVal + lVal;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.