Comparing 4G and WiFi Modules
4G modules require a SIM card with active data plan to establish internet connectivity, similar to mobile devices. WiFi modules connect through existing router networks.
Key differences:
- 4G modules provide mboility with cellular coverage
- WiFi requires fixed infrastructure
- Both require valid credentials (SIM data plan for 4G, WiFi password for routers)
Hardware Setup
Module Initialization
Essential AT commands (include \r\n termination):
ATE1- Enable command echoAT+CPIN?- Verify SIM card presenceAT+CREG?- Check network registrationAT+CSQ- Signal strength indicatorAT+CGSN- Retrieve device IMEI
/**
* @brief Verify EC20 module readiness
* @return EC20_OK if initialization succeeds
*/
uint8_t check_module_status() {
char signal_strength[BUFFER_SIZE] = {0};
// Basic communication test
if(!send_at_command("AT", "OK", 150)) {
if(!send_at_command("AT", "OK", 200)) {
return COMM_FAILURE;
}
}
if(!send_at_command("ATE1", "OK", 150)) {
return COMM_FAILURE;
}
if(!send_at_command("AT+CPIN?", "READY", 150)) {
return SIM_MISSING;
}
// Network registration check
char* reg_status = get_response_buffer();
if(!send_at_command("AT+CREG?", "2,1", 150)) {
if(!strstr(reg_status, "0,1") && !strstr(reg_status, "0,5")) {
if(send_at_command("AT+CSQ", "OK", 200)) {
strncpy(signal_strength, reg_status+15, 2);
}
return NETWORK_FAILURE;
}
}
if(!send_at_command("AT+CGSN", "OK", 150)) {
return IMEI_ERROR;
}
store_imei(reg_status+10);
return EC20_OK;
}
MQTT Connection Setup
// Establish MQTT connection sequence
int setup_mqtt_connection() {
if(!send_at_command("AT+CGATT?", ": 1", 140)) return 1;
if(!send_at_command("AT+QIACT?", "OK", 100)) return 2;
if(!send_at_command("AT+QIDEACT=1", "OK", 120)) return 3;
// MQTT configuration
if(!send_at_command("AT+QMTCFG=\"recv/mode\",0,0,1", "OK", 250)) return 12;
char cmd_buffer[BUFFER_SIZE];
snprintf(cmd_buffer, sizeof(cmd_buffer),
"AT+QMTOPEN=0,\"%s\",%d",
mqtt_server, mqtt_port);
if(!send_at_command(cmd_buffer, "+QMTOPEN: 0,0", 1500)) return 13;
snprintf(cmd_buffer, sizeof(cmd_buffer),
"AT+QMTCONN=0,\"%s\",\"%s\",\"%s\"",
client_id, username, password);
if(!send_at_command(cmd_buffer, "+QMTCONN: 0,0,0", 250)) return 14;
return 0;
}
Troubleshooting
Common connectivity issues:
-
No Data Plan:
- Verify SIM with
AT+QCCID - Check
AT+CREG?response (0,0 or 0,3 indicates issues) AT+CGATT?returning 0 means no network connection
- Verify SIM with
-
Performance Factors:
- Antenna positioning (optimal outdoor placement)
- Cellular tower proximity
- Debugger interference (disconnect SWD during operation)
- Command timing (adjust based on empirical testing)
Useful AT Commands
Location Tracking:
AT+COPS=3,2
AT+COPS?
AT+CREG=2
AT+CREG?
SMS Operations:
AT+CMGF=1
AT+CMGS="phone_number"
message_content
Ctrl+Z (0x1A)
Time Synchronization:
AT+QLTS=2