Skip to content

Commit ee0366e

Browse files
committed
Improve example and add some comments to the header
1 parent 2f84198 commit ee0366e

File tree

2 files changed

+70
-50
lines changed

2 files changed

+70
-50
lines changed

libraries/PPP/examples/PPP_Basic/PPP_Basic.ino

+36-35
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44
#define PPP_MODEM_PIN "0000" // or NULL
55

66
// WaveShare SIM7600 HW Flow Control
7-
//#define PPP_MODEM_RST 25
8-
//#define PPP_MODEM_RST_LOW false //active HIGH
9-
//#define PPP_MODEM_TX 21
10-
//#define PPP_MODEM_RX 22
11-
//#define PPP_MODEM_RTS 26
12-
//#define PPP_MODEM_CTS 27
13-
//#define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_HW
14-
//#define PPP_MODEM_MODEL PPP_MODEM_SIM7600
7+
#define PPP_MODEM_RST 25
8+
#define PPP_MODEM_RST_LOW false //active HIGH
9+
#define PPP_MODEM_TX 21
10+
#define PPP_MODEM_RX 22
11+
#define PPP_MODEM_RTS 26
12+
#define PPP_MODEM_CTS 27
13+
#define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_HW
14+
#define PPP_MODEM_MODEL PPP_MODEM_SIM7600
1515

1616
// SIM800 basic module with just TX,RX and RST
17-
#define PPP_MODEM_RST 0
18-
#define PPP_MODEM_RST_LOW true //active LOW
19-
#define PPP_MODEM_TX 2
20-
#define PPP_MODEM_RX 19
21-
#define PPP_MODEM_RTS -1
22-
#define PPP_MODEM_CTS -1
23-
#define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_NONE
24-
#define PPP_MODEM_MODEL PPP_MODEM_SIM800
25-
26-
static bool ppp_connected = false;
17+
// #define PPP_MODEM_RST 0
18+
// #define PPP_MODEM_RST_LOW true //active LOW
19+
// #define PPP_MODEM_TX 2
20+
// #define PPP_MODEM_RX 19
21+
// #define PPP_MODEM_RTS -1
22+
// #define PPP_MODEM_CTS -1
23+
// #define PPP_MODEM_FC ESP_MODEM_FLOW_CONTROL_NONE
24+
// #define PPP_MODEM_MODEL PPP_MODEM_SIM800
2725

2826
void onEvent(arduino_event_id_t event, arduino_event_info_t info)
2927
{
@@ -36,26 +34,16 @@ void onEvent(arduino_event_id_t event, arduino_event_info_t info)
3634
break;
3735
case ARDUINO_EVENT_PPP_GOT_IP:
3836
Serial.println("PPP Got IP");
39-
Serial.println(PPP);
40-
ppp_connected = true;
41-
break;
42-
case ARDUINO_EVENT_PPP_GOT_IP6:
43-
Serial.println("PPP Got IPv6");
44-
Serial.println(PPP);
4537
break;
4638
case ARDUINO_EVENT_PPP_LOST_IP:
4739
Serial.println("PPP Lost IP");
48-
ppp_connected = false;
4940
break;
5041
case ARDUINO_EVENT_PPP_DISCONNECTED:
5142
Serial.println("PPP Disconnected");
52-
ppp_connected = false;
5343
break;
5444
case ARDUINO_EVENT_PPP_STOP:
5545
Serial.println("PPP Stopped");
56-
ppp_connected = false;
5746
break;
58-
5947
default:
6048
break;
6149
}
@@ -79,22 +67,28 @@ void testClient(const char * host, uint16_t port) {
7967

8068
void setup() {
8169
Serial.begin(115200);
70+
71+
// Listen for modem events
8272
Network.onEvent(onEvent);
73+
74+
// Configure the modem
8375
PPP.setApn(PPP_MODEM_APN);
8476
PPP.setPin(PPP_MODEM_PIN);
8577
PPP.setResetPin(PPP_MODEM_RST, PPP_MODEM_RST_LOW);
8678
PPP.setPins(PPP_MODEM_TX, PPP_MODEM_RX, PPP_MODEM_RTS, PPP_MODEM_CTS, PPP_MODEM_FC);
79+
8780
Serial.println("Starting the modem. It might take a while!");
8881
PPP.begin(PPP_MODEM_MODEL);
8982

90-
Serial.print("Name: "); Serial.println(PPP.moduleName());
83+
Serial.print("Manufacturer: "); Serial.println(PPP.cmd("AT+CGMI", 10000));
84+
Serial.print("Model: "); Serial.println(PPP.moduleName());
9185
Serial.print("IMEI: "); Serial.println(PPP.IMEI());
9286

9387
bool attached = PPP.attached();
9488
if(!attached){
9589
int i=0;
9690
unsigned int s = millis();
97-
Serial.print("Waiting to attach ");
91+
Serial.print("Waiting to connect to network");
9892
while(!attached && ((++i) < 600)){
9993
Serial.print(".");
10094
delay(100);
@@ -112,18 +106,25 @@ void setup() {
112106
Serial.print("IMSI: "); Serial.println(PPP.IMSI());
113107
Serial.print("RSSI: "); Serial.println(PPP.RSSI());
114108
int ber = PPP.BER();
115-
Serial.print("BER: "); Serial.println(ber);
116-
if(ber){
109+
if(ber > 0){
110+
Serial.print("BER: "); Serial.println(ber);
117111
Serial.print("NetMode: "); Serial.println(PPP.networkMode());
118112
}
119113

120-
// PPP.mode(ESP_MODEM_MODE_DATA); // Data ONLY mode
121-
PPP.mode(ESP_MODEM_MODE_CMUX); // Data and Commands mode
114+
Serial.println("Switching to data mode...");
115+
PPP.mode(ESP_MODEM_MODE_CMUX); // Data and Command mixed mode
116+
if(!PPP.waitStatusBits(ESP_NETIF_CONNECTED_BIT, 1000)){
117+
Serial.println("Failed to connect to internet!");
118+
} else {
119+
Serial.println("Connected to internet!");
120+
}
121+
} else {
122+
Serial.println("Failed to connect to network!");
122123
}
123124
}
124125

125126
void loop() {
126-
if (ppp_connected) {
127+
if (PPP.connected()) {
127128
testClient("google.com", 80);
128129
}
129130
delay(20000);

libraries/PPP/src/PPP.h

+34-15
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,56 @@ class PPPClass: public NetworkInterface {
2323
PPPClass();
2424
~PPPClass();
2525

26+
bool begin(ppp_modem_model_t model, uint8_t uart_num=1, int baud_rate=115200);
27+
void end();
28+
29+
// Required for connecting to internet
2630
bool setApn(const char * apn);
31+
32+
// Required only if the SIM card is protected by PIN
2733
bool setPin(const char * pin);
2834

35+
// If the modem supports hardware flow control, it's best to use it
2936
bool setPins(int8_t tx, int8_t rx, int8_t rts=-1, int8_t cts=-1, esp_modem_flow_ctrl_t flow_ctrl=ESP_MODEM_FLOW_CONTROL_NONE);
30-
void setResetPin(int8_t rst, bool active_low=true);
3137

32-
bool begin(ppp_modem_model_t model, uint8_t uart_num=1, int baud_rate=115200);
33-
void end();
38+
// Using the reset pin of the module ensures that proper communication can be achieved
39+
void setResetPin(int8_t rst, bool active_low=true);
3440

3541
// Modem DCE APIs
3642
int RSSI() const;
3743
int BER() const;
3844
String IMSI() const;
3945
String IMEI() const;
40-
String moduleName() const;
41-
String operatorName() const;
42-
int networkMode() const;
43-
int radioState() const;
44-
bool attached() const;
45-
bool sync() const;
46-
47-
esp_modem_dce_mode_t mode() const { return _mode; }
46+
String moduleName() const; // modem module name
47+
String operatorName() const; // network operator name
48+
int networkMode() const; // network type (GSM, LTE, etc.)
49+
int radioState() const; // 0:minimal, 1:full
50+
bool attached() const; // true is attached to network
51+
bool sync() const; // true if responds to 'AT'
52+
53+
// Switch the communication mode
4854
bool mode(esp_modem_dce_mode_t m);
55+
esp_modem_dce_mode_t mode() const { return _mode; }
4956

50-
bool powerDown();
51-
bool reset();
52-
bool storeProfile();
53-
57+
// Change temporary the baud rate of communication
5458
bool setBaudrate(int baudrate);
59+
60+
// Sens SMS message to a number
5561
bool sms(const char * num, const char * message);
62+
bool sms(String num, String message){
63+
return sms(num.c_str(), message.c_str());
64+
}
65+
66+
// Send AT command with timeout in milliseconds
5667
String cmd(const char * at_command, int timeout);
68+
String cmd(String at_command, int timeout){
69+
return cmd(at_command.c_str(), timeout);
70+
}
71+
72+
// untested
73+
bool powerDown();
74+
bool reset();
75+
bool storeProfile();
5776

5877
esp_modem_dce_t * handle() const;
5978

0 commit comments

Comments
 (0)