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

WIP: make sensors web configurable #54

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
64 changes: 57 additions & 7 deletions src/MqttPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct MqttConfig
char username[128] = "";
char password[128] = "";
char topic[128] = "iot/smartmeter/";
char jsonPayload[9] = "";
};

class MqttPublisher
Expand All @@ -50,10 +51,20 @@ class MqttPublisher
client.setCredentials(config.username, config.password);
}
client.setCleanSession(true);
client.setWill(lastWillTopic.c_str(), MQTT_LWT_QOS, MQTT_LWT_RETAIN, MQTT_LWT_PAYLOAD_OFFLINE);
if(config.jsonPayload[0] == 's')
{
if(lastWillJsonPayload != 0){
delete lastWillJsonPayload;
}
lastWillJsonPayload = new_json_wrap(lastWillTopic.c_str(), MQTT_LWT_PAYLOAD_OFFLINE);
client.setWill(lastWillTopic.c_str(), MQTT_LWT_QOS, MQTT_LWT_RETAIN, lastWillJsonPayload);
}
else
{
client.setWill(lastWillTopic.c_str(), MQTT_LWT_QOS, MQTT_LWT_RETAIN, MQTT_LWT_PAYLOAD_OFFLINE);
}
client.setKeepAlive(MQTT_RECONNECT_DELAY * 3);
this->registerHandlers();

}

void debug(const char *message)
Expand Down Expand Up @@ -145,7 +156,11 @@ class MqttPublisher
}
DEBUG(F("MQTT: Disconnecting from broker..."));
client.disconnect();
this->reconnectTimer.detach();
}

bool isConnected()
{
return connected;
}

private:
Expand All @@ -155,6 +170,7 @@ class MqttPublisher
Ticker reconnectTimer;
String baseTopic;
String lastWillTopic;
const char* lastWillJsonPayload = 0;

void publish(const String &topic, const String &payload, uint8_t qos=0, bool retain=false)
{
Expand All @@ -175,14 +191,48 @@ class MqttPublisher
if (this->connected)
{
DEBUG(F("MQTT: Publishing to %s:"), topic);
DEBUG(F("%s\n"), payload);
client.publish(topic, qos, retain, payload, strlen(payload));
if(config.jsonPayload[0] == 's')
{
const char* buf = new_json_wrap(topic, payload);
DEBUG(F("%s\n"), buf);
client.publish(topic, qos, retain, buf, strlen(buf));
delete buf;
}
else
{
DEBUG(F("%s\n"), payload);
client.publish(topic, qos, retain, payload, strlen(payload));
}
}
}

void registerHandlers()
const char* new_json_wrap(const char* topic, const char* payload)
{
const char* subtopic = topic + baseTopic.length();
size_t bufsize = strlen(payload) + strlen(subtopic) + 8;
char* buf = new char[bufsize];
bool payloadIsNumber = true;
for (const char* i = payload; *i != '\0'; i++)
{
if (!isdigit(*i))
{
payloadIsNumber = false;
break;
}
}
if(payloadIsNumber)
{
sniprintf(buf, bufsize, "{\"%s\":%s}", subtopic, payload);
}
else
{
sniprintf(buf, bufsize, "{\"%s\":\"%s\"}", subtopic, payload);
}
return buf;
}

void registerHandlers()
{
client.onConnect([this](bool sessionPresent) {
this->connected = true;
this->reconnectTimer.detach();
Expand All @@ -197,7 +247,7 @@ class MqttPublisher
DEBUG(F("MQTT: Disconnected. Reason: %d."), reason);
reconnectTimer.attach(MQTT_RECONNECT_DELAY, [this]() {
if (WiFi.isConnected()) {
this->connect();
this->connect();
}
});
});
Expand Down
21 changes: 14 additions & 7 deletions src/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ uint64_t millis64()
class SensorConfig
{
public:
const uint8_t pin;
const char *name;
const bool numeric_only;
const bool status_led_enabled;
const bool status_led_inverted;
const uint8_t status_led_pin;
const uint8_t interval;
uint8_t pin;
char* name;
bool numeric_only;
bool status_led_enabled;
bool status_led_inverted;
uint8_t status_led_pin;
uint16_t interval;
};

class Sensor
Expand Down Expand Up @@ -73,6 +73,11 @@ class Sensor
this->init_state();
}

bool hasProcessedMessage()
{
return processedMessage;
}

void loop()
{
this->run_current_state();
Expand All @@ -95,6 +100,7 @@ class Sensor
State state = INIT;
void (*callback)(byte *buffer, size_t len, Sensor *sensor) = NULL;
unique_ptr<JLed> status_led;
bool processedMessage;

void run_current_state()
{
Expand Down Expand Up @@ -283,6 +289,7 @@ class Sensor
// Call listener
if (this->callback != NULL)
{
this->processedMessage = true;
this->callback(this->buffer, this->position, this);
}

Expand Down
13 changes: 1 addition & 12 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@ const char *VERSION = "3.0.0";

// Modifying the config version will probably cause a loss of the existig configuration.
// Be careful!
const char *CONFIG_VERSION = "1.0.2";
const char *CONFIG_VERSION = "2.0.0";

const char *WIFI_AP_SSID = "SMLReader";
const char *WIFI_AP_DEFAULT_PASSWORD = "";

static const SensorConfig SENSOR_CONFIGS[] = {
{.pin = D2,
.name = "1",
.numeric_only = false,
.status_led_enabled = true,
.status_led_inverted = true,
.status_led_pin = LED_BUILTIN,
.interval = 0}};

const uint8_t NUM_OF_SENSORS = sizeof(SENSOR_CONFIGS) / sizeof(SensorConfig);

#endif
Loading