RAK11161 Breakout Board Wi-Fi Example Using AT Commands
The RAK11161 Breakout Board integrates an STM32WLE5 MCU and an ESP8684 co-processor. The STM32WLE5 handles LoRa communication, while the ESP8684 provides Wi-Fi and Bluetooth Low Energy (BLE) connectivity.
This example demonstrates how to configure the ESP8684 using ESP-AT commands sent through the STM32WLE5 UART interface. It covers connecting to a Wi-Fi access point and communicating with an MQTT broker.
Before configuring the ESP8684 module using AT commands, connect the module to your computer using a USB-to-UART TTL adapter. For detailed instructions, refer to the RAK11161 Breakout Board Quick Start Guide.
There are many options to setup the ESP8684, only the basic commands are shown here. For a full description how to control the ESP8684, please check the Espressif AT command manual.
Enable ESP8684 UART Loop-Through Mode
To send AT commands from the console or an external host MCU, you have to change the UART mode of the STM32WLE5 to loop-through mode. This is done with the AT command AT+ESP=x.
To enable loop-through mode:
AT+ESP=1
To disable loop-through mode:
AT+ESP=0
Connect to a Wi-Fi Access Point
To connect to a WiFi AP, the ESP8684 must be set to station mode. Then the AP name and AP access password must be provided. The Wi-Fi SSID and password are stored in the ESP8684 and reused after reboot to reconnect to the access point automatically.
Connect to a WiFi AP and enable auto-connect:
AT+ESP=1
AT+CWMODE=1,1
AT+CWJAP="<MQTT_WIFI_APN>","<MQTT_WIFI_PW>"
Replace <MQTT_WIFI_APN> and <MQTT_WIFI_PW> with the AP name and access credential of the WiFi access point.
If the ESP8684 can successfully connect to the WiFi AP, it will respond with
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
Connect to an MQTT Broker over Wi-Fi
To connect to the MQTT broker two AT commands are required to setup the connection.
Define user:
AT+MQTTUSERCFG=0,1,"<MQTT_USER>","<MQTT_USERNAME>","<MQTT_PASSWORD>",0,0,""
Replace <MQTT_USER>, <MQTT_USERNAME> and <MQTT_PASSWORD> with the user ID, username and password required to connect.
Define connection and start connection:
AT+MQTTCONN=0,"<MQTT_URL>",<MQTT_PORT>,0
Replace <MQTT_URL> and <MQTT_PORT> with the URL or IP address of the MQTT broker and the used port.
If the setup and connection is successful, the ESP8684 will response with OK and +MQTTCONNECTED:
AT+MQTTUSERCFG=0,1,"<MQTT_USER>","<MQTT_USERNAME>","<MQTT_PASSWORD>",0,0,""
OK
AT+MQTTCONN=0,"<MQTT_URL>",<MQTT_PORT>,0
+MQTTCONNECTED:0,1,"<MQTT_URL>","<MQTT_PORT>","",0
OK
Then it is possible to publish data with:
AT+MQTTPUB=0,"<MQTT_PUB>","<data>",0,0
Replace <MQTT_PUB> with the topic the data should be published to
Replace <data> with a string with the data to be published.
It is as well possible to subscribe to a topic on the MQTT broker with:
AT+MQTTSUB=0,"<MQTT_TOPIC>",1
Replace <MQTT_TOPIC> with the topic to subscribe to.
The received data of the subscription will be reported by the ESP8684 with an asynchronous event. To receive this event, the loop-through of the ESP8684 to the host MCU must stay enabled.
