Skip to content

Commit fa2bd80

Browse files
committed
added examples T1S
1 parent 7b7b826 commit fa2bd80

File tree

8 files changed

+768
-0
lines changed

8 files changed

+768
-0
lines changed

Diff for: examples/T1S/ModbusT1SClient/ModbusT1SClient.ino

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Modbus T1S Client Toggle
3+
4+
This sketch demonstrates how to send commands to a Modbus T1S server connected
5+
via T1S Single Pair Ethernet.
6+
7+
Circuit:
8+
- T1S shield
9+
- Uno WiFi R4
10+
*/
11+
12+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
13+
#include <ArduinoModbus.h>
14+
#include "arduino_secrets.h"
15+
/**************************************************************************************
16+
CONSTANTS
17+
**************************************************************************************/
18+
static uint8_t const T1S_PLCA_NODE_ID = 2;
19+
20+
static IPAddress const ip_addr {
21+
192, 168, 42, 100 + T1S_PLCA_NODE_ID
22+
};
23+
static IPAddress const network_mask {
24+
255, 255, 255, 0
25+
};
26+
static IPAddress const gateway {
27+
192, 168, 42, 100
28+
};
29+
30+
static T1SPlcaSettings const t1s_plca_settings {
31+
T1S_PLCA_NODE_ID
32+
};
33+
static T1SMacSettings const t1s_default_mac_settings;
34+
35+
static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 0};
36+
37+
/**************************************************************************************
38+
GLOBAL VARIABLES
39+
**************************************************************************************/
40+
auto const tc6_io = new TC6::TC6_Io
41+
( SPI
42+
, CS_PIN
43+
, RESET_PIN
44+
, IRQ_PIN);
45+
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
46+
Arduino_10BASE_T1S_UDP udp_client;
47+
48+
49+
void setup() {
50+
Serial.begin(115200);
51+
while (!Serial);
52+
53+
Serial.println("Modbus T1S Client Toggle");
54+
55+
/* Initialize digital IO interface for interfacing
56+
with the LAN8651.
57+
*/
58+
pinMode(IRQ_PIN, INPUT_PULLUP);
59+
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),
60+
[]() {
61+
tc6_io->onInterrupt();
62+
},
63+
FALLING);
64+
65+
/* Initialize IO module. */
66+
if (!tc6_io->begin())
67+
{
68+
Serial.println("'tc6_io::begin(...)' failed.");
69+
for (;;) { }
70+
}
71+
72+
MacAddress const mac_addr = MacAddress::create_from_uid();
73+
74+
if (!tc6_inst->begin(ip_addr
75+
, network_mask
76+
, gateway
77+
, mac_addr
78+
, t1s_plca_settings
79+
, t1s_default_mac_settings))
80+
{
81+
Serial.println("'TC6::begin(...)' failed.");
82+
for (;;) { }
83+
}
84+
85+
Serial.print("IP\t");
86+
Serial.println(ip_addr);
87+
Serial.println(mac_addr);
88+
Serial.println(t1s_plca_settings);
89+
Serial.println(t1s_default_mac_settings);
90+
91+
if (!udp_client.begin(UDP_CLIENT_PORT))
92+
{
93+
Serial.println("begin(...) failed for UDP client");
94+
for (;;) { }
95+
}
96+
97+
/* A0 -> LOCAL_ENABLE -> DO NOT feed power from board to network. */
98+
tc6_inst->digitalWrite(TC6::DIO::A0, false);
99+
/* A1 -> T1S_DISABLE -> Open the switch connecting network to board by pulling EN LOW. */
100+
tc6_inst->digitalWrite(TC6::DIO::A1, false);
101+
102+
ModbusT1SClient.setServerIp(UDP_SERVER_IP_ADDR);
103+
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
104+
ModbusT1SClient.setModbusId(MODBUS_ID);
105+
106+
Serial.println("UDP_Client");
107+
}
108+
109+
void loop() {
110+
tc6_inst->service();
111+
112+
static unsigned long prev_beacon_check = 0;
113+
static unsigned long prev_udp_packet_sent = 0;
114+
115+
auto const now = millis();
116+
117+
if ((now - prev_beacon_check) > 1000)
118+
{
119+
prev_beacon_check = now;
120+
if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) {
121+
Serial.println("getPlcaStatus(...) failed");
122+
}
123+
}
124+
// for (slave) id 1: write the value of 0x01, to the coil at address 0x00
125+
int res = ModbusT1SClient.coilRead(0x00, &udp_client, UDP_READ_COIL_PORT);
126+
127+
if (res == -1) {
128+
Serial.println("Failed to read coil! ");
129+
} else {
130+
Serial.print("Coil value: ");
131+
Serial.println(res);
132+
}
133+
134+
res = ModbusT1SClient.coilWrite(0x00, 1, &udp_client, UDP_WRITE_COIL_PORT);
135+
if (res == -1) {
136+
Serial.println("Failed to write coil! ");
137+
} else {
138+
Serial.println("write done");
139+
}
140+
141+
res = ModbusT1SClient.inputRegisterRead(0x00, &udp_client, UDP_READ_IR_PORT);
142+
if (res == -1) {
143+
Serial.println("Failed to read Input Register! ");
144+
} else {
145+
Serial.print("Input Register value: ");
146+
Serial.println(res);
147+
}
148+
149+
}
150+
151+
static void OnPlcaStatus(bool success, bool plcaStatus)
152+
{
153+
if (!success)
154+
{
155+
Serial.println("PLCA status register read failed");
156+
return;
157+
}
158+
159+
if (plcaStatus) {
160+
Serial.println("PLCA Mode active");
161+
} else {
162+
Serial.println("CSMA/CD fallback");
163+
tc6_inst->enablePlca();
164+
}
165+
}

Diff for: examples/T1S/ModbusT1SClient/arduino_secrets.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
static uint16_t const UDP_CLIENT_PORT = 8888;
2+
static uint16_t const UDP_SERVER_PORT = 8889;
3+
#define UDP_READ_COIL_PORT 1
4+
#define UDP_WRITE_COIL_PORT 2
5+
#define UDP_READ_DI_PORT 3
6+
#define UDP_READ_IR_PORT 4
7+
#define UDP_READ_HR_PORT 5
8+
#define UDP_WRITE_HR_PORT 6
9+
#define MODBUS_ID 42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Modbus T1S Client Temperature Humidity sensor
3+
4+
This sketch creates a Modbus T1S Client and demonstrates
5+
how to use the ModbusT1S API to communicate.
6+
- Arduino Uno Wifi R4
7+
- T1S shield
8+
- SPE ethernet connected to the client
9+
- all the terminations placed on the hardware
10+
*/
11+
12+
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
13+
#include <ArduinoModbus.h>
14+
#include "arduino_secrets.h"
15+
/**************************************************************************************
16+
CONSTANTS
17+
**************************************************************************************/
18+
static uint8_t const T1S_PLCA_NODE_ID = 2;
19+
20+
static IPAddress const ip_addr {
21+
192, 168, 42, 100 + T1S_PLCA_NODE_ID
22+
};
23+
static IPAddress const network_mask {
24+
255, 255, 255, 0
25+
};
26+
static IPAddress const gateway {
27+
192, 168, 42, 100
28+
};
29+
30+
static T1SPlcaSettings const t1s_plca_settings {
31+
T1S_PLCA_NODE_ID
32+
};
33+
static T1SMacSettings const t1s_default_mac_settings;
34+
35+
static IPAddress const UDP_SERVER_IP_ADDR = {192, 168, 42, 100 + 0};
36+
37+
/**************************************************************************************
38+
GLOBAL VARIABLES
39+
**************************************************************************************/
40+
auto const tc6_io = new TC6::TC6_Io
41+
( SPI
42+
, CS_PIN
43+
, RESET_PIN
44+
, IRQ_PIN);
45+
auto const tc6_inst = new TC6::TC6_Arduino_10BASE_T1S(tc6_io);
46+
Arduino_10BASE_T1S_UDP udp_client;
47+
48+
49+
void setup() {
50+
Serial.begin(115200);
51+
while (!Serial);
52+
53+
Serial.println("Modbus T1S Client Toggle");
54+
55+
/* Initialize digital IO interface for interfacing
56+
with the LAN8651.
57+
*/
58+
pinMode(IRQ_PIN, INPUT_PULLUP);
59+
attachInterrupt(digitalPinToInterrupt(IRQ_PIN),
60+
[]() {
61+
tc6_io->onInterrupt();
62+
},
63+
FALLING);
64+
65+
/* Initialize IO module. */
66+
if (!tc6_io->begin())
67+
{
68+
Serial.println("'tc6_io::begin(...)' failed.");
69+
for (;;) { }
70+
}
71+
72+
MacAddress const mac_addr = MacAddress::create_from_uid();
73+
74+
if (!tc6_inst->begin(ip_addr
75+
, network_mask
76+
, gateway
77+
, mac_addr
78+
, t1s_plca_settings
79+
, t1s_default_mac_settings))
80+
{
81+
Serial.println("'TC6::begin(...)' failed.");
82+
for (;;) { }
83+
}
84+
85+
Serial.print("IP\t");
86+
Serial.println(ip_addr);
87+
Serial.println(mac_addr);
88+
Serial.println(t1s_plca_settings);
89+
Serial.println(t1s_default_mac_settings);
90+
91+
if (!udp_client.begin(UDP_CLIENT_PORT))
92+
{
93+
Serial.println("begin(...) failed for UDP client");
94+
for (;;) { }
95+
}
96+
97+
/* A0 -> LOCAL_ENABLE -> DO NOT feed power from board to network. */
98+
tc6_inst->digitalWrite(TC6::DIO::A0, false);
99+
/* A1 -> T1S_DISABLE -> Open the switch connecting network to board by pulling EN LOW. */
100+
tc6_inst->digitalWrite(TC6::DIO::A1, true);
101+
102+
ModbusT1SClient.setServerIp(UDP_SERVER_IP_ADDR);
103+
ModbusT1SClient.setServerPort(UDP_SERVER_PORT);
104+
105+
106+
Serial.println("UDP_Client");
107+
}
108+
109+
unsigned long start = 0;
110+
void loop() {
111+
tc6_inst->service();
112+
113+
static unsigned long prev_beacon_check = 0;
114+
static unsigned long prev_udp_packet_sent = 0;
115+
116+
auto const now = millis();
117+
118+
if ((now - prev_beacon_check) > 1000)
119+
{
120+
prev_beacon_check = now;
121+
if (!tc6_inst->getPlcaStatus(OnPlcaStatus)) {
122+
Serial.println("getPlcaStatus(...) failed");
123+
}
124+
}
125+
126+
if ((millis() - start) > 1000)
127+
{
128+
int res = ModbusT1SClient.inputRegisterRead(1, 0x01, &udp_client, UDP_READ_IR_PORT);
129+
if (res == -1) {
130+
Serial.println("Failed to read temperature! ");
131+
} else {
132+
int16_t const temperature_raw =res;
133+
float const temperature_deg = temperature_raw / 10.f;
134+
Serial.print("Temperature: ");
135+
Serial.println(temperature_deg);
136+
}
137+
138+
res = ModbusT1SClient.inputRegisterRead(1, 0x02, &udp_client, UDP_READ_IR_PORT);
139+
if (res == -1) {
140+
Serial.println("Failed to read humidity! ");
141+
} else {
142+
int16_t const humidity_raw = res;
143+
float const humidity_per_cent = humidity_raw / 10.f;
144+
Serial.print("Humidity: ");
145+
Serial.println(humidity_per_cent);
146+
}
147+
start = millis();
148+
}
149+
}
150+
151+
static void OnPlcaStatus(bool success, bool plcaStatus)
152+
{
153+
if (!success)
154+
{
155+
Serial.println("PLCA status register read failed");
156+
return;
157+
}
158+
159+
if (plcaStatus) {
160+
Serial.println("PLCA Mode active");
161+
} else {
162+
Serial.println("CSMA/CD fallback");
163+
tc6_inst->enablePlca();
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
static uint16_t const UDP_CLIENT_PORT = 8888;
2+
static uint16_t const UDP_SERVER_PORT = 8889;
3+
#define UDP_READ_COIL_PORT 1
4+
#define UDP_WRITE_COIL_PORT 2
5+
#define UDP_READ_DI_PORT 3
6+
#define UDP_READ_IR_PORT 4
7+
#define UDP_READ_HR_PORT 5
8+
#define UDP_WRITE_HR_PORT 6

0 commit comments

Comments
 (0)