Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed code of Smartnest and updated documentation #4001

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 23 additions & 10 deletions usermods/smartnest/readme.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# Smartnest

Enables integration with `smartnest.cz` service which provides MQTT integration with voice assistants.
Enables integration with `smartnest.cz` service which provides MQTT integration with voice assistants, for example Google Home, Alexa, Siri, Home Assistant and more!

In order to setup Smartnest follow the [documentation](https://www.docu.smartnest.cz/).
- You can create up to 5 different devices
- To add the project to Google Home you can find the information [here](https://www.docu.smartnest.cz/google-home-integration)
- To add the project to Alexa you can find the information [here](https://www.docu.smartnest.cz/alexa-integration)

## MQTT API

The API is described in the Smartnest [Github repo](https://github.com/aososam/Smartnest/blob/master/Devices/lightRgb/lightRgb.ino).


## Usermod installation

1. Register the usermod by adding `#include "../usermods/smartnest/usermod_smartnest.h"` at the top and `usermods.add(new Smartnest());` at the bottom of `usermods_list.cpp`.
or
2. Use `#define USERMOD_SMARTNEST` in wled.h or `-D USERMOD_SMARTNEST` in your platformio.ini
1. Use `#define USERMOD_SMARTNEST` in wled.h or `-D USERMOD_SMARTNEST` in your platformio.ini (recommended).
## It is not necessary since the main branch of WLED brings it with it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not necessary - remove

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed up


2. Register the usermod by adding `#include "../usermods/smartnest/usermod_smartnest.h"` at the top and `usermods.add(new Smartnest());` at the bottom of `usermods_list.cpp`.
or

Example **usermods_list.cpp**:

Expand Down Expand Up @@ -51,11 +56,19 @@ void registerUsermods()

Usermod has no configuration, but it relies on the MQTT configuration.\
Under Config > Sync Interfaces > MQTT:
* Enable MQTT check box
* Set the `Broker` field to: `smartnest.cz`
* The `Username` and `Password` fields are the login information from the `smartnest.cz` website.

* Enable `MQTT` check box.
* Set the `Broker` field to: `smartnest.cz` or `3.122.209.170`(both work).
* Set the `Port` field to: `1883`
* The `Username` and `Password` fields are the login information from the `smartnest.cz` website (It is located above in the 3 points).
* `Client ID` field is obtained from the device configuration panel in `smartnest.cz`.
* `Device Topic` is obtained by entering the ClientID/report , remember to replace ClientId with your real information (Because they can ban your device).
* `Group Topic` keep the same Group Topic.

Wait `1 minute` after turning it on, as it usually takes a while.

## Change log
2022-09
* First implementation.
2024-05
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In changelog append the changes instead of removing the old log.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed up

* Solved code.
* Updated documentation.
* Second implementation.
43 changes: 41 additions & 2 deletions usermods/smartnest/usermod_smartnest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
class Smartnest : public Usermod
{
private:
bool initialized = false;
unsigned long lastMqttReport = 0;
unsigned long mqttReportInterval = 60000; // Report every minute
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this static const (or constexpr is better)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue to allow it to be just unsigned and use it as a run-time settable variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I better leave it like this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add configuration item like other usermods.


void sendToBroker(const char *const topic, const char *const message)
{
if (!WLED_MQTT_CONNECTED)
Expand Down Expand Up @@ -61,7 +65,7 @@ class Smartnest : public Usermod
int position = 0;

// We need to copy the string in order to keep it read only as strtok_r function requires mutable string
color_ = (char *)malloc(strlen(color));
color_ = (char *)malloc(strlen(color) + 1);
if (NULL == color_) {
return -1;
}
Expand Down Expand Up @@ -150,7 +154,7 @@ class Smartnest : public Usermod
delay(100);
sendToBroker("report/firmware", versionString); // Reports the firmware version
delay(100);
sendToBroker("report/ip", (char *)WiFi.localIP().toString().c_str()); // Reports the ip
sendToBroker("report/ip", (char *)WiFi.localIP().toString().c_str()); // Reports the IP
delay(100);
sendToBroker("report/network", (char *)WiFi.SSID().c_str()); // Reports the network name
delay(100);
Expand All @@ -168,4 +172,39 @@ class Smartnest : public Usermod
{
return USERMOD_ID_SMARTNEST;
}

/**
* setup() is called once at startup to initialize the usermod.
*/
void setup() {
// Initialization code here
if (!initialized) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is initialized needed? This function is called once (isn't it?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly my thoughts. 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed up

DEBUG_PRINTF("Smartnest usermod setup initializing...");

// Publish initial status
sendToBroker("report/status", "Smartnest usermod initialized");

initialized = true;
}
}

/**
* loop() is called continuously to keep the usermod running.
*/
void loop() {
// Periodically report status to MQTT broker
unsigned long currentMillis = millis();
if (currentMillis - lastMqttReport >= mqttReportInterval) {
lastMqttReport = currentMillis;

// Report current brightness
char brightnessMsg[6];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Max %u output may be 4,294,967,295 which needs 11 characters (including \0)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed up

sprintf(brightnessMsg, "%u", bri);
sendToBroker("report/brightness", brightnessMsg);

// Report current signal strength
String signal(WiFi.RSSI(), 10);
sendToBroker("report/signal", signal.c_str());
}
}
};