Hi,
I have a RAK 4631 with a RAK2305 WiFi board connected. I am using the example sketch ‘connect_ap’ to get WiFi connectivity, all which works fine.
What’s I’m trying to achieve is pushing data over Wifi to an MQTT server. There seem to be a ton of MQTT libraries available via the Arduino IDE. However, they all seem to require a Wifi ‘client’ object.
The example WiFi sketch is using AT commands to create the network connection, there’s no WiFi client object as such.
So… I have been looking at the EXPRESSIF documentation and I can see that there are AT commands to drive MQTT messaging, which I have been playing with (no joy so far).
My question(s) are:
- Is there a library to create a WiFi client for this board, which I could then use with some of the ‘off the shelf’ MQTT libraries?
- Can someone help me decipher why my AT commands for MQTT aren’t working…?
My current sketch is below, which is just the ap_connect example with what I think is the right MQTT AT command structure.
TIA!
/**
* @file connect_ap.ino
* @author Taylor lee ([email protected])
* @brief connect a known AP and PING the IP "8.8.8.8" every 5 seconds
* Please see the detailed commands from
* https://docs.espressif.com/projects/esp-at/en/latest/AT_Command_Set/index.html
* @version 0.1
* @date 2020-07-28
*
* @copyright Copyright (c) 2020
*
* @note RAK4631 GPIO mapping to nRF52840 GPIO ports
RAK4631 <-> nRF52840
WB_IO1 <-> P0.17 (GPIO 17)
WB_IO2 <-> P1.02 (GPIO 34)
WB_IO3 <-> P0.21 (GPIO 21)
WB_IO4 <-> P0.04 (GPIO 4)
WB_IO5 <-> P0.09 (GPIO 9)
WB_IO6 <-> P0.10 (GPIO 10)
WB_SW1 <-> P0.01 (GPIO 1)
WB_A0 <-> P0.04/AIN2 (AnalogIn A2)
WB_A1 <-> P0.31/AIN7 (AnalogIn A7)
*/
#include <Wire.h>
#define WIFI_SSID "My-Wifi"
#define WIFI_PASSWORD "secretsquirrel"
/**
@brief execute at command
@param at: the at command you want to execute
@param expect: the respond you want to get
@param timeout: the timout of receive respond
*/
void execute_at(char *at, char *expect = NULL, int32_t timeout = 1000)
{
String resp = "";
Serial1.write(at);
delay(10);
while (timeout--)
{
if (Serial1.available())
{
resp += Serial1.readString();
}
delay(1);
}
Serial.println(resp);
if (expect != NULL)
{
if (resp.indexOf(expect) != -1)
{
Serial.println("Execute OK.");
}
else
{
Serial.println("Execute Fail.");
}
}
resp = "";
}
/**
@brief Arduino setup function. Called once after power on or reset
*/
void setup()
{
char cmd[128] = "";
time_t timeout = millis();
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
Serial.println("================================");
Serial.println("RAK2305 WiFI example");
Serial.println("================================");
Serial1.begin(115200);
delay(1000);
// Set RAK2305 as AP and STA role
execute_at("AT+CWMODE=3\r\n", "OK");
// Set contry code
execute_at("AT+CWCOUNTRY=0,\"AU\",1,13\r\n", "OK");
// Connect AP with ssid and password
snprintf(cmd, sizeof(cmd), "AT+CWJAP=\"%s\",\"%s\"\r\n", WIFI_SSID, WIFI_PASSWORD);
execute_at(cmd, "OK");
}
/**
@brief Arduino loop. Runs forever until power off or reset
*/
void loop()
{
Serial1.println("AT+PING=\"8.8.8.8\"");
// ping 8.8.8.8
execute_at("AT+QPING=1,\"8.8.8.8\"\r\n", "OK");
//execute_at("AT+MQTTUSERCFG=0,1,\"ESP32\",\"espressif\",\"1234567890\",0,0,\"");
execute_at("AT+MQTTCONN=0,\"192.168.0.91\",1883,0");
execute_at("AT+MQTTPUB=0,\"topic\",\"test\",1,0");
execute_at("AT+MQTTCLEAN=0");
delay(5000);
}
type or paste code here