-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathPPP_Basic.ino
128 lines (113 loc) · 3.78 KB
/
PPP_Basic.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
128
#include <PPP.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
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"); break;
case ARDUINO_EVENT_PPP_LOST_IP: Serial.println("PPP Lost IP"); break;
case ARDUINO_EVENT_PPP_DISCONNECTED: Serial.println("PPP Disconnected"); break;
case ARDUINO_EVENT_PPP_STOP: Serial.println("PPP Stopped"); break;
default: break;
}
}
void testClient(const char *host, uint16_t port) {
NetworkClient client;
if (!client.connect(host, port)) {
Serial.println("Connection Failed");
return;
}
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
while (client.connected() && !client.available());
while (client.available()) {
client.read(); //Serial.write(client.read());
}
Serial.println("Connection Success");
client.stop();
}
void setup() {
Serial.begin(115200);
// Listen for modem events
Network.onEvent(onEvent);
// 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() {
if (PPP.connected()) {
testClient("google.com", 80);
}
delay(20000);
}