feature/add_mqtt_and_more #2

Merged
tliu93 merged 3 commits from feature/add_mqtt_and_more into main 2025-08-27 17:30:22 +02:00
4 changed files with 58 additions and 8 deletions
Showing only changes of commit d8943c00ab - Show all commits

View File

@@ -55,5 +55,6 @@
"thread": "cpp", "thread": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp"
} },
"idf.portWin": "COM4"
} }

View File

@@ -2,8 +2,11 @@
#include "light.h" #include "light.h"
#include "mqtt.h" #include "mqtt.h"
constexpr uint16_t CONFIG_MSG_SIZE = 1024; constexpr uint16_t configMsgSize = 1024;
constexpr uint16_t STATUS_MSG_SIZE = 128; constexpr uint16_t statusMsgSize = 128;
constexpr uint8_t minPwmValue = 0;
constexpr float gammaCorrection = 2.2f;
constexpr uint8_t maxPwm = 255;
const struct { const struct {
String available = "online"; String available = "online";
@@ -19,6 +22,14 @@ Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Mqtt* mqttClient, std::string uniq
subscribeToMqttTopics(); subscribeToMqttTopics();
} }
Light::Light(Pin* pinR, Pin* pinG, Pin* pinB, Pin* pinCW, Pin* pinWW, Mqtt* mqttClient, std::string uniqueId)
: pinR(pinR), pinG(pinG), pinB(pinB), pinCW(pinCW), pinWW(pinWW), mqttClient(mqttClient) {
lightInfo.uniqueId = uniqueId;
lightType = LightType::rgbww;
publishInitialState();
subscribeToMqttTopics();
}
void Light::publishInitialState() { void Light::publishInitialState() {
// Publish the initial state of the light // Publish the initial state of the light
JsonDocument configInfo; JsonDocument configInfo;
@@ -89,12 +100,30 @@ void Light::operatePin() {
} }
float brightnessFactor = brightness / 255.0f; float brightnessFactor = brightness / 255.0f;
rSetpoint = static_cast<uint32_t>(r * brightnessFactor); rSetpoint = static_cast<uint32_t>(r * brightnessFactor);
rSetpoint = correctGamma(rSetpoint);
// if (rSetpoint < minPwmValue && rSetpoint > 0) {
// rSetpoint = minPwmValue;
// }
gSetpoint = static_cast<uint32_t>(g * brightnessFactor); gSetpoint = static_cast<uint32_t>(g * brightnessFactor);
gSetpoint = correctGamma(gSetpoint);
// if (gSetpoint < minPwmValue && gSetpoint > 0) {
// gSetpoint = minPwmValue;
// }
bSetpoint = static_cast<uint32_t>(b * brightnessFactor); bSetpoint = static_cast<uint32_t>(b * brightnessFactor);
bSetpoint = correctGamma(bSetpoint);
// if (bSetpoint < minPwmValue && bSetpoint > 0) {
// bSetpoint = minPwmValue;
// }
Serial.printf("Setting RGB: R=%d, G=%d, B=%d with brightness factor: %.2f\n", rSetpoint, gSetpoint, bSetpoint, brightnessFactor); Serial.printf("Setting RGB: R=%d, G=%d, B=%d with brightness factor: %.2f\n", rSetpoint, gSetpoint, bSetpoint, brightnessFactor);
pinR->setLedLevel(rSetpoint); pinR->setLedLevel(rSetpoint);
pinG->setLedLevel(gSetpoint); pinG->setLedLevel(gSetpoint);
pinB->setLedLevel(bSetpoint); pinB->setLedLevel(bSetpoint);
if (pinCW != nullptr) {
pinCW->setLedLevel(cw);
}
if (pinWW != nullptr) {
pinWW->setLedLevel(ww);
}
Serial.printf("Set RGB: R=%d, G=%d, B=%d\n", r, g, b); Serial.printf("Set RGB: R=%d, G=%d, B=%d\n", r, g, b);
} }
@@ -129,8 +158,14 @@ void Light::handleCommand(const std::string& command) {
r = color["r"] | 255; // Default to 255 if not provided r = color["r"] | 255; // Default to 255 if not provided
g = color["g"] | 255; // Default to 255 if not provided g = color["g"] | 255; // Default to 255 if not provided
b = color["b"] | 255; // Default to 255 if not provided b = color["b"] | 255; // Default to 255 if not provided
if (lightType == LightType::rgbw || lightType == LightType::rgbww) {
ww = color["w"] | 255; // Default to 255 if not provided
} }
if (lightType == LightType::rgb) if (lightType == LightType::rgbww) {
cw = color["cw"] | 255; // Default to 255 if not provided
}
}
if (lightType == LightType::rgb || lightType == LightType::rgbw || lightType == LightType::rgbww)
{ {
operatePin(); operatePin();
} }
@@ -184,3 +219,9 @@ void Light::publishCurrentState() {
Serial.println("Publishing current state: " + String(stateJson.c_str())); Serial.println("Publishing current state: " + String(stateJson.c_str()));
mqttClient->publish(lightInfo.stateTopic, stateJson); mqttClient->publish(lightInfo.stateTopic, stateJson);
} }
uint32_t Light::correctGamma(uint32_t originalPwm) {
// Apply gamma correction to the PWM value
float pwmPercentage = originalPwm / 255.0f;
return static_cast<uint32_t>(pow(pwmPercentage, 1 / gammaCorrection) * 255);
}

View File

@@ -74,6 +74,7 @@ public:
private: private:
void handleCommand(const std::string& command); void handleCommand(const std::string& command);
void operatePin(); void operatePin();
uint32_t correctGamma(uint32_t originalPwm);
uint8_t r = 255; // Default to white uint8_t r = 255; // Default to white
uint8_t g = 255; // Default to white uint8_t g = 255; // Default to white
uint8_t b = 255; // Default to white uint8_t b = 255; // Default to white

View File

@@ -16,9 +16,11 @@ Task *updateTask = nullptr;
Task *mqttTickTask = nullptr; Task *mqttTickTask = nullptr;
Task *mqttCheckConnectionTask = nullptr; Task *mqttCheckConnectionTask = nullptr;
Pin *pinR = new Pin(14, true, true, 5000, 0); // Example pin numbers, adjust as needed Pin *pinR = new Pin(16, true, true, 5000, 0); // Example pin numbers, adjust as needed
Pin *pinG = new Pin(15, true, true, 5000, 1); Pin *pinG = new Pin(17, true, true, 5000, 1);
Pin *pinB = new Pin(16, true, true, 5000, 2); Pin *pinB = new Pin(18, true, true, 5000, 2);
Pin *pinCW = new Pin(19, true, true, 5000, 3);
Pin *pinWW = new Pin(21, true, true, 5000, 4);
Scheduler *scheduler; Scheduler *scheduler;
@@ -28,12 +30,17 @@ void setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
Serial.begin(115200); Serial.begin(115200);
Serial.println("Starting Smart RGB ESP32..."); Serial.println("Starting Smart RGB ESP32...");
pinR->setLedLevel(0);
pinG->setLedLevel(0);
pinB->setLedLevel(0);
pinCW->setLedLevel(0);
pinWW->setLedLevel(0);
network = new Network("smart-rgb"); network = new Network("smart-rgb");
otaHandler = new OTAHandler("smart-rgb-ota"); otaHandler = new OTAHandler("smart-rgb-ota");
network->registerMDNS(); network->registerMDNS();
Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt"); Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
delay(1000); // Wait for MQTT connection to stabilize delay(1000); // Wait for MQTT connection to stabilize
light = new Light(pinR, pinG, pinB, mqttClient, "smart_rgb_light"); light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
initializeScheduler(); initializeScheduler();
} }