2017-10-25 22:44:56 +02:00

126 lines
5.5 KiB
C++

/*
* Simple sketch to simulate reading data from a Kamstrup
* AMS Meter.
*
* Created 24. October 2017 by Roar Fredriksen
*/
#include <HanReader.h>
#include <Kamstrup.h>
// The HAN Port reader
HanReader hanReader;
byte samples[] =
// [2017-10-20 04.43.32.368 - Received 229 (0xE5) bytes]
{
// List #1
0x7E, 0xA0, 0xE3, 0x2B, 0x21, 0x13, 0x98, 0x86, 0xE6, 0xE7, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00,
0x09, 0x0C, 0x07, 0xE1, 0x0A, 0x14, 0x05, 0x03, 0x2B, 0x1E, 0xFF, 0x80, 0x00, 0x00, 0x02, 0x19,
0x0A, 0x0E, 0x4B, 0x61, 0x6D, 0x73, 0x74, 0x72, 0x75, 0x70, 0x5F, 0x56, 0x30, 0x30, 0x30, 0x31,
0x09, 0x06, 0x01, 0x01, 0x00, 0x00, 0x05, 0xFF, 0x0A, 0x10, 0x35, 0x37, 0x30, 0x36, 0x35, 0x36,
0x37, 0x32, 0x37, 0x34, 0x33, 0x38, 0x39, 0x37, 0x30, 0x32, 0x09, 0x06, 0x01, 0x01, 0x60, 0x01,
0x01, 0xFF, 0x0A, 0x12, 0x36, 0x38, 0x34, 0x31, 0x31, 0x32, 0x31, 0x42, 0x4E, 0x32, 0x34, 0x33,
0x31, 0x30, 0x31, 0x30, 0x34, 0x30, 0x09, 0x06, 0x01, 0x01, 0x01, 0x07, 0x00, 0xFF, 0x06, 0x00,
0x00, 0x05, 0xBC, 0x09, 0x06, 0x01, 0x01, 0x02, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00,
0x09, 0x06, 0x01, 0x01, 0x03, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x09, 0x06, 0x01,
0x01, 0x04, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x01, 0xCE, 0x09, 0x06, 0x01, 0x01, 0x1F, 0x07,
0x00, 0xFF, 0x06, 0x00, 0x00, 0x02, 0x34, 0x09, 0x06, 0x01, 0x01, 0x33, 0x07, 0x00, 0xFF, 0x06,
0x00, 0x00, 0x00, 0xCA, 0x09, 0x06, 0x01, 0x01, 0x47, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x01,
0xFF, 0x09, 0x06, 0x01, 0x01, 0x20, 0x07, 0x00, 0xFF, 0x12, 0x00, 0xE8, 0x09, 0x06, 0x01, 0x01,
0x34, 0x07, 0x00, 0xFF, 0x12, 0x00, 0xE4, 0x09, 0x06, 0x01, 0x01, 0x48, 0x07, 0x00, 0xFF, 0x12,
0x00, 0xE9, 0xA1, 0xA5, 0x7E,
// List #2
0x7E, 0xA1, 0x2D, 0x2B, 0x21, 0x13, 0x47, 0x18, 0xE6, 0xE7, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x09, 0x0C, 0x07, 0xE1, 0x0A, 0x14, 0x05, 0x05,
0x00, 0x05, 0xFF, 0x80, 0x00, 0x00, 0x02, 0x23, 0x0A, 0x0E, 0x4B, 0x61, 0x6D, 0x73, 0x74, 0x72, 0x75, 0x70, 0x5F, 0x56, 0x30, 0x30, 0x30, 0x31,
0x09, 0x06, 0x01, 0x01, 0x00, 0x00, 0x05, 0xFF, 0x0A, 0x10, 0x35, 0x37, 0x30, 0x36, 0x35, 0x36, 0x37, 0x32, 0x37, 0x34, 0x33, 0x38, 0x39, 0x37,
0x30, 0x32, 0x09, 0x06, 0x01, 0x01, 0x60, 0x01, 0x01, 0xFF, 0x0A, 0x12, 0x36, 0x38, 0x34, 0x31, 0x31, 0x32, 0x31, 0x42, 0x4E, 0x32, 0x34, 0x33,
0x31, 0x30, 0x31, 0x30, 0x34, 0x30, 0x09, 0x06, 0x01, 0x01, 0x01, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x0C, 0xF0, 0x09, 0x06, 0x01, 0x01, 0x02,
0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x09, 0x06, 0x01, 0x01, 0x03, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x09, 0x06, 0x01,
0x01, 0x04, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x01, 0xB9, 0x09, 0x06, 0x01, 0x01, 0x1F, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x05, 0x33, 0x09,
0x06, 0x01, 0x01, 0x33, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0xD5, 0x09, 0x06, 0x01, 0x01, 0x47, 0x07, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x05,
0x25, 0x09, 0x06, 0x01, 0x01, 0x20, 0x07, 0x00, 0xFF, 0x12, 0x00, 0xE6, 0x09, 0x06, 0x01, 0x01, 0x34, 0x07, 0x00, 0xFF, 0x12, 0x00, 0xE2, 0x09,
0x06, 0x01, 0x01, 0x48, 0x07, 0x00, 0xFF, 0x12, 0x00, 0xE8, 0x09, 0x06, 0x00, 0x01, 0x01, 0x00, 0x00, 0xFF, 0x09, 0x0C, 0x07, 0xE1, 0x0A, 0x14,
0x05, 0x05, 0x00, 0x05, 0xFF, 0x80, 0x00, 0x00, 0x09, 0x06, 0x01, 0x01, 0x01, 0x08, 0x00, 0xFF, 0x06, 0x00, 0x06, 0x85, 0xB7, 0x09, 0x06, 0x01,
0x01, 0x02, 0x08, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x00, 0x09, 0x06, 0x01, 0x01, 0x03, 0x08, 0x00, 0xFF, 0x06, 0x00, 0x00, 0x00, 0x50, 0x09,
0x06, 0x01, 0x01, 0x04, 0x08, 0x00, 0xFF, 0x06, 0x00, 0x00, 0xF1, 0x97, 0x5C, 0x83, 0x7E
};
int sampleIndex = 0;
void setup() {
setupDebugPort();
// initialize the HanReader
// (passing no han port, as we are feeding data manually, but provide Serial for debugging)
hanReader.setup(NULL, &Serial);
}
void setupDebugPort()
{
// Initialize the Serial port for debugging
Serial.begin(115200);
while (!Serial) {}
Serial.setDebugOutput(true);
Serial.println("Serial1");
Serial.println("Serial debugging port initialized");
}
void loop() {
// Read one byte from the "port", and see if we got a full package
if (hanReader.read(samples[sampleIndex++]))
{
// Get the list identifier
int list = hanReader.getList();
Serial.println("");
Serial.print("List #");
Serial.print((byte)list, HEX);
Serial.print(": ");
// Only care for the ACtive Power Imported, which is found in the first list
if (list == (int)Kamstrup::List1)
{
String id = hanReader.getString((int)Kamstrup_List1::ListVersionIdentifier);
Serial.println(id);
time_t time = hanReader.getPackageTime();
Serial.print("Time of the package is: ");
Serial.println(time);
int power = hanReader.getInt((int)Kamstrup_List1::ActiveImportPower);
Serial.print("Power consumtion is right now: ");
Serial.print(power);
Serial.println(" W");
float current[3];
current[0] = (float)hanReader.getInt((int)Kamstrup_List1::CurrentL1) / 100.0;
current[1] = (float)hanReader.getInt((int)Kamstrup_List1::CurrentL2) / 100.0;
current[2] = (float)hanReader.getInt((int)Kamstrup_List1::CurrentL3) / 100.0;
int voltage[3];
voltage[0] = hanReader.getInt((int)Kamstrup_List1::VoltageL1);
voltage[1] = hanReader.getInt((int)Kamstrup_List1::VoltageL2);
voltage[2] = hanReader.getInt((int)Kamstrup_List1::VoltageL3);
for (int i = 0; i < 3; i++)
{
Serial.print("L");
Serial.print(i + 1);
Serial.print(" is ");
Serial.print(voltage[i]);
Serial.print(" V (");
Serial.print(current[i]);
Serial.println(" A)");
}
}
}
delay(10);
if (sampleIndex >= sizeof(samples))
{
delay(2000);
sampleIndex = 0;
}
}