📶 Phase 5

4G LTE MQTT Integration

WiFi se aage jakar field deployment style MQTT over LTE architecture banana

📋 Topics Covered in Phase 5

14G LTE module overview
2TinyGSM setup and network bring-up
3MQTT communication over LTE
4Auto reconnect strategy for unstable network
5Production-ready IoT system blueprint

📱 Step 1: 4G LTE Module Overview

💡

Popular 4G/LTE Modules for IoT:
• SIM7600E-H (4G LTE, global bands) - Recommended
• SIM800L (2G/GPRS) - Budget option
• SIM7000E (NB-IoT + LTE) - Low power option
• Quectel EG95 (4G LTE) - Industrial grade

Hardware Connections

// SIM7600E-H Pin Connections to ESP32 // =========================================== // Hardware Wiring // =========================================== ESP32 SIM7600E-H ------- ------------ GPIO16 (RX) → TX GPIO17 (TX) → RX GND → GND 3.3V/5V → VBUS (Power) (Extra) → PWRKEY (via 10K resistor) (Extra) → RESET (optional) // Optional Power Control // Power on the module automatically or via code

⚙️ Step 2: TinyGSM Library Setup

// Install these libraries via Arduino Library Manager: // 1. TinyGSM by Volodymyr Shymanskyy // 2. PubSubClient by Nick O'Leary // 3. ArduinoJson by Benoit Blanchon // =========================================== // esp32-lte-mqtt.h - LTE MQTT Configuration // =========================================== #include <TinyGsmClient.h> #include <PubSubClient.h> #include <ArduinoJson.h> // =========================================== // Serial pins for SIM7600 // =========================================== HardwareSerial SerialLTE(1); // Use second UART const int LTE_RX = 16; const int LTE_TX = 17; // =========================================== // APN Configuration // IMPORTANT: Change these for your carrier! // =========================================== const char* APN = "internet"; // Common for most carriers const char* USER = ""; // Leave empty if not needed const char* PWD = ""; // =========================================== // MQTT Configuration // =========================================== const char* MQTT_BROKER = "broker.example.com"; const int MQTT_PORT = 1883; const char* DEVICE_ID = "lte-device-001"; const char* MQTT_USER = ""; const char* MQTT_PASSWORD = ""; // MQTT Topics const char* TOPIC_CMD = "lte/device/001/command"; const char* TOPIC_STATUS = "lte/device/001/status"; const char* TOPIC_TELEMETRY = "lte/device/001/telemetry"; // =========================================== // Global Objects // =========================================== TinyGsm modem(SerialLTE); TinyGsmClient gsmClient(modem); PubSubClient mqttClient(gsmClient);

📝 Step 3: Complete LTE MQTT Sketch

// esp32-4g-lte-mqtt.ino // Complete ESP32 + SIM7600 4G LTE MQTT Client // Supports auto-reconnect and handles network instability #include <TinyGsmClient.h> #include <PubSubClient.h> #include <ArduinoJson.h> // =========================================== // Configuration - CHANGE THESE! // =========================================== // Serial for LTE module HardwareSerial SerialLTE(1); const int LTE_RX_PIN = 16; const int LTE_TX_PIN = 17; // LTE APN - Get from your carrier! const char* APN = "airtelgprs.com"; // Airtel India // const char* APN = "internet"; // Generic // const char* APN = "data.mtnl.net.in"; // MTNL Delhi // MQTT Broker const char* MQTT_BROKER = "your-broker-domain.com"; const int MQTT_PORT = 1883; const char* DEVICE_ID = "lte-001"; const char* MQTT_USER = ""; const char* MQTT_PASSWORD = ""; // Topics const char* TOPIC_CMD = "lte/" DEVICE_ID "/command"; const char* TOPIC_STATUS = "lte/" DEVICE_ID "/status"; const char* TOPIC_TELEMETRY = "lte/" DEVICE_ID "/telemetry"; // Hardware const int RELAY_PIN = 26; const int LED_PIN = 2; // =========================================== // Global Objects // =========================================== TinyGsm modem(SerialLTE); TinyGsmClient gsmClient(modem); PubSubClient mqttClient(gsmClient); // State bool relayState = false; bool networkConnected = false; bool mqttConnected = false; unsigned long lastTelemetry = 0; const int TELEMETRY_INTERVAL = 60000; // 60 seconds // =========================================== // SETUP // =========================================== void setup() { Serial.begin(115200); SerialLTE.begin(115200, SERIAL_8N1, LTE_RX_PIN, LTE_TX_PIN); pinMode(RELAY_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT); digitalWrite(RELAY_PIN, LOW); Serial.println("🚀 LTE MQTT Client Starting..."); // Initialize modem initModem(); // Setup MQTT mqttClient.setServer(MQTT_BROKER, MQTT_PORT); mqttClient.setCallback(mqttCallback); Serial.println("✅ Setup Complete!"); } // =========================================== // MAIN LOOP // =========================================== void loop() { // Check network status if (!modem.isNetworkConnected()) { Serial.println("⚠️ Network disconnected!"); networkConnected = false; digitalWrite(LED_PIN, LOW); connectNetwork(); } else { networkConnected = true; digitalWrite(LED_PIN, HIGH); } // Maintain MQTT if (networkConnected && !mqttClient.connected()) { Serial.println("⚠️ MQTT disconnected! Reconnecting..."); connectMQTT(); } mqttClient.loop(); // Send telemetry if (mqttConnected && millis() - lastTelemetry > TELEMETRY_INTERVAL) { sendTelemetry(); lastTelemetry = millis(); } delay(100); } // =========================================== // Modem Initialization // =========================================== void initModem() { Serial.println("🔄 Initializing modem..."); // Reset modem modem.restart(); delay(3000); // Get modem info String modemName = modem.getModemName(); Serial.printf("📱 Modem: %s\n", modemName.c_str()); // Wait for network connectNetwork(); } // =========================================== // Network Connection // =========================================== void connectNetwork() { Serial.println("📶 Connecting to network..."); // Wait for network registration int attempts = 0; while (!modem.waitForNetwork() && attempts < 30) { Serial.print("."); delay(1000); attempts++; } if (modem.isNetworkConnected()) { Serial.println(); Serial.println("✅ Network connected!"); // Connect to GPRS connectGPRS(); } else { Serial.println(); Serial.println("❌ Network failed!"); } } void connectGPRS() { Serial.print("🌐 Connecting GPRS with APN: "); Serial.println(APN); // Set APN modem.setNetworkMode(38); // LTE only modem.setPreferredMode(1); // Connect with GPRS bool success = modem.gprsConnect(APN, USER, PWD); if (success) { Serial.println("✅ GPRS connected!"); // Show signal quality int rssi = modem.getSignalQuality(); Serial.printf("📶 Signal quality: %d\n", rssi); // Get IP Serial.printf("🌍 IP: %s\n", modem.getLocalIP().c_str()); } else { Serial.println("❌ GPRS failed!"); } } // =========================================== // MQTT Connection // =========================================== void connectMQTT() { Serial.print("🔌 Connecting to MQTT broker: "); Serial.println(MQTT_BROKER); // Generate unique client ID char clientID[50]; snprintf(clientID, sizeof(clientID), "%s-%lu", DEVICE_ID, millis()); // Connect bool success; if (strlen(MQTT_USER) > 0) { success = mqttClient.connect(clientID, MQTT_USER, MQTT_PASSWORD); } else { success = mqttClient.connect(clientID); } if (success) { Serial.println("✅ MQTT connected!"); mqttConnected = true; // Subscribe mqttClient.subscribe(TOPIC_CMD); Serial.printf("📥 Subscribed: %s\n", TOPIC_CMD); // Publish online status publishStatus("online"); } else { Serial.printf("❌ MQTT failed, rc=%d\n", mqttClient.state()); mqttConnected = false; } } // =========================================== // MQTT Message Handler // =========================================== void mqttCallback(char* topic, byte* payload, unsigned int length) { char msg[length + 1]; memcpy(msg, payload, length); msg[length] = '\0'; Serial.printf("📩 [%s]: %s\n", topic, msg); // Handle commands if (strcmp(msg, "ON") == 0) { relayState = true; digitalWrite(RELAY_PIN, HIGH); publishStatus("ON"); } else if (strcmp(msg, "OFF") == 0) { relayState = false; digitalWrite(RELAY_PIN, LOW); publishStatus("OFF"); } else if (strcmp(msg, "STATUS") == 0) { publishStatus(relayState ? "ON" : "OFF"); } } // =========================================== // Publish Functions // =========================================== void publishStatus(const char* state) { char payload[200]; snprintf(payload, sizeof(payload), "{\"device\":\"%s\",\"state\":\"%s\",\"rssi\":%d,\"ip\":\"%s\",\"uptime\":%lu}", DEVICE_ID, state, modem.getSignalQuality(), modem.getLocalIP().c_str(), millis()/1000 ); mqttClient.publish(TOPIC_STATUS, payload, true); Serial.printf("📤 Status: %s\n", payload); } void sendTelemetry() { char payload[200]; snprintf(payload, sizeof(payload), "{\"device\":\"%s\",\"temperature\":%.1f,\"battery\":%.2f,\"rssi\":%d,\"uptime\":%lu}", DEVICE_ID, temperatureRead(), // Read actual sensor values here 3.7, // Battery voltage example modem.getSignalQuality(), millis()/1000 ); mqttClient.publish(TOPIC_TELEMETRY, payload); Serial.printf("📤 Telemetry: %s\n", payload); }
⚠️

APN Configuration: APN settings carrier-specific hain. Apne carrier ka APN website se check karo ya customer care se poocho. Wrong APN se GPRS connect nahi hoga.

🔄 Step 4: Auto Reconnect Strategy

// =========================================== // Production-Grade Reconnect Logic // =========================================== // Exponential backoff for reconnections const int INITIAL_RETRY_DELAY = 1000; // 1 second const int MAX_RETRY_DELAY = 300000; // 5 minutes const int MAX_RETRY_COUNT = 10; int retryCount = 0; unsigned long lastRetryTime = 0; int currentRetryDelay = INITIAL_RETRY_DELAY; // Check if we should retry connection bool shouldRetry() { if (retryCount >= MAX_RETRY_COUNT) { Serial.println("⚠️ Max retries reached! Resetting counter."); retryCount = 0; currentRetryDelay = INITIAL_RETRY_DELAY; return true; } if (millis() - lastRetryTime >= currentRetryDelay) { return true; } return false; } // Handle connection failure with backoff void handleConnectionFailure() { retryCount++; lastRetryTime = millis(); // Exponential backoff currentRetryDelay = min(currentRetryDelay * 2, MAX_RETRY_DELAY); Serial.printf("⚠️ Retry %d/%d in %d ms\n", retryCount, MAX_RETRY_COUNT, currentRetryDelay); } // Reset retry state on successful connection void resetRetryState() { retryCount = 0; currentRetryDelay = INITIAL_RETRY_DELAY; lastRetryTime = 0; } // Watchdog timer to detect hangs void feedWatchdog() { // ESP32 software watchdog yield(); // Feed watchdog } // Deep sleep to conserve power during network issues void enterPowerSaveMode(int sleepSeconds) { Serial.printf("😴 Entering sleep for %d seconds\n", sleepSeconds); modem.gprsDisconnect(); // esp_sleep_enable_timer_wakeup(sleepSeconds * 1000000); // esp_deep_sleep_start(); }

🏭 Step 5: Production IoT System Blueprint

// =========================================== // Production Architecture Overview // =========================================== /* ┌─────────────────────────────────────────────────────────────┐ │ FIELD DEVICES │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ ESP32+LTE │ │ ESP32+LTE │ │ ESP32+LTE │ │ │ │ Device 001 │ │ Device 002 │ │ Device N │ │ │ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ │ │ │ │ │ │ │ └────────────────┼────────────────┘ │ │ │ │ │ 4G/LTE Network │ └──────────────────────────┼──────────────────────────────────┘ │ ▼ ┌──────────────────────────────────────────────────────────────┐ │ CLOUD LAYER │ │ │ │ ┌──────────────────┐ ┌──────────────────┐ │ │ │ MQTT Broker │ │ Load Balancer │ │ │ │ (Mosquitto) │◄────│ + TLS/SSL │ │ │ │ Cluster │ │ Termination │ │ │ └────────┬─────────┘ └──────────────────┘ │ │ │ │ │ ├──────────────────────────────────┐ │ │ │ │ │ │ ▼ ▼ │ │ ┌──────────────────┐ ┌──────────────────┐ │ │ │ Node.js Backend │ │ Web Dashboard │ │ │ │ - Auth │ │ (Real-time) │ │ │ │ - Data Process │ │ - MQTT over WS │ │ │ │ - Device Mgmt │ │ │ │ │ └──────────────────┘ └──────────────────┘ │ │ │ │ │ ▼ │ │ ┌──────────────────┐ │ │ │ Database │ │ │ │ - Timeseries │ │ │ │ - Device State │ │ │ └──────────────────┘ │ └──────────────────────────────────────────────────────────────┘ */ // =========================================== // Production Checklist // =========================================== // 1. Security // - [ ] TLS/SSL for MQTT (port 8883) // - [ ] Device authentication with certificates // - [ ] API authentication (JWT) // - [ ] Encrypted storage for credentials // 2. Reliability // - [ ] QoS 1 for commands // - [ ] Retained messages for state // - [ ] LWT (Last Will Testament) for offline detection // - [ ] Exponential backoff for reconnects // 3. Monitoring // - [ ] Central logging // - [ ] Alerting for offline devices // - [ ] Data freshness monitoring // - [ ] Bandwidth usage tracking // 4. Cost Optimization // - [ ] Data compression // - [ ] Batched telemetry (not per-second) // - [ ] Wake-on-schedule instead of always-on // - [ ] Data deduplication