-
Notifications
You must be signed in to change notification settings - Fork 7
/
esp8266-router.ino
127 lines (111 loc) · 3.35 KB
/
esp8266-router.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
#include <ESP8266WiFi.h>
#include <lwip/dns.h>
#include <dhcpserver.h>
#if LWIP_FEATURES
#define SERIAL_Enabled false // To show details on serial
// Write Your Router SSID which is your wifi name and your wifi PASSWORD.
const char *SSID = "ahmetozer.org";
const char *PASSWORD = "12345678";
// Your ESP Wifi
const char *SOFT_AP_SSID = "ahmet.engineer";
const char *SOFT_AP_PASSWORD = PASSWORD; // My preference is same as my home SSID PASSWORD.
int SOFT_AP_Channel = 11;
// ESP 8266 Local Network Configuration
IPAddress SOFT_AP_Local_IP(10, 0, 1, 1);
IPAddress SOFT_AP_Gateway(10, 0, 1, 1);
IPAddress SOFT_AP_Subnet(255, 255, 255, 0);
#define Copy_DNS_Settings true // if this is set to true
// System copy connected modem dns settings to dns server, other wise set to Cloudflare Public DNS
IPAddress SOFT_AP_DNS1(1, 1, 1, 1);
IPAddress SOFT_AP_DNS2(1, 0, 0, 1);
// Rename Your ESP name to find easily on your router
const char *espHostname = SOFT_AP_SSID; // By default, hostname is AP SSID but if you want you can set by manual by replace SOFT_AP_SSID with "YourHostName";
#define NAT_Enabled false
#if NAT_Enabled && !LWIP_IPV6
#define NAPT 1000
#define NAPT_PORT 10
#include <lwip/napt.h>
#endif
void setup()
{
#if SERIAL_Enabled
Serial.begin(115200);
Serial.println();
Serial.println("\n\n\nStarting ...");
#endif
WiFi.mode(WIFI_AP_STA);
#if SERIAL_Enabled
Serial.print("Connecting to ");
Serial.println(SSID);
#endif
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
#if SERIAL_Enabled
Serial.print(".");
#endif
delay(500);
}
WiFi.hostname(espHostname);
#if Copy_DNS_Settings
SOFT_AP_DNS1 = WiFi.dnsIP(0);
SOFT_AP_DNS2 = WiFi.dnsIP(1);
#endif
dhcps_set_dns(0, SOFT_AP_DNS1);
dhcps_set_dns(1, SOFT_AP_DNS2);
WiFi.softAPConfig(SOFT_AP_Local_IP, SOFT_AP_Gateway, SOFT_AP_Subnet);
WiFi.softAP(SOFT_AP_SSID, SOFT_AP_PASSWORD, SOFT_AP_Channel);
#if SERIAL_Enabled
// Wifi Client Part
Serial.printf("\nConnected WiFi %s\n MAC addr: %s\nESP8266 client ip address: %s \nDNS adresses: %s , %s\n",
SSID, WiFi.softAPmacAddress().c_str(),
WiFi.localIP().toString().c_str(),
WiFi.dnsIP(0).toString().c_str(),
WiFi.dnsIP(1).toString().c_str());
// Wifi AP Part
Serial.printf("Ap Info\nEsp AP IPAddress: %s \nDHCP Settings\nGateway: %s \nSubnet: %s\nDNS: %s - %s\n",
WiFi.softAPIP().toString().c_str(),
SOFT_AP_Gateway.toString().c_str(),
SOFT_AP_Subnet.toString().c_str(),
SOFT_AP_DNS1.toString().c_str(),SOFT_AP_DNS2.toString().c_str());
#endif
#if NAT_Enabled && !LWIP_IPV6
err_t nat_err = ip_napt_init(NAPT, NAPT_PORT);
#if SERIAL_Enabled
Serial.println("Initializing NAT");
#endif
if (nat_err == ERR_OK)
{
nat_err = ip_napt_enable_no(SOFTAP_IF, 1);
#if SERIAL_Enabled
Serial.println("Enabling NAT on AP");
#endif
if (nat_err == ERR_OK)
{
#if SERIAL_Enabled
Serial.printf("Nat Is Enabled on '%s' \n", SOFT_AP_SSID);
#endif
}
}
if (nat_err != ERR_OK)
{
#if SERIAL_Enabled
Serial.println("ERR: NAT is not initialized");
#endif
}
#elif NAT_Enabled
Serial.println("ERR: NAT is not initialized");
Serial.println("\n\n\nPlease select LwIP Variant: v2 Higher Bandwidth or v2 Lower Memory");
#endif
}
#else
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("\n\n\nPlease select LwIP Variant: v2 Higher Bandwidth or v2 Lower Memory");
}
#endif
void loop()
{
}