diff --git a/main/User_config.h b/main/User_config.h index 95274b8..2f91ff2 100755 --- a/main/User_config.h +++ b/main/User_config.h @@ -193,6 +193,7 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE; //#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32 //#define ZgatewayPilight "Pilight" //ESP8266, Arduino, ESP32 //#define ZgatewayWeatherStation "WeatherStation" //ESP8266, Arduino, ESP32 +//#define ZgatewayGFSunInverter "GFSunInverter" //ESP32 //#define ZgatewayBT "BT" //ESP8266, ESP32 //#define ZgatewayRF2 "RF2" //ESP8266, Arduino, ESP32 //#define ZgatewaySRFB "SRFB" // Sonoff RF Bridge diff --git a/main/ZgatewayGFSunInverter.ino b/main/ZgatewayGFSunInverter.ino new file mode 100644 index 0000000..a84e4cd --- /dev/null +++ b/main/ZgatewayGFSunInverter.ino @@ -0,0 +1,88 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your GridFree SUN-2000G inverter and a MQTT broker. + Send inverter metrics by MQTT. + + This gateway enables to: + - publish MQTT data, which are received by RS232 ModBus. + + Library for read metrics from GWL Power, GridFree SUN-2000G inverter . + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway 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. + + OpenMQTTGateway 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 . +*/ +#include "User_config.h" + +#ifdef ZgatewayGFSunInverter + +GfSun2000 GF = GfSun2000(); + +void GFSunInverterDataHandler(GfSun2000Data data) { + StaticJsonBuffer<2 * JSON_MSG_BUFFER> jsonBuffer; + JsonObject& jdata = jsonBuffer.createObject(); + + jdata.set("device_id", (char*)data.deviceID); + Log.trace(F("Device ID : %s\n" CR), data.deviceID); + jdata.set("ac_voltage", data.ACVoltage); + Log.trace(F("AC Voltage : %.1f\tV\n" CR), data.ACVoltage); + jdata.set("dc_voltage", data.DCVoltage); + Log.trace(F("DC Voltage : %.1f\tV\n" CR), data.DCVoltage); + jdata.set("power", data.averagePower); + Log.trace(F("Output Power : %.1f\tW (5min avg)\n" CR), data.averagePower); + jdata.set("c_energy", data.customEnergyCounter); + Log.trace(F("Custom Energy : %.1f\tkW/h (can be reseted)\n" CR), data.customEnergyCounter); + jdata.set("t_energy", data.totalEnergyCounter); + Log.trace(F("Total Energy : %.1f\tkW/h\n" CR), data.totalEnergyCounter); + +# ifdef GFSUNINVERTER_DEVEL + StaticJsonBuffer jsonBuffer2; + JsonObject& jregister = jsonBuffer2.createObject(); + char buffer[4]; + std::map::iterator itr; + for (itr = data.modbusRegistry.begin(); itr != data.modbusRegistry.end(); ++itr) { + Log.notice("%d: %d\n", itr->first, itr->second); + sprintf(buffer, "%d", itr->first); + jregister.set(buffer, itr->second); + } + jdata.set("register", jregister); +# endif + pub(subjectRFtoMQTT, jdata); +} + +void GFSunInverterErrorHandler(int errorId, char* errorMessage) { + char buffer[50]; + sprintf(buffer, "Error response: %02X - %s\n", errorId, errorMessage); + Log.error(buffer); + StaticJsonBuffer jsonBuffer; + JsonObject& jdata = jsonBuffer.createObject(); + jdata.set("status", "error"); + jdata.set("msg", errorMessage); + jdata.set("id", errorId); + pub(subjectRFtoMQTT, jdata); +} + +void setupGFSunInverter() { + GF.setup(Serial2); + GF.setDataHandler(GFSunInverterDataHandler); + GF.setErrorHandler(GFSunInverterErrorHandler); + Log.trace(F("ZgatewayGFSunInverter setup done " CR)); +} + +void ZgatewayGFSunInverterMQTT() { + GF.readData(); + delay(GFSUNINVERTER_DELAY); +} + +#endif diff --git a/main/config_GFSunInverter.h b/main/config_GFSunInverter.h new file mode 100644 index 0000000..7cc1643 --- /dev/null +++ b/main/config_GFSunInverter.h @@ -0,0 +1,47 @@ +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your GridFree SUN-2000G inverter and a MQTT broker. + Send inverter metrics by MQTT. + + This gateway enables to: + - publish MQTT data, which are received by RS232 ModBus. + + Library for read metrics from GWL Power, GridFree SUN-2000G inverter . + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway 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. + + OpenMQTTGateway 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 . +*/ +#ifndef config_GFSunInverter_h +#define config_GFSunInverter_h + +#include +#include +#include + +extern void setupGFSunInverter(); +extern void ZgatewayGFSunInverterMQTT(); + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define subjectRFtoMQTT "/GFSuntoMQTT" +#ifndef GFSUNINVERTER_DELAY +# define GFSUNINVERTER_DELAY 10000 +#endif + +// Enable to publish a whole dbus registry +// #define GFSUNINVERTER_DEVEL 1 + +#endif \ No newline at end of file diff --git a/main/main.ino b/main/main.ino index 4fedd05..ffc13a1 100644 --- a/main/main.ino +++ b/main/main.ino @@ -56,6 +56,13 @@ unsigned long timer_sys_measures = 0; StaticJsonBuffer modulesBuffer; JsonArray& modules = modulesBuffer.createArray(); +#ifndef ZgatewayGFSunInverter +// Arduino IDE compiles, it automatically creates all the header declarations for all the functions you have in your *.ino file. +// Unfortunately it ignores #if directives. +// This is a simple workaround for this problem. +struct GfSun2000Data {}; +#endif + // Modules config inclusion #if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight) || defined(ZactuatorSomfy) || defined(ZgatewayRTL_433) # include "config_RF.h" @@ -63,6 +70,9 @@ JsonArray& modules = modulesBuffer.createArray(); #ifdef ZgatewayWeatherStation # include "config_WeatherStation.h" #endif +#ifdef ZgatewayGFSunInverter +# include "config_GFSunInverter.h" +#endif #ifdef ZgatewayLORA # include "config_LORA.h" #endif @@ -653,6 +663,10 @@ void setup() { setupWeatherStation(); modules.add(ZgatewayWeatherStation); #endif +#ifdef ZgatewayGFSunInverter + setupGFSunInverter(); + modules.add(ZgatewayGFSunInverter); +#endif #ifdef ZgatewaySRFB setupSRFB(); modules.add(ZgatewaySRFB); @@ -1253,6 +1267,9 @@ void loop() { #ifdef ZgatewayWeatherStation ZgatewayWeatherStationtoMQTT(); #endif +#ifdef ZgatewayGFSunInverter + ZgatewayGFSunInverterMQTT(); +#endif #ifdef ZgatewayPilight PilighttoMQTT(); #endif diff --git a/platformio.ini b/platformio.ini index eca9118..2459089 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,6 +38,7 @@ extra_configs = ;default_envs = esp32dev-somfy-cc1101 ;default_envs = esp32dev-pilight-somfy-cc1101 ;default_envs = esp32dev-weatherstation +;default_envs = esp32dev-gf-sun-inverter ;default_envs = esp32dev-ir ;default_envs = esp32dev-ble ;default_envs = esp32dev-ble-cont @@ -120,6 +121,8 @@ stl = https://github.com/mike-matera/ArduinoSTL.git#7411816 shtc3 = https://github.com/sparkfun/SparkFun_SHTC3_Arduino_Library somfy_remote=Somfy_Remote_Lib@0.2.0 rtl_433_ESP = https://github.com/NorthernMan54/rtl_433_ESP#38ee89a +emodbus = miq19/eModbus@1.0.0 +gfSunInverter = https://github.com/BlackSmith/GFSunInverter.git#v1.0.1 [env] framework = arduino @@ -314,6 +317,18 @@ build_flags = '-DZgatewayWeatherStation="WeatherStation"' '-DGateway_Name="OpenMQTTGateway_ESP32_WeatherStation"' +[env:esp32dev-gf-sun-inverter] +platform = ${com.esp32_platform} +board = esp32dev +lib_deps = + ${com-esp.lib_deps} + ${libraries.emodbus} + ${libraries.gfSunInverter} +build_flags = + ${com-esp.build_flags} + '-DZgatewayGFSunInverter="GFSunInverter"' + '-DGateway_Name="OpenMQTTGateway_ESP32_GFSunInverter"' + [env:esp32dev-ir] platform = ${com.esp32_platform} board = esp32dev