|
| 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: Serial.println("PPP Started"); break; |
| 122 | + case ARDUINO_EVENT_PPP_CONNECTED: Serial.println("PPP Connected"); break; |
| 123 | + case ARDUINO_EVENT_PPP_GOT_IP: |
| 124 | + Serial.println("PPP Got IP"); |
| 125 | + Serial.println(PPP); |
| 126 | + WiFi.AP.enableNAPT(true); |
| 127 | + break; |
| 128 | + case ARDUINO_EVENT_PPP_LOST_IP: |
| 129 | + Serial.println("PPP Lost IP"); |
| 130 | + WiFi.AP.enableNAPT(false); |
| 131 | + break; |
| 132 | + case ARDUINO_EVENT_PPP_DISCONNECTED: |
| 133 | + Serial.println("PPP Disconnected"); |
| 134 | + WiFi.AP.enableNAPT(false); |
| 135 | + break; |
| 136 | + case ARDUINO_EVENT_PPP_STOP: Serial.println("PPP Stopped"); break; |
| 137 | + |
| 138 | + case ARDUINO_EVENT_WIFI_AP_START: |
| 139 | + Serial.println("AP Started"); |
| 140 | + Serial.println(WiFi.AP); |
| 141 | + break; |
| 142 | + case ARDUINO_EVENT_WIFI_AP_STACONNECTED: Serial.println("AP STA Connected"); break; |
| 143 | + case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: Serial.println("AP STA Disconnected"); break; |
| 144 | + case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: |
| 145 | + Serial.print("AP STA IP Assigned: "); |
| 146 | + Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr)); |
| 147 | + break; |
| 148 | + case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED: Serial.println("AP Probe Request Received"); break; |
| 149 | + case ARDUINO_EVENT_WIFI_AP_STOP: Serial.println("AP Stopped"); break; |
| 150 | + |
| 151 | + default: break; |
| 152 | + } |
| 153 | +} |
0 commit comments