-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMODBUS_TCP_WITH_WT32.ino
More file actions
91 lines (70 loc) · 1.73 KB
/
MODBUS_TCP_WITH_WT32.ino
File metadata and controls
91 lines (70 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <ETH.h>
#include <WiFi.h>
#include <ModbusIP_ESP8266.h>
ModbusIP mb;
// Static IP configuration
IPAddress local_IP(192, 168, 0, 88); // ← your WT32 IP
IPAddress gateway(192, 168, 0, 1); // ← your router/PLC gateway
IPAddress subnet(255, 255, 255, 0);
const int REG_FLOAT = 0;
float value = 0.01;
float value2 = 1000.0;
void writeFloatToHreg(int reg, float val, float val2) {
union {
float f;
uint16_t w[2];
} data;
union {
float f;
uint16_t w[2];
} data2;
data.f = val;
data2.f = val2;
mb.Hreg(reg + 0, data.w[1]); // High word
Serial.print("HIGH W1: ");
Serial.print(data.w[0]);
Serial.print(" - HIGH W2 ");
Serial.print(data2.w[0]);
Serial.print(" -||- ");
mb.Hreg(reg + 1, data.w[0]); // Low word
Serial.print("LOW W1: ");
Serial.print(data.w[1]);
Serial.print(" - LOW W2: ");
mb.Hreg(reg + 2, data2.w[1]);
mb.Hreg(reg + 3, data2.w[0]);
Serial.print(data2.w[1]);
Serial.print(" -||- ");
Serial.print("ACTUAL VAL1: ");
Serial.print(val);
Serial.print(" - VAL2: ");
Serial.print(val2);
Serial.println(" ");
}
void setup() {
Serial.begin(115200);
// Start Ethernet
ETH.begin();
if (!ETH.config(local_IP, gateway, subnet)) {
Serial.println("Static IP configuration failed!");
}
// Wait for link
while (!ETH.linkUp()) {
delay(100);
}
Serial.print("Static IP: ");
Serial.println(ETH.localIP());
// Start Modbus TCP
mb.server();
mb.addHreg(REG_FLOAT, 0);
mb.addHreg(REG_FLOAT + 1, 0);
mb.addHreg(REG_FLOAT + 2, 0);
mb.addHreg(REG_FLOAT + 3, 0);
}
void loop() {
value += 0.01;
if (value > 1000.0) value = 0.01;
value2 = 1000 - value;
writeFloatToHreg(REG_FLOAT, value, value2);
mb.task();
delay(1000);
}