Skip to content

Wi-Fi Connectivity

Overview

The base station connects to Wi-Fi networks for data transmission, remote monitoring, real-time updates, and Over-the-Air (OTA) firmware upgrades. When connected, the device sends status reports and sensor data via MQTT and can receive commands from the backend server. Wi-Fi is the primary means of communication, with LTE as a fallback if wifiOnly is set to false.

Key Features

Automatic Wi-Fi Connection

  • Startup Scan & Connection: The system searches for known (previously saved) Wi-Fi networks during startup (connectToWiFi() in main.ino).
  • Stored Credentials: If valid credentials (SSID and password) are stored in preferences, it attempts to connect automatically.
  • Credential Provisioning via BLE: New Wi-Fi credentials (SSID and password) can be provided and saved via the BLE "Wi-Fi Credentials" characteristic (f3b5a8d4-2b6e-4a1b-8b1e-3c4d5e6f7a94). The credentials sent from the mobile app are XOR-encrypted using the device's MAC address as the key. Upon successful saving, the device restarts to apply the new settings (WifiCredentialsCallbacks::onWrite() in main.ino).
  • No Saved Credentials: If no credentials are found, the OLED will display "No Wi-Fi Set via App" (connectToWiFi() in main.ino).
  • Connection Status:
    • The state of the Wi-Fi connection (wifi_connected boolean) is maintained and used throughout the system.
    • The OLED display shows Wi-Fi status messages like "WAITING FOR WIFI", "Connecting to Wi-Fi...", "Wi-Fi connected!", and "Wi-Fi connection failed" (various functions in OLED.ino and main.ino).
    • Wi-Fi signal strength (wifi_signal_strength) is obtained using WiFi.RSSI() after a successful connection and can be sent via MQTT (sendWifiMqttData() in MQTT.ino).

Connectivity Management

  • Primary Communication: Wi-Fi is the preferred method for MQTT communication.
  • Internet Check: After connecting to Wi-Fi, the system verifies actual internet connectivity by pinging google.com (connectivityCheck() in SMARTA_L_v2.ino).
  • MQTT Connection: Once Wi-Fi is connected and internet access is confirmed, the device establishes an MQTT connection (reconnectWiFiMqtt() in MQTT.ino). The MQTT server, port, and credentials are defined in config.h.
  • Automatic Reconnection: The system attempts to reconnect to MQTT if the connection drops (reconnectWiFiMqtt()).
  • Failover to LTE: If wifiOnly is false (configurable via BLE/MQTT, stored in preferences), and Wi-Fi connection fails or is unavailable, the system will attempt to use LTE connectivity. If Wi-Fi becomes available, it will switch back.
  • Reboot on Prolonged Connectivity Loss: If rebootOnConnectivityLoss is true (default, configurable), and both Wi-Fi and LTE are unavailable for a defined number of attempts (internet_reconnect_attempts), the device will automatically reboot (connectivityCheck() in SMARTA_L_v2.ino).

Data Transmission

  • MQTT Publishing: Sensor data, device status, version information, and other relevant data are published to the MQTT broker over Wi-Fi using publishWifiMqttMessage() and sendWifiMqttData() (in MQTT.ino).
  • MQTT Subscriptions: The device subscribes to topics for receiving commands, configuration updates, and OTA update instructions (wifiCallback() in MQTT.ino, reconnectWiFiMqtt() in MQTT.ino).

Configuration & Diagnostics

  • BLE Wi-Fi Credentials Characteristic:
    • UUID: f3b5a8d4-2b6e-4a1b-8b1e-3c4d5e6f7a94
    • Write: Accepts XOR-encrypted SSID and password. The firmware decrypts these using the device MAC address as a key, saves them to preferences, and reboots.
    • Read/Notify: Can expose the currently configured SSID and password (potentially in plaintext for debugging, as noted in ble.md and WifiCredentialsCallbacks::onRead() in main.ino).
  • Reset Wi-Fi Credentials:
    • Via MQTT command ("CW" message type in handleMessage() in MQTT.ino): Sets credentials to a default "SMARTA HOTSPOT" / "SMARTA123" and reboots.
    • resetWiFiSettings() function in main.ino can clear Wi-Fi settings from preferences.
  • wifiOnly Setting: A boolean flag (wifiOnly) stored in preferences determines if the device should exclusively use Wi-Fi or can failover to LTE. This can be set via MQTT (handleMessage() in MQTT.ino).

Over-the-Air (OTA) Updates

  • Wi-Fi connectivity is essential for receiving OTA firmware updates.
  • OTA updates can be initiated via MQTT commands, with the firmware downloaded over the active Wi-Fi connection (details in OTAHandler.h, MQTT.ino's wifiCallback for OTA topics, and initializeOTA() in main.ino).
  • OTA check is typically performed after a stable Wi-Fi connection is established for a certain duration (loop() in SMARTA_L_v2.ino).

Refer to main.ino, SMARTA_L_v2.ino, MQTT.ino, OLED.ino, and config.h for detailed implementation.