-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Stefany
committed
Jun 11, 2021
1 parent
2abc74c
commit 11b02ca
Showing
9 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,9 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
// See http://go.microsoft.com/fwlink/?LinkId=827846 | ||
// for the documentation about the extensions.json format | ||
"recommendations": [ | ||
"platformio.platformio-ide" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
# EastronMITM | ||
Eastron SDM630 Modbus (Man) In The Middle | ||
|
||
## Origin | ||
I have a small Solar Power Plant constructed from InfiniSolar 10 kW Hybrid Inverter, Hoepecke battery and Axitec solar panels. | ||
Inverter uses Eastron SDM630 Modbus electricity power meter to measure feed-in to the grid and balance phases. | ||
This is done via RS485 Modbus RTU protocol (InfiniSolar as Modbus Master, Eastron as Modbus slave). SDM630 measures various interesting values, | ||
but these cannot be access since Modbus is single-master multi-slave only. | ||
This prototype acts as a "man-in-the-middle": | ||
- reads all registers from Eastron SDM630 meter and stores them in local registers | ||
- these are then accessible to both InfiniSolar via separate Modbus RTU serial interface | ||
and Modbus TCP clients |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
This directory is intended for project header files. | ||
|
||
A header file is a file containing C declarations and macro definitions | ||
to be shared between several project source files. You request the use of a | ||
header file in your project source file (C, C++, etc) located in `src` folder | ||
by including it, with the C preprocessing directive `#include'. | ||
|
||
```src/main.c | ||
|
||
#include "header.h" | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
Including a header file produces the same results as copying the header file | ||
into each source file that needs it. Such copying would be time-consuming | ||
and error-prone. With a header file, the related declarations appear | ||
in only one place. If they need to be changed, they can be changed in one | ||
place, and programs that include the header file will automatically use the | ||
new version when next recompiled. The header file eliminates the labor of | ||
finding and changing all the copies as well as the risk that a failure to | ||
find one copy will result in inconsistencies within a program. | ||
|
||
In C, the usual convention is to give header files names that end with `.h'. | ||
It is most portable to use only letters, digits, dashes, and underscores in | ||
header file names, and at most one dot. | ||
|
||
Read more about using header files in official GCC documentation: | ||
|
||
* Include Syntax | ||
* Include Operation | ||
* Once-Only Headers | ||
* Computed Includes | ||
|
||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
This directory is intended for project specific (private) libraries. | ||
PlatformIO will compile them to static libraries and link into executable file. | ||
|
||
The source code of each library should be placed in a an own separate directory | ||
("lib/your_library_name/[here are source files]"). | ||
|
||
For example, see a structure of the following two libraries `Foo` and `Bar`: | ||
|
||
|--lib | ||
| | | ||
| |--Bar | ||
| | |--docs | ||
| | |--examples | ||
| | |--src | ||
| | |- Bar.c | ||
| | |- Bar.h | ||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | ||
| | | ||
| |--Foo | ||
| | |- Foo.c | ||
| | |- Foo.h | ||
| | | ||
| |- README --> THIS FILE | ||
| | ||
|- platformio.ini | ||
|--src | ||
|- main.c | ||
|
||
and a contents of `src/main.c`: | ||
``` | ||
#include <Foo.h> | ||
#include <Bar.h> | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
|
||
``` | ||
|
||
PlatformIO Library Dependency Finder will find automatically dependent | ||
libraries scanning project source files. | ||
|
||
More information about PlatformIO Library Dependency Finder | ||
- https://docs.platformio.org/page/librarymanager/ldf.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:nodemcu-32s] | ||
platform = espressif32 | ||
board = nodemcu-32s | ||
framework = arduino | ||
lib_deps = emelianov/[email protected] | ||
monitor_speed = 115200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
#include <Arduino.h> | ||
#include <HardwareSerial.h> | ||
#include <ModbusIP_ESP8266.h> | ||
#include <ModbusRTU.h> | ||
#include <WiFi.h> | ||
|
||
// https://support.innon.com/PowerMeters/SDM630-MOD-MID/Manual/SDM630-Modbus_Protocol.pdf | ||
#define EASTRON_SLAVE_ID 2 | ||
#define INFINISOLAR_SLAVE_ID 2 | ||
#define EASTRON_NUM_REGS 382 | ||
|
||
#define WIFI_SSID "smarthome2_IoT" | ||
#define WIFI_PASSWORD "strongpassword" | ||
#define WIFI_TIMEOUT_MS 10000 | ||
|
||
IPAddress esp_ip(192, 168, 2, 49); | ||
IPAddress esp_mask(255, 255, 255, 0); | ||
IPAddress esp_gw(192, 168, 2, 1); | ||
IPAddress esp_dns1(192, 168, 2, 2); | ||
|
||
HardwareSerial EastronSerial(1); | ||
HardwareSerial InfiniSolarSerial(2); | ||
|
||
ModbusRTU EastronModbus; | ||
ModbusRTU InfinisolarModbus; | ||
ModbusIP EspModbus; | ||
|
||
BaseType_t xWiFiReturned; | ||
TaskHandle_t xWifiTaskHandle = NULL; | ||
|
||
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { | ||
if (event != Modbus::EX_SUCCESS) { | ||
Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap()); | ||
} | ||
return true; | ||
} | ||
|
||
bool cbSomebodyConnected(IPAddress ip) { | ||
Serial.print("New TCP client connected: "); | ||
Serial.println(ip); | ||
return true; | ||
} | ||
|
||
void keepWifiTask(void *pvParameters) { | ||
for(;;) { | ||
if (WiFi.status() == WL_CONNECTED) { | ||
vTaskDelay(10000 / portTICK_PERIOD_MS); | ||
continue; | ||
} | ||
|
||
WiFi.config(esp_ip, esp_gw, esp_mask, esp_dns1); | ||
|
||
Serial.print("Attempting to connect to SSID: "); | ||
Serial.println(WIFI_SSID); | ||
|
||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
|
||
unsigned long startAttemptTime = millis(); | ||
|
||
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT_MS) {} | ||
|
||
if (WiFi.status() != WL_CONNECTED) { | ||
Serial.println("Wi-Fi connection failed"); | ||
vTaskDelay(20000 / portTICK_PERIOD_MS); | ||
continue; | ||
} | ||
|
||
Serial.println("WiFi connected"); | ||
Serial.print("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
if (eTaskGetState(xWifiTaskHandle) == eSuspended) { | ||
vTaskResume(xWifiTaskHandle); | ||
} | ||
} | ||
} | ||
|
||
void eastronTask(void *pvParameters) { | ||
// Modbus RTU to power meter | ||
EastronSerial.begin(19200, SERIAL_8N1, 22, 23); | ||
EastronModbus.begin(&EastronSerial); | ||
EastronModbus.master(); | ||
|
||
for(;;) { | ||
if (!EastronModbus.slave()) { | ||
EastronModbus.pullIreg(EASTRON_SLAVE_ID, 0, 0, 80, cb); | ||
while(EastronModbus.slave()) { | ||
EastronModbus.task(); | ||
delay(10); | ||
} | ||
|
||
EastronModbus.pullIreg(EASTRON_SLAVE_ID, 80, 80, 26, cb); | ||
while(EastronModbus.slave()) { | ||
EastronModbus.task(); | ||
delay(10); | ||
} | ||
|
||
EastronModbus.pullIreg(EASTRON_SLAVE_ID, 200, 200, 8, cb); | ||
while(EastronModbus.slave()) { | ||
EastronModbus.task(); | ||
delay(10); | ||
} | ||
|
||
EastronModbus.pullIreg(EASTRON_SLAVE_ID, 224, 224, 46, cb); | ||
while(EastronModbus.slave()) { | ||
EastronModbus.task(); | ||
delay(10); | ||
} | ||
|
||
EastronModbus.pullIreg(EASTRON_SLAVE_ID, 334, 334, 48, cb); | ||
while(EastronModbus.slave()) { | ||
EastronModbus.task(); | ||
delay(10); | ||
} | ||
} | ||
vTaskDelay(1000 / portTICK_PERIOD_MS); | ||
} | ||
} | ||
|
||
void infiniTask(void *pvParameters) { | ||
// Modbus RTU to inverter | ||
InfiniSolarSerial.begin(19200, SERIAL_8N1, 18, 19); | ||
InfinisolarModbus.begin(&InfiniSolarSerial); | ||
InfinisolarModbus.slave(INFINISOLAR_SLAVE_ID); | ||
InfinisolarModbus.addIreg(0, 0, EASTRON_NUM_REGS); | ||
|
||
for(;;) { | ||
InfinisolarModbus.task(); | ||
delay(10); | ||
} | ||
} | ||
|
||
void espTask(void *pvParameters) { | ||
// Modbus IP for HA | ||
EspModbus.onConnect(cbSomebodyConnected); | ||
EspModbus.server(); | ||
EspModbus.addIreg(0, 0, EASTRON_NUM_REGS); | ||
|
||
for(;;) { | ||
EspModbus.task(); | ||
delay(10); | ||
} | ||
} | ||
|
||
void setup() { | ||
// serial console | ||
Serial.begin(115200); | ||
|
||
xTaskCreatePinnedToCore( | ||
keepWifiTask, | ||
"WifiTask", | ||
5000, | ||
NULL, | ||
1, | ||
NULL, | ||
CONFIG_ARDUINO_RUNNING_CORE | ||
); | ||
|
||
xTaskCreate( | ||
infiniTask, | ||
"InfiniSolar", | ||
1000, | ||
NULL, | ||
1, | ||
NULL | ||
); | ||
|
||
xTaskCreatePinnedToCore( | ||
eastronTask, | ||
"Eastron", | ||
1000, | ||
NULL, | ||
2, | ||
NULL, | ||
CONFIG_ARDUINO_RUNNING_CORE | ||
); | ||
|
||
xWiFiReturned = xTaskCreate( | ||
espTask, | ||
"ModbusTCP", | ||
5000, | ||
NULL, | ||
1, | ||
&xWifiTaskHandle | ||
); | ||
|
||
if (xWiFiReturned == pdPASS) { | ||
vTaskSuspend(xWifiTaskHandle); | ||
} | ||
} | ||
|
||
void loop() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
This directory is intended for PlatformIO Unit Testing and project tests. | ||
|
||
Unit Testing is a software testing method by which individual units of | ||
source code, sets of one or more MCU program modules together with associated | ||
control data, usage procedures, and operating procedures, are tested to | ||
determine whether they are fit for use. Unit testing finds problems early | ||
in the development cycle. | ||
|
||
More information about PlatformIO Unit Testing: | ||
- https://docs.platformio.org/page/plus/unit-testing.html |