Add Eastron SDM120 energy meter

5.13.1a
* Add Eastron SDM120 energy meter (#2694)
This commit is contained in:
Theo Arends 2018-05-11 14:44:00 +02:00
parent c51bcd77d1
commit f7ecc99209
22 changed files with 372 additions and 20 deletions

View File

@ -9,8 +9,9 @@
* Add Portuguese in Brazil language file
* Add rule state test for On/Off in addition to 0/1 (#2613)
* Add hardware serial option to MHZ-19 sensor (#2659)
* Add Eastron SDM120 energy meter (#2694)
* Updated Italian language file (#2618)
* Update TasmotaSerial to 2.0.0 allowing Hardware Serial Fallback when correct connection are configured
* Update TasmotaSerial to 2.0.0 allowing Hardware Serial Fallback when correct connections are configured
* Optimize command handling
*
* 5.13.1 20180501

View File

@ -60,6 +60,7 @@
#define D_JSON_FLASHMODE "FlashMode"
#define D_JSON_FLASHSIZE "FlashSize"
#define D_JSON_FREEMEMORY "Free"
#define D_JSON_FREQUENCY "Frequency"
#define D_JSON_FROM "from"
#define D_JSON_GAS "Gas"
#define D_JSON_GATEWAY "Gateway"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Невярно"
#define D_FILE "Файл"
#define D_FREE_MEMORY "Свободна памет"
#define D_FREQUENCY "Frequency"
#define D_GAS "Газ"
#define D_GATEWAY "Шлюз"
#define D_GROUP "Група"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "А"
#define D_UNIT_CENTIMETER "см"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "ч"
#define D_UNIT_KILOOHM "кОм"
#define D_UNIT_KILOWATTHOUR "кВт/ч"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Nepravda"
#define D_FILE "Soubor"
#define D_FREE_MEMORY "Volná paměť"
#define D_FREQUENCY "Frequency"
#define D_GAS "Plyn"
#define D_GATEWAY "Výchozí brána"
#define D_GROUP "Skupina"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "hod"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "falsch"
#define D_FILE "Datei"
#define D_FREE_MEMORY "Freier Arbeitsspeicher"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gas"
#define D_GATEWAY "Gateway"
#define D_GROUP "Gruppe"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "h"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Λάθος"
#define D_FILE "Αρχείο"
#define D_FREE_MEMORY "Ελεύθερη Μνήμη"
#define D_FREQUENCY "Frequency"
#define D_GAS "Γκάζι"
#define D_GATEWAY "Πύλη"
#define D_GROUP "Ομάδα"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "Hr"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "False"
#define D_FILE "File"
#define D_FREE_MEMORY "Free Memory"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gas"
#define D_GATEWAY "Gateway"
#define D_GROUP "Group"
@ -453,6 +454,8 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
@ -476,6 +479,7 @@
#define D_UNIT_VOLT "V"
#define D_UNIT_WATT "W"
#define D_UNIT_WATTHOUR "Wh"
#define D_UNIT_HERTZ "Hz"
// Log message prefix
#define D_LOG_APPLICATION "APP: " // Application

View File

@ -91,6 +91,7 @@
#define D_FALSE "Falso"
#define D_FILE "Archivo"
#define D_FREE_MEMORY "Memoria Libre"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gas"
#define D_GATEWAY "Gateway"
#define D_GROUP "Grupo"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "Hr"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Faux"
#define D_FILE "Fichier"
#define D_FREE_MEMORY "Mémoire libre"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gaz"
#define D_GATEWAY "Passerelle"
#define D_GROUP "Groupe"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "h"
#define D_UNIT_KILOOHM "kΩ"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Hamis"
#define D_FILE "File"
#define D_FREE_MEMORY "Szabad Memória"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gáz"
#define D_GATEWAY "Gateway"
#define D_GROUP "Csoport"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "ó"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Falso"
#define D_FILE "File"
#define D_FREE_MEMORY "Memoria Libera"
#define D_FREQUENCY "Frequenza"
#define D_GAS "Gas"
#define D_GATEWAY "Gateway"
#define D_GROUP "Gruppo"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "Hr"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -28,7 +28,7 @@
* Use online command StateText to translate ON, OFF, HOLD and TOGGLE.
* Use online command Prefix to translate cmnd, stat and tele.
*
* Updated until v5.12.0l
* Updated until v5.13.1a
\*********************************************************************/
//#define LANGUAGE_MODULE_NAME // Enable to display "Module Generic" (ie Spanish), Disable to display "Generic Module" (ie English)
@ -91,6 +91,7 @@
#define D_FALSE "Onwaar"
#define D_FILE "Bestand"
#define D_FREE_MEMORY "Vrij geheugen"
#define D_FREQUENCY "Frequentie"
#define D_GAS "Gas"
#define D_GATEWAY "Gateway"
#define D_GROUP "Groep"
@ -376,24 +377,24 @@
#define D_TIMER_ACTION "Actie"
// xdrv_10_knx.ino
#define D_CONFIGURE_KNX "Configure KNX"
#define D_CONFIGURE_KNX "Configureer KNX"
#define D_KNX_PARAMETERS "KNX Parameters"
#define D_KNX_GENERAL_CONFIG "General"
#define D_KNX_PHYSICAL_ADDRESS "Physical Address"
#define D_KNX_PHYSICAL_ADDRESS_NOTE "( Must be unique on the KNX network )"
#define D_KNX_ENABLE "Enable KNX"
#define D_KNX_GROUP_ADDRESS_TO_WRITE "Data to Send to Group Addresses"
#define D_ADD "Add"
#define D_DELETE "Delete"
#define D_REPLY "Reply"
#define D_KNX_GROUP_ADDRESS_TO_READ "Group Addresses to Receive Data from"
#define D_KNX_GENERAL_CONFIG "Algemeen"
#define D_KNX_PHYSICAL_ADDRESS "Eigen adres"
#define D_KNX_PHYSICAL_ADDRESS_NOTE "(Moet uniek zijn op het KNX netwerk)"
#define D_KNX_ENABLE "KNX inschakelen"
#define D_KNX_GROUP_ADDRESS_TO_WRITE "Stuur gegevens naar groep adressen"
#define D_ADD "Toevoegen"
#define D_DELETE "Verwijder"
#define D_REPLY "Antwoord"
#define D_KNX_GROUP_ADDRESS_TO_READ "Ontvang gegevens van groep adressen"
#define D_LOG_KNX "KNX: "
#define D_RECEIVED_FROM "Received from"
#define D_KNX_COMMAND_WRITE "Write"
#define D_KNX_COMMAND_READ "Read"
#define D_KNX_COMMAND_OTHER "Other"
#define D_SENT_TO "sent to"
#define D_KNX_WARNING "The group address ( 0 / 0 / 0 ) is reserved and can not be used."
#define D_RECEIVED_FROM "Ontvangen van"
#define D_KNX_COMMAND_WRITE "Schrijven"
#define D_KNX_COMMAND_READ "Lezen"
#define D_KNX_COMMAND_OTHER "Overige"
#define D_SENT_TO "verzend naar"
#define D_KNX_WARNING "Groep adres (0/0/0) is gereserveerd en mag niet worden gebruikt."
// xdrv_03_energy.ino
#define D_ENERGY_TODAY "Verbruik vandaag"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "h"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Fałsz"
#define D_FILE "Plik"
#define D_FREE_MEMORY "Wolna pamięć"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gas"
#define D_GATEWAY "Brama"
#define D_GROUP "Grupa"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "Godz"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Falso"
#define D_FILE "Arquivo"
#define D_FREE_MEMORY "Memória Livre"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gás"
#define D_GATEWAY "Gateway"
#define D_GROUP "Grupo"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "H"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Falso"
#define D_FILE "Ficheiro"
#define D_FREE_MEMORY "Memoria Livre"
#define D_FREQUENCY "Frequency"
#define D_GAS "Gás"
#define D_GATEWAY "Gateway"
#define D_GROUP "Grupo"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "A"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "Hr"
#define D_UNIT_KILOOHM "kOhm"
#define D_UNIT_KILOWATTHOUR "kWh"

View File

@ -91,6 +91,7 @@
#define D_FALSE "Ложно"
#define D_FILE "Файл"
#define D_FREE_MEMORY "Свободная память"
#define D_FREQUENCY "Frequency"
#define D_GAS "Газ"
#define D_GATEWAY "Шлюз"
#define D_GROUP "Группа"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "А"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "Ч"
#define D_UNIT_KILOOHM "кОм"
#define D_UNIT_KILOWATTHOUR "кВт"

View File

@ -91,6 +91,7 @@
#define D_FALSE "False"
#define D_FILE "文件:"
#define D_FREE_MEMORY "空闲内存"
#define D_FREQUENCY "Frequency"
#define D_GAS "气体"
#define D_GATEWAY "网关"
#define D_GROUP "组:"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "安"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "时"
#define D_UNIT_KILOOHM "千欧"
#define D_UNIT_KILOWATTHOUR "千瓦时"

View File

@ -91,6 +91,7 @@
#define D_FALSE "False"
#define D_FILE "文件:"
#define D_FREE_MEMORY "可用記憶體"
#define D_FREQUENCY "Frequency"
#define D_GAS "氣體"
#define D_GATEWAY "網關"
#define D_GROUP "組:"
@ -453,10 +454,13 @@
#define D_SENSOR_SBR_TX "SerBr Tx"
#define D_SENSOR_SR04_TRIG "SR04 Tri"
#define D_SENSOR_SR04_ECHO "SR04 Ech"
#define D_SENSOR_SDM120_TX "SDM120 Tx"
#define D_SENSOR_SDM120_RX "SDM120 Rx"
// Units
#define D_UNIT_AMPERE "安"
#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_HERTZ "Hz"
#define D_UNIT_HOUR "時"
#define D_UNIT_KILOOHM "千歐"
#define D_UNIT_KILOWATTHOUR "千瓦時"

View File

@ -93,6 +93,8 @@ enum UserSelectablePins {
GPIO_SBR_RX, // Serial Bridge Serial interface
GPIO_SR04_TRIG, // SR04 Trigger pin
GPIO_SR04_ECHO, // SR04 Echo pin
GPIO_SDM120_TX, // SDM120 Serial interface
GPIO_SDM120_RX, // SDM120 Serial interface
GPIO_SENSOR_END };
// Programmer selectable GPIO functionality offset by user selectable GPIOs
@ -136,7 +138,8 @@ const char kSensorNames[] PROGMEM =
D_SENSOR_SPI_CS "|" D_SENSOR_SPI_DC "|" D_SENSOR_BACKLIGHT "|"
D_SENSOR_PMS5003 "|" D_SENSOR_SDS0X1 "|"
D_SENSOR_SBR_TX "|" D_SENSOR_SBR_RX "|"
D_SENSOR_SR04_TRIG "|" D_SENSOR_SR04_ECHO;
D_SENSOR_SR04_TRIG "|" D_SENSOR_SR04_ECHO "|"
D_SENSOR_SDM120_TX "|" D_SENSOR_SDM120_RX;
/********************************************************************************************/

View File

@ -280,6 +280,7 @@
#define USE_NOVA_SDS // Add support for SDS011 and SDS021 particle concentration sensor (+0k7 code)
#define USE_PZEM004T // Add support for PZEM004T Energy monitor (+2k code)
#define USE_SERIAL_BRIDGE // Add support for software Serial Bridge (+0k8 code)
#define USE_SDM120 // Add support for Eastron SDM120-Modbus energy meter (+1k2 code)
// -- Low level interface devices -----------------
#define USE_IR_REMOTE // Send IR remote commands using library IRremoteESP8266 and ArduinoJson (+4k code, 0k3 mem, 48 iram)

View File

@ -740,7 +740,7 @@ void HandleModuleConfiguration()
for (byte i = 0; i < MAX_GPIO_PIN; i++) {
if (GPIO_USER == cmodule.gp.io[i]) {
snprintf_P(stemp, 3, PINS_WEMOS +i*2);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("<tr><td style='width:190px'>%s <b>" D_GPIO "%d</b> %s</td><td style='width:126px'><select id='g%d' name='g%d'></select></td></tr>"),
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("<tr><td style='width:190px'>%s <b>" D_GPIO "%d</b> %s</td><td style='width:146px'><select id='g%d' name='g%d'></select></td></tr>"),
(WEMOS==Settings.module)?stemp:"", i, (0==i)? D_SENSOR_BUTTON "1":(1==i)? D_SERIAL_OUT :(3==i)? D_SERIAL_IN :(12==i)? D_SENSOR_RELAY "1":(13==i)? D_SENSOR_LED "1i":(14==i)? D_SENSOR :"", i, i);
page += mqtt_data;
}

282
sonoff/xsns_23_sdm120.ino Normal file
View File

@ -0,0 +1,282 @@
/*
xsns_23_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota
Copyright (C) 2018 Gennaro Tortone
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_SDM120
/*********************************************************************************************\
* Eastron SDM120-Modbus energy meter
*
* Based on: https://github.com/reaper7/SDM_Energy_Meter
\*********************************************************************************************/
#include <TasmotaSerial.h>
TasmotaSerial *SDM120Serial;
uint8_t sdm120_type = 1;
uint8_t sdm120_state = 0;
float sdm120_voltage = 0;
float sdm120_current = 0;
float sdm120_power = 0;
float sdm120_power_factor = 0;
float sdm120_frequency = 0;
float sdm120_energy_total = 0;
bool SDM_ModbusReceiveReady()
{
return (SDM120Serial->available() > 1);
}
void SDM_ModbusSend(uint8_t function_code, uint16_t start_address, uint16_t register_count)
{
uint8_t frame[8];
frame[0] = 0x01; // default SDM120 Modbus address
frame[1] = function_code;
frame[2] = (uint8_t)(start_address >> 8);
frame[3] = (uint8_t)(start_address);
frame[4] = (uint8_t)(register_count >> 8);
frame[5] = (uint8_t)(register_count);
uint16_t crc = SDM_calculateCRC(frame, 6); // calculate out crc only from first 6 bytes
frame[6] = lowByte(crc);
frame[7] = highByte(crc);
while (SDM120Serial->available() > 0) { // read serial if any old data is available
SDM120Serial->read();
}
SDM120Serial->flush();
SDM120Serial->write(frame, sizeof(frame));
}
uint8_t SDM_ModbusReceive(float *value)
{
uint8_t buffer[9];
*value = NAN;
uint8_t len = 0;
while (SDM120Serial->available() > 0) {
buffer[len++] = (uint8_t)SDM120Serial->read();
}
if (len < 9)
return 3; // SDM_ERR_NOT_ENOUGHT_BYTES
if (len == 9) {
if (buffer[0] == 0x01 && buffer[1] == 0x04 && buffer[2] == 4) { // check node number, op code and reply bytes count
if((SDM_calculateCRC(buffer, 7)) == ((buffer[8] << 8) | buffer[7])) { //calculate crc from first 7 bytes and compare with received crc (bytes 7 & 8)
((uint8_t*)value)[3] = buffer[3];
((uint8_t*)value)[2] = buffer[4];
((uint8_t*)value)[1] = buffer[5];
((uint8_t*)value)[0] = buffer[6];
} else return 1; // SDM_ERR_CRC_ERROR
} else return 2; // SDM_ERR_WRONG_BYTES
}
return 0; // SDM_ERR_NO_ERROR
}
uint16_t SDM_calculateCRC(uint8_t *frame, uint8_t num)
{
uint16_t crc, flag;
crc = 0xFFFF;
for (uint8_t i = 0; i < num; i++) {
crc ^= frame[i];
for (uint8_t j = 8; j; j--) {
if ((crc & 0x0001) != 0) { // If the LSB is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
} else { // Else LSB is not set
crc >>= 1; // Just shift right
}
}
}
return crc;
}
/*********************************************************************************************/
const uint16_t sdm_start_addresses[] {
0x0000, // SDM120C_VOLTAGE [V]
0x0006, // SDM120C_CURRENT [A]
0x000C, // SDM120C_POWER [W]
0x0012, // SDM120C_ACTIVE_APPARENT_POWER [VA]
0x0018, // SDM120C_REACTIVE_APPARENT_POWER [VAR]
0x001E, // SDM120C_POWER_FACTOR
0x0046, // SDM120C_FREQUENCY [Hz]
0x0048, // SDM120C_IMPORT_ACTIVE_ENERGY [Wh]
0x004A, // SDM120C_EXPORT_ACTIVE_ENERGY [Wh]
0x0156 // SDM120C_TOTAL_ACTIVE_ENERGY [Wh]
};
uint8_t sdm120_read_state = 0;
uint8_t sdm120_send_retry = 0;
void SDM12050ms() // Every 50 mSec
{
sdm120_state++;
if (6 == sdm120_state) { // Every 300 mSec
sdm120_state = 0;
float value = 0;
bool data_ready = SDM_ModbusReceiveReady();
if (data_ready) {
uint8_t error = SDM_ModbusReceive(&value);
if (error) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DEBUG "SDM120 response error %d"), error);
AddLog(LOG_LEVEL_DEBUG);
} else {
switch(sdm120_read_state) {
case 0:
sdm120_voltage = value;
break;
case 1:
sdm120_current = value;
break;
case 2:
sdm120_power = value;
break;
case 5:
sdm120_power_factor = value;
break;
case 6:
sdm120_frequency = value;
break;
case 9:
sdm120_energy_total = value;
break;
} // end switch
sdm120_read_state++;
if (sizeof(sdm_start_addresses)/2 == sdm120_read_state) {
sdm120_read_state = 0;
}
}
} // end data ready
if (0 == sdm120_send_retry || data_ready) {
sdm120_send_retry = 5;
SDM_ModbusSend(0x04, sdm_start_addresses[sdm120_read_state], 2);
} else {
sdm120_send_retry--;
}
} // end 300 ms
}
void SDM120Init()
{
sdm120_type = 0;
if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) {
SDM120Serial = new TasmotaSerial(pin[GPIO_SDM120_RX], pin[GPIO_SDM120_TX], 1);
if (SDM120Serial->begin(9600)) {
if (SDM120Serial->hardwareSerial()) { ClaimSerial(); }
sdm120_type = 1;
}
}
}
#ifdef USE_WEBSERVER
const char HTTP_SNS_SDM120_DATA[] PROGMEM = "%s"
"{s}SDM120 " D_VOLTAGE "{m}%s " D_UNIT_VOLT "{e}"
"{s}SDM120 " D_CURRENT "{m}%s " D_UNIT_AMPERE "{e}"
"{s}SDM120 " D_POWERUSAGE "{m}%s " D_UNIT_WATT "{e}"
"{s}SDM120 " D_POWER_FACTOR "{m}%s{e}"
"{s}SDM120 " D_FREQUENCY "{m}%s " D_UNIT_HERTZ "{e}"
"{s}SDM120 " D_ENERGY_TOTAL "{m}%s " D_UNIT_KILOWATTHOUR "{e}";
#endif // USE_WEBSERVER
void SDM120Show(boolean json)
{
char voltage[10];
char current[10];
char power[10];
char power_factor[10];
char frequency[10];
char energy_total[10];
dtostrfd(sdm120_voltage, Settings.flag2.voltage_resolution, voltage);
dtostrfd(sdm120_current, Settings.flag2.current_resolution, current);
dtostrfd(sdm120_power, Settings.flag2.wattage_resolution, power);
dtostrfd(sdm120_power_factor, 2, power_factor);
dtostrfd(sdm120_frequency, 2, frequency);
dtostrfd(sdm120_energy_total, Settings.flag2.energy_resolution, energy_total);
if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"" D_RSLT_ENERGY "\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_POWERUSAGE "\":%s,\"" D_JSON_FREQUENCY "\":%s,\"" D_JSON_POWERFACTOR "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s}"),
mqtt_data, energy_total, power, frequency, power_factor, voltage, current);
#ifdef USE_DOMOTICZ
if (0 == tele_period) {
DomoticzSensor(DZ_VOLTAGE, voltage);
DomoticzSensor(DZ_CURRENT, current);
DomoticzSensorPowerEnergy((uint16_t)sdm120_power, energy_total);
}
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_SDM120_DATA, mqtt_data, voltage, current, power, power_factor, frequency, energy_total);
}
#endif // USE_WEBSERVER
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
#define XSNS_23
boolean Xsns23(byte function)
{
boolean result = false;
if (sdm120_type) {
switch (function) {
case FUNC_INIT:
SDM120Init();
break;
case FUNC_EVERY_50_MSECOND:
SDM12050ms();
break;
case FUNC_JSON_APPEND:
SDM120Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_APPEND:
SDM120Show(0);
break;
#endif // USE_WEBSERVER
}
}
return result;
}
#endif // USE_SDM120