-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_ethernet.ino
233 lines (213 loc) · 5.29 KB
/
connection_ethernet.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Settings Ethernet connection
* MQTT via Ethernet
*/
#include <SPI.h>
#include <EthernetSPI2.h>
#include <EthernetUdp.h>
#include <PubSubClient.h>
#include <stdlib.h>
EthernetClient ethClient;
PubSubClient mqttClientEthernet(ethClient);
// A UDP instance to let us send and receive packets over UDP.
EthernetUDP ethernetUdp;
/*
* Wiz W5500 reset function. Change this for the specific reset
* sequence required for your particular board or module.
*/
void wizReset()
{
Serial.print("Resetting Wiz W5500 Ethernet Board... ");
pinMode(ETHERNET_PIN_RST, OUTPUT);
digitalWrite(ETHERNET_PIN_RST, HIGH);
delay(250);
digitalWrite(ETHERNET_PIN_RST, LOW);
delay(50);
digitalWrite(ETHERNET_PIN_RST, HIGH);
delay(350);
Serial.println("Done.");
}
/*
* This is a crock. It's here in an effort
* to help people debug hardware problems with
* their W5100 ~ W5500 board setups. It's
* a copy of the Ethernet library enums and
* should, at the very least, be regenerated
* from Ethernet.h automatically before the
* compile starts (that's a TODO item).
*
*/
/*
* Print the result of the hardware status enum
* as a string.
* Ethernet.h currently contains these values:-
*
* enum EthernetHardwareStatus {
* EthernetNoHardware,
* EthernetW5100,
* EthernetW5200,
* EthernetW5500
* };
*
*/
void prt_hwval(uint8_t refval)
{
switch (refval)
{
case 0:
Serial.println("No hardware detected.");
break;
case 1:
Serial.println("WizNet W5100 detected.");
break;
case 2:
Serial.println("WizNet W5200 detected.");
break;
case 3:
Serial.println("WizNet W5500 detected.");
break;
default:
Serial.println
("UNKNOWN - Update espnow_gw.ino to match Ethernet.h");
}
}
/*
* Print the result of the ethernet connection
* status enum as a string.
* Ethernet.h currently contains these values:-
*
* enum EthernetLinkStatus {
* Unknown,
* LinkON,
* LinkOFF
* };
*
*/
void prt_ethval(uint8_t refval) {
switch (refval)
{
case 0:
Serial.println("Uknown status.");
break;
case 1:
Serial.println("Link flagged as UP.");
break;
case 2:
Serial.println("Link flagged as DOWN. Check cable connection.");
break;
default:
Serial.println
("UNKNOWN - Update espnow_gw.ino to match Ethernet.h");
}
}
void setupEthernet()
{
delay(500);
Serial.println("\n\tUDP NTP Client v3.0\r\n");
// Use Ethernet.init(pin) to configure the CS pin.
Ethernet.init(ETHERNET_PIN_SPI_CS);
wizReset();
/*
* Network configuration - all except the MAC are optional.
*
* IMPORTANT NOTE - The mass-produced W5500 boards do -not-
* have a built-in MAC address (depite
* comments to the contrary elsewhere). You
* -must- supply a MAC address here.
*/
Serial.println("Starting ETHERNET connection...");
Ethernet.begin(MAC_ADDRESS, IP, DNS_SERVER, GATEWAY, SUBNET_MASK);
// Enable DHCP
//Ethernet.begin(eth_MAC);
delay(200);
Serial.print("Ethernet IP is: ");
Serial.println(Ethernet.localIP());
/*
* Sanity checks for W5500 and cable connection.
*/
Serial.print("Checking connection.");
if (!getStatusEthernet())
{
Serial.println("\n\r\tHardware fault, or cable problem... cannot continue.");
Serial.print("Hardware Status: ");
prt_hwval(Ethernet.hardwareStatus());
Serial.print(" Cable Status: ");
prt_ethval(Ethernet.linkStatus());
isConnection = false;
}
else
{
isConnection = true;
Serial.println(" OK");
ethernetUdp.begin(localPort);
setMqtt();
}
}
void callbackMqttEthernet(char* topic, byte* payload, unsigned int length)
{
String messageTemp;
for (int i = 0; i < length; i++)
{
messageTemp += (char)payload[i];
parseTopic(topic, messageTemp);
}
}
void setMqtt()
{
mqttClientEthernet.setServer(MQTT_HOST, MQTT_PORT);
// Sets the callback function
mqttClientEthernet.setCallback(callbackMqttEthernet);
connectMqttEthernet();
}
void connectMqttEthernet()
{
if (!mqttClientEthernet.connected())
{
String clientId = roomName;
//clientId += String(random(0xffff), HEX);
if (mqttClientEthernet.connect(clientId.c_str(), MQTT_USER, MQTT_PASSWORD))
{
setSubscribe();
isConnectionOfMQTTBroker = true;
Serial.println("Connected to MQTT broker.");
}
else
{
isConnectionOfMQTTBroker = false;
Serial.println("Problem with connection to MQTT broker.");
}
}
}
void mqttLoopEthernet()
{
if(getStatusEthernet())
{
if (!mqttClientEthernet.connected())
{
connectMqttEthernet();
}
else
{
mqttClientEthernet.loop();
}
}
}
bool getStatusEthernet()
{
for (uint8_t i = 0; i <= 20; i++)
{
if ((Ethernet.hardwareStatus() == EthernetNoHardware) || (Ethernet.linkStatus() == LinkOFF))
{
isConnection = false;
delay(80);
Serial.println("Disconnect cable");
}
else
{
isConnection = true;
Serial.println("Connect cable");
break;
}
}
return isConnection;
}