Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bram2202 committed Aug 16, 2021
2 parents 0f2babd + 2baf52e commit 17aae4d
Show file tree
Hide file tree
Showing 8 changed files with 536 additions and 96 deletions.
402 changes: 402 additions & 0 deletions AutoConfig.cpp

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions AutoConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <ArduinoJson.h>
#include "MQTTPublisher.h"
#include "Settings.h"
#include "Logger.h"

class AutoConfig
{
private:
Logger logger;
MQTTPublisher _mqttPublisher;
String _identifier;

public:
AutoConfig(MQTTPublisher& mqttPublisher, String identifier);
AutoConfig();

void SendConfig();
};
49 changes: 22 additions & 27 deletions MQTTPublisher.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
#include "MQTTPublisher.h"
#include "Settings.h"

#pragma once

WiFiClient espClient;
PubSubClient client(espClient);

MQTTPublisher::MQTTPublisher(String clientId)
MQTTPublisher::MQTTPublisher(String identifier)
{
_identifier = identifier;
randomSeed(micros());
_clientId = clientId;
logger = Logger("MQTTPublisher");
logger.debug("ClientId:" + _clientId);
}

MQTTPublisher::~MQTTPublisher()
{
client.publish(getTopic("status").c_str(), "offline");
client.disconnect();
}

String MQTTPublisher::getTopic(String name)
{
if (USE_CLIENT_ID)
{
return String(MQTT_PREFIX) + '/' + _clientId + '/' + name;
}
else
{
return String(MQTT_PREFIX) + '/' + name;
}
}
MQTTPublisher::MQTTPublisher() {}

bool MQTTPublisher::reconnect()
{
Expand All @@ -41,12 +26,12 @@ bool MQTTPublisher::reconnect()
if (String(MQTT_USER_NAME).length())
{
logger.info("Connecting with credientials");
clientConnected = client.connect(_clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
clientConnected = client.connect(_identifier.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
logger.info("Connecting without credentials");
clientConnected = client.connect(_clientId.c_str());
clientConnected = client.connect(_identifier.c_str());
}

if (clientConnected)
Expand All @@ -56,8 +41,8 @@ bool MQTTPublisher::reconnect()
hasMQTT = true;

// Once connected, publish an announcement...
client.publish(getTopic("status").c_str(), "online");

client.publish(String(_identifier + "/status").c_str(), String("online").c_str());
return true;
}
else
Expand All @@ -70,6 +55,7 @@ bool MQTTPublisher::reconnect()

void MQTTPublisher::start()
{

if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
logger.warn("disabled. No hostname or port set.");
Expand All @@ -79,6 +65,9 @@ void MQTTPublisher::start()
logger.debug("enabled. Connecting.");

client.setServer(MQTT_HOST_NAME, MQTT_PORT);
client.setKeepAlive(10);
client.setBufferSize(2048);

reconnect();
isStarted = true;
}
Expand All @@ -101,11 +90,17 @@ void MQTTPublisher::handle()
}
}

bool MQTTPublisher::publishOnMQTT(String topicSuffix, String msg)
bool MQTTPublisher::publish(String topic, String msg, bool addIdentifier)
{
String topic = getTopic(topicSuffix);
logger.debug("Publish to '" + topic + "':" + msg);
auto retVal = client.publish(topic.c_str(), msg.c_str());
if (addIdentifier)
topic = _identifier + "/" + topic;
logger.debug("Publish to: " + topic + ": " + msg);

auto retVal = client.publish(topic.c_str(), msg.c_str(), true);

yield();
if (!retVal)
logger.debug("!error : " + String(client.state()));

return retVal;
}
11 changes: 5 additions & 6 deletions MQTTPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ class MQTTPublisher
private:
Logger logger;
bool _debugMode;
String _clientId;
String _identifier;
bool isStarted;

uint32_t lastConnectionAttempt = 0; // last reconnect
uint32_t lastUpdateMqtt; // last data send

bool reconnect();
String getTopic(String name);

public:
MQTTPublisher(String clientId = String(ESP.getChipId(), HEX));
~MQTTPublisher();
MQTTPublisher(String identifier);
MQTTPublisher();

void start();
void stop();

void handle();
bool publishOnMQTT(String topic, String msg);
bool publish(String topic, String msg, bool addIdentifier);
};
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Can be powered directly from the meter itself, no external power supply needed*.
The code should work on DSRM v2.2 and higher, only tested on V4.2.<br/>
Supports Dutch and Belgian smart meters.<br/>

Can be used with Home Assistant (mqtt auto discover)

## Board in action
![esp8266-dsmr](/docs/PCB_IRL.png "esp8266-dsmr")

Expand All @@ -22,8 +24,17 @@ Supports Dutch and Belgian smart meters.<br/>
- [PubSubClient](https://pubsubclient.knolleary.net) - MQTT client
- [Core for ESP8266](https://github.com/esp8266/Arduino) - Arduino core for ESP8266 WiFi chip

## Home Assistant

If MQTT Discovery is enabled in Home Assistant [INFO](https://www.home-assistant.io/docs/mqtt/discovery/), <br/>
the majority of the entities are auto-created and can be found on the integration page.<br/>
Its posible to disale the gas and phase entities in the settings, [See Settings](#Settings).

## Messages

The `MQTT_TOPIC` consists of "ESP_DSMR_" + ESP chip ID.<br/>
For example: `ESP_DSMR_C491F3`

### Info
| Name | unit | DSMR code | MQTT topic |
|:---- |:-------|:------ |:------|
Expand All @@ -45,12 +56,12 @@ Supports Dutch and Belgian smart meters.<br/>
| instant current phase 1 | A | 1-0:31.7.0 |<MQTT_TOPIC>/power/phase_1/current |
| instant current phase 2 | A | 1-0:51.7.0 |<MQTT_TOPIC>/power/phase_2/current |
| instant current phase 3 | A | 1-0:71.7.0 |<MQTT_TOPIC>/power/phase_3/current |
| instant usage phase 1 | kW | 1-0:21.7.0 |<MQTT_TOPIC>/power/phase_1/usage |
| instant usage phase 2 | kW | 1-0:41.7.0 |<MQTT_TOPIC>/power/phase_2/usage |
| instant usage phase 3 | kW | 1-0:61.7.0 |<MQTT_TOPIC>/power/phase_3/usage |
| instant delivery phase 1 | kW | 1-0:22.7.0 |<MQTT_TOPIC>/power/phase_1/delivery |
| instant delivery phase 2 | kW | 1-0:42.7.0 |<MQTT_TOPIC>/power/phase_2/delivery |
| instant delivery phase 3 | kW | 1-0:62.7.0 |<MQTT_TOPIC>/power/phase_3/delivery |
| instant consumption phase 1 | kW | 1-0:21.7.0 |<MQTT_TOPIC>/power/phase_1/consumption |
| instant consumption phase 2 | kW | 1-0:41.7.0 |<MQTT_TOPIC>/power/phase_2/consumption |
| instant consumption phase 3 | kW | 1-0:61.7.0 |<MQTT_TOPIC>/power/phase_3/consumption |
| instant production phase 1 | kW | 1-0:22.7.0 |<MQTT_TOPIC>/power/phase_1/production |
| instant production phase 2 | kW | 1-0:42.7.0 |<MQTT_TOPIC>/power/phase_2/production |
| instant production phase 3 | kW | 1-0:62.7.0 |<MQTT_TOPIC>/power/phase_3/production |
| short drops phase 1 | - | 1-0:32.32.0 | <MQTT_TOPIC>/power/phase_1/drops |
| short drops phase 2 | - | 1-0:52.32.0 | <MQTT_TOPIC>/power/phase_2/drops |
| short drops phase 3 | - | 1-0:72.32.0 | <MQTT_TOPIC>/power/phase_3/drops |
Expand Down Expand Up @@ -81,17 +92,17 @@ The values for the day and night tariffs are different in the Netherlands compar
Copy `Settings.example.h` to `Settings.h` and fill in the correct data.

| Setting | default | Description|
|:------------- |:----- |:-------------:|
|:------------- |:----- |:-------------|
| WIFI_HOSTNAME | ESP-DSMR | device name on network |
| WIFI_SSID | - | Wifi name to connect to |
| WIFI_PASSWORD | - | Wifi password |
| MQTT_HOST_NAME | - | MQTT broker address |
| MQTT_PORT | 1833 | MQTT broker port |
| MQTT_PORT | 1883 | MQTT broker port |
| MQTT_USER_NAME| - | MQTT user name |
| MQTT_PASSWORD | - | MQTT password |
| MQTT_HOSTNAME| ESP-DSMR | MQTT name |
| MQTT_PREFIX | dsmr | MQTT prefix |
| USE_CLIENT_ID | FALSE | Use clientId in prefix |
| AUTOCONFIG_GAS | true | setup gas entity in HA |
| AUTOCONFIG_POWER_EXTENDED | true | setup all power entities in HA |
| LOG_LEVEL | INFO | ( DEBUG / INFO / WARN ) |


Expand Down
6 changes: 0 additions & 6 deletions Settings.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,5 @@
//publish online status name
#define MQTT_HOSTNAME "ESP-DSMR"

//default MQTT prefix
#define MQTT_PREFIX "dsmr"

// Use Client ID in MQTT prefix
#define USE_CLIENT_ID false

//for debugging, print info on serial (DEBUG, INFO, WARN)
#define LOG_LEVEL INFO
1 change: 0 additions & 1 deletion WifiConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ WifiConnector::WifiConnector()
// Set params and try to connect
void WifiConnector::start()
{

logger.info("Start");

// Setup Wifi
Expand Down
Loading

0 comments on commit 17aae4d

Please sign in to comment.