-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
#include <PubSubClient.h> | ||
#include "eth.h" | ||
|
||
EthernetClient ethClient; | ||
PubSubClient mqttClient(ethClient); | ||
uint32_t mqttLastReconnect = 0; | ||
|
||
bool mqttPublish(const char* topic, const uint8_t* payload, size_t plength, bool retained = false) { | ||
DBG.printf("MQTT publishing %s [%d]: ", topic, plength); | ||
DBG.write(payload, plength); | ||
DBG.printf("\n"); | ||
if (mqttClient.connected()) { | ||
bool success = mqttClient.publish(topic, payload, plength, retained); | ||
if (!success) { | ||
DBG.println("MQTT publish error"); | ||
} | ||
return success; | ||
} | ||
DBG.println("MQTT publish error: Not connected"); | ||
return false; | ||
} | ||
|
||
bool mqttPublish(const char* topic, const uint8_t* payload, bool retained = false) { | ||
if (mqttClient.connected()) { | ||
return mqttClient.publish(topic, payload, retained); | ||
} | ||
return false; | ||
} | ||
|
||
void mqttCallback(char* topic, byte* payload, unsigned int length) { | ||
// handle message arrived | ||
DBG.printf("MQTT /%s: ", topic); | ||
DBG.write(payload, length); | ||
DBG.println(); | ||
} | ||
|
||
bool mqttReconnect() { | ||
DBG.print("MQTT connecting... "); | ||
if (mqttClient.connect(HOSTNAME, MQTT_USER, MQTT_PASSWORD)) { | ||
// mqttClient.publish("test", "hello world"); | ||
// mqttClient.subscribe("test"); | ||
DBG.println("success!"); | ||
} | ||
else { | ||
DBG.println("failed."); | ||
} | ||
return mqttClient.connected(); | ||
} | ||
|
||
void mqttLoop() { | ||
#if USE_MQTT | ||
if (!mqttClient.connected()) { | ||
if (millis() - mqttLastReconnect > 5000) { | ||
mqttLastReconnect = millis(); | ||
mqttReconnect(); | ||
} | ||
} | ||
mqttClient.loop(); | ||
#endif | ||
} | ||
|
||
void mqttInit() { | ||
DBG.println("MQTT init"); | ||
mqttClient.setServer(MQTT_HOST, MQTT_PORT); | ||
mqttClient.setCallback(mqttCallback); | ||
mqttClient.setBufferSize(4096); | ||
mqttReconnect(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
// rename to secrets.h | ||
|
||
#define HOSTNAME "Janitza-Modbus2MQTT" | ||
|
||
#define INFLUX_HOST "hostnameofinflux" | ||
#define INFLUX_PORT 1883 | ||
#define INFLUX_DB "databaseName" | ||
#define INFLUX_MEASUREMENT "measurementName" | ||
//#define INFLUX_USER "username" // actually not used, use base64 below | ||
//#define INFLUX_PASS "password" | ||
#define INFLUX_AUTH_BASE64 "dXNlcjpwYXNz" // format: user:pass base64 encoded | ||
#define INFLUX_AUTH_BASE64 "dXNlcjpwYXNz" // format: user:pass base64 encoded | ||
|
||
#define MQTT_HOST "mqttServer" | ||
#define MQTT_PORT 1883 | ||
#define MQTT_USER "mqttUser" // set to NULL when unused (without "") | ||
#define MQTT_PASSWORD "mqttPasswd" // set to NULL when unused (without "") | ||
#define MQTT_BASE_TOPIC "power/janitza/" // has to end with slash |