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
/
Copy pathnRF52_SimpleServer.ino
244 lines (184 loc) · 6.97 KB
/
nRF52_SimpleServer.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
/****************************************************************************************************************************
nRF52_SimpleServer.ino
For all Generic boards such as ESP8266, ESP32, WT32_ETH01, SAMD21/SAMD51, nRF52, STM32F/L/H/G/WB/MP1,Teensy, Portenta_H7
with WiFiNINA, ESP8266/ESP32 WiFi, ESP8266/ESP32-AT, W5x00, ENC28J60, Native-Ethernet, Portenta Ethernet/WiFi
DDNS_Generic is a library to automatically add port mappings to router using UPnP SSDP
(Simple Service Discovery Protocol) in order to provide access to the local Web Services from the Internet.
Based on and modified from Ofek Pearl's TinyUPnP Library (https://github.com/ofekp/TinyUPnP)
Built by Khoi Hoang https://github.com/khoih-prog/UPnP_Generic
Licensed under GPL-3.0 license
*****************************************************************************************************************************/
/*
Note: This example uses the DDNS_Generic library (https://github.com/khoih-prog/DDNS_Generic)
You can access this WebServer by either localIP:LISTEN_PORT such as 192.169.2.100:5952
or DDNS_Host:LISTEN_PORT, such as account.duckdns.org:5952
*/
#include "defines.h"
#define UPNP_USING_ETHERNET true
#include <UPnP_Generic.h>
#define LISTEN_PORT 5952
#define LEASE_DURATION 36000 // seconds
#define FRIENDLY_NAME "NRF52-W5X00" // this name will appear in your router port forwarding section
UPnP* uPnP;
EthernetWebServer server(LISTEN_PORT);
#if defined(LED_BLUE)
#define LED_PIN LED_BLUE // BLUE_LED on nRF52840 Feather Express, Itsy-Bitsy
#else
#define LED_PIN 3 // RED LED
#endif
const int led = LED_PIN;
void onUpdateCallback(const char* oldIP, const char* newIP)
{
Serial.print(F("DDNSGeneric - IP Change Detected: oldIP = "));
Serial.print(oldIP);
Serial.print(F(", newIP = "));
Serial.println(newIP);
}
void handleRoot()
{
#define BUFFER_SIZE 512
digitalWrite(led, 1);
char temp[BUFFER_SIZE];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
int day = hr / 24;
hr = hr % 24;
snprintf(temp, BUFFER_SIZE - 1,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>%s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from %s</h1>\
<h3>running UPnP_Generic & DDNS_Generic</h3>\
<h3>on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
</body>\
</html>", BOARD_NAME, BOARD_NAME, SHIELD_TYPE, day, hr, min % 60, sec % 60);
server.send(200, F("text/html"), temp);
digitalWrite(led, 0);
}
void handleNotFound()
{
digitalWrite(led, 1);
String message = F("File Not Found\n\n");
message += F("URI: ");
message += server.uri();
message += F("\nMethod: ");
message += (server.method() == HTTP_GET) ? F("GET") : F("POST");
message += F("\nArguments: ");
message += server.args();
message += F("\n");
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, F("text/plain"), message);
digitalWrite(led, 0);
}
void initEthernet()
{
#if USE_ETHERNET_GENERIC
ET_LOGWARN(F("=========== USE_ETHERNET_GENERIC ==========="));
#elif USE_ETHERNET_ENC
ET_LOGWARN(F("=========== USE_ETHERNET_ENC ==========="));
#elif USE_UIP_ETHERNET
ET_LOGWARN(F("=========== USE_UIP_ETHERNET ==========="));
#else
ET_LOGWARN(F("=========== USE_CUSTOM_ETHERNET ==========="));
#endif
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
ET_LOGWARN(F("Default SPI pinout:"));
ET_LOGWARN1(F("MOSI:"), MOSI);
ET_LOGWARN1(F("MISO:"), MISO);
ET_LOGWARN1(F("SCK:"), SCK);
ET_LOGWARN1(F("SS:"), SS);
ET_LOGWARN(F("========================="));
// 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 )
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[index], ip);
Ethernet.begin(mac[index]);
}
#define RETRY_TIMES 4
void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print("\nStart nRF52_SimpleServer on "); Serial.print(BOARD_NAME);
Serial.print(" using "); Serial.println(SHIELD_TYPE);
Serial.println(UPNP_GENERIC_VERSION);
initEthernet();
IPAddress localIP = Ethernet.localIP();
Serial.print(F("Connected! IP address: "));
Serial.println(localIP);
////////////////
DDNSGeneric.service("duckdns"); // Enter your DDNS Service Name - "duckdns" / "noip"
/*
For DDNS Providers where you get a token:
DDNSGeneric.client("domain", "token");
For DDNS Providers where you get username and password: ( Leave the password field empty "" if not required )
DDNSGeneric.client("domain", "username", "password");
*/
DDNSGeneric.client("account.duckdns.org", "12345678-1234-1234-1234-123456789012");
DDNSGeneric.onUpdate(onUpdateCallback);
////////////////
uPnP = new UPnP(60000); // -1 means blocking, preferably, use a timeout value (ms)
if (uPnP)
{
uPnP->addPortMappingConfig(localIP, LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME);
bool portMappingAdded = false;
int retries = 0;
while (!portMappingAdded && (retries < RETRY_TIMES))
{
Serial.println("Add Port Forwarding, Try # " + String(++retries));
int result = uPnP->commitPortMappings();
portMappingAdded = ( (result == PORT_MAP_SUCCESS) || (result == ALREADY_MAPPED) );
//Serial.println("commitPortMappings result =" + String(result));
if (!portMappingAdded)
{
// for debugging, you can see this in your router too under forwarding or UPnP
//uPnP->printAllPortMappings();
//Serial.println(F("This was printed because adding the required port mapping failed"));
if (retries < RETRY_TIMES)
delay(10000); // 10 seconds before trying again
}
}
uPnP->printAllPortMappings();
Serial.println(F("\nUPnP done"));
}
server.on(F("/"), handleRoot);
server.on(F("/inline"), []()
{
server.send(200, F("text/plain"), F("This works as well"));
});
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.print(localIP);
Serial.print(F(", port = "));
Serial.println(LISTEN_PORT);
}
void loop()
{
DDNSGeneric.update(300000);
uPnP->updatePortMappings(600000); // 10 minutes
server.handleClient();
}