Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Wi-Fi Controlled RGB LED On-Air Lamp with Wemos D1 Mini

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled RGB LED On-Air Lamp with Wemos D1 Mini

Circuit Documentation

Summary

This circuit is designed to control an "On Air" lamp using a Wemos D1 Mini microcontroller. The lamp features an RGB LED and an optocoupler for external control. The microcontroller connects to a Wi-Fi network and can be controlled via a web server. The RGB LED indicates the status of the lamp, and the optocoupler allows for external signaling.

Component List

  1. RGB LED

    • Description: A multi-color LED with Red, Green, and Blue channels.
    • Pins: B, G, R, GND
  2. Resistor (100k Ohms)

    • Description: Limits current to protect components.
    • Pins: pin1, pin2
    • Properties: Resistance: 100k Ohms
  3. Resistor (470 Ohms)

    • Description: Limits current to protect components.
    • Pins: pin1, pin2
    • Properties: Resistance: 470 Ohms
  4. Resistor (220 Ohms)

    • Description: Limits current to protect components.
    • Pins: pin1, pin2
    • Properties: Resistance: 220 Ohms
  5. Wemos D1 Mini

    • Description: A microcontroller based on the ESP8266, with Wi-Fi capabilities.
    • Pins: RST, A0, D0, D5, D6, D7, D8, 3V3, 5V, G, D4, D3, D2, D1, RX, TX
  6. External Connection

    • Description: Provides an interface for external connections.
    • Pins: External
  7. Octocoupler

    • Description: An optocoupler for isolating different sections of the circuit.
    • Pins: Anode, Cathode, Collector, Emitter

Wiring Details

RGB LED

Resistor (100k Ohms)

Resistor (470 Ohms)

Resistor (220 Ohms)

Wemos D1 Mini

Octocoupler

External Connection

Code Documentation

/*
  On Air Lamp, Version 1.0.0
  Copyright (c) 2024, Heise Medien GmbH & Co. KG / c’t
  Beschreibung des Programms in:
    Hajo Schulz
    Achtung, Aufnahme
    Bastelprojekt: „On Air“-Lampe mit WLAN
    c’t 25/2024, S. 90
    https://www.heise.de/select/ct/archiv/2024/25/seite-090
*/

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

// ---
// Configurable settings
// ---
const char* WifiSsid = "Ihre WLAN-SSID";
const char* WifiPwd = "Ihr WLAN-Passwort";
const char* ServerName = "onairlamp";

// ---
// GPIO pins
// ---
// RGB LED on D5 - D7
const int RgbRedPin = D6;
const int RgbGreenPin = D7;
const int RgbBluePin = D5;
// Optocoupler on D1
const int OptoPin = D1;
// LSR on A0;
const int LsrPin = A0;

// ---
// Helpers for RGB LED
// ---
enum RgbColor {
  BLACK  = 0,
  RED    = 1,
  GREEN  = 2,
  YELLOW = 3,
  BLUE   = 4,
  PURPLE = 5,
  CYAN   = 6,
  WHITE  = 7
};

void setRgb(RgbColor color)
{
  digitalWrite(RgbRedPin, color & 1 ? HIGH : LOW);
  digitalWrite(RgbGreenPin, color & 2 ? HIGH : LOW);
  digitalWrite(RgbBluePin, color & 4 ? HIGH : LOW);
}

// ---
// The lamp
// ---
int lampState = 0;
// If you want the lamp to pulsate when "On Air" instead of shining
// continuously, just swap the names of the following two functions.
void lampOn()
{
  digitalWrite(OptoPin, HIGH);
  delay(100);
  digitalWrite(OptoPin, LOW);
  delay(200);
}
void lampOff()
{
  digitalWrite(OptoPin, HIGH);
  delay(100);
  digitalWrite(OptoPin, LOW);
  delay(200);
  digitalWrite(OptoPin, HIGH);
  delay(100);
  digitalWrite(OptoPin, LOW);
  delay(200);
}
void switchLamp(int desiredState = -1)
{
  if(desiredState == -1) {
    desiredState = (lampState == 1 ? 0 : 1);
  }
  if(desiredState != lampState) {
    if(desiredState == 1) {
      lampOn();
    }
    else {
      lampOff();
    }
    lampState = desiredState;
  }
}

// ---
// The web server
// ---
ESP8266WebServer server(80);
bool usingMDNS = false;

String prepareAnswer()
{
  String htmlPage;
  htmlPage.reserve(1024);               // prevent RAM fragmentation
  htmlPage = "<!DOCTYPE HTML>\r\n"
             "<html>\r\n"
             "<head>\r\n"
             "<meta charset=\"utf-8\">\r\n"
             "<title>On-Air-Lampe</title>\r\n"
             "</head>\r\n"
             "<body>\r\n"
             "<h1>On-Air-Lampe</h1>\r\n"
             "<p>Die Lampe ist <span id=\"lampstate\" style=\"font-weight:bold;\">";
  htmlPage += (lampState == 1 ? "an" : "aus");
  htmlPage += "</span>.</p>\r\n"
              "</body></html>\r\n";
  return htmlPage;
}

void handleLamp(int request)
{
  setRgb(YELLOW);
  if(request == 0 || request == 1) {
    switchLamp(request);
  }
  String answer = prepareAnswer();
  server.send(200, "text/html; charset=utf-8", answer.c_str());
  setRgb(GREEN);
}

void handleRoot()
{
  handleLamp(-1);
}

void handleOn()
{
  handleLamp(1);
}

void handleOff()
{
  handleLamp(0);
}

void handleNotFound()
{
  setRgb(RED);
  server.send(404, "text/plain", "404 not found\r\n");
  setRgb(GREEN);
}

// ---
// Initialize everything
// --
void setup(void)
{
  pinMode(RgbRedPin, OUTPUT);
  pinMode(RgbGreenPin, OUTPUT);
  pinMode(RgbBluePin, OUTPUT);
  pinMode(OptoPin, OUTPUT);
  pinMode(LsrPin, INPUT);
  setRgb(BL