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()inmain.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()inmain.ino). - No Saved Credentials: If no credentials are found, the OLED will display "No Wi-Fi Set via App" (
connectToWiFi()inmain.ino). - Connection Status:
- The state of the Wi-Fi connection (
wifi_connectedboolean) 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.inoandmain.ino). - Wi-Fi signal strength (
wifi_signal_strength) is obtained usingWiFi.RSSI()after a successful connection and can be sent via MQTT (sendWifiMqttData()inMQTT.ino).
- The state of the Wi-Fi connection (
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()inSMARTA_L_v2.ino). - MQTT Connection: Once Wi-Fi is connected and internet access is confirmed, the device establishes an MQTT connection (
reconnectWiFiMqtt()inMQTT.ino). The MQTT server, port, and credentials are defined inconfig.h. - Automatic Reconnection: The system attempts to reconnect to MQTT if the connection drops (
reconnectWiFiMqtt()). - Failover to LTE: If
wifiOnlyisfalse(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
rebootOnConnectivityLossis 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()inSMARTA_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()andsendWifiMqttData()(inMQTT.ino). - MQTT Subscriptions: The device subscribes to topics for receiving commands, configuration updates, and OTA update instructions (
wifiCallback()inMQTT.ino,reconnectWiFiMqtt()inMQTT.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.mdandWifiCredentialsCallbacks::onRead()inmain.ino).
- UUID:
- Reset Wi-Fi Credentials:
- Via MQTT command (
"CW"message type inhandleMessage()inMQTT.ino): Sets credentials to a default "SMARTA HOTSPOT" / "SMARTA123" and reboots. resetWiFiSettings()function inmain.inocan clear Wi-Fi settings from preferences.
- Via MQTT command (
wifiOnlySetting: 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()inMQTT.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'swifiCallbackfor OTA topics, andinitializeOTA()inmain.ino). - OTA check is typically performed after a stable Wi-Fi connection is established for a certain duration (
loop()inSMARTA_L_v2.ino).
Refer to main.ino, SMARTA_L_v2.ino, MQTT.ino, OLED.ino, and config.h for detailed implementation.