This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Ethernet_STM32.ino
267 lines (214 loc) · 7.08 KB
/
Ethernet_STM32.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/****************************************************************************************************************************
Ethernet_STM32.ino
For W5x00, ENC28J60 and built-in LAN8742A Ethernet shields.
Ethernet_Manager is a library for STM32F/L/H/G/WB/MP1 with Ethernet W5x00, ENC28J60 or built-in LAN8742A shields,
to enable easy configuration/reconfiguration of Credentials and autoconnect/autoreconnect of Ethernet.
Built by Khoi Hoang https://github.com/khoih-prog/Ethernet_Manager_STM32
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
Ethernet_Manager ethernet_manager;
IPAddress localEthernetIP;
// Use to detect W5100 shield. The linkStatus() is not working with W5100
// in Ethernet and EthernetLarge libraries
bool isW5500 = true;
void heartBeatPrint()
{
static int num = 1;
static int linkStatus = 0;
localEthernetIP = Ethernet.localIP();
#if (USE_ETHERNET_GENERIC)
// To modify Ethernet2 library
linkStatus = Ethernet.link();
ETM_LOGINFO3(F("localEthernetIP = "), localEthernetIP, F(", linkStatus = "),
(linkStatus == 1) ? F("LinkON") : F("LinkOFF") );
if ( ( linkStatus == 1 ) && ((uint32_t) localEthernetIP != 0) )
#else
// The linkStatus() is not working with W5100. Just using IP != 0.0.0.0
// Better to use ping for W5100
linkStatus = (int) Ethernet.linkStatus();
ETM_LOGINFO3(F("localEthernetIP = "), localEthernetIP, F(", linkStatus = "),
(linkStatus == LinkON) ? F("LinkON") : F("LinkOFF") );
if ( ( (linkStatus == LinkON) || !isW5500 ) && ((uint32_t) localEthernetIP != 0) )
#endif
{
Serial.print(F("H"));
}
else
Serial.print(F("F"));
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
#define STATUS_CHECK_INTERVAL 10000L
static unsigned long checkstatus_timeout = STATUS_CHECK_INTERVAL;
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout))
{
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
void initEthernet()
{
#if ( defined(USE_BUILTIN_ETHERNET) && USE_BUILTIN_ETHERNET )
ETM_LOGWARN(F("======== USE_BUILTIN_ETHERNET ========"));
#elif ( defined(USE_UIP_ETHERNET) && USE_UIP_ETHERNET )
ETM_LOGWARN(F("======== USE_UIP_ETHERNET ========"));
#elif USE_ETHERNET_GENERIC
ETM_LOGWARN(F("=========== USE_ETHERNET_GENERIC ==========="));
#elif USE_ETHERNET_ENC
ETM_LOGWARN(F("=========== USE_ETHERNET_ENC ==========="));
#else
ETM_LOGWARN(F("========================="));
#endif
#if !(USE_BUILTIN_ETHERNET )
ETM_LOGWARN(F("Default SPI pinout:"));
ETM_LOGWARN1(F("MOSI:"), MOSI);
ETM_LOGWARN1(F("MISO:"), MISO);
ETM_LOGWARN1(F("SCK:"), SCK);
ETM_LOGWARN1(F("SS:"), SS);
ETM_LOGWARN(F("========================="));
// unknown board, do nothing, use default SS = 10
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 10 // For other boards
#endif
#if defined(BOARD_NAME)
ETM_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
#else
ETM_LOGWARN1(F("Unknown board setCsPin:"), USE_THIS_SS_PIN);
#endif
#endif
#if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
#if (defined(ETHERNET_WITH_SD_CARD) && ETHERNET_WITH_SD_CARD)
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
#endif
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif // ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
#endif
#if !(USE_BUILTIN_ETHERNET )
// Just info to know how to connect correctly
#if defined(CUR_PIN_MISO)
ETM_LOGWARN(F("Currently Used SPI pinout:"));
ETM_LOGWARN1(F("MOSI:"), CUR_PIN_MOSI);
ETM_LOGWARN1(F("MISO:"), CUR_PIN_MISO);
ETM_LOGWARN1(F("SCK:"), CUR_PIN_SCK);
ETM_LOGWARN1(F("SS:"), CUR_PIN_SS);
#else
ETM_LOGWARN(F("Currently Used SPI pinout:"));
ETM_LOGWARN1(F("MOSI:"), MOSI);
ETM_LOGWARN1(F("MISO:"), MISO);
ETM_LOGWARN1(F("SCK:"), SCK);
ETM_LOGWARN1(F("SS:"), SS);
#endif
ETM_LOGWARN(F("========================="));
#endif
}
#if USING_CUSTOMS_STYLE
const char NewCustomsStyle[] /*PROGMEM*/ =
"<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}\
button{background-color:blue;color:white;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>";
#endif
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial);
Serial.print(F("\nStart Ethernet_STM32 on "));
Serial.println(BOARD_NAME);
Serial.print(F("Ethernet Shield type : "));
Serial.println(SHIELD_TYPE);
Serial.println(ETHERNET_MANAGER_STM32_VERSION);
Serial.println(DOUBLERESETDETECTOR_GENERIC_VERSION);
initEthernet();
//////////////////////////////////////////////
#if USING_CUSTOMS_STYLE
ethernet_manager.setCustomsStyle(NewCustomsStyle);
#endif
#if USING_CUSTOMS_HEAD_ELEMENT
ethernet_manager.setCustomsHeadElement("<style>html{filter: invert(10%);}</style>");
#endif
#if USING_CORS_FEATURE
ethernet_manager.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
ethernet_manager.begin();
//////////////////////////////////////////////
localEthernetIP = Ethernet.localIP();
#if (USE_ETHERNET2 || USE_ETHERNET3)
// To modify Ethernet2 library
if ( (uint32_t) localEthernetIP != 0 )
#else
if ( (uint32_t) localEthernetIP != 0 )
#endif
{
Serial.print(F("Connected! IP address: "));
Serial.println(localEthernetIP);
}
else
{
Serial.println(F("Ethernet not Connected! Please check."));
}
// Detect W5100 only in Ethernet and EthernetLarge libraries
#if ( USE_ETHERNET || USE_ETHERNET_LARGE)
isW5500 = (Ethernet.hardwareStatus() == EthernetW5500);
Serial.print(F("Ethernet type is "));
Serial.println(isW5500 ? F("W5500") : F("W5100"));
#endif
}
#if (USE_DYNAMIC_PARAMETERS)
void displayCredentials()
{
Serial.println(F("\nYour stored Credentials :"));
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.print(myMenuItems[i].displayName);
Serial.print(" = ");
Serial.println(myMenuItems[i].pdata);
}
}
void displayCredentialsOnce()
{
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
}
#endif
void loop()
{
ethernet_manager.run();
check_status();
#if (USE_DYNAMIC_PARAMETERS)
displayCredentialsOnce();
#endif
}