1 | #include <Ethernet.h>
|
2 | #include <ArduinoModbus.h>
|
3 |
|
4 | // Enter a MAC address for your controller below.
|
5 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
6 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
7 |
|
8 | EthernetClient ethClient;
|
9 | ModbusTCPClient modbus(ethClient);
|
10 |
|
11 | const IPAddress serverAddress(192,168,0,33);
|
12 | const byte CONSUMPTION_METER_UID = 241;
|
13 | unsigned int AC_POWER_REGISTER_ADDR = 40087;
|
14 | unsigned int AC_POWER_SF_REGISTER_ADDR = 40091;
|
15 | const byte AC_POWER_REQUEST_LENGTH = AC_POWER_SF_REGISTER_ADDR - AC_POWER_REGISTER_ADDR + 1;
|
16 |
|
17 | void setup() {
|
18 | Serial.begin(115200);
|
19 |
|
20 | // start the Ethernet connection:
|
21 | Serial.println("Initialize Ethernet with DHCP:");
|
22 | Ethernet.init(10);
|
23 | if (Ethernet.begin(mac) == 0) {
|
24 | Serial.println("Failed to configure Ethernet using DHCP");
|
25 | while (true) {
|
26 | delay(1); // do nothing, no point running without Ethernet hardware
|
27 | }
|
28 | } else {
|
29 | Serial.print(" DHCP assigned IP ");
|
30 | Serial.println(Ethernet.localIP());
|
31 | }
|
32 |
|
33 | }
|
34 |
|
35 | void loop() {
|
36 | int err = modbusConnection();
|
37 | if (err != 0) {
|
38 | Serial.println(err);
|
39 | Serial.println("ich kann mich nicht verbunden");
|
40 | } else {
|
41 | Serial.println("ich bin verbunden");
|
42 |
|
43 | int l = modbus.requestFrom(3, INPUT_REGISTERS, 30867, 2);
|
44 | if (l != 2) {
|
45 | Serial.println(modbus.lastError());
|
46 | } else {
|
47 | short daten[2];
|
48 | daten[0] = modbus.read();
|
49 | daten[1] = modbus.read();
|
50 | /*
|
51 | Serial.println("ersten 16bit");
|
52 | Serial.println(daten[0]);
|
53 | Serial.println("zweiten 16bit");
|
54 | */
|
55 | Serial.println(daten[1]);
|
56 | }
|
57 |
|
58 | }
|
59 | delay(1000);
|
60 | }
|
61 |
|
62 | int modbusConnection() {
|
63 | if (!modbus.connected()) {
|
64 | modbus.stop();
|
65 | if (!modbus.begin(serverAddress, 502))
|
66 | return -1;
|
67 | }
|
68 | return 0;
|
69 | }
|