1
\$\begingroup\$

What I am trying to do is control a simple 2m 5050 RGB Led Light Strip though a ESP-01 that receives http requests through Homebridge. I am doing this through the better-http-rgb Homebridge plugin and using the ESP-01 as the controller and server. The ESP-01 successfully gets the request from the Home app to turn the lights on or off or change the colour but doesn't actually change the lights. When I turn the lights off from the Home app I see the the RGB light strip glow faintly more brighter and when I turn them on from the Home app the glow faintly dimmer. I have also tried changing the colour of the light strip with an Arduino Nano I had and it worked. Therefore I assume there is something wrong with my code or the ESP-01.

Here is my ESP-01 code (programmed in Arduino IDE):

#include <ESP8266WiFi.h>
#define max(a,b) ((a)>(b)?(a):(b))

#define redPin 0 //GPIO_0 - Red channel
#define grnPin 2 //GPIO_2 - Green channel
#define bluPin 3 //GPIO_3/RXD - Blue channel

WiFiServer server(80); //Set server port

String readString;           //String to hold incoming request
String hexString = "000000"; //Define inititial color here (hex value)

int state;

int r;
int g;
int b;

float R;
float G;
float B;

int x;
int V;

///// WiFi SETTINGS - Replace with your values /////////////////
const char* ssid = "ssid";
const char* password = "password";
IPAddress ip(10,1,1,198);      // set a fixed IP
IPAddress gateway(10,1,1,1);  // Your router IP
IPAddress subnet(255,255,255,0); // Subnet mask
////////////////////////////////////////////////////////////////////

void WiFiStart() {
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print("_");
  }
  Serial.println();
  Serial.println("Done");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");

  server.begin();                    
}

void allOff() {
  state = 0;
  analogWrite(redPin, 0);
  analogWrite(grnPin, 0);
  analogWrite(bluPin, 0);
}

//Write requested hex-color to the pins (10bit pwm)
void setHex() {
  state = 1;
  long number = (long) strtol( &hexString[0], NULL, 16);
  r = number >> 16;
  g = number >> 8 & 0xFF;
  b = number & 0xFF;
  r = map(r, 0, 255, 0, 1023);  //added for 10bit pwm
  g = map(g, 0, 255, 0, 1023);  //added for 10bit pwm
  b = map(b, 0, 255, 0, 1023);  //added for 10bit pwm
  analogWrite(redPin, (r));
  analogWrite(grnPin, (g));
  analogWrite(bluPin, (b));
}

//Compute current brightness value
void getV() {
  R = roundf(r/10.23);  //for 10bit pwm, was (r/2.55);
  G = roundf(g/10.23);  //for 10bit pwm, was (g/2.55);
  B = roundf(b/10.23);  //for 10bit pwm, was (b/2.55);
  x = max(R,G);
  V = max(x, B);
}

//For serial debugging only
void showValues() {
  Serial.print("Status on/off: ");
  Serial.println(state);
  Serial.print("RGB color: ");
  Serial.print(r);
  Serial.print(".");
  Serial.print(g);
  Serial.print(".");
  Serial.println(b);
  Serial.print("Hex color: ");
  Serial.println(hexString);
  getV();
  Serial.print("Brightness: ");
  Serial.println(V);
  Serial.println("");
}

void setup(){
  Serial.begin(115200);
  setHex(); //Set initial color after booting. Value defined above
  WiFi.mode(WIFI_STA);
  WiFiStart();
  //showValues(); //Uncomment for serial output
}

void loop() {
  //Reconnect on lost WiFi connection
  if (WiFi.status() != WL_CONNECTED) {
    WiFiStart();
  }

  WiFiClient client = server.available();

  if (!client) {
    return;
  }

  while(client.connected() && !client.available()) {
    delay(1);
  }

  //Respond on certain Homebridge HTTP requests
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        } 
        if (c == '\n') {
          Serial.print("Request: "); //Uncomment for serial output 
          Serial.println(readString); //Uncomment for serial output

          //Send reponse
          client.println("HTTP/1.1 200 OK"); 
          client.println("Content-Type: text/html");
          client.println();

          //On
          if(readString.indexOf("on") >0) {
            setHex();
            showValues();
          }

          //Off
          if(readString.indexOf("off") >0) {
            allOff();
            showValues();
          }

          //Set color
          if(readString.indexOf("set") >0) {
            hexString = "";
            hexString = (readString.substring(9,15));
            setHex();
            showValues();
          }

          //Status on/off
          if(readString.indexOf("status") >0) {
          client.println(state);
          }

          //Status color (hex)
          if(readString.indexOf("color") >0) {
          client.println(hexString);
          }

          //Status brightness (%)
          if(readString.indexOf("bright") >0) {
          getV();
          client.println(V);
          }

          delay(1);
          client.stop();
          readString="";
        }
      }
    }
  }
}

