-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWiFiNatRouter.ino
More file actions
67 lines (50 loc) · 1.44 KB
/
WiFiNatRouter.ino
File metadata and controls
67 lines (50 loc) · 1.44 KB
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
#include <ESP8266WiFi.h>
#include "lwip/lwip_napt.h"
#include "lwip/app/dhcpserver.h"
// credentials for ESP8266 STA
const char* sta_ssid = "your_ssid";
const char* sta_password = "your_pw";
// credentials for ESP8266 AP
const char *ap_ssid = "ESPap";
const char *ap_password = "password";
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_AP_STA);
Serial.println("Starting NAT demo");
WiFi.begin(sta_ssid, sta_password);
//WiFi.config(ip, gateway, subnet);
//Wifi connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(sta_ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("dnsIP address: ");
Serial.println(WiFi.dnsIP());
Serial.print("gatewayIP address: ");
Serial.println(WiFi.gatewayIP());
Serial.print("subnetMask address: ");
Serial.println(WiFi.subnetMask());
Serial.println("");
Serial.println("Configuring access point...");
WiFi.softAP(ap_ssid, ap_password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// Initialize the NAT feature
ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);
// Enable NAT on the AP interface
ip_napt_enable_no(1, 1);
// Set the DNS server for clients of the AP to the one we also use for the STA interface
dhcps_set_DNS(WiFi.dnsIP());
}
void loop()
{
delay(500);
}