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

Commit

Permalink
Update Readme
Browse files Browse the repository at this point in the history
- Fix debug print
- Add Know issues
  • Loading branch information
Bram van Deventer committed Aug 23, 2020
1 parent 6377c69 commit 47cf9b2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 112 deletions.
219 changes: 109 additions & 110 deletions MQTTPublisher.cpp
Original file line number Diff line number Diff line change
@@ -1,110 +1,109 @@
#include "MQTTPublisher.h"
#include "Settings.h"

WiFiClient espClient;
PubSubClient client(espClient);

MQTTPublisher::MQTTPublisher(bool inDebugMode)
{
randomSeed(micros());
debugMode = inDebugMode;
}

MQTTPublisher::~MQTTPublisher()
{
client.publish(MQTT_HOSTNAME, "offline");
client.disconnect();
}

bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();

if (debugMode)
{
Serial.println("MQTT) Attempt connection to server: ");
Serial.print(MQTT_HOST_NAME);
}

// Create a random client ID
String clientId = String(MQTT_HOSTNAME) + "-";
clientId += String(random(0xffff), HEX);

// Attempt to connect
bool clientConnected;
if (String(MQTT_USER_NAME).length())
{
Serial.println("MQTT) Connecting with credientials");
clientConnected = client.connect(clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
Serial.println("MQTT) Connecting without credentials");
clientConnected = client.connect(clientId.c_str());
}

if (clientConnected)
{
if (debugMode) {
Serial.println("MQTT) connected");
}

hasMQTT = true;

// Once connected, publish an announcement...
client.publish(MQTT_HOSTNAME, "online");

return true;
} else {

if (debugMode)
{
Serial.println("MQTT) failed, rc=");
Serial.println(client.state());
}

}

return false;
}


void MQTTPublisher::start()
{
if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
Serial.println("MQTT) disabled. No hostname or port set.");
return; //not configured
}

if (debugMode){
Serial.println("MQTT) enabled. Connecting.");
}

client.setServer(MQTT_HOST_NAME, MQTT_PORT);
reconnect();
isStarted = true;
}

void MQTTPublisher::stop()
{
isStarted = false;
}

void MQTTPublisher::handle()
{
if (!isStarted)
return;

if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT) {
hasMQTT = false;
if (!reconnect()) return;
}
}

bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value)
{
auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str());
yield();
return retVal;
}
#include "MQTTPublisher.h"
#include "Settings.h"

WiFiClient espClient;
PubSubClient client(espClient);

MQTTPublisher::MQTTPublisher(bool inDebugMode)
{
randomSeed(micros());
debugMode = inDebugMode;
}

MQTTPublisher::~MQTTPublisher()
{
client.publish(MQTT_HOSTNAME, "offline");
client.disconnect();
}

bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();

if (debugMode)
{
Serial.println("MQTT) Attempt connection to server: " + String(MQTT_HOST_NAME));
}

// Create a random client ID
String clientId = String(MQTT_HOSTNAME) + "-";
clientId += String(random(0xffff), HEX);

// Attempt to connect
bool clientConnected;
if (String(MQTT_USER_NAME).length())
{
Serial.println("MQTT) Connecting with credientials");
clientConnected = client.connect(clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
Serial.println("MQTT) Connecting without credentials");
clientConnected = client.connect(clientId.c_str());
}

if (clientConnected)
{
if (debugMode) {
Serial.println("MQTT) connected");
}

hasMQTT = true;

// Once connected, publish an announcement...
client.publish(MQTT_HOSTNAME, "online");

return true;
} else {

if (debugMode)
{
Serial.println("MQTT) failed, rc=");
Serial.println(client.state());
}

}

return false;
}


void MQTTPublisher::start()
{
if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
Serial.println("MQTT) disabled. No hostname or port set.");
return; //not configured
}

if (debugMode){
Serial.println("MQTT) enabled. Connecting.");
}

client.setServer(MQTT_HOST_NAME, MQTT_PORT);
reconnect();
isStarted = true;
}

void MQTTPublisher::stop()
{
isStarted = false;
}

void MQTTPublisher::handle()
{
if (!isStarted)
return;

if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT) {
hasMQTT = false;
if (!reconnect()) return;
}
}

bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value)
{
auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str());
yield();
return retVal;
}
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ Copy `Settings.example.h` to `Settings.h` and fill in the correct data.
## Circuit
view [scheme.pdf](scheme.pdf).

Using a level shifter inverter to get the serial output from the meter into the ESP.
The board is powered directly from the meters power supply.
Using a level shifter inverter to get the serial output from the meter into the ESP.<br>
The board is powered directly from the meters power supply.<br>

**Flash the firmware before attaching the circuit,** see "know issue"!

### Parts
| Type | Amount |
Expand All @@ -75,3 +77,10 @@ Connecting to the DSMR witn a RJ11 in Port 1 (P1), found on most smart meters.
| 4 | N.C. | N.C. |
| 5 | Data | 5 (Data)|
| 6 | Power GND | 6 (GND) |


## Known issues
- If the level shifter inverter is connected, it's impossible to flash the firmware.<br>
Pin RX is used, disconnect the pin to flash new firmware.
- Some DSMR cannot deliver enough power to run the Wemos stably.<br>
Connect a 5V usb supply to fix this.

3 comments on commit 47cf9b2

@hbbst
Copy link

@hbbst hbbst commented on 47cf9b2 Mar 24, 2021

Choose a reason for hiding this comment

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

Hi!

I'm very newbie but I would like to build this data sender.
I drew the schema in KiCad.
Could you check it, please if you have a little time?
Thank you!

https://ibb.co/qNwKpL9

@bram2202
Copy link
Owner

@bram2202 bram2202 commented on 47cf9b2 Mar 24, 2021

Choose a reason for hiding this comment

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

Hey @hbbst, your schema looks good 👍🏻.
Just keep in mind that if the RX pin is connected, its not possible to flash any firmware.
You can fix this by using something like a jumper pin.

@hbbst
Copy link

@hbbst hbbst commented on 47cf9b2 Mar 24, 2021

Choose a reason for hiding this comment

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

Great, thanks! :)
Yes, I will flash it before. Jumper is good idea! Thank you!

Please sign in to comment.