Skip to content

Commit 23cd2a3

Browse files
BlackSmithMartin Korbel
andauthored
GridFree SUN-2000G (#926)
* Support for GridFree Sun-2000G inverter * Bump version of GFSunInverter Co-authored-by: Martin Korbel <korbel@blackserver.cz>
1 parent e7b964e commit 23cd2a3

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

main/User_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE;
193193
//#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32
194194
//#define ZgatewayPilight "Pilight" //ESP8266, Arduino, ESP32
195195
//#define ZgatewayWeatherStation "WeatherStation" //ESP8266, Arduino, ESP32
196+
//#define ZgatewayGFSunInverter "GFSunInverter" //ESP32
196197
//#define ZgatewayBT "BT" //ESP8266, ESP32
197198
//#define ZgatewayRF2 "RF2" //ESP8266, Arduino, ESP32
198199
//#define ZgatewaySRFB "SRFB" // Sonoff RF Bridge

main/ZgatewayGFSunInverter.ino

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
OpenMQTTGateway - ESP8266 or Arduino program for home automation
3+
4+
Act as a wifi or ethernet gateway between your GridFree SUN-2000G inverter and a MQTT broker.
5+
Send inverter metrics by MQTT.
6+
7+
This gateway enables to:
8+
- publish MQTT data, which are received by RS232 ModBus.
9+
10+
Library for read metrics from GWL Power, GridFree SUN-2000G inverter <https://shop.gwl.eu/GridFree-Inverters/GridFree-AC-Inverter-with-limiter-2kW-SUN-2000G-45-90V.html>.
11+
12+
This file is part of OpenMQTTGateway.
13+
14+
OpenMQTTGateway is free software: you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 3 of the License, or
17+
(at your option) any later version.
18+
19+
OpenMQTTGateway is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
You should have received a copy of the GNU General Public License
24+
along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
*/
26+
#include "User_config.h"
27+
28+
#ifdef ZgatewayGFSunInverter
29+
30+
GfSun2000 GF = GfSun2000();
31+
32+
void GFSunInverterDataHandler(GfSun2000Data data) {
33+
StaticJsonBuffer<2 * JSON_MSG_BUFFER> jsonBuffer;
34+
JsonObject& jdata = jsonBuffer.createObject();
35+
36+
jdata.set("device_id", (char*)data.deviceID);
37+
Log.trace(F("Device ID : %s\n" CR), data.deviceID);
38+
jdata.set("ac_voltage", data.ACVoltage);
39+
Log.trace(F("AC Voltage : %.1f\tV\n" CR), data.ACVoltage);
40+
jdata.set("dc_voltage", data.DCVoltage);
41+
Log.trace(F("DC Voltage : %.1f\tV\n" CR), data.DCVoltage);
42+
jdata.set("power", data.averagePower);
43+
Log.trace(F("Output Power : %.1f\tW (5min avg)\n" CR), data.averagePower);
44+
jdata.set("c_energy", data.customEnergyCounter);
45+
Log.trace(F("Custom Energy : %.1f\tkW/h (can be reseted)\n" CR), data.customEnergyCounter);
46+
jdata.set("t_energy", data.totalEnergyCounter);
47+
Log.trace(F("Total Energy : %.1f\tkW/h\n" CR), data.totalEnergyCounter);
48+
49+
# ifdef GFSUNINVERTER_DEVEL
50+
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer2;
51+
JsonObject& jregister = jsonBuffer2.createObject();
52+
char buffer[4];
53+
std::map<int16_t, int16_t>::iterator itr;
54+
for (itr = data.modbusRegistry.begin(); itr != data.modbusRegistry.end(); ++itr) {
55+
Log.notice("%d: %d\n", itr->first, itr->second);
56+
sprintf(buffer, "%d", itr->first);
57+
jregister.set(buffer, itr->second);
58+
}
59+
jdata.set("register", jregister);
60+
# endif
61+
pub(subjectRFtoMQTT, jdata);
62+
}
63+
64+
void GFSunInverterErrorHandler(int errorId, char* errorMessage) {
65+
char buffer[50];
66+
sprintf(buffer, "Error response: %02X - %s\n", errorId, errorMessage);
67+
Log.error(buffer);
68+
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
69+
JsonObject& jdata = jsonBuffer.createObject();
70+
jdata.set("status", "error");
71+
jdata.set("msg", errorMessage);
72+
jdata.set("id", errorId);
73+
pub(subjectRFtoMQTT, jdata);
74+
}
75+
76+
void setupGFSunInverter() {
77+
GF.setup(Serial2);
78+
GF.setDataHandler(GFSunInverterDataHandler);
79+
GF.setErrorHandler(GFSunInverterErrorHandler);
80+
Log.trace(F("ZgatewayGFSunInverter setup done " CR));
81+
}
82+
83+
void ZgatewayGFSunInverterMQTT() {
84+
GF.readData();
85+
delay(GFSUNINVERTER_DELAY);
86+
}
87+
88+
#endif

main/config_GFSunInverter.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
OpenMQTTGateway - ESP8266 or Arduino program for home automation
3+
4+
Act as a wifi or ethernet gateway between your GridFree SUN-2000G inverter and a MQTT broker.
5+
Send inverter metrics by MQTT.
6+
7+
This gateway enables to:
8+
- publish MQTT data, which are received by RS232 ModBus.
9+
10+
Library for read metrics from GWL Power, GridFree SUN-2000G inverter <https://shop.gwl.eu/GridFree-Inverters/GridFree-AC-Inverter-with-limiter-2kW-SUN-2000G-45-90V.html>.
11+
12+
This file is part of OpenMQTTGateway.
13+
14+
OpenMQTTGateway is free software: you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License as published by
16+
the Free Software Foundation, either version 3 of the License, or
17+
(at your option) any later version.
18+
19+
OpenMQTTGateway is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
GNU General Public License for more details.
23+
24+
You should have received a copy of the GNU General Public License
25+
along with this program. If not, see <http://www.gnu.org/licenses/>.
26+
*/
27+
#ifndef config_GFSunInverter_h
28+
#define config_GFSunInverter_h
29+
30+
#include <Arduino.h>
31+
#include <ArduinoJson.h>
32+
#include <GfSun2000.h>
33+
34+
extern void setupGFSunInverter();
35+
extern void ZgatewayGFSunInverterMQTT();
36+
37+
/*----------------------------USER PARAMETERS-----------------------------*/
38+
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
39+
#define subjectRFtoMQTT "/GFSuntoMQTT"
40+
#ifndef GFSUNINVERTER_DELAY
41+
# define GFSUNINVERTER_DELAY 10000
42+
#endif
43+
44+
// Enable to publish a whole dbus registry
45+
// #define GFSUNINVERTER_DEVEL 1
46+
47+
#endif

main/main.ino

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@ unsigned long timer_sys_measures = 0;
5656
StaticJsonBuffer<JSON_MSG_BUFFER> modulesBuffer;
5757
JsonArray& modules = modulesBuffer.createArray();
5858

59+
#ifndef ZgatewayGFSunInverter
60+
// Arduino IDE compiles, it automatically creates all the header declarations for all the functions you have in your *.ino file.
61+
// Unfortunately it ignores #if directives.
62+
// This is a simple workaround for this problem.
63+
struct GfSun2000Data {};
64+
#endif
65+
5966
// Modules config inclusion
6067
#if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight) || defined(ZactuatorSomfy) || defined(ZgatewayRTL_433)
6168
# include "config_RF.h"
6269
#endif
6370
#ifdef ZgatewayWeatherStation
6471
# include "config_WeatherStation.h"
6572
#endif
73+
#ifdef ZgatewayGFSunInverter
74+
# include "config_GFSunInverter.h"
75+
#endif
6676
#ifdef ZgatewayLORA
6777
# include "config_LORA.h"
6878
#endif
@@ -653,6 +663,10 @@ void setup() {
653663
setupWeatherStation();
654664
modules.add(ZgatewayWeatherStation);
655665
#endif
666+
#ifdef ZgatewayGFSunInverter
667+
setupGFSunInverter();
668+
modules.add(ZgatewayGFSunInverter);
669+
#endif
656670
#ifdef ZgatewaySRFB
657671
setupSRFB();
658672
modules.add(ZgatewaySRFB);
@@ -1253,6 +1267,9 @@ void loop() {
12531267
#ifdef ZgatewayWeatherStation
12541268
ZgatewayWeatherStationtoMQTT();
12551269
#endif
1270+
#ifdef ZgatewayGFSunInverter
1271+
ZgatewayGFSunInverterMQTT();
1272+
#endif
12561273
#ifdef ZgatewayPilight
12571274
PilighttoMQTT();
12581275
#endif

platformio.ini

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extra_configs =
3838
;default_envs = esp32dev-somfy-cc1101
3939
;default_envs = esp32dev-pilight-somfy-cc1101
4040
;default_envs = esp32dev-weatherstation
41+
;default_envs = esp32dev-gf-sun-inverter
4142
;default_envs = esp32dev-ir
4243
;default_envs = esp32dev-ble
4344
;default_envs = esp32dev-ble-cont
@@ -120,6 +121,8 @@ stl = https://github.com/mike-matera/ArduinoSTL.git#7411816
120121
shtc3 = https://github.com/sparkfun/SparkFun_SHTC3_Arduino_Library
121122
somfy_remote=Somfy_Remote_Lib@0.2.0
122123
rtl_433_ESP = https://github.com/NorthernMan54/rtl_433_ESP#38ee89a
124+
emodbus = miq19/eModbus@1.0.0
125+
gfSunInverter = https://github.com/BlackSmith/GFSunInverter.git#v1.0.1
123126

124127
[env]
125128
framework = arduino
@@ -314,6 +317,18 @@ build_flags =
314317
'-DZgatewayWeatherStation="WeatherStation"'
315318
'-DGateway_Name="OpenMQTTGateway_ESP32_WeatherStation"'
316319

320+
[env:esp32dev-gf-sun-inverter]
321+
platform = ${com.esp32_platform}
322+
board = esp32dev
323+
lib_deps =
324+
${com-esp.lib_deps}
325+
${libraries.emodbus}
326+
${libraries.gfSunInverter}
327+
build_flags =
328+
${com-esp.build_flags}
329+
'-DZgatewayGFSunInverter="GFSunInverter"'
330+
'-DGateway_Name="OpenMQTTGateway_ESP32_GFSunInverter"'
331+
317332
[env:esp32dev-ir]
318333
platform = ${com.esp32_platform}
319334
board = esp32dev

0 commit comments

Comments
 (0)