-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net): Add NAPT examples (#10100)
* feat(net): Add NAPT examples for STA and ETH * feat(net): Add NAPT example for PPP * feat(net): Add CI configs to skip H2 * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9339c00
commit dd86244
Showing
6 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
libraries/Ethernet/examples/ETH_WIFI_BRIDGE/ETH_WIFI_BRIDGE.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <WiFi.h> | ||
#include <ETH.h> | ||
#include <SPI.h> | ||
|
||
#define ETH_TYPE ETH_PHY_W5500 | ||
#define ETH_ADDR 1 | ||
#define ETH_CS 15 | ||
#define ETH_IRQ 4 | ||
#define ETH_RST 5 | ||
#define ETH_SPI_SCK 14 | ||
#define ETH_SPI_MISO 12 | ||
#define ETH_SPI_MOSI 13 | ||
|
||
#define AP_SSID "ESP32-ETH-WIFI-BRIDGE" | ||
#define AP_PASS "password" | ||
|
||
IPAddress ap_ip(192, 168, 4, 1); | ||
IPAddress ap_mask(255, 255, 255, 0); | ||
IPAddress ap_leaseStart(192, 168, 4, 2); | ||
IPAddress ap_dns(8, 8, 4, 4); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.setDebugOutput(true); | ||
Network.onEvent(onEvent); | ||
|
||
WiFi.AP.begin(); | ||
WiFi.AP.config(ap_ip, ap_ip, ap_mask, ap_leaseStart, ap_dns); | ||
WiFi.AP.create(AP_SSID, AP_PASS); | ||
if (!WiFi.AP.waitStatusBits(ESP_NETIF_STARTED_BIT, 1000)) { | ||
Serial.println("Failed to start AP!"); | ||
return; | ||
} | ||
delay(100); | ||
|
||
SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI); | ||
ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, SPI); | ||
} | ||
|
||
void loop() { | ||
delay(20000); | ||
} | ||
|
||
void onEvent(arduino_event_id_t event, arduino_event_info_t info) { | ||
switch (event) { | ||
case ARDUINO_EVENT_ETH_START: Serial.println("ETH Started"); break; | ||
case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break; | ||
case ARDUINO_EVENT_ETH_GOT_IP: | ||
Serial.println("ETH Got IP"); | ||
Serial.println(ETH); | ||
WiFi.AP.enableNAPT(true); | ||
break; | ||
case ARDUINO_EVENT_ETH_LOST_IP: | ||
Serial.println("ETH Lost IP"); | ||
WiFi.AP.enableNAPT(false); | ||
break; | ||
case ARDUINO_EVENT_ETH_DISCONNECTED: | ||
Serial.println("ETH Disconnected"); | ||
WiFi.AP.enableNAPT(false); | ||
break; | ||
case ARDUINO_EVENT_ETH_STOP: Serial.println("ETH Stopped"); break; | ||
|
||
case ARDUINO_EVENT_WIFI_AP_START: | ||
Serial.println("AP Started"); | ||
Serial.println(WiFi.AP); | ||
break; | ||
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: Serial.println("AP STA Connected"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: Serial.println("AP STA Disconnected"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: | ||
Serial.print("AP STA IP Assigned: "); | ||
Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr)); | ||
break; | ||
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: Serial.println("AP Probe Request Received"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STOP: Serial.println("AP Stopped"); break; | ||
|
||
default: break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"targets": { | ||
"esp32h2": false | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
libraries/PPP/examples/PPP_WIFI_BRIDGE/PPP_WIFI_BRIDGE.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#include <PPP.h> | ||
#include <WiFi.h> | ||
|
||
#define PPP_MODEM_APN "internet" | ||
#define PPP_MODEM_PIN "0000" // or NULL | ||
|
||
// WaveShare SIM7600 HW Flow Control | ||
#define PPP_MODEM_RST 25 | ||
#define PPP_MODEM_RST_LOW false //active HIGH | ||
#define PPP_MODEM_RST_DELAY 200 | ||
#define PPP_MODEM_TX 21 | ||
#define PPP_MODEM_RX 22 | ||
#define PPP_MODEM_RTS 26 | ||
#define PPP_MODEM_CTS 27 | ||
#define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_HW | ||
#define PPP_MODEM_MODEL PPP_MODEM_SIM7600 | ||
|
||
// SIM800 basic module with just TX,RX and RST | ||
// #define PPP_MODEM_RST 0 | ||
// #define PPP_MODEM_RST_LOW true //active LOW | ||
// #define PPP_MODEM_TX 2 | ||
// #define PPP_MODEM_RX 19 | ||
// #define PPP_MODEM_RTS -1 | ||
// #define PPP_MODEM_CTS -1 | ||
// #define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_NONE | ||
// #define PPP_MODEM_MODEL PPP_MODEM_SIM800 | ||
|
||
// WiFi Access Point Config | ||
#define AP_SSID "ESP32-ETH-WIFI-BRIDGE" | ||
#define AP_PASS "password" | ||
|
||
IPAddress ap_ip(192, 168, 4, 1); | ||
IPAddress ap_mask(255, 255, 255, 0); | ||
IPAddress ap_leaseStart(192, 168, 4, 2); | ||
IPAddress ap_dns(8, 8, 4, 4); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.setDebugOutput(true); | ||
|
||
// Listen for modem events | ||
Network.onEvent(onEvent); | ||
|
||
// Start the Access Point | ||
WiFi.AP.begin(); | ||
WiFi.AP.config(ap_ip, ap_ip, ap_mask, ap_leaseStart, ap_dns); | ||
WiFi.AP.create(AP_SSID, AP_PASS); | ||
if (!WiFi.AP.waitStatusBits(ESP_NETIF_STARTED_BIT, 1000)) { | ||
Serial.println("Failed to start AP!"); | ||
return; | ||
} | ||
|
||
// Configure the modem | ||
PPP.setApn(PPP_MODEM_APN); | ||
PPP.setPin(PPP_MODEM_PIN); | ||
PPP.setResetPin(PPP_MODEM_RST, PPP_MODEM_RST_LOW, PPP_MODEM_RST_DELAY); | ||
PPP.setPins(PPP_MODEM_TX, PPP_MODEM_RX, PPP_MODEM_RTS, PPP_MODEM_CTS, PPP_MODEM_FC); | ||
|
||
Serial.println("Starting the modem. It might take a while!"); | ||
PPP.begin(PPP_MODEM_MODEL); | ||
|
||
Serial.print("Manufacturer: "); | ||
Serial.println(PPP.cmd("AT+CGMI", 10000)); | ||
Serial.print("Model: "); | ||
Serial.println(PPP.moduleName()); | ||
Serial.print("IMEI: "); | ||
Serial.println(PPP.IMEI()); | ||
|
||
bool attached = PPP.attached(); | ||
if (!attached) { | ||
int i = 0; | ||
unsigned int s = millis(); | ||
Serial.print("Waiting to connect to network"); | ||
while (!attached && ((++i) < 600)) { | ||
Serial.print("."); | ||
delay(100); | ||
attached = PPP.attached(); | ||
} | ||
Serial.print((millis() - s) / 1000.0, 1); | ||
Serial.println("s"); | ||
attached = PPP.attached(); | ||
} | ||
|
||
Serial.print("Attached: "); | ||
Serial.println(attached); | ||
Serial.print("State: "); | ||
Serial.println(PPP.radioState()); | ||
if (attached) { | ||
Serial.print("Operator: "); | ||
Serial.println(PPP.operatorName()); | ||
Serial.print("IMSI: "); | ||
Serial.println(PPP.IMSI()); | ||
Serial.print("RSSI: "); | ||
Serial.println(PPP.RSSI()); | ||
int ber = PPP.BER(); | ||
if (ber > 0) { | ||
Serial.print("BER: "); | ||
Serial.println(ber); | ||
Serial.print("NetMode: "); | ||
Serial.println(PPP.networkMode()); | ||
} | ||
|
||
Serial.println("Switching to data mode..."); | ||
PPP.mode(ESP_MODEM_MODE_CMUX); // Data and Command mixed mode | ||
if (!PPP.waitStatusBits(ESP_NETIF_CONNECTED_BIT, 1000)) { | ||
Serial.println("Failed to connect to internet!"); | ||
} else { | ||
Serial.println("Connected to internet!"); | ||
} | ||
} else { | ||
Serial.println("Failed to connect to network!"); | ||
} | ||
} | ||
|
||
void loop() { | ||
delay(20000); | ||
} | ||
|
||
void onEvent(arduino_event_id_t event, arduino_event_info_t info) { | ||
switch (event) { | ||
case ARDUINO_EVENT_PPP_START: Serial.println("PPP Started"); break; | ||
case ARDUINO_EVENT_PPP_CONNECTED: Serial.println("PPP Connected"); break; | ||
case ARDUINO_EVENT_PPP_GOT_IP: | ||
Serial.println("PPP Got IP"); | ||
Serial.println(PPP); | ||
WiFi.AP.enableNAPT(true); | ||
break; | ||
case ARDUINO_EVENT_PPP_LOST_IP: | ||
Serial.println("PPP Lost IP"); | ||
WiFi.AP.enableNAPT(false); | ||
break; | ||
case ARDUINO_EVENT_PPP_DISCONNECTED: | ||
Serial.println("PPP Disconnected"); | ||
WiFi.AP.enableNAPT(false); | ||
break; | ||
case ARDUINO_EVENT_PPP_STOP: Serial.println("PPP Stopped"); break; | ||
|
||
case ARDUINO_EVENT_WIFI_AP_START: | ||
Serial.println("AP Started"); | ||
Serial.println(WiFi.AP); | ||
break; | ||
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: Serial.println("AP STA Connected"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: Serial.println("AP STA Disconnected"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: | ||
Serial.print("AP STA IP Assigned: "); | ||
Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr)); | ||
break; | ||
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: Serial.println("AP Probe Request Received"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STOP: Serial.println("AP Stopped"); break; | ||
|
||
default: break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"targets": { | ||
"esp32h2": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <WiFi.h> | ||
|
||
#define STA_SSID "YOUR-SSID" | ||
#define STA_PASS "YOUR-PASS" | ||
|
||
#define AP_SSID "ESP32-WIFI-EXTENDER" | ||
#define AP_PASS "password" | ||
|
||
IPAddress ap_ip(192, 168, 4, 1); | ||
IPAddress ap_mask(255, 255, 255, 0); | ||
IPAddress ap_leaseStart(192, 168, 4, 2); | ||
IPAddress ap_dns(8, 8, 4, 4); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.setDebugOutput(true); | ||
Network.onEvent(onEvent); | ||
|
||
WiFi.AP.begin(); | ||
WiFi.AP.config(ap_ip, ap_ip, ap_mask, ap_leaseStart, ap_dns); | ||
WiFi.AP.create(AP_SSID, AP_PASS); | ||
if (!WiFi.AP.waitStatusBits(ESP_NETIF_STARTED_BIT, 1000)) { | ||
Serial.println("Failed to start AP!"); | ||
return; | ||
} | ||
|
||
WiFi.begin(STA_SSID, STA_PASS); | ||
} | ||
|
||
void loop() { | ||
delay(20000); | ||
} | ||
|
||
void onEvent(arduino_event_id_t event, arduino_event_info_t info) { | ||
switch (event) { | ||
case ARDUINO_EVENT_WIFI_STA_START: Serial.println("STA Started"); break; | ||
case ARDUINO_EVENT_WIFI_STA_CONNECTED: Serial.println("STA Connected"); break; | ||
case ARDUINO_EVENT_WIFI_STA_GOT_IP: | ||
Serial.println("STA Got IP"); | ||
Serial.println(WiFi.STA); | ||
WiFi.AP.enableNAPT(true); | ||
break; | ||
case ARDUINO_EVENT_WIFI_STA_LOST_IP: | ||
Serial.println("STA Lost IP"); | ||
WiFi.AP.enableNAPT(false); | ||
break; | ||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: | ||
Serial.println("STA Disconnected"); | ||
WiFi.AP.enableNAPT(false); | ||
break; | ||
case ARDUINO_EVENT_WIFI_STA_STOP: Serial.println("STA Stopped"); break; | ||
|
||
case ARDUINO_EVENT_WIFI_AP_START: | ||
Serial.println("AP Started"); | ||
Serial.println(WiFi.AP); | ||
break; | ||
case ARDUINO_EVENT_WIFI_AP_STACONNECTED: Serial.println("AP STA Connected"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: Serial.println("AP STA Disconnected"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: | ||
Serial.print("AP STA IP Assigned: "); | ||
Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr)); | ||
break; | ||
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: Serial.println("AP Probe Request Received"); break; | ||
case ARDUINO_EVENT_WIFI_AP_STOP: Serial.println("AP Stopped"); break; | ||
|
||
default: break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"targets": { | ||
"esp32h2": false | ||
} | ||
} |