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
+ }
0 commit comments