RAK11161 Breakout Board Wi-Fi Example Using RUI3
This example demonstrates how to connect the RAK11161 Breakout Board to a Wi-Fi access point in station mode using a RUI3 application.
The STM32WLE5 controls the ESP8684 through UART. The RUI3 APIs are used to control the ESP8684 power and serial interface, while ESP-AT commands configure the Wi-Fi connection.
Before using the examples in this guide, make sure you have completed the hardware setup and Arduino IDE installation described in the RAK11161 Breakout Board Quick Start Guide.
Initialize ESP8684 Communication
The STM32WLE5 controls the ESP8684 power through the PA0 GPIO pin and communicates with it through the Serial1 interface.
Set PA0 to HIGH to power up the ESP8684. Setting it to LOW shuts down the ESP8684.
Initialize Serial1 at 115200 baud, then send the AT command to check communication with the ESP8684. A successful connection returns OK.
Expected response is
AT
OK
Click to view the example code.
// Initialize interface to ESP8684
Serial1.begin(115200);
// Enable ESP8684
pinMode(WB_ESP8684, OUTPUT);
digitalWrite(WB_ESP8684, HIGH);
// Wait for ESP8684 bootup
bool found_esp8684 = false;
time_t start = millis();
while ((millis() - start) < 30000)
{
Serial1.println("AT");
Serial1.flush();
if (wait_ok_response(10000))
{
Serial.println("ESP8684 found");
found_esp8684 = true;
break;
}
delay(500);
}
Set Wi-Fi Station Mode
Set the ESP8684 to Wi-Fi station mode before connecting to a Wi-Fi access point.
Expected response is
AT+CWMODE=1,1
OK
Click to view the example code.
// Set ESP8684 into station mode and enable auto connect AT+CWMODE=1,1
// Clear send buffer
memset(wrx_buf, 0, 512);
snprintf(wrx_buf, 511, "AT+CWMODE=1,1\r\n");
Serial1.printf("%s", wrx_buf);
Serial1.flush();
if (wait_ok_response(10000))
{
Serial.printf("\r\nESP8684 mode set: ==>%s<==\r\n", wtx_buf);
}
else
{
Serial.printf("\r\nESP8684 mode failed: ==>%s<==\r\n", wtx_buf);
}
Connect to a Wi-Fi Access Point
Request connection to the selected WiFi AP.
Expected response is
AT+CWJAP="<MQTT_WIFI_APN>","<MQTT_WIFI_PW>"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
Click to view the example code.
// Connect ESP8684 to WiFi AP
// Clear send buffer
memset(wrx_buf, 0, 512);
snprintf(wrx_buf, 511, "AT+CWJAP=\"%s\",\"%s\"\r\n", MQTT_WIFI_APN, MQTT_WIFI_PW);
Serial1.printf("%s", wrx_buf);
Serial1.flush();
/** Expected response ********************
AT+cwjap="<MQTT_WIFI_APN>","<MQTT_WIFI_PW>"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
*****************************************/
if (wait_ok_response(10000))
{
Serial.printf("\r\nESP8684 connected: ==>%s<==\r\n", wtx_buf);
}
else
{
Serial.printf("\r\nESP8684 not connected: ==>%s<==\r\n", wtx_buf);
}
Complete Example Code
Click to view the example code.
#include <Arduino.h>
// Define enable pin for ESP8684
#define WB_ESP8684 PA0
/** WiFi TX buffer */
char wtx_buf[512];
/** WiFi RX buffer */
char wrx_buf[512];
/** WiFi AP name */
char MQTT_WIFI_APN[32] = "RAKwireless";
/** WiFi AP password */
char MQTT_WIFI_PW[32] = "RAKwireless";
/**
* @brief Wait for response from ESP8684
*
* @param timeout time to wait in milliseconds
* @param wait_for character array to wait for
* @return true "OK" string received
* @return false "OK" string not received, timeout
*/
bool wait_ok_response(time_t timeout)
{
time_t start = millis();
int buff_idx = 0;
bool got_ok = false;
// Clear TX buffer
memset(wtx_buf, 0, 512);
while ((millis() - start) < timeout)
{
if (Serial1.available() != 0)
{
char rcvd = Serial1.read();
wtx_buf[buff_idx] = rcvd;
buff_idx++;
if (buff_idx == 512)
{
// Buffer overflow, return false
return false;
}
if (strstr(wtx_buf, "OK") != NULL)
{
return true;
}
}
delay(5);
}
return false;
}
/**
* @brief Arduino setup function, called once
*
*/
void setup(void)
{
// Start Serial
Serial.begin(115200);
// Delay for 5 seconds to give the chance for AT+BOOT
delay(5000);
api.system.firmwareVersion.set("RAK11161-WIFI");
Serial.println("RAK11161 WiFi");
Serial.println("---------------------------------------------------------------");
Serial.println("Setup the device with WisToolBox or AT commands before using it");
Serial.println("---------------------------------------------------------------");
// Initialize interface to ESP8684
Serial1.begin(115200);
// Enable ESP8684
pinMode(WB_ESP8684, OUTPUT);
digitalWrite(WB_ESP8684, HIGH);
// Wait for ESP8684 bootup
bool found_esp8684 = false;
time_t start = millis();
while ((millis() - start) < 30000)
{
Serial1.println("AT");
Serial1.flush();
/** Expected response ********************
AT
OK
*****************************************/
if (wait_ok_response(10000))
{
Serial.println("ESP8684 found");
found_esp8684 = true;
break;
}
delay(500);
}
// Set ESP8684 into station mode and enable auto connect AT+CWMODE=1,1
// Clear send buffer
memset(wrx_buf, 0, 512);
snprintf(wrx_buf, 511, "AT+CWMODE=1,1\r\n");
Serial1.printf("%s", wrx_buf);
Serial1.flush();
/** Expected response ********************
AT+CWMODE=1,1
OK
*****************************************/
if (wait_ok_response(10000))
{
Serial.printf("\r\nESP8684 mode set: ==>%s<==\r\n", wtx_buf);
}
else
{
Serial.printf("\r\nESP8684 mode failed: ==>%s<==\r\n", wtx_buf);
}
// Connect ESP8684 to WiFi AP
// Clear send buffer
memset(wrx_buf, 0, 512);
snprintf(wrx_buf, 511, "AT+CWJAP=\"%s\",\"%s\"\r\n", MQTT_WIFI_APN, MQTT_WIFI_PW);
Serial1.printf("%s", wrx_buf);
Serial1.flush();
/** Expected response ********************
AT+cwjap="<MQTT_WIFI_APN>","<MQTT_WIFI_PW>"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
*****************************************/
if (wait_ok_response(10000))
{
Serial.printf("\r\nESP8684 connected: ==>%s<==\r\n", wtx_buf);
}
else
{
Serial.printf("\r\nESP8684 not connected: ==>%s<==\r\n", wtx_buf);
}
}
/**
* @brief Arduino loop. Not used
*
*/
void loop(void)
{
api.system.sleep.all();
}
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.
More RAK11161 examples, including a LoRa P2P-to-MQTT gateway application, are available in the RUI3 Best Practices repository.