Here are my connections: Conectin

schematic

simulate this circuit – Schematic created using CircuitLab

Your help would be greatly appreciated.

\$\endgroup\$
7
  • \$\begingroup\$ You should post a real schematic in order to be able to help you. \$\endgroup\$
    – nickagian
    Jul 18, 2017 at 9:07
  • \$\begingroup\$ Do you have any suggestions of programs to create good schematics? \$\endgroup\$
    – Username
    Jul 18, 2017 at 9:08
  • \$\begingroup\$ When you post a question, there is a small icon called "Schematic", next to the icon you use to attach an image. That's the easiest way. \$\endgroup\$
    – nickagian
    Jul 18, 2017 at 9:11
  • \$\begingroup\$ I didn't even know you could do that. Thanks, I'll create one now. \$\endgroup\$
    – Username
    Jul 18, 2017 at 9:13
  • \$\begingroup\$ Your breadboard might be okay, but your schematic has the FET connections all wrong. Source should go to ground, drain to the LEDs, gate to the ESP. But you're showing source to the ESP, drain to ground, and gate to the load. \$\endgroup\$ Jul 24, 2017 at 3:51

3 Answers 3

1
\$\begingroup\$

It may be a stupid thing but I think you did not defined the type of the pins you are using (Input/Output) in the setup function.

\$\endgroup\$
1
\$\begingroup\$

As you are using one of the serial port pin(RxD) as GPIO, avoid writing at Serial port. Better don't initialize Serial. Hope that will solve your issue.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to EESS. However, I hope you realize that you are answering a question from more than 1 year ago. It is unlikely that the original user still has this problem, cares, or is even around. \$\endgroup\$ Dec 10, 2018 at 19:24
0
\$\begingroup\$

Check and provide link to LEDstrip wiring.

If I'm not mistaken, Colour LEDSTRIPs use a common 12V or 24V and then RGB or RGBY or RGBW are switched to ground to control current.

This permits easy of low side switching with Nch Logic Level MOSFETs using common source to Return or Gnd and Drain to each LED strip channel for low side switching and inverted logic +3.3V for a 1V threshold MOSFET with 0~3.3V PWM.

It is important to remember FETs need at least 3x the threshold for Vgs for low RdsOn.

If the LEDstrips have a common ground and 12V to each colour, then you need a PCh MOSFET with a level shifter or extra transistor to shift from 3.3V to 0V on Pch Gate to turn on and the 0V to pull up with an R to V+ on the gate.

\$\endgroup\$
8
  • \$\begingroup\$ I am already using an N-Channel MOSFET. I should update the Fritzing diagram now. \$\endgroup\$
    – Username
    Jul 19, 2017 at 0:31
  • \$\begingroup\$ But is it a 1V FET? with output from Drain RGB and common 12V. \$\endgroup\$ Jul 19, 2017 at 0:31
  • \$\begingroup\$ The transistors I am using though are actually these mosfets though and provide more than 1v. IRF540N N-Channel MOSFET \$\endgroup\$
    – Username
    Jul 19, 2017 at 0:36
  • \$\begingroup\$ I have tried it with a Arduino Nano and I can successfully change the colour with that, so I don't think its a problem with the transistors \$\endgroup\$
    – Username
    Jul 19, 2017 at 0:37
  • \$\begingroup\$ I'm pretty sure now it is . Understand what \$V_{GS(th)}\$ Gate Threshold Voltage means and why I said gate voltage must be 3x this value from RdsON curve. The old IRF series uses 2~4 threshold to switch rated threshold of 250uA. \$\endgroup\$ Jul 19, 2017 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.