mohan-bee/light-controller

This code defines the hardware sections and symbols for a PCB involving USB power, voltage regulation, ESP12F module, RGB outputs, I2C sensors, and debug points, along with rendering labels for test points.

Version
1.0.30
License
unset
Stars
2

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone mohan-bee/light-controller  # download the package files into ./light-controller
tsci add mohan-bee/light-controller    # add it to your project as @tsci/mohan-bee.light-controller

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

firmware/SAMPLE_LIGHT_CONTROLLER/SAMPLE_LIGHT_CONTROLLER.ino

#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>

// Replace these with your Wi-Fi details before uploading.
const char* WIFI_NAME = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// RGB output pins on the light-controller board.
constexpr uint8_t RED_PIN = 12;
constexpr uint8_t GREEN_PIN = 13;
constexpr uint8_t BLUE_PIN = 14;

ESP8266WebServer server(80);

const char CONTROL_PAGE[] PROGMEM = R"HTML(
<!doctype html>
<html>
  <head>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>RGB Light Controller</title>
    <style>
      body { font-family: sans-serif; text-align: center; margin-top: 3rem; }
      input { width: 12rem; height: 8rem; border: 0; }
      button { margin-top: 1rem; padding: .8rem 2rem; font-size: 1rem; }
    </style>
  </head>
  <body>
    <h1>RGB Light Controller</h1>
    <p>Choose a color:</p>
    <input id="color" type="color" value="#ff8000">
    <br>
    <button onclick="setColor('#000000')">Turn off</button>
    <script>
      const picker = document.getElementById('color');

      function setColor(hex) {
        const value = hex.replace('#', '');
        const r = parseInt(value.slice(0, 2), 16);
        const g = parseInt(value.slice(2, 4), 16);
        const b = parseInt(value.slice(4, 6), 16);
        fetch(`/color?r=${r}&g=${g}&b=${b}`);
      }

      picker.addEventListener('input', () => setColor(picker.value));
    </script>
  </body>
</html>
)HTML";

void setRgb(uint8_t red, uint8_t green, uint8_t blue) {
  // ESP8266 PWM uses the range 0-1023. The API and web page use 0-255.
  analogWrite(RED_PIN, map(red, 0, 255, 0, 1023));
  analogWrite(GREEN_PIN, map(green, 0, 255, 0, 1023));
  analogWrite(BLUE_PIN, map(blue, 0, 255, 0, 1023));
}

void runRgbSelfTest() {
  setRgb(255, 0, 0);
  delay(300);
  setRgb(0, 255, 0);
  delay(300);
  setRgb(0, 0, 255);
  delay(300);
  setRgb(0, 0, 0);
}

uint8_t readColorValue(const String& name) {
  if (!server.hasArg(name)) return 0;
  return static_cast<uint8_t>(constrain(server.arg(name).toInt(), 0, 255));
}

void showControlPage() {
  server.send_P(200, "text/html", CONTROL_PAGE);
}

void updateColor() {
  setRgb(readColorValue("r"), readColorValue("g"), readColorValue("b"));
  server.send(200, "text/plain", "OK");
}

void setup() {
  Serial.begin(115200);

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  analogWriteRange(1023);
  analogWriteFreq(1000);
  setRgb(0, 0, 0);
  runRgbSelfTest();

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_NAME, WIFI_PASSWORD);

  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print('.');
  }

  Serial.println();
  Serial.print("Open this address in your phone: http://");
  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, showControlPage);
  server.on("/color", HTTP_GET, updateColor);
  server.begin();
}

void loop() {
  server.handleClient();
}