Last active
November 15, 2016 16:36
-
-
Save kenci/0830e0fa9323f7a5b3036e3652c03f77 to your computer and use it in GitHub Desktop.
Emon pulse counter
This file contains hidden or 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
//--------------------------------------------------------------------- | |
// Pulse String Decoder Sketch with Ethernet forwarding | |
// GNU GPL openenergymonitor.org | |
// Author: Trystan Lea | |
// Modified: kenci | |
// Added DSB1820 Sensors | |
//--------------------------------------------------------------------- | |
#include <Ethernet.h> | |
#include <SPI.h> | |
#include <PubSubClient.h> | |
#define DEBUG | |
#define MQTT | |
#define MQTT_USER "" | |
#define MQTT_PASS "" | |
EthernetClient ethClient; | |
PubSubClient client(ethClient); | |
long lastReconnectAttempt = 0; | |
byte mac[] = {0x54,0x55,0x53,0x10,0x04,0x28}; | |
IPAddress ip(192, 168, 1, 205); | |
IPAddress server(192, 168, 1, 101); | |
char message_buff[100]; | |
unsigned long utime; | |
//Float to string functions, see attached: ftoa | |
//thanks to Don Kinzer arduino forums | |
void fmtDouble(double val, byte precision, char *buf, unsigned bufLen = 0xffff); | |
unsigned fmtUnsigned(unsigned long val, char *buf, unsigned bufLen = 0xffff, byte width = 0); | |
bool publishMessage(const char* topic, const double value) { | |
char buf[20]; | |
fmtDouble(value, 2, buf); | |
client.publish(topic, buf); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("emonClient", MQTT_USER, MQTT_PASS)) { | |
Serial.println(" connected"); | |
// Once connected, publish an announcement... | |
client.publish("kenocontrol/homematic/emon/decoder/version", "1"); | |
// ... and resubscribe | |
//client.subscribe("kenocontrol/homematic/shutter/+/move"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); //Needs to be 115200 for fast transfer between pulse counting arduino and this arduino | |
#ifdef MQTT | |
client.setServer(server, 1883); | |
#endif | |
Ethernet.begin(mac, ip); | |
delay(1500); | |
// give the Ethernet shield a second to initialize: | |
delay(1000); | |
#ifdef DEBUG | |
Serial.println(F("connecting...")); | |
#endif | |
} | |
void loop() | |
{ | |
decode_pulse_string(); //Method decodes string send from pulse counting arduino | |
#ifdef MQTT | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
#endif | |
if (millis()-utime>10000) | |
{ | |
//Serial.println("Alle 10 Sekunden"); | |
utime = millis(); | |
// Hausverbrauch | |
#ifdef DEBUG | |
Serial.print("P0_power: ");Serial.println(getPower(0, 0.0005)); | |
Serial.print("P1_power: ");Serial.println(getPower(1, 0.001)); | |
Serial.print("P2_power: ");Serial.println(getPower(2, 0.0005)); | |
Serial.print("P3_power: ");Serial.println(getPower(3, 0.0005)); | |
Serial.print("P4_power: ");Serial.println(getPower(4, 0.0005)); | |
Serial.print("P5_power: ");Serial.println(getPower(5, 0.0005)); | |
Serial.print("P6_power: ");Serial.println(getPower(6, 0.001)); | |
#endif | |
#ifdef MQTT | |
publishMessage("emon/100/P0_power", getPower(0, 0.0005)); | |
publishMessage("emon/100/P1_power", getPower(1, 0.001)); | |
publishMessage("emon/100/P2_power", getPower(2, 0.0005)); | |
publishMessage("emon/100/P3_power", getPower(3, 0.0005)); | |
publishMessage("emon/100/P4_power", getPower(4, 0.0005)); | |
publishMessage("emon/100/P5_power", getPower(5, 0.0005)); | |
publishMessage("emon/100/P6_power", getPower(6, 0.001)); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment