Hi,
I’m using AMB82 mini to test MQTT connection.
Here is the code based on the MQTT example code. Didn’t change much from the example.
Arduino IDE = 2.3.4
Hardware: Realtek AMB82-mini AI Camera (Ameba RTL8735B)
Tried borker: test.mosquitto.org, mqttgo.io
WiFi RSSI: -53
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "crzL"; // your network SSID (name)
char pass[] = "00000000"; // your network password
int status = WL_IDLE_STATUS; // Indicator of Wifi status
char mqttServer[] = "test.mosquitto.org";
char clientId[] = "amebaClient-000005";
char publishTopic[] = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)(payload[i]));
}
Serial.println();
}
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void reconnect()
{
// Loop until we're reconnected
while (!(client.connected())) {
Serial.print("\r\nAttempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
// Once connected, publish an announcement and resubscribe
client.publish(publishTopic, publishPayload);
client.subscribe(subscribeTopic);
} else {
Serial.println("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
// Initialize serial and wait for port to open:
Serial.begin(115200);
// wait for serial port to connect.
while (!Serial) {
;
}
// Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("\r\nAttempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
client.setServer(mqttServer, 1883);
client.setCallback(callback);
// Allow Hardware to sort itself out
delay(1500);
}
void loop()
{
if (!(client.connected())) {
reconnect();
}
client.loop();
}
The connection works however it disconnected immediately. It keeps reconnect to the server every 5 seconds and never prints connected.
Here’s the output:
16:44:30.730 -> Attempting MQTT connection...
16:44:31.374 -> [INFO] Create socket successfully
16:44:31.697 ->
16:44:31.697 -> [INFO] Connect to Server successfully!
16:45:31.691 -> failed, rc=
16:45:31.691 -> -4 try again in 5 seconds
16:45:36.661 ->
16:45:36.661 -> Attempting MQTT connection...
16:45:36.707 -> [INFO] Create socket successfully
16:45:37.253 ->
16:45:37.253 -> [INFO] Connect to Server successfully!
16:46:37.290 -> failed, rc=
16:46:37.290 -> -4 try again in 5 seconds
16:46:42.287 ->
16:46:42.287 -> Attempting MQTT connection...
What SDK version are you using? Can you try mqtt.eclipseprojects.io as the broker? Sometimes I tried test.mosquitto.org, it is not as stable as the other public broker.
You will also need to set non blocking mode for your wifiClient.
I’m using Ameba SDK version = 4.0.8
I tried mqtt.eclipseprojects.io but the issue remains.
I’m quite new with Arduino. As far as i know, the wi-fi is currently working with non blocking mode?
Here is the code updated:
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "SSID";
char pass[] = "password";
int status = WL_IDLE_STATUS;
char mqttServer[] = "mqtt.eclipseprojects.io";
const int mqttPort = 1883;
//char clientId[] = "mqttamb_6e82b1fa";
char mqttUser[] = ""; // MQTT username
char mqttPassword[] = ""; // MQTT password
char publishTopic[] = "testtopicAerc/topic";
char publishPayload[] = "hello world yoyoyo";
char subscribeTopic[] = "inTestaerc";
unsigned long previousMillis = 0;
unsigned long mqttReconnectPreviousMillis = 0;
const long mqttReconnectInterval = 5000;
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)(payload[i]));
}
Serial.println();
}
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void setup()
{
// Initialize serial and wait for port to open:
Serial.begin(115200);
// wait for serial port to connect.
while (!Serial) {
;
}
// Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("\r\nAttempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(1000);
}
Serial.println("\nWiFi connected!");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
// Allow Hardware to sort itself out
delay(1500);
}
void reconnectNonBlocking() {
if (!client.connected()) {
unsigned long currentMillis = millis();
String clientId = "Device-" + String(random(0xffff), HEX);
client.setKeepAlive(60);
if (currentMillis - mqttReconnectPreviousMillis >= mqttReconnectInterval) {
mqttReconnectPreviousMillis = currentMillis;
Serial.println("Attempting MQTT connection...");
if (client.connect(clientId.c_str(), "", "")) {
Serial.println("MQTT connected");
client.publish(publishTopic, publishPayload);
client.subscribe(subscribeTopic);
Serial.println("Message published");
} else {
Serial.println("MQTT connection failed. Retrying...");
}
}
}
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 10000) {
previousMillis = currentMillis;
Serial.println("Attempting WiFi connection...");
WiFi.begin(ssid, pass);
}
} else {
Serial.println("WiFi connected");
}
if (!(client.connected()))
{
reconnectNonBlocking();
}
client.loop();
}
It seems to connect the broker for a short time and disconnects immediately. Here is the output: