Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@
- [Network management](#network-management)
- [NTP time synchronization](#ntp-time-synchronization)
- [Start/stop services](#startstop-services)
- [Services callbacks](#services-callbacks)
- [Callbacks](#callbacks)
- [TCPClient features](#tcpclient-features)
- [Connecting to a TCP server](#connecting-to-a-tcp-server)
- [Transferring data](#transferring-data)
- [Callbacks](#callbacks)
- [Callbacks](#callbacks-1)
- [MQTTClient features](#mqttclient-features)
- [Connecting to an MQTT broker](#connecting-to-an-mqtt-broker)
- [Publishing and subscribing to topics](#publishing-and-subscribing-to-topics)
- [Callbacks](#callbacks-1)
- [Callbacks](#callbacks-2)
- [Troubleshooting](#troubleshooting)
- [Module doesn't respond](#module-doesnt-respond)
- [Can't register on network](#cant-register-on-network)
Expand Down Expand Up @@ -486,6 +486,11 @@ void setup() {
} else {
Serial.println("Modem is not registered on the network.");
}

// Get last known registration status without querying the modem
Serial.printf("Currently registered on network: %s, Status: %u\r\n",
modem.isCurrentlyRegisteredOnNetwork() ? "Yes" : "No",
static_cast<uint8_t>(modem.getCurrentRegistrationStatus()));
}
```

Expand Down Expand Up @@ -555,7 +560,7 @@ void setup() {
}
```

### Services callbacks
### Callbacks

The `SIM7600::Modem` class allows you to set callbacks for various services events, such as TCP/IP
and MQTT. You need to define your callback functions with the appropriate signatures and then set them using the
Expand All @@ -569,6 +574,13 @@ and MQTT. You need to define your callback functions with the appropriate signat
using namespace SIM7600;
Modem modem(&Serial1); // Use Serial1 for communication

// Network changed event callback
void networkChangedCB(const bool registered, const RegStatus reg_status) {
Serial.printf("Event: Network changed! Registered: %s, Status: %u\r\n",
registered ? "Yes" : "No",
static_cast<uint8_t>(reg_status));
}

// TCP/IP closed event callback
// IMPORTANT: If this event occurs, you need to start the TCP/IP service again before using it.
void tcpNetworkClosedCB() { Serial.println("Event: TCP network connection closed!"); }
Expand All @@ -580,6 +592,9 @@ void mqttNetworkClosedCB() { Serial.println("Event: MQTT network connection clos
void setup() {
// ... Initialize modem as shown in the previous example

// Set network changed event callback
modem.setNetworkChangedCallback(networkChangedCB);

// Set TCP/IP closed event callback
modem.setTCPNetworkClosedCallback(tcpNetworkClosedCB);

Expand Down
79 changes: 47 additions & 32 deletions examples/ModemTest/ModemTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ SIM7600::MQTTClient mqtt(&modem);
void handleSerial();

/* Callback functions definitions */
// Modem
void networkChangedCB(const bool registered, const SIM7600::RegStatus reg_status) {
Serial.printf("Event: Network changed! Registered: %s, Status: %u\r\n",
registered ? "Yes" : "No",
static_cast<uint8_t>(reg_status));
}

// TCP
void tcpNetworkClosedCB() { Serial.println("Event: TCP network connection closed!"); }

Expand Down Expand Up @@ -124,6 +131,7 @@ void setup() {
digitalWrite(GSM_GPIO_ENABLE, HIGH);

// Modem callbacks
modem.setNetworkChangedCallback(networkChangedCB);
modem.setTCPNetworkClosedCallback(tcpNetworkClosedCB);
modem.setMQTTNetworkClosedCallback(mqttNetworkClosedCB);

Expand Down Expand Up @@ -227,6 +235,13 @@ void handleSerial() {
} break;

case '4':
{
Serial.printf("Currently registered on network: %s, Status: %u\r\n",
modem.isCurrentlyRegisteredOnNetwork() ? "Yes" : "No",
static_cast<uint8_t>(modem.getCurrentRegistrationStatus()));
} break;

case '5':
{
SIM7600::NTPSyncStatus ntp_status;
status = modem.setNTPServer(ntp_status, "pool.ntp.org", 0);
Expand All @@ -239,7 +254,7 @@ void handleSerial() {
}
} break;

case '5':
case '6':
{
SIM7600::NTPTimeData time_data;

Expand All @@ -259,7 +274,7 @@ void handleSerial() {
}
} break;

case '6':
case '7':
{
status = modem.setGPSAntennaVoltage(3050);

Expand All @@ -271,7 +286,7 @@ void handleSerial() {
}
} break;

case '7':
case '8':
{
uint16_t voltage_mv;
status = modem.getGPSAntennaVoltage(voltage_mv);
Expand All @@ -284,7 +299,7 @@ void handleSerial() {
}
} break;

case '8':
case '9':
{
status = modem.enableGPSAntennaVoltage();

Expand All @@ -296,7 +311,7 @@ void handleSerial() {
}
} break;

case '9':
case '0':
{
status = modem.disableGPSAntennaVoltage();

Expand All @@ -308,7 +323,7 @@ void handleSerial() {
}
} break;

case '0':
case 'q':
{
bool enabled;
status = modem.isGPSAntennaVoltageEnabled(enabled);
Expand All @@ -321,7 +336,7 @@ void handleSerial() {
}
} break;

case 'q':
case 'w':
{
status = modem.enableGPS();

Expand All @@ -332,7 +347,7 @@ void handleSerial() {
}
} break;

case 'w':
case 'e':
{
status = modem.disableGPS();

Expand All @@ -343,7 +358,7 @@ void handleSerial() {
}
} break;

case 'e':
case 'r':
{
bool enabled;
status = modem.isGPSEnabled(enabled);
Expand All @@ -355,7 +370,7 @@ void handleSerial() {
}
} break;

case 'r':
case 't':
{
status = modem.enableGPSAutoStart(true);

Expand All @@ -366,7 +381,7 @@ void handleSerial() {
}
} break;

case 't':
case 'y':
{
status = modem.enableGPSAutoStart(false);

Expand All @@ -377,7 +392,7 @@ void handleSerial() {
}
} break;

case 'y':
case 'u':
{
bool enabled;
status = modem.getGPSAutoStart(enabled);
Expand All @@ -390,7 +405,7 @@ void handleSerial() {
}
} break;

case 'u':
case 'i':
{
static SIM7600::GPSData gps_data;
status = modem.getGPSData(gps_data);
Expand Down Expand Up @@ -427,7 +442,7 @@ void handleSerial() {
} break;

// TCP Client tests
case 'i':
case 'o':
{
status = modem.startTCPIPService();

Expand All @@ -438,7 +453,7 @@ void handleSerial() {
}
} break;

case 'o':
case 'p':
{
status = modem.stopTCPIPService();

Expand All @@ -449,7 +464,7 @@ void handleSerial() {
}
} break;

case 'p':
case 'a':
{
status = tcp.connectToHost(TCP_SERVER, TCP_PORT);

Expand All @@ -460,7 +475,7 @@ void handleSerial() {
}
} break;

case 'a':
case 's':
{
status = tcp.disconnect();

Expand All @@ -471,7 +486,7 @@ void handleSerial() {
}
} break;

case 's':
case 'd':
{
bool connected;
status = tcp.isConnected(connected);
Expand All @@ -483,7 +498,7 @@ void handleSerial() {
}
} break;

case 'd':
case 'f':
{
// Don't send the null terminator
static const size_t request_size = sizeof(TCP_HTTP_GET_REQ) - 1;
Expand All @@ -500,7 +515,7 @@ void handleSerial() {
}
} break;

case 'f':
case 'g':
{
size_t available = 0;
status = tcp.getAvailableBytes(available);
Expand All @@ -513,7 +528,7 @@ void handleSerial() {
}
} break;

case 'g':
case 'h':
{
static char rx_buffer[512];
size_t bytes_read = 0;
Expand All @@ -540,7 +555,7 @@ void handleSerial() {
} break;

// MQTT Client tests
case 'h':
case 'j':
{
status = modem.startMQTTService();

Expand All @@ -551,7 +566,7 @@ void handleSerial() {
}
} break;

case 'j':
case 'k':
{
status = modem.stopMQTTService();

Expand All @@ -562,7 +577,7 @@ void handleSerial() {
}
} break;

case 'k':
case 'l':
{
status = mqtt.acquireClient("sim7600", false, SIM7600::MQTTVersion::V3_1_1);

Expand All @@ -573,7 +588,7 @@ void handleSerial() {
}
} break;

case 'l':
case 'z':
{
status = mqtt.releaseClient();

Expand All @@ -584,7 +599,7 @@ void handleSerial() {
}
} break;

case 'z':
case 'x':
{
status =
mqtt.setLastWillMessage(MQTT_WILL_TOPIC, MQTT_WILL_MESSAGE, SIM7600::MQTTQoS::AtLeastOnce);
Expand All @@ -596,7 +611,7 @@ void handleSerial() {
}
} break;

case 'x':
case 'c':
{
status = mqtt.connect(MQTT_SERVER);

Expand All @@ -607,7 +622,7 @@ void handleSerial() {
}
} break;

case 'c':
case 'v':
{
status = mqtt.disconnect();

Expand All @@ -618,7 +633,7 @@ void handleSerial() {
}
} break;

case 'v':
case 'b':
{
bool connected;
status = mqtt.isConnected(connected);
Expand All @@ -630,7 +645,7 @@ void handleSerial() {
}
} break;

case 'b':
case 'n':
{
status = mqtt.subscribe(MQTT_TOPIC, SIM7600::MQTTQoS::AtLeastOnce);

Expand All @@ -642,7 +657,7 @@ void handleSerial() {
}
} break;

case 'n':
case 'm':
{
status = mqtt.unsubscribe(MQTT_TOPIC);

Expand All @@ -655,7 +670,7 @@ void handleSerial() {
}
} break;

case 'm':
case ',':
{
static char mqtt_payload[16];

Expand Down
3 changes: 3 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ build_flags =

; Enable USB CDC on boot
-DARDUINO_USB_CDC_ON_BOOT=1

; Enable library debug (for ESP32)
-DSIM7600_LOG_LEVEL=5
Loading