Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lightbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Lightbar::~Lightbar()
{
}

void Lightbar::toggleInternalState()
{
onState = !onState;
}

bool Lightbar::getOnState()
{
return onState;
}

uint32_t Lightbar::getSerial()
{
return this->serial;
Expand Down Expand Up @@ -44,12 +54,6 @@ void Lightbar::onOff()
onState = !onState;
}

void Lightbar::setOnOff(bool on)
{
if (onState != on)
this->onOff();
}

void Lightbar::brighter()
{
this->sendRawCommand(Lightbar::Command::BRIGHTER);
Expand Down
5 changes: 3 additions & 2 deletions lightbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class Lightbar
void cooler();
void reset();
void pair();
void setOnOff(bool on);
void setTemperature(uint8_t value);
void setMiredTemperature(uint mireds);
void setBrightness(uint8_t value);
void toggleInternalState();
bool getOnState();

private:
Radio *radio;
bool onState = false;
bool onState = true;
uint32_t serial;
String serialString;
const char *name;
Expand Down
59 changes: 55 additions & 4 deletions mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ void MQTT::onMessage(char *topic, byte *payload, unsigned int length)
return;
}

if (!strcmp(topic, String(this->getCombinedRootTopic() + "/" + lightbar->getSerialString() + "/toggle_internal").c_str()))
{
lightbar->toggleInternalState();
this->publishLightbarState(lightbar);
return;
}

if (strcmp(topic, String(this->getCombinedRootTopic() + "/" + lightbar->getSerialString() + "/command").c_str()))
continue;

Expand All @@ -75,8 +82,8 @@ void MQTT::onMessage(char *topic, byte *payload, unsigned int length)

if (command.hasOwnProperty("state"))
{
const char *state = command["state"];
lightbar->setOnOff(strcmp(state, "ON"));
lightbar->onOff();
this->publishLightbarState(lightbar);
}

if (command.hasOwnProperty("brightness"))
Expand All @@ -91,6 +98,25 @@ void MQTT::onMessage(char *topic, byte *payload, unsigned int length)
}
}

Lightbar *MQTT::get_lightbar_by_serial(uint32_t serial){
for (int i = 0; i < this->lightbarCount; i++){
if (this->lightbars[i]->getSerial() == serial){
return this->lightbars[i];
}
}
}

void MQTT::publishLightbarState(Lightbar *lightbar){
JSONVar stateJson;
stateJson["state"] = lightbar->getOnState() ? "ON" : "OFF";

String payload = JSON.stringify(stateJson);
String stateTopic = this->getCombinedRootTopic() + "/" + lightbar->getSerialString() + "/state";

// Publish with retain flag set to true
this->client->publish(stateTopic.c_str(), payload.c_str(), true);
}

void MQTT::setup()
{
Serial.print("[MQTT] Device ID: ");
Expand Down Expand Up @@ -121,8 +147,15 @@ void MQTT::setup()
this->client->publish(String(this->getCombinedRootTopic() + "/availability").c_str(), "online", true);
this->client->subscribe(String(this->getCombinedRootTopic() + "/+/command").c_str());
this->client->subscribe(String(this->getCombinedRootTopic() + "/+/pair").c_str());
this->client->subscribe(String(this->getCombinedRootTopic() + "/+/toggle_internal").c_str());

this->sendAllHomeAssistantDiscoveryMessages();

//send your current state after init, should be on by default
for (int i = 0; i < this->lightbarCount; i++){
this->publishLightbarState(this->lightbars[i]);
}

}

bool MQTT::addLightbar(Lightbar *lightbar)
Expand Down Expand Up @@ -179,7 +212,7 @@ bool MQTT::removeRemote(Remote *remote)
{
if (this->remotes[i] == remote)
{
this->remotes[i]->registerCommandListener(this->remoteCommandHandler);
this->remotes[i]->unregisterCommandListener(this->remoteCommandHandler);
for (int j = i; j < this->remoteCount - 1; j++)
{
this->remotes[j] = this->remotes[j + 1];
Expand Down Expand Up @@ -256,6 +289,7 @@ void MQTT::sendHomeAssistantLightbarDiscoveryMessages(Lightbar *lightbar)
"brightness_scale": 15,
"name": "Light bar",
"cmd_t": "~/command",
"stat_t": "~/state",
"uniq_id": ")json" + topicClient +
R"json(_lightbar",
"max_mireds": 370,
Expand All @@ -280,6 +314,19 @@ void MQTT::sendHomeAssistantLightbarDiscoveryMessages(Lightbar *lightbar)
this->client->beginPublish(String(homeAssistantDiscoveryPrefix + "/button/" + topicClient + "/pair/config").c_str(), rendevous_str.length(), true);
this->client->print(rendevous_str);
this->client->endPublish();

rendevous_str = "{" +
baseConfig +
R"json(
"name": "Toggle Internal State",
"cmd_t": "~/toggle_internal",
"uniq_id": ")json" +
topicClient + R"json(_toggle_internal",
"p": "button"
)json" + "}";
this->client->beginPublish(String(homeAssistantDiscoveryPrefix + "/button/" + topicClient + "/toggle_internal/config").c_str(), rendevous_str.length(), true);
this->client->print(rendevous_str);
this->client->endPublish();
}

void MQTT::sendHomeAssistantRemoteDiscoveryMessages(Remote *remote)
Expand Down Expand Up @@ -382,9 +429,13 @@ void MQTT::sendAction(Remote *remote, byte command, byte options)
String action;
switch ((uint8_t)command)
{
case Lightbar::Command::ON_OFF:
case Lightbar::Command::ON_OFF:{
action = "press";
Lightbar *lightbar = this->get_lightbar_by_serial(remote->getSerial());
lightbar->toggleInternalState();
this->publishLightbarState(lightbar);
break;
}

case Lightbar::Command::BRIGHTER:
action = "turn_clockwise";
Expand Down
3 changes: 3 additions & 0 deletions mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class MQTT
void sendAction(Remote *remote, byte command, byte options);
const String getCombinedRootTopic();
const String getClientId();
boolean get_MQTT_connection_failed();
void publishLightbarState(Lightbar *lightbar);
Lightbar *get_lightbar_by_serial(uint32_t serial);

private:
WiFiClient *wifiClient;
Expand Down
12 changes: 10 additions & 2 deletions radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void Radio::setup()
Serial.println("[Radio] Setting up radio...");
this->radio.failureDetected = false;

this->radio.openReadingPipe(0, Radio::address);
this->radio.openReadingPipe(0, Radio::receive_address);

this->radio.setChannel(68);
this->radio.setDataRate(RF24_2MBPS);
Expand All @@ -166,7 +166,7 @@ void Radio::setup()
this->radio.setAutoAck(false);
this->radio.setRetries(15, 15);

this->radio.openWritingPipe(Radio::address);
this->radio.openWritingPipe(Radio::send_address);

this->radio.startListening();
Serial.println("[Radio] done!");
Expand Down Expand Up @@ -261,6 +261,14 @@ void Radio::handlePackage()
Serial.println("[Radio] Ignoring package with too low package number!");
return;
}

if (package_id_for_serial->never_read) {
package_id_for_serial->never_read = false;
}
else if (package_id_for_serial->package_id == package_id){
return;
}

package_id_for_serial->package_id = package_id;

Serial.println("[Radio] Package received!");
Expand Down
4 changes: 3 additions & 1 deletion radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Remote;
struct PackageIdForSerial
{
uint32_t serial;
boolean never_read = true;
uint8_t package_id;
};

Expand All @@ -35,7 +36,8 @@ class Radio
Remote *remotes[constants::MAX_REMOTES];
uint8_t num_remotes = 0;

static const uint64_t address = 0xAAAAAAAAAAAA;
static const uint64_t send_address = 0x5555555555;
static const uint64_t receive_address = 0xAAAAAAAAAA;
static constexpr byte preamble[8] = {0x53, 0x39, 0x14, 0xDD, 0x1C, 0x49, 0x34, 0x12};

// For details on how these parameters were chosen, see
Expand Down