[TTN] Include all 6 values for P102 PZEM004Tv3

Those values are already collected from the sensor, so why not send them all in a single TTN message.
This commit is contained in:
TD-er
2021-04-29 13:26:02 +02:00
parent d67f847736
commit d62a1a6849
4 changed files with 34 additions and 86 deletions
-28
View File
@@ -658,16 +658,6 @@ function Converter(decoded, port) {
case 102:
converted.name = "PZEM004T v30";
// This plugin can output any value, so show string representation
// of the unit of measure
converted.unit1 = getPZEM004TUnit(converted.unit1);
converted.unit2 = getPZEM004TUnit(converted.unit2);
converted.unit3 = getPZEM004TUnit(converted.unit3);
converted.unit4 = getPZEM004TUnit(converted.unit4);
converted.v1 = converted.val_1;
converted.v2 = converted.val_2;
converted.v3 = converted.val_3;
converted.v4 = converted.val_4;
break;
case 106:
@@ -764,24 +754,6 @@ function getAcuDC243Unit(unit_id) {
return "unknown" + unit_id;
};
function getPZEM004TUnit(unit_id) {
switch (unit_id) {
case 0: // Voltage 0
return "V";
case 1: // Current 1
return "A";
case 2: // Power (Watt) 2
return "W";
case 3: // Energy (Wh) 3
return "Wh";
case 4: // Power Factor 4
return "cosphi";
case 5: // Frequency(Hz) 5
return "Hz";
}
return "unknown" + unit_id;
};
function getDDS238_xUnit(unit_id) {
switch (unit_id) {
case 0: // P108_QUERY_V 0
+5 -6
View File
@@ -34,23 +34,22 @@ function Decoder(bytes, port) {
case 85:
// AcuDC243
// FIXME TD-er: Same code as in P102, Make new type for this with 4 selectable variables
// FIXME TD-er: Same code as in P108, Make new type for this with 4 selectable variables
return decode(bytes, [header, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4],
['header', 'unit1', 'val_1', 'unit2', 'val_2', 'unit3', 'val_3', 'unit4', 'val_4']);
case 102:
// PZEM004T v30
// FIXME TD-er: Same code as in P085, Make new type for this with 4 selectable variables
return decode_plugin(input.fPort, input.bytes, [header, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4],
['header', 'unit1', 'val_1', 'unit2', 'val_2', 'unit3', 'val_3', 'unit4', 'val_4']);
break;
data = decode(bytes, [header, int16_1e1, int32_1e3, int32_1e1, int32_1e1, uint16_1e2, uint8_1e1],
['header', 'voltage', 'current', 'power', 'energy', 'powerfactor', 'frequency']);
data.frequency += 40;
return data;
case 108:
// DDS238-x ZN
// FIXME TD-er: Same code as in P085, Make new type for this with 4 selectable variables
return decode_plugin(input.fPort, input.bytes, [header, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4],
['header', 'unit1', 'val_1', 'unit2', 'val_2', 'unit3', 'val_3', 'unit4', 'val_4']);
break;
}
+7 -35
View File
@@ -1,5 +1,5 @@
// Decoder for device payload encoder "PACKED"
// copy&paste to TTN Console V3 -> Applications -> Payload formatters -> Uplink -> Javascript
// copy & paste to TTN Console V3 -> Applications -> Payload formatters -> Uplink -> Javascript
function decodeUplink(input) {
var data = {};
@@ -37,7 +37,7 @@ function decodeUplink(input) {
case 85:
// AcuDC243
// FIXME TD-er: Same code as in P102, Make new type for this with 4 selectable variables
// FIXME TD-er: Same code as in P108, Make new type for this with 4 selectable variables
data = decode_plugin(input.fPort, input.bytes, [header, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4],
['header', 'unit1', 'val_1', 'unit2', 'val_2', 'unit3', 'val_3', 'unit4', 'val_4']);
decoded = true;
@@ -45,9 +45,9 @@ function decodeUplink(input) {
case 102:
// PZEM004T v30
// FIXME TD-er: Same code as in P085, Make new type for this with 4 selectable variables
data = decode_plugin(input.fPort, input.bytes, [header, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4],
['header', 'unit1', 'val_1', 'unit2', 'val_2', 'unit3', 'val_3', 'unit4', 'val_4']);
data = decode(bytes, [header, int16_1e1, int32_1e3, int32_1e1, int32_1e1, uint16_1e2, uint8_1e1],
['header', 'voltage', 'current', 'power', 'energy', 'powerfactor', 'frequency']);
data.frequency += 40;
decoded = true;
break;
@@ -85,8 +85,8 @@ function decodeUplink(input) {
}
}
data.bytes = input.bytes; // comment out if you do not want to include the original payload
data.port = input.fPort; // comment out if you do not want to inlude the port
data.bytes = input.bytes;
data.port = input.fPort;
return {
data: data,
@@ -1138,16 +1138,6 @@ function Converter(decoded, fPort) {
case 102:
converted.name = "PZEM004T v30";
// This plugin can output any value, so show string representation
// of the unit of measure
converted.unit1 = getPZEM004TUnit(converted.unit1);
converted.unit2 = getPZEM004TUnit(converted.unit2);
converted.unit3 = getPZEM004TUnit(converted.unit3);
converted.unit4 = getPZEM004TUnit(converted.unit4);
converted.v1 = converted.val_1;
converted.v2 = converted.val_2;
converted.v3 = converted.val_3;
converted.v4 = converted.val_4;
break;
case 106:
@@ -1241,24 +1231,6 @@ function getAcuDC243Unit(unit_id) {
};
function getPZEM004TUnit(unit_id) {
switch (unit_id) {
case 0: // Voltage 0
return "V";
case 1: // Current 1
return "A";
case 2: // Power (Watt) 2
return "W";
case 3: // Energy (Wh) 3
return "Wh";
case 4: // Power Factor 4
return "cosphi";
case 5: // Frequency(Hz) 5
return "Hz";
}
return "unknown" + unit_id;
};
function getDDS238_xUnit(unit_id) {
switch (unit_id) {
case 0: // P108_QUERY_V 0
+22 -17
View File
@@ -298,24 +298,29 @@ boolean Plugin_102(byte function, struct EventStruct *event, String& string)
#ifdef USES_PACKED_RAW_DATA
case PLUGIN_GET_PACKED_RAW_DATA:
{
// FIXME TD-er: Same code as in P085, share in LoRa code.
P102_data_struct *P102_data =
static_cast<P102_data_struct *>(getPluginTaskData(event->TaskIndex));
// Matching JS code:
// return decode(bytes, [header, int16_1e1, int32_1e3, int32_1e1, int32_1e1, uint16_1e2, uint8_1e1],
// ['header', 'voltage', 'current', 'power', 'energy', 'powerfactor', 'frequency']);
//
// Resolutions:
// Voltage: 0.1V => int16_1e1 (range 80-260V)
// Current: 0.001A => int32_1e3
// Power: 0.1W => int32_1e1
// Energy: 1Wh => int32_1e1
// PowerFactor: 0.01 => uint16_1e2
// Frequency: 0.1Hz => uint8_1e1 (range 45Hz - 65Hz), offset 40Hz
if ((nullptr != P102_data) && P102_data->isInitialized()) {
// Matching JS code:
// return decode(bytes, [header, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4, uint8, int32_1e4],
// ['header', 'unit1', 'val_1', 'unit2', 'val_2', 'unit3', 'val_3', 'unit4', 'val_4']);
for (byte i = 0; i < VARS_PER_TASK; ++i) {
const byte pconfigIndex = i + P102_QUERY1_CONFIG_POS;
const byte choice = PCONFIG(pconfigIndex);
string += LoRa_addInt(choice, PackedData_uint8);
string += LoRa_addFloat(UserVar[event->BaseVarIndex + i], PackedData_int32_1e4);
}
event->Par1 = 8; // valuecount
success = true;
}
// FIXME TD-er: Calling these functions is probably done within the 200 msec timeout used in the library.
// If not, this should be cached in a task data struct.
string += LoRa_addFloat(P102_PZEM_sensor->voltage(), PackedData_int16_1e1);
string += LoRa_addFloat(P102_PZEM_sensor->current(), PackedData_int32_1e3);
string += LoRa_addFloat(P102_PZEM_sensor->power(), PackedData_int32_1e1);
string += LoRa_addFloat(P102_PZEM_sensor->energy(), PackedData_int32_1e1);
string += LoRa_addFloat(P102_PZEM_sensor->pf(), PackedData_uint16_1e2);
string += LoRa_addFloat(P102_PZEM_sensor->frequency() - 40, PackedData_uint8_1e1);
event->Par1 = 6; // valuecount
success = true;
break;
}
#endif // USES_PACKED_RAW_DATA