I was trying to use WEMOS (ESP8266EX) to build a low-key version Hue bulb. On WEMOS’s datasheet, it says “All of the IO pins have interrupt/pwm/I2C/one-wire support except D0.” However, For uncertain reasons, I can not use PWM to control my common anode RGB LED. So I used digital IO to change color of LED. Here is my code and result:

My next step will explore two Wifi modes of ESP8266EX: Station and Access Point to make a physical controller.,


RGB Color Reference
https://learn.sparkfun.com/tutorials/lilypad-protosnap-plus-activity-guide/3-custom-color-mixing


#include <ESP8266WiFi.h>

const char* ssid = "****";
const char* password = "****";

int redPin = D6;
int redVal = HIGH;
int greenPin = D5;
int greenVal = HIGH;
int bluePin = D0;
int blueVal = HIGH;

int vCC = D7;
int vCC_dim = 0;

WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  delay(10);// wait for Serial

  analogWrite(vCC, vCC_dim);

  pinMode(redPin, OUTPUT);
  digitalWrite(redPin, redVal);
  pinMode(greenPin, OUTPUT);
  digitalWrite(greenPin, greenVal);
  pinMode(bluePin, OUTPUT);
  digitalWrite(bluePin, blueVal);

  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");

  server.begin();

  Serial.println("Server started");

  Serial.println(WiFi.localIP());


}

void loop() {


  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  if (request.indexOf("/DIM") != -1) {

    int temp = dimVal(request);

    //Serial.println(vCC_dim);

    if ((vCC_dim + temp) > 255) {

      vCC_dim = 255;

      analogWrite(vCC, vCC_dim);

    } else if ((vCC_dim + temp) < 0) {

      vCC_dim = 0;

      analogWrite(vCC, vCC_dim);

    } else {
      vCC_dim = vCC_dim + temp;
      analogWrite(vCC, vCC_dim);

    }

  }

  if (request.indexOf("/R=ON") != -1) {
    digitalWrite(redPin, LOW);
    redVal = LOW;
  }
  if (request.indexOf("/R=OFF") != -1) {
    digitalWrite(redPin, HIGH);
    redVal = HIGH;
  }

  if (request.indexOf("/G=ON") != -1) {
    digitalWrite(greenPin, LOW);
    greenVal = LOW;
  }
  if (request.indexOf("/G=OFF") != -1) {
    digitalWrite(greenPin, HIGH);
    greenVal = HIGH;
  }

  if (request.indexOf("/B=ON") != -1) {
    digitalWrite(bluePin, LOW);
    blueVal = LOW;
  }
  if (request.indexOf("/B=OFF") != -1) {
    digitalWrite(bluePin, HIGH);
    blueVal = HIGH;
  }

  if (request.indexOf("/LED=ON") != -1) {

    vCC_dim = 255;
    analogWrite(vCC, vCC_dim);

    redVal = LOW;
    greenVal = LOW;
    blueVal = LOW;

    digitalWrite(redPin, redVal);
    digitalWrite(greenPin, greenVal);
    digitalWrite(bluePin, blueVal);

  }

  if (request.indexOf("/LED=OFF") != -1) {

    vCC_dim = 0;
    analogWrite(vCC, vCC_dim);

  }

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one

  client.print("<!DOCTYPE HTML><html>RGB:<br>/LED=(ON or OFF)<br>/DIM=(-255 ~ 255)<br>/R=(ON or OFF)<br>/B=(ON or OFF)<br>/G=(ON or OFF)<br>");
  client.print(WiFi.localIP());
  client.print("</html>");

  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");

}

int dimVal(String request) {

  char data[30];
  int val = 0;
  request.toCharArray(data, 30);
  sscanf(data, "GET /DIM=%d HTTP/1.1", &val);

  return val;

}