Close

Back to the source

A project log for LED floorlamp with MQTT control

Make a bedroom floorlamp with control for smooth wake-up

simon-jansenSimon Jansen 08/24/2022 at 10:020 Comments

I've found the source code!

It was on an older laptop that got passed along and that I happened to get on my desk for some updates. Anyway, glad to have it. Now I can improve on it, add OTA, MQTT-autodiscover etc. 

And try to ignore the big cringe you get when you see your own software from a few years ago :|

#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <PubSubClient.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRrecv.h>
#include <IRutils.h>

#define PIN_IRLED 10
#define PIN_IRSENSOR 4

uint16_t RECV_PIN = PIN_IRSENSOR;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long interval = 60000;

const char* ssid     = "***";
const char* password = "***";
IPAddress mqtt_server(192,168,1,***);

char* message = "";
WiFiClient espClient;
PubSubClient client(espClient);

IRsend irsend(PIN_IRLED);  // An IR LED is controlled by GPIO pin 4 (D2)
IRrecv irrecv(RECV_PIN);
decode_results results;

void callback(char* topic, byte* payload, unsigned int length) {
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i=0;i<length;i++) {
    char receivedChar = (char)payload[i];
    Serial.print(receivedChar);
    if (receivedChar == '0'){
      Serial.println(" - Off received");
      client.publish("LEDStalamp/Response"," - ALL OFF received");
      irsend.sendNEC(0xFF827D, 32);
      irsend.sendNEC(0xFFE817, 32);
    }
    if (receivedChar == '1'){
      Serial.println(" - On received");
      client.publish("LEDStalamp/Response"," - ALL ON received");
      irsend.sendNEC(0xFF02FD, 32);
      irsend.sendNEC(0xFF6897, 32);
    }
    if (receivedChar == 'U'){
      Serial.println("RGB+ received");
      client.publish("LEDStalamp/Response"," - RGB+ received");
      irsend.sendNEC(0xFF3AC5, 32);
    }
    if (receivedChar == 'X'){
      Serial.println("RGB- received");
      client.publish("LEDStalamp/Response"," - RGB- received");
      irsend.sendNEC(0xFFBA45, 32);
    }
    if (receivedChar == 'Y'){
      Serial.println("RGB OFF received");
      client.publish("LEDStalamp/Response"," - RGB OFF received");
      irsend.sendNEC(0xFF827D, 32);
    }
    if (receivedChar == 'Z'){
      Serial.println("RGB ON received");
      client.publish("LEDStalamp/Response"," - RGB ON received");
      irsend.sendNEC(0xFF02FD, 32);
    }
    if (receivedChar == 'R'){
      Serial.println("RGB RED received");
      client.publish("LEDStalamp/Response"," - RGB RED received");
      irsend.sendNEC(0xFF1AE5, 32);
    }
    if (receivedChar == 'G'){
      Serial.println("RGB GREEN received");
      client.publish("LEDStalamp/Response"," - RGB GREEN received");
      irsend.sendNEC(0xFF9A65, 32);
    }
    if (receivedChar == 'B'){
      Serial.println("RGB BLUE received");
      client.publish("LEDStalamp/Response"," - RGB BLUE received");
      irsend.sendNEC(0xFFA25D, 32);
    }
    if (receivedChar == 'W'){
      Serial.println("RGB WHITE received");
      client.publish("LEDStalamp/Response"," - RGB WHITE received");
      irsend.sendNEC(0xFF22DD, 32);
    }
    if (receivedChar == 'C'){
      Serial.println("RGB R1 received");
      client.publish("LEDStalamp/Response"," - RGB R1 received");
      irsend.sendNEC(0xFF2AD5, 32);
    }
    if (receivedChar == 'D'){
      Serial.println("RGB R2 received");
      client.publish("LEDStalamp/Response"," - RGB R2 received");
      irsend.sendNEC(0xFF0AF5, 32);
    }
    if (receivedChar == 'E'){
      Serial.println("RGB R3 received");
      client.publish("LEDStalamp/Response"," - RGB R3 received");
      irsend.sendNEC(0xFF38C7, 32);
    }
    if (receivedChar == 'F'){
      Serial.println("RGB R4 received");
      client.publish("LEDStalamp/Response"," - RGB R4 received");
      irsend.sendNEC(0xFF18E7, 32);
    }
    if (receivedChar == 'H'){
      Serial.println("RGB G1 received");
      client.publish("LEDStalamp/Response"," - RGB G1 received");
      irsend.sendNEC(0xFFAA55, 32);
    }
    if (receivedChar == 'I'){
      Serial.println("RGB G2 received");
      client.publish("LEDStalamp/Response"," - RGB G2 received");
      irsend.sendNEC(0xFF8A75, 32);
    }
    if (receivedChar == 'J'){
      Serial.println("RGB G3 received");
      client.publish("LEDStalamp/Response"," - RGB G3 received");
      irsend.sendNEC(0xFFB847, 32);
    }
    if (receivedChar == 'K'){
      Serial.println("RGB G4 received");
      client.publish("LEDStalamp/Response"," - RGB G4 received");
      irsend.sendNEC(0xFF9867, 32);
    }
    if (receivedChar == 'L'){
      Serial.println("RGB B1 received");
      client.publish("LEDStalamp/Response"," - RGB B1 received");
      irsend.sendNEC(0xFF926D, 32);
    }
    if (receivedChar == 'M'){
      Serial.println("RGB B2 received");
      client.publish("LEDStalamp/Response"," - RGB B2 received");
      irsend.sendNEC(0xFFB24D, 32);
    }
    if (receivedChar == 'N'){
      Serial.println("RGB B2 received");
      client.publish("LEDStalamp/Response"," - RGB B3 received");
      irsend.sendNEC(0xFF7887, 32);
    }
    if (receivedChar == 'O'){
      Serial.println("RGB B4 received");
      client.publish("LEDStalamp/Response"," - RGB B4 received");
      irsend.sendNEC(0xFF58A7, 32);
    }
    if (receivedChar == 'P'){
      Serial.println("RGB W1 received");
      client.publish("LEDStalamp/Response"," - RGB W1 received");
      irsend.sendNEC(0xFF12ED, 32);
    }
    if (receivedChar == 'Q'){
      Serial.println("RGB W2 received");
      client.publish("LEDStalamp/Response"," - RGB W2 received");
      irsend.sendNEC(0xFF32CD, 32);
    }
    if (receivedChar == 'S'){
      Serial.println("RGB W3 received");
      client.publish("LEDStalamp/Response"," - RGB W3 received");
      irsend.sendNEC(0xFFF807, 32);
    }
    if (receivedChar == 'T'){
      Serial.println("RGB W4 received");
      client.publish("LEDStalamp/Response"," - RGB W4 received");
      irsend.sendNEC(0xFFD827, 32);
    }
    if (receivedChar == 'a'){
      Serial.println("WHITE+ received");
      client.publish("LEDStalamp/Response"," - WHITE+ received");
      irsend.sendNEC(0xFF28D7, 32);
    }
    if (receivedChar == 'b'){
      Serial.println("WHITE- received");
      client.publish("LEDStalamp/Response"," - WHITE- received");
      irsend.sendNEC(0xFFA857, 32);
    }
    if (receivedChar == 'c'){
      Serial.println("WHITE OFF received");
      client.publish("LEDStalamp/Response"," - WHITE OFF received");
      irsend.sendNEC(0xFF6897, 32);
    }
    if (receivedChar == 'd'){
      Serial.println("WHITE ON received");
      client.publish("LEDStalamp/Response"," - WHITE ON received");
      irsend.sendNEC(0xFFE817, 32);
    }
    if (receivedChar == 'e'){
      Serial.println("WHITE 25% received");
      client.publish("LEDStalamp/Response"," - WHITE 25% received");
      irsend.sendNEC(0xFF08F7, 32);
    }
    if (receivedChar == 'f'){
      Serial.println("WHITE 50% received");
      client.publish("LEDStalamp/Response"," - WHITE 50% received");
      irsend.sendNEC(0xFF8877, 32);
    }
    if (receivedChar == 'g'){
      Serial.println("WHITE 75% received");
      client.publish("LEDStalamp/Response"," - WHITE 75% received");
      irsend.sendNEC(0xFF48B7, 32);
    }
    if (receivedChar == 'h'){
      Serial.println("WHITE 100% received");
      client.publish("LEDStalamp/Response"," - WHITE 100% received");
      irsend.sendNEC(0xFFC837, 32);
    }
    if (receivedChar == 'i'){
      Serial.println("JUMP3 received");
      client.publish("LEDStalamp/Response"," - JUMP3 received");
      irsend.sendNEC(0xFF30CF, 32);
    }
    if (receivedChar == 'j'){
      Serial.println("FADE3 received");
      client.publish("LEDStalamp/Response"," - FADE3 received");
      irsend.sendNEC(0xFFB04F, 32);
    }
    if (receivedChar == 'k'){
      Serial.println("JUMP7 received");
      client.publish("LEDStalamp/Response"," - JUMP7 received");
      irsend.sendNEC(0xFF708F, 32);
    }
    if (receivedChar == 'l'){
      Serial.println("QUICK received");
      client.publish("LEDStalamp/Response"," - QUICK received");
      irsend.sendNEC(0xFFF00F, 32);
    }
    if (receivedChar == 'm'){
      Serial.println("FADE7 received");
      client.publish("LEDStalamp/Response"," - FADE7 received");
      irsend.sendNEC(0xFF10EF, 32);
    }
    if (receivedChar == 'n'){
      Serial.println("FLASH received");
      client.publish("LEDStalamp/Response"," - FLASH received");
      irsend.sendNEC(0xFF906F, 32);
    }
    if (receivedChar == 'o'){
      Serial.println("AUTO received");
      client.publish("LEDStalamp/Response"," - AUTO received");
      irsend.sendNEC(0xFF50AF, 32);
    }
    if (receivedChar == 'p'){
      Serial.println("SLOW received");
      client.publish("LEDStalamp/Response"," - SLOW received");
      irsend.sendNEC(0xFFD02F, 32);
    }
  }
  Serial.println();
}


void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect("ESP8266", "snokkie", "jiggiejiggie")) {
  Serial.println("connected");
  // ... and subscribe to topic
  client.subscribe("LEDStalamp/Command");
  message = "MQTT connected: ";
  client.publish("LEDStalamp/Response",message);
 } else {
  Serial.print("failed, rc=");
  Serial.print(client.state());
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(5000);
  }
 }
}
void setup(){
  irsend.begin();
  irrecv.enableIRIn();  // Start the receiver
  pinMode(PIN_IRSENSOR,INPUT_PULLUP);
  pinMode(PIN_IRLED, OUTPUT);
  // nothing to setup
  Serial.begin(115200);
  Serial.println("\n--START--");
  WiFiMulti.addAP(ssid, password);
    while(WiFiMulti.run() != WL_CONNECTED) {
      Serial.print(".");
      delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
void loop(){
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  currentMillis = millis();
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;   
      client.publish("LEDStalamp/Response"," - Still allive!");
  }
    if (irrecv.decode(&results)) {
    // print() & println() can't handle printing long longs. (uint64_t)
    serialPrintUint64(results.value, HEX);
    Serial.println("");
    irrecv.resume();  // Receive the next value
  }
}

Discussions