Giter Club home page Giter Club logo

espsparsnasgateway's Introduction

EspSparsnasGateway

This is a MQTT Gateway for Ikeas energy monitor Sparsnas. The monitor sends encoded data by radio to a control panel. This device collects the data and sends it to MQTT-enabled receivers in json-format.

The data is also printed to the serial port. If the reception is bad, the received data can be bad. This gives a CRC-error, the data is in this case not sent via MQTT but printed via the serial port.

The data sent via MQTT is in json format and looks like this:

{
    "error": "",
    "seq": 28767,
    "timestamp": 1592040611,
    "watt": 1920,
    "total": 15016,
    "battery": 100,
    "rssi": -123,
    "power": 80,
    "pulse": 150160049
}

The device uses two Mqtt topics to publish, EspSparsnasGateway/<sensor_id>/valuesV2 and `EspSparsnasGateway/debugV2'. The latter can be ignored if you not are interrested in bad packets, just listen on topic 'EspSparsnasGateway/valuesV2'.

Dependencies

This requires the following packages:

  • ArduinoJson (5.x)
  • PubSubClient (2.7)
  • SPIFlash_LowPowerLabs (101.1)
  • RFM69_LowPowerLabs (1.2.0)

Packages can be installed using the Arduino libs, see the docs for more info

Using

Load the project files in Atom/VS Code with PlatformIO. Then copy the file include/settings.example.h to include/settings.h. Adjust the values in settings.h to fit your environment and save it. Upload to your hardware and enjoy :)

Hardware

The hardware used is a ESP8266-based wifi-enabled MCU. You can use different devices like a Wemos D1mini or a NodeMCU, but take care of the GPIO labels that can differ. The receiver is a RFM69HCW radio transciever (the RFMCW also works but note that it have a different pinout). I use a 868MHz device, but a 900MHz should work as well. To this a simple antenna is connected, I use a straight wire, 86 millimeters long connected to the RFM's Ant-connection. The wire shall be vertical, standing up. You can also add a similar wire to the GND-connection next to the antenna connection, pointing down, opposite to the first wire.

The connection for the RFM69 is hardcoded. This is standard SPI connections set in the SPI-library that can't be changed. See https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/using-the-arduino-addon.

The schematic shows a NodeMCU, but you can use another ESP8266-based device if you want (except the Esp-01). Use these pin mappings:

RFM69 RFM69HCW RFM69CW D1mini/
NodeMCU
ESP8266/
ESP12/
ESP32
DIO0 14 9 D1 Gpio05
SCK 4 6 D5 Gpio14
MISO 2 8 D6 Gpio12
MOSI 3 5 D7 Gpio13
EN/NSS 5 7 D8 Gpio15
ANT 9 1 - -

Wiring diagram

Note! Adafruit modules requires a connection from RST to GND! (Ref: https://www.mysensors.org/build/connect_radio#wiring-the-rfm69-radio).

LEDs

The code also supports three optional LEDs indicating status connected as follows:

LED D1mini/
NodeMCU
Usage
RED D0 Error (typically CRC errors)
GREEN D3 Used during boot
BLUE D2 Indicates successfully recieved package

It is possible to use the onboard (blue) LED on the D1mini by changing #define LED_BLUE D2 to #define LED_BLUE D4.

Parts

You can build your own device using these parts: (To see the language specific page make sure to select the language at the top of the page or it will give a 404.)

U1 - NodeMCU V3 https://www.lawicel-shop.se/microkontroller/esp8266-esp32/nodemcu-v3-with-esp-12e-ch340

Part1 - RFM69HCW https://www.lawicel-shop.se/rfm69hcw-transceiver-bob

Optional (but highly recommended)

C2 - Capacitor 100nF se: https://www.lawicel-shop.se/elektronik/komponenter/kondensatorer/capacitor-100nf-10-pack en: https://www.lawicel-shop.se/components/komponenter/capacitors/capacitor-100nf-10-pack

C1 - Capacitor 1000uF se: https://www.lawicel-shop.se/components/komponenter/capacitors/capacitor-1000uf-50v en: https://www.lawicel-shop.se/elektronik/komponenter/kondensatorer/capacitor-1000uf-50v

Also add two capacitors, 330-470uF and 100nF, to Vin in and Gnd for stability.

Hardware hacks to ensure good RF performance.

Keep the wires between the RFM69 module and the NodeMCU as short as possible and DO NOT make them 8 cm long hence that calculates into 1/4 wavelength of 868 MHz. You will experience interference and very poor performance if the above is not applied and followed.

If you want to learn more about the RFM69 and get some tips & tricks, look at https://learn.sparkfun.com/tutorials/rfm69hcw-hookup-guide.

If it doesn't work

As usual, check your connections one more time. If possible, solder the connections. Also make sure to use a good power supply, both the ESP and the RFM69 want's that.

Connect to computer

You can use the device with a simple USB power supply and get data via MQTT. The device also puts out more information via the serial port. You can connect it to a computer and look at the messages with a serial monitor, for example the one in the Arduino IDE or Minicom. The baudrate is 115200.

Enable debug

Further more information is given by the device if debug is activated:

#define DEBUG 1

Change the channel filter width

With some RFM's a software adjustment can be tested if the code doesn't work. Line 213 in RFM69functions.cpp looks like this:

/* 0x19 */ {REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_4}, // p26 in datasheet, filters out noise

You can try to change this to:

/* 0x19 */ {REG_RXBW, RF_RXBW_DCCFREQ_010 | RF_RXBW_MANT_16 | RF_RXBW_EXP_3}, // p26 in datasheet, filters out noise

This makes the channel filter wider, 62.5khz instead of 31.3khz.

Home Assistant integration

Sensors for power (Watt) and energy (kWh) will be created automatically if Home Assistant is configured to support discovery. The MQTT data can however be used anywhere, here's an example for the Home Automation software Home Assistant. In Home Assistant the sensors can look like this:

Home assistant version 2022.9 and newer

configuration.yaml

mqtt: !include mqtt.yaml

mqtt.yaml

sensor:
  - state_topic: "EspSparsnasGateway/valuesV2"
    name: "House power usage"
    unit_of_measurement: "W"
    value_template: "{{ float(value_json.watt) | round(0)  }}"
    icon: mdi:flash-circle
    state_class: measurement
    device_class: power

  - state_topic: "EspSparsnasGateway/valuesV2"
    name: "House energy usage"
    unit_of_measurement: "kWh"
    value_template: "{{ float(value_json.total) | round(0)  }}"
    icon: mdi:flash-circle
    state_class: measurement
    device_class: energy

  - state_topic: "EspSparsnasGateway/valuesV2"
    name: "House energy meter batt"
    unit_of_measurement: "%"
    value_template: "{{ float(value_json.battery) }}"
    icon: mdi:battery-outline

  - state_topic: "EspSparsnasGateway/valuesV2"
    name: "House energy meter power"
    value_template: "{{ float(value_json.power) }}"
    icon: mdi:power-socket-eu
    state_class: measurement

  - state_topic: "EspSparsnasGateway/valuesV2"
    name: "House energy meter signal"
    unit_of_measurement: "dBm"
    value_template: "{{ float(value_json.rssi) }}"
    icon: mdi:antenna

Home assistant version 2022.9 and older

Since 2022.9 the platform mqtt yaml configuration is depricated and a notification warning is shown since 2022.6 If you are running a home assistant version before 2022.9 you can use this

- platform: mqtt
  state_topic: "EspSparsnasGateway/+/state"
  name: "House power usage"
  unit_of_measurement: "W"
  value_template: '{{ float(value_json.watt) | round(0)  }}'

- platform: mqtt
  state_topic: "EspSparsnasGateway/+/state"
  name: "House energy usage"
  unit_of_measurement: "kWh"
  value_template: '{{ float(value_json.total) | round(0)  }}'

- platform: mqtt
  state_topic: "EspSparsnasGateway/+/state"
  name: "House energy meter batt"
  unit_of_measurement: "%"
  value_template: '{{ float(value_json.battery) }}'

Sensor result

The above results in these sensors:

- sensor.house_energy_meter_batt
- sensor.house_energy_usage
- sensor.house_power_usage

The result can be seen in SparsnasHass.png.

alt text

Protocol analysis

For much more information about the hardware, the protocol and how to analyse the transmission, see Kodarn's Github, https://github.com/kodarn/Sparsnas.

Thanks!

The code is based on Sommarlovs version of Ludvig Strigeus code: http://elektronikforumet.com/forum/viewtopic.php?f=2&t=85006&start=255 Strigeus original code for use with a DVB-T Usb dongle: https://github.com/strigeus/sparsnas_decoder

espsparsnasgateway's People

Contributors

bphermansson avatar dogmatic69 avatar fredrike avatar kalj avatar lerra avatar naesstrom avatar riksarchen avatar sperra avatar tubalainen avatar whyz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

espsparsnasgateway's Issues

Rssi always 0

The function to get the Rssi from the receiver always returns 0, should return something like -67.
If we set forceTrigger = true in uint16_t readRSSI (line 304) the code crashes when valid data arrives.

Port or merge this project to the existing OpenMQTTGateway project?

@bphermansson might it be a good idea to port or merge this to the existing OpenMQTTGateway project and maybe join their development team (formerly known as 433toMQTTto433 gateway by 1technophile)?

https://github.com/1technophile/OpenMQTTGateway

OpenMQTTGateway is also a ESP8266 and RFM69 (from HopeRF) RF to MQTT project however it also keeps compatibility with ESP32 and Arduino + W5100 too, and as an additional bonus it supports InfraRed (IR) transmitters and recievers as well:

https://1technophile.github.io/OpenMQTTGateway/

Blog:

https://1technophile.blogspot.se/

RM69 specific blog:

https://1technophile.blogspot.se/2017/09/why-adding-rfm69-compatibility-to.html

Wiki:

https://github.com/1technophile/OpenMQTTGateway/wiki

End-point device compatibility:

https://community.home-assistant.io/t/433tomqttto433-gateway-device-list/7819/7
https://docs.google.com/spreadsheets/d/1DdtVtSsN25nwP6BZI5q6C9yDGz37tUWjw2SQ1RGwBxU/

Good or bad idea?

Lack of info when not in debug mode

Maybe it's a good idea to present more info on the serial port when running in normal mode, ie not with DEBUG active? Like IP-adress, topics and such.

Cannot upload sketch to NodeMCU

Im trying to upload your code to the NodeMCU v.3, but always get an error in arduino saying:
call of overloaded 'readRSSI()' is ambiguous

and this line is marked red:
uint16_t srssi = readRSSI();
I´ve downloaded the libraries needed but still get the error. Hope you can help, thanks.

Copy of error message:

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\EspSparsnasGateway.ino: In function 'void interruptHandler()':

EspSparsnasGateway:424: error: call of overloaded 'readRSSI()' is ambiguous

   uint16_t srssi = readRSSI();

                             ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\EspSparsnasGateway.ino:424:33: note: candidates are:

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino:45:10: note: uint16_t readRSSI()

uint16_t readRSSI() {

      ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\EspSparsnasGateway.ino:303:10: note: uint16_t readRSSI(bool)

uint16_t readRSSI(bool forceTrigger = false) { // Settings this to true gives a crash...

      ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'uint32_t getFrequency()':

RFM69functions:1: error: redefinition of 'uint32_t getFrequency()'

uint32_t getFrequency() {

      ^

EspSparsnasGateway:268: error: 'uint32_t getFrequency()' previously defined here

uint32_t getFrequency() {

      ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'void receiveBegin()':

RFM69functions:4: error: redefinition of 'void receiveBegin()'

void receiveBegin() {

  ^

EspSparsnasGateway:272: error: 'void receiveBegin()' previously defined here

void receiveBegin() {

  ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'uint8_t readReg(uint8_t)':

RFM69functions:16: error: redefinition of 'uint8_t readReg(uint8_t)'

uint8_t readReg(uint8_t addr) {

     ^

EspSparsnasGateway:320: error: 'uint8_t readReg(uint8_t)' previously defined here

uint8_t readReg(uint8_t addr) {

     ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'void writeReg(uint8_t, uint8_t)':

RFM69functions:24: error: redefinition of 'void writeReg(uint8_t, uint8_t)'

void writeReg(uint8_t addr, uint8_t value) {

  ^

EspSparsnasGateway:328: error: 'void writeReg(uint8_t, uint8_t)' previously defined here

void writeReg(uint8_t addr, uint8_t value) {

  ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'void select()':

RFM69functions:32: error: redefinition of 'void select()'

void select() {

  ^

EspSparsnasGateway:336: error: 'void select()' previously defined here

void select() {

  ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'void unselect()':

RFM69functions:38: error: redefinition of 'void unselect()'

void unselect() {

  ^

EspSparsnasGateway:342: error: 'void unselect()' previously defined here

void unselect() {

  ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'uint16_t readRSSI()':

RFM69functions:56: error: 'rssi' was not declared in this scope

rssi = -readReg(REG_RSSIVALUE);

^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'bool receiveDone()':

RFM69functions:64: error: redefinition of 'bool receiveDone()'

bool receiveDone() {

  ^

EspSparsnasGateway:284: error: 'bool receiveDone()' previously defined here

bool receiveDone() {

  ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\RFM69functions.ino: In function 'uint16_t crc16(volatile uint8_t*, size_t)':

RFM69functions:82: error: redefinition of 'uint16_t crc16(volatile uint8_t*, size_t)'

uint16_t crc16(volatile uint8_t *data, size_t n) {

      ^

EspSparsnasGateway:522: error: 'uint16_t crc16(volatile uint8_t*, size_t)' previously defined here

uint16_t crc16(volatile uint8_t *data, size_t n) {

      ^

C:\Users\Peter Juul Ornstrup\Documents\EspSparsnasGateway-master\EspSparsnasGateway\callback.ino: In function 'void callback(char*, byte*, unsigned int)':

callback:2: error: redefinition of 'void callback(char*, byte*, unsigned int)'

void callback(char* topic, byte* payload, unsigned int length) {

  ^

EspSparsnasGateway:566: error: 'void callback(char*, byte*, unsigned int)' previously defined here

void callback(char* topic, byte* payload, unsigned int length) {

  ^

callback:38: error: 'EEPROM' was not declared in this scope

     EEPROM.write(addr, 1); // Indicate stored settings by writing 1 to Eeprom address 0

     ^

callback:52: error: 'mqtt_debug_topic' was not declared in this scope

     client.publish(mqtt_debug_topic, msg);

                    ^

callback:66: error: 'EEPROM' was not declared in this scope

   EEPROM.write(addr, 1);    // Indicate stored settings by writing 1 to Eeprom address 1

   ^

callback:82: error: 'mqtt_debug_topic' was not declared in this scope

   client.publish(mqtt_debug_topic, msg);

                  ^

callback:90: error: 'EEPROM' was not declared in this scope

     EEPROM.write(i, 0);

     ^

callback:92: error: 'EEPROM' was not declared in this scope

   EEPROM.commit();

   ^

callback:96: error: 'mqtt_debug_topic' was not declared in this scope

   client.publish(mqtt_debug_topic, msg);

                  ^

callback:105: error: 'mqtt_debug_topic' was not declared in this scope

   client.publish(mqtt_debug_topic, msg);

                  ^

exit status 1
call of overloaded 'readRSSI()' is ambiguous

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Bad data

Now I have it running, but the data seems to be bad. I've tried to change the flag in the config mentioned in the readme but that did not help.

    if (TEMPDATA[0] != 0x11 || TEMPDATA[1] != (SENSOR_ID & 0xFF) || TEMPDATA[3] != 0x07 || rcv_sensor_id != SENSOR_ID) {
      Serial.println("Bad data");

      Serial.print("data_0: ");
      Serial.println(TEMPDATA[0]);

      Serial.print("data_1: ");
      Serial.println(TEMPDATA[1]);

      Serial.print("SENSOR_ID & 0xFF: ");
      Serial.println(SENSOR_ID & 0xFF);

      Serial.print("data_3: ");
      Serial.println(TEMPDATA[3]);

      Serial.print("rcv_sensor_id: ");
      Serial.println(rcv_sensor_id);

      Serial.print("SENSOR_ID: ");
      Serial.println(SENSOR_ID);

output:

08:51:27.321 -> Got rf data
08:51:27.321 -> Bad data
08:51:27.321 -> data_0: 238
08:51:27.321 -> data_1: 101
08:51:27.321 -> SENSOR_ID & 0xFF: 93
08:51:27.355 -> data_3: 196
08:51:27.355 -> rcv_sensor_id: 215117836
08:51:27.355 -> SENSOR_ID: 542045
08:51:33.333 -> Got rf data
08:51:33.333 -> Bad data
08:51:33.333 -> data_0: 85
08:51:33.333 -> data_1: 93
08:51:33.333 -> SENSOR_ID & 0xFF: 93
08:51:33.333 -> data_3: 69
08:51:33.333 -> rcv_sensor_id: 4157798087
08:51:33.333 -> SENSOR_ID: 542045

Compilation errors with Arduino IDE 1.8.8

I get error messages like this:

EspSparsnasGateway:349:17: error: 'uint32_t' was not declared in this scope
 bool initialize(uint32_t frequency) {
                 ^
EspSparsnasGateway:604:14: error: variable or field 'setMode' declared void
 void setMode(uint8_t newMode) {
              ^
EspSparsnasGateway:604:14: error: 'uint8_t' was not declared in this scope
RFM69functions:1:1: error: 'uint32_t' does not name a type
 uint32_t getFrequency() {
 ^
RFM69functions:16:1: error: 'uint8_t' does not name a type
 uint8_t readReg(uint8_t addr) {
 ^
RFM69functions:24:15: error: variable or field 'writeReg' declared void
 void writeReg(uint8_t addr, uint8_t value) {
               ^
RFM69functions:24:15: error: 'uint8_t' was not declared in this scope
RFM69functions:24:29: error: 'uint8_t' was not declared in this scope
 void writeReg(uint8_t addr, uint8_t value) {
                             ^
RFM69functions:82:1: error: 'uint16_t' does not name a type
 uint16_t crc16(volatile uint8_t *data, size_t n) {
 ^
callback:2:28: error: 'byte' has not been declared
 void callback(char* topic, byte* payload, unsigned int length) {
                            ^
EspSparsnasGateway:109:6: error: previous declaration of 'void setup()' with 'C++' linkage
 void setup() {
      ^
In file included from /home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino:47:0:
/home/kalle/.arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Arduino.h:221:16: error: conflicts with new declaration with 'C' linkage
 void setup(void);
                ^
EspSparsnasGateway:650:6: error: previous declaration of 'void loop()' with 'C++' linkage
 void loop() {
      ^
In file included from /home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino:47:0:
/home/kalle/.arduino15/packages/esp8266/hardware/esp8266/2.4.2/cores/esp8266/Arduino.h:222:15: error: conflicts with new declaration with 'C' linkage
 void loop(void);
               ^
/home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino: In function 'void setup()':
EspSparsnasGateway:134:30: error: no matching function for call to 'PubSubClient::setCallback(void (&)(char*, int*, unsigned int))'
   client.setCallback(callback); // What to do when a Mqtt message arrives
                              ^
/home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino:134:30: note: candidate is:
In file included from /home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino:71:0:
/home/kalle/code/Arduino/libraries/PubSubClient/src/PubSubClient.h:121:18: note: PubSubClient& PubSubClient::setCallback(std::function<void(char*, unsigned char*, unsigned int)>)
    PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
                  ^
/home/kalle/code/Arduino/libraries/PubSubClient/src/PubSubClient.h:121:18: note:   no known conversion for argument 1 from 'void(char*, int*, unsigned int)' to 'std::function<void(char*, unsigned char*, unsigned int)>'
EspSparsnasGateway:319:24: error: 'initialize' cannot be used as a function
   if (!initialize(ifreq)) {
                        ^
EspSparsnasGateway:340:57: error: 'getFrequency' was not declared in this scope
     String temp = "Listening on " + String(getFrequency()) + "hz. Done in setup.";
                                                         ^
/home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino: In function 'bool initialize(uint32_t)':
EspSparsnasGateway:349:35: error: 'bool initialize(uint32_t)' redeclared as different kind of symbol
 bool initialize(uint32_t frequency) {
                                   ^
EspSparsnasGateway:349:6: error: previous declaration of 'bool initialize'
 bool initialize(uint32_t frequency) {
      ^
EspSparsnasGateway:397:34: error: 'writeReg' was not declared in this scope
     writeReg(REG_SYNCVALUE1, 0xAA);
                                  ^
EspSparsnasGateway:399:34: error: 'readReg' was not declared in this scope
   } while (readReg(REG_SYNCVALUE1) != 0xaa && millis() - start < timeout);
                                  ^
EspSparsnasGateway:402:34: error: 'writeReg' was not declared in this scope
     writeReg(REG_SYNCVALUE1, 0x55);
                                  ^
EspSparsnasGateway:407:40: error: 'writeReg' was not declared in this scope
     writeReg(CONFIG[i][0], CONFIG[i][1]);
                                        ^
EspSparsnasGateway:411:28: error: 'setMode' was not declared in this scope
   setMode(RF69_MODE_STANDBY);
                            ^
/home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino: In function 'void interruptHandler()':
EspSparsnasGateway:458:54: error: 'readReg' was not declared in this scope
   if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)) {
                                                      ^
EspSparsnasGateway:466:30: error: 'setMode' was not declared in this scope
     setMode(RF69_MODE_STANDBY);
                              ^
EspSparsnasGateway:479:38: error: 'crc16' was not declared in this scope
     uint16_t crc = crc16(TEMPDATA, 18);
                                      ^
/home/kalle/code/Arduino/EspSparsnasGateway/EspSparsnasGateway.ino: In function 'void setMode(uint8_t)':
EspSparsnasGateway:613:35: error: 'readReg' was not declared in this scope
   uint8_t val = readReg(REG_OPMODE);
                                   ^
EspSparsnasGateway:616:64: error: 'writeReg' was not declared in this scope
       writeReg(REG_OPMODE, (val & 0xE3) | RF_OPMODE_TRANSMITTER);
                                                                ^
/home/kalle/code/Arduino/EspSparsnasGateway/RFM69functions.ino: In function 'uint32_t getFrequency()':
RFM69functions:2:53: error: 'readReg' was not declared in this scope
   return RF69_FSTEP * (((uint32_t)readReg(REG_FRFMSB) << 16) + ((uint16_t)readReg(REG_FRFMID) << 8) + readReg(REG_FRFLSB));
                                                     ^
/home/kalle/code/Arduino/EspSparsnasGateway/RFM69functions.ino: In function 'void receiveBegin()':
RFM69functions:7:28: error: 'readReg' was not declared in this scope
   if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) {
                            ^
RFM69functions:10:68: error: 'writeReg' was not declared in this scope
     writeReg(REG_PACKETCONFIG2, (val & 0xFB) | RF_PACKET2_RXRESTART);

I had to downgrade to Arduino IDE 1.6.13

Calculates wrongly when low/none power usage

The code calculates watts extremely oddly when there isnt a significant load, for example, a load greater than 1000 W.

It reports like 39302 watts when there should be 7 W only.

Setting the frequency via Mqtt

Let the code listen for incoming Mqtt messages with a setting for the receiver frequency. To set the frequency, send something like this:
mosquitto_pub -h 192.168.1.79 -t 'EspSparsnasGateway/settings' -m '{freq:867.999939}'

Merging or starting over?

Looking at the code now I think it's a mess... I see some interesting forks, but I wonder why most of you have removed the extra source files and put all code in EspSparsnasGateway.ino?
I'm thinking of making a "v2.0", but I like splitting the code. Any good reasons why I shouldn't?

Use the RFM69 libs directly

Currently there is a "copy" of that lib in here meaning we need to maintain it. We should replace all the RMF69 code with what is provided with the lib.

That will also make this work on more boards out the box.

No default value for frequency

If no frequency is set via Mqtt, the frequency is set to 0. Change this so a default frequency is used instead. "if freq=0 then freq=8680000".

SPI write problem

Hi,

I have built my own EspSparsnäsGateway with the same parts as in your parts list, but cannot get SPI to write any values. Reading is fine, I always read the default values of the RFM69, no matter what values I have written. For example, first thing that happens in your program is writing 0xAA to REG_SYNCVALUE1, but it always reads as 0x00.
I have connected a logic analyzer on the SPI signals, and it looks fine, MSB is set in the address byte when doing a write.

Also, shouldn't the connection D8(NodeMCU) - EN(RFM69) be D8(NodeMCU) - CS(RFM69) ?

Any help appreciated!

BR
Leif Persson

MQTT is not stable, many missed packets.

After running for a while this is my logs, you can see many missed pakets. The Sparsnas is sending around every 15 seconds (seen in the serial debugger), but the MQTT subscriber is only getting a fraction of what is sent.

Look at the seq below, this is what MQTT is actually processing.

EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43362,"watt":2792.727,"total":205.451,"pulse":"205451","battery":100,"rssi":"-101","power":"1320"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43384,"watt":2378.323,"total":205.702,"pulse":"205702","battery":100,"rssi":"-86","power":"1550"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43558,"watt":2963.344,"total":207.677,"pulse":"207677","battery":100,"rssi":"-85","power":"1244"}
EspSparsnasGateway/values {"seq":43564,"watt":3056.716,"total":207.753,"pulse":"207753","battery":100,"rssi":"-93","power":"1206"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43608,"watt":1253.025,"total":208.207,"pulse":"208207","battery":100,"rssi":"-84","power":"2942"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43652,"watt":1466.348,"total":208.467,"pulse":"208467","battery":100,"rssi":"-94","power":"2514"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43677,"watt":701.3699,"total":208.602,"pulse":"208602","battery":100,"rssi":"-86","power":"5256"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43788,"watt":607.7151,"total":208.878,"pulse":"208878","battery":100,"rssi":"-82","power":"6066"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43793,"watt":920.6793,"total":208.892,"pulse":"208892","battery":100,"rssi":"-91","power":"4004"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43840,"watt":1044.602,"total":209.011,"pulse":"209011","battery":100,"rssi":"-90","power":"3529"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43882,"watt":7200,"total":209.107,"pulse":"209107","battery":100,"rssi":"-88","power":"512"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43935,"watt":641.3361,"total":209.275,"pulse":"209275","battery":100,"rssi":"-87","power":"5748"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43939,"watt":560.7545,"total":209.285,"pulse":"209285","battery":100,"rssi":"-86","power":"6574"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":43972,"watt":457.0295,"total":209.371,"pulse":"209371","battery":100,"rssi":"-85","power":"8066"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44011,"watt":1142.715,"total":209.579,"pulse":"209579","battery":100,"rssi":"-88","power":"3226"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44126,"watt":996.3243,"total":210.235,"pulse":"210235","battery":100,"rssi":"-90","power":"3700"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44171,"watt":3145.393,"total":210.74,"pulse":"210740","battery":100,"rssi":"-85","power":"1172"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44199,"watt":3432.402,"total":211.139,"pulse":"211139","battery":100,"rssi":"-85","power":"1074"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44371,"watt":6083.168,"total":212.872,"pulse":"212872","battery":100,"rssi":"-82","power":"606"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44381,"watt":3824.066,"total":213.062,"pulse":"213062","battery":100,"rssi":"-87","power":"964"}
EspSparsnasGateway/values {"seq":44382,"watt":4015.686,"total":213.078,"pulse":"213078","battery":100,"rssi":"-94","power":"918"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44399,"watt":5008.696,"total":213.4,"pulse":"213400","battery":100,"rssi":"-91","power":"736"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44410,"watt":4596.509,"total":213.623,"pulse":"213623","battery":100,"rssi":"-89","power":"802"}
EspSparsnasGateway/values {"seq":44411,"watt":4585.075,"total":213.642,"pulse":"213642","battery":100,"rssi":"-82","power":"804"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44425,"watt":2949.12,"total":213.838,"pulse":"213838","battery":100,"rssi":"-88","power":"1250"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44462,"watt":4738.303,"total":214.514,"pulse":"214514","battery":100,"rssi":"-83","power":"778"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44488,"watt":4517.647,"total":215.11,"pulse":"215110","battery":100,"rssi":"-90","power":"816"}
EspSparsnasGateway/values {"seq":44489,"watt":4506.602,"total":215.129,"pulse":"215129","battery":100,"rssi":"-81","power":"818"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44495,"watt":4619.549,"total":215.243,"pulse":"215243","battery":100,"rssi":"-87","power":"798"}
EspSparsnasGateway/values {"seq":44496,"watt":4573.697,"total":215.263,"pulse":"215263","battery":100,"rssi":"-88","power":"806"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44511,"watt":3963.871,"total":215.495,"pulse":"215495","battery":100,"rssi":"-89","power":"930"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44573,"watt":2972.903,"total":216.365,"pulse":"216365","battery":100,"rssi":"-85","power":"1240"}
EspSparsnasGateway/values {"seq":44574,"watt":3006.852,"total":216.377,"pulse":"216377","battery":100,"rssi":"-85","power":"1226"}
EspSparsnasGateway/values {"seq":44575,"watt":3006.852,"total":216.39,"pulse":"216390","battery":100,"rssi":"-88","power":"1226"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44578,"watt":2963.344,"total":216.427,"pulse":"216427","battery":100,"rssi":"-82","power":"1244"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44583,"watt":3245.07,"total":216.493,"pulse":"216493","battery":100,"rssi":"-95","power":"1136"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44607,"watt":4837.795,"total":216.847,"pulse":"216847","battery":100,"rssi":"-83","power":"762"}
EspSparsnasGateway/values {"seq":44608,"watt":4775.129,"total":216.869,"pulse":"216869","battery":100,"rssi":"-84","power":"772"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44614,"watt":3194.454,"total":216.978,"pulse":"216978","battery":100,"rssi":"-88","power":"1154"}
EspSparsnasGateway/values {"seq":44615,"watt":3188.927,"total":216.991,"pulse":"216991","battery":100,"rssi":"-91","power":"1156"}
EspSparsnasGateway/values {"seq":44616,"watt":3245.07,"total":217.005,"pulse":"217005","battery":100,"rssi":"-89","power":"1136"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44619,"watt":4726.154,"total":217.05,"pulse":"217050","battery":100,"rssi":"-83","power":"780"}
EspSparsnasGateway/values {"seq":44620,"watt":3092.617,"total":217.066,"pulse":"217066","battery":100,"rssi":"-88","power":"1192"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44639,"watt":3309.156,"total":217.207,"pulse":"217207","battery":100,"rssi":"-82","power":"1114"}
EspSparsnasGateway/values {"seq":44640,"watt":3256.537,"total":217.221,"pulse":"217221","battery":100,"rssi":"-94","power":"1132"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44648,"watt":3504.183,"total":217.334,"pulse":"217334","battery":100,"rssi":"-82","power":"1052"}
EspSparsnasGateway/values {"seq":44649,"watt":1647.185,"total":217.343,"pulse":"217343","battery":100,"rssi":"-89","power":"2238"}
EspSparsnasGateway/values {"seq":44650,"watt":1482.864,"total":217.349,"pulse":"217349","battery":100,"rssi":"-101","power":"2486"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44655,"watt":1647.185,"total":217.384,"pulse":"217384","battery":100,"rssi":"-88","power":"2238"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44657,"watt":1590.337,"total":217.397,"pulse":"217397","battery":100,"rssi":"-84","power":"2318"}
EspSparsnasGateway/values {"seq":44658,"watt":1628.269,"total":217.405,"pulse":"217405","battery":100,"rssi":"-90","power":"2264"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44660,"watt":1615.425,"total":217.419,"pulse":"217419","battery":100,"rssi":"-92","power":"2282"}
EspSparsnasGateway/values {"seq":44661,"watt":1641.318,"total":217.426,"pulse":"217426","battery":100,"rssi":"-98","power":"2246"}
EspSparsnasGateway/values {"seq":44662,"watt":1635.492,"total":217.433,"pulse":"217433","battery":100,"rssi":"-90","power":"2254"}
EspSparsnasGateway/values {"seq":44663,"watt":1598.612,"total":217.439,"pulse":"217439","battery":100,"rssi":"-94","power":"2306"}
EspSparsnasGateway/values {"seq":44664,"watt":1644.246,"total":217.446,"pulse":"217446","battery":100,"rssi":"-92","power":"2242"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44676,"watt":3369.653,"total":217.533,"pulse":"217533","battery":100,"rssi":"-87","power":"1094"}
EspSparsnasGateway/values {"seq":44677,"watt":3382.018,"total":217.547,"pulse":"217547","battery":100,"rssi":"-93","power":"1090"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44708,"watt":1594.464,"total":217.809,"pulse":"217809","battery":100,"rssi":"-93","power":"2312"}
EspSparsnasGateway/debug {"status":"Connected to Mqtt broker as EspSparsnasGateway"}
EspSparsnasGateway/values {"seq":44713,"watt":2944.409,"total":217.841,"pulse":"217841","battery":100,"rssi":"-84","power":"1252"}

Messages are sent much more frequently...

16:54:18.081 -> Attempting MQTT connection...Data here????
16:54:18.114 -> Connected to Mqtt broker as EspSparsnasGateway
16:54:21.280 -> Got rf data
16:54:26.681 -> Got rf data
16:54:32.314 -> Got rf data
16:54:32.781 -> Got rf data
16:54:32.781 -> Memory usage: 36256
16:54:32.781 -> Sending to mqtt: {"seq":44814,"watt":1265.065,"total":218.368,"pulse":"218368","battery":100,"rssi":"-84","power":"2914"}
16:54:36.599 -> Got rf data
16:54:39.397 -> Got rf data
16:54:39.530 -> Attempting MQTT connection...Data here????
16:54:39.564 -> Connected to Mqtt broker as EspSparsnasGateway
16:54:42.492 -> Got rf data
16:54:47.752 -> Got rf data
16:54:47.752 -> Memory usage: 36256
16:54:47.785 -> Sending to mqtt: {"seq":44815,"watt":1249.627,"total":218.373,"pulse":"218373","battery":100,"rssi":"-81","power":"2950"}
16:54:55.417 -> Got rf data
16:54:57.651 -> Got rf data
16:55:01.526 -> Attempting MQTT connection...Data here????
16:55:01.560 -> Connected to Mqtt broker as EspSparsnasGateway
16:55:02.767 -> Got rf data
16:55:02.767 -> Memory usage: 37600
16:55:02.767 -> Sending to mqtt: {"seq":44816,"watt":1237.878,"total":218.379,"pulse":"218379","battery":100,"rssi":"-85","power":"2978"}

Function to find correct frequency

The transmitter is assumed to send at exactly 868MHz, but that seems to differ between devices. By trial and errror you can get better reception, by changing FREQUENCY in line 69:

uint32_t FREQUENCY = 867980000

It would be nice if the code could find the best frequency by itself.

Text corrections?

Just wanted to check what instructions to follow, in the build instructions you suggest using a "330-470uF" capacitor but in the links and the schematic it's a 1000uF?

Listening to the wrong frequency

For some reason the log gives me "Listening on 1023999936hz. Done in setup." even though I've set the frequency: "Stored frequency: 867999900". Since it is listening to the wrong freq. I don't get any data. I've tried updating the freq. with MQTT with no luck.

Logfile:

Debug on
Vcc=3014
Stored data: 1
There are stored settings
Stored frequency: 867999900
Stored sender id: d2a94a
Over The Air programming enabled, port: EspSparsnasGateway
Attempting MQTT connection...Connected to Mqtt broker as EspSparsnasGateway
Ready
IP address: 192.168.1.XX
EspSparsnasGateway @ IP:192.168.1.XX SSID: XXXXXX
Stored freq: 867999900
RFM69 init done
Radio initialized.
Listening on 1023999936hz. Done in setup.

Example code will not work out-of-the-box with energy dashboard in HA

In the example code we have the following

  - state_topic: "EspSparsnasGateway/valuesV2"
    name: "House energy usage"
    unit_of_measurement: "kWh"
    value_template: "{{ float(value_json.total) | round(0)  }}"
    icon: mdi:flash-circle
    state_class: measurement
    device_class: energy

But when this sensor is added as the "Grid consumption" sensor, home assistant complains about "The following entities have state class 'measurement' but 'last_reset' is missing".

Changing state_class to total seems to (maybe) fix the problem.

Enable second transmitter id

By monitoring more than one energy meter at home it would be possible to keep consumption from the Car and the Heating system on separate "tabs". I imagine that the code change would be rather small, but I can't achieve this myself with my lacking programming skills. My current need is to receive data from two transmitters.

My idea to solve the need:
Setup transmitter_id2 in settings.h
either add watt2, total2 and battery2 to the MQTT message or create a separate message in case two transmitter ids have been set.

Compilation problem

Hi
I´m trying to compile to project and got error that functions can not be found.
I´m guessing there are something missing, but cant find it.
The board is Wemos D1 running at 115200.

error.txt

regards Peter

Socket error

MQTT connects and then disconnects within a second.

1568413360: New client connected from 192.168.1.251 as EspSparsnasGatewayV2 (p2, c1, k30, u'hidden').
1568413360: Socket error on client EspSparsnasGatewayV2, disconnecting.

Worked fine with old version.

Hardware i use is a nodemcu esp8266, same as the one described in the guide.

Drastically wrong power reported when there is few pulses

Wrong power is being reported when there is a low load in the system making it take longer time between the light pulses on the meter.
For example, when there is 250-350W being consumed, the EspSparsnasGateway reports 10-20W, (with 100 pulses/Kwh for the meter, therefore approximately 3 minutes between the pulses).
But higher loads, for example, 2000w gets reported correctly.

Attached serial log shows first an apartment at approx 300w, then a kettle is turned on raising the power to above 2000w and then it is turned off again.
(Most of the bad packets received have been filtered away to make the log shorter.)

debug_log.txt

Suggestion: create a Sparsnas class that handles everything related to the received package

It's been bugging me the whole day yesterday and I think the sparsnäs decoding should be extracted to a separate class. This will enable easy access to multiple sensors like @Sperra suggested and also much cleaner code base.

My suggestion is that we move most parts of interruptHandler() into a class like this one:

class Sparsnas
{
private:
    uint32_t my_sensor_id;
    uint16_t my_pulses_per_kwh;
    uint8_t my_received_data[21];
    uint8_t my_decrypted_data[21];
 
 static:
    bool check_crc(unit8_t data[18]);

public:
    Sparsnas(uint32_t sensor_id, uint16_t pulses_per_kwh, uint8_t received_data[21);
    bool isValidPackage();  // This will do the decryption and crc_check.
    uint16_t getPower();
    float getWatt();
    uint16_t getSequence();
    char* getStatusString();
    char* getJsonString();
};

This means that https://github.com/fredrike/EspSparsnasGateway/blob/ce54c56d7d22fca426e0615b3bc3ebdf0c01dba9/src/RFM69functions.cpp#L315-L464 can be replaced with (and that the Sparsnas class can be reused in other projects):

    // Read 20 bytes
    for (uint8_t i = 0; i < 20; i++) {
      TEMPDATA[i] = SPI.transfer(0);
    }
    Sparsnas* sp = new Sparsnas(SENSOR_ID, PULSES_PER_KWH, TEMPDATA);

    if(sp->isValidPackage()) {
        mClient.publish(mqtt_status_topic, sp->getJsonString());
    } else {
        mClient.publish(mqtt_debug_topic, sp->getJsonString());
    }

We might need to add some fancy handling of LEDs and such (in my example above), the class should be native c++ but this is a base :).

Issue that values written to MQTT broker does not show the right values

When publishing to a mosquitto broker (at least)
i got the values {"seq":989,"power":true,"total":true,"battery":100}
Interprited as bolean values or somting?

i "fixed" the issue by changing to float from string (starting line 458 in the main .ino file)

  else {
    root["seq"] = seq;
    root["power"] = float(watt);
    root["total"] = float(pulse / PULSES_PER_KWH);
    root["battery"] = battery;
    root["rssi"] = float(srssi);
  }

Thanx for a GREAT project :)

Add software RST since it's missing

So reading up on the manual for the BOB I found a couple of pieces that might be interesting here.

  1. Define a rst pin
#if defined (__AVR_ATmega328P__) // UNO or Feather 328P w/wing
#define RFM69_INT 3 //
#define RFM69_CS 4 //
#define RFM69_RST 2 // "A"
#define LED 13
#endif

Setup

We begin by setting up the serial console and hard-resetting the RFM69

void setup()
{
Serial.begin(115200);
//while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer
pinMode(LED, OUTPUT);
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, LOW);
Serial.println("Feather RFM69 RX Test!");
Serial.println();
// manual reset
digitalWrite(RFM69_RST, HIGH);
delay(10);
digitalWrite(RFM69_RST, LOW);
delay(10);
  1. Setting the transmit power needed for the HCW board

Initializing Radio

For transmission power you can select from 14 to 20 dBi. Lower numbers use less power, but have less range. The second argument to the function is whether it is an HCW type radio, with extra amplifier. This should always be set to true!

if (!rf69.init()) {
Serial.println("RFM69 radio init failed");
while (1);
}
Serial.println("RFM69 radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
// No encryption
if (!rf69.setFrequency(RF69_FREQ)) {
Serial.println("setFrequency failed");
}
// If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
// ishighpowermodule flag set like this:
rf69.setTxPower(20, true); // range from 14-20 for power, 2nd arg must be true for 69HCW
// The encryption key has to be the same as the one in the server
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
rf69.setEncryptionKey(key);

And after that it's just the normal transmit code but I think we could use some of these changes for this one since it's the RFM69HCW that's listed in the materials section!

No values?

So I finally got around to assembling this, even added a sma contact and a 868 antenna to the breakout board.
Serial monitor just gives me this:

Attempting MQTT connection...Connected to Mqtt broker as EspSparsnasGateway
Stored data: 
There are no stored frequency, using default value
There are no stored senderid, using default value
Senderid: 570631
Frequency: 868000000
61.04
Over The Air programming enabled, port: EspSparsnasGateway
Ready
IP addre

The senderid is the one I entered and the frequency looks right to, the strange thing is that it cuts out before it even tells me the IP... Debug is set to 1 btw.

On the mqtt side of things it repeats the following 4 rows a couple of times

{"status":"Connected to Mqtt broker as EspSparsnasGateway"}
{"status":"There are no stored frequency, using default value"}
{"status":"There are no stored senderid, using default value"}
{"status":"EspSparsnasGateway @ IP:192.168.1.201 SSID: Katrinelund"}

The really strange thing tough is that if I disconnect the ESP from everything and run it the monitor seems to work?

Attempting MQTT connection...Connected to Mqtt broker as EspSparsnasGateway
Stored data: 
There are no stored frequency, using default value
There are no stored senderid, using default value
Senderid: 570631
Frequency: 868000000
61.04
Over The Air programming enabled, port: EspSparsnasGateway
Ready
IP address: 192.168.1.201
EspSparsnasGateway @ IP:192.168.1.201 SSID: Katrinelund
In initialize, frequency = 868100000
Adjusted freq: 14222950
61.04
RFM69 init done
Radio initialized.
Listening on 1023999936hz. Done in setup.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.