Skip to content

Commit 4872f1d

Browse files
committed
feat(net): Add NAPT example for PPP
1 parent 9ea4b3d commit 4872f1d

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#include <PPP.h>
2+
#include <WiFi.h>
3+
4+
#define PPP_MODEM_APN "internet"
5+
#define PPP_MODEM_PIN "0000" // or NULL
6+
7+
// WaveShare SIM7600 HW Flow Control
8+
#define PPP_MODEM_RST 25
9+
#define PPP_MODEM_RST_LOW false //active HIGH
10+
#define PPP_MODEM_RST_DELAY 200
11+
#define PPP_MODEM_TX 21
12+
#define PPP_MODEM_RX 22
13+
#define PPP_MODEM_RTS 26
14+
#define PPP_MODEM_CTS 27
15+
#define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_HW
16+
#define PPP_MODEM_MODEL PPP_MODEM_SIM7600
17+
18+
// SIM800 basic module with just TX,RX and RST
19+
// #define PPP_MODEM_RST 0
20+
// #define PPP_MODEM_RST_LOW true //active LOW
21+
// #define PPP_MODEM_TX 2
22+
// #define PPP_MODEM_RX 19
23+
// #define PPP_MODEM_RTS -1
24+
// #define PPP_MODEM_CTS -1
25+
// #define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_NONE
26+
// #define PPP_MODEM_MODEL PPP_MODEM_SIM800
27+
28+
// WiFi Access Point Config
29+
#define AP_SSID "ESP32-ETH-WIFI-BRIDGE"
30+
#define AP_PASS "password"
31+
32+
IPAddress ap_ip(192, 168, 4, 1);
33+
IPAddress ap_mask(255, 255, 255, 0);
34+
IPAddress ap_leaseStart(192, 168, 4, 2);
35+
IPAddress ap_dns(8, 8, 4, 4);
36+
37+
void setup() {
38+
Serial.begin(115200);
39+
Serial.setDebugOutput(true);
40+
41+
// Listen for modem events
42+
Network.onEvent(onEvent);
43+
44+
// Start the Access Point
45+
WiFi.AP.begin();
46+
WiFi.AP.config(ap_ip, ap_ip, ap_mask, ap_leaseStart, ap_dns);
47+
WiFi.AP.create(AP_SSID, AP_PASS);
48+
if(!WiFi.AP.waitStatusBits(ESP_NETIF_STARTED_BIT, 1000)){
49+
Serial.println("Failed to start AP!");
50+
return;
51+
}
52+
53+
// Configure the modem
54+
PPP.setApn(PPP_MODEM_APN);
55+
PPP.setPin(PPP_MODEM_PIN);
56+
PPP.setResetPin(PPP_MODEM_RST, PPP_MODEM_RST_LOW, PPP_MODEM_RST_DELAY);
57+
PPP.setPins(PPP_MODEM_TX, PPP_MODEM_RX, PPP_MODEM_RTS, PPP_MODEM_CTS, PPP_MODEM_FC);
58+
59+
Serial.println("Starting the modem. It might take a while!");
60+
PPP.begin(PPP_MODEM_MODEL);
61+
62+
Serial.print("Manufacturer: ");
63+
Serial.println(PPP.cmd("AT+CGMI", 10000));
64+
Serial.print("Model: ");
65+
Serial.println(PPP.moduleName());
66+
Serial.print("IMEI: ");
67+
Serial.println(PPP.IMEI());
68+
69+
bool attached = PPP.attached();
70+
if (!attached) {
71+
int i = 0;
72+
unsigned int s = millis();
73+
Serial.print("Waiting to connect to network");
74+
while (!attached && ((++i) < 600)) {
75+
Serial.print(".");
76+
delay(100);
77+
attached = PPP.attached();
78+
}
79+
Serial.print((millis() - s) / 1000.0, 1);
80+
Serial.println("s");
81+
attached = PPP.attached();
82+
}
83+
84+
Serial.print("Attached: ");
85+
Serial.println(attached);
86+
Serial.print("State: ");
87+
Serial.println(PPP.radioState());
88+
if (attached) {
89+
Serial.print("Operator: ");
90+
Serial.println(PPP.operatorName());
91+
Serial.print("IMSI: ");
92+
Serial.println(PPP.IMSI());
93+
Serial.print("RSSI: ");
94+
Serial.println(PPP.RSSI());
95+
int ber = PPP.BER();
96+
if (ber > 0) {
97+
Serial.print("BER: ");
98+
Serial.println(ber);
99+
Serial.print("NetMode: ");
100+
Serial.println(PPP.networkMode());
101+
}
102+
103+
Serial.println("Switching to data mode...");
104+
PPP.mode(ESP_MODEM_MODE_CMUX); // Data and Command mixed mode
105+
if (!PPP.waitStatusBits(ESP_NETIF_CONNECTED_BIT, 1000)) {
106+
Serial.println("Failed to connect to internet!");
107+
} else {
108+
Serial.println("Connected to internet!");
109+
}
110+
} else {
111+
Serial.println("Failed to connect to network!");
112+
}
113+
}
114+
115+
void loop() {
116+
delay(20000);
117+
}
118+
119+
void onEvent(arduino_event_id_t event, arduino_event_info_t info) {
120+
switch (event) {
121+
case ARDUINO_EVENT_PPP_START:
122+
Serial.println("PPP Started");
123+
break;
124+
case ARDUINO_EVENT_PPP_CONNECTED:
125+
Serial.println("PPP Connected");
126+
break;
127+
case ARDUINO_EVENT_PPP_GOT_IP:
128+
Serial.println("PPP Got IP");
129+
Serial.println(PPP);
130+
WiFi.AP.enableNAPT(true);
131+
break;
132+
case ARDUINO_EVENT_PPP_LOST_IP:
133+
Serial.println("PPP Lost IP");
134+
WiFi.AP.enableNAPT(false);
135+
break;
136+
case ARDUINO_EVENT_PPP_DISCONNECTED:
137+
Serial.println("PPP Disconnected");
138+
WiFi.AP.enableNAPT(false);
139+
break;
140+
case ARDUINO_EVENT_PPP_STOP:
141+
Serial.println("PPP Stopped");
142+
break;
143+
144+
case ARDUINO_EVENT_WIFI_AP_START:
145+
Serial.println("AP Started");
146+
Serial.println(WiFi.AP);
147+
break;
148+
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
149+
Serial.println("AP STA Connected");
150+
break;
151+
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
152+
Serial.println("AP STA Disconnected");
153+
break;
154+
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
155+
Serial.print("AP STA IP Assigned: ");
156+
Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr));
157+
break;
158+
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED:
159+
Serial.println("AP Probe Request Received");
160+
break;
161+
case ARDUINO_EVENT_WIFI_AP_STOP:
162+
Serial.println("AP Stopped");
163+
break;
164+
165+
default:
166+
break;
167+
}
168+
}

0 commit comments

Comments
 (0)