forked from andresarmento/modbus-esp8266
-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathclientSync.ino
55 lines (44 loc) · 1.33 KB
/
clientSync.ino
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
/*
Modbus Library for Arduino Example - Modbus IP Client (ESP8266/ESP32)
Read Holding Register from Modbus Server in blocking way
(c)2020 Alexander Emelianov (a.m.emelianov@gmail.com)
https://github.com/emelianov/modbus-esp8266
*/
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#else
#error "Unsupported platform"
#endif
#include <ModbusTCP.h>
const int REG = 528; // Modbus Hreg Offset
IPAddress remote(192, 168, 30, 13); // Address of Modbus Slave device
ModbusIP mb; //ModbusTCP object
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
mb.client();
}
uint16_t res = 0;
void loop() {
if (mb.isConnected(remote)) { // Check if connection to Modbus Slave is established
uint16_t trans = mb.readHreg(remote, REG, &res); // Initiate Read Hreg from Modbus Server
while(mb.isTransaction(trans)) { // Check if transaction is active
mb.task();
delay(10);
}
Serial.println(res); // At this point res is filled with responce value
} else {
mb.connect(remote); // Try to connect if no connection
}
delay(100);
}