AMB82mini MQTT connection

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...

Hi @zylee,

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.

Thank you.

Hi @Kelvin_Huang ,

Thanks a lot for your replying.

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:

18:39:58.065 -> WiFi connected!

18:39:59.577 -> WiFi connected

18:39:59.577 -> Attempting MQTT connection...

18:39:59.856 ->

18:39:59.856 -> [INFO] Create socket successfully

18:40:00.027 ->

18:40:00.027 -> [INFO] Connect to Server successfully!

18:40:15.019 -> MQTT connection failed. Retrying...

18:40:15.053 -> WiFi connected

18:40:15.053 -> Attempting MQTT connection...

18:40:15.053 ->

18:40:15.053 -> [INFO] Create socket successfully

18:40:15.224 ->

18:40:15.224 -> [INFO] Connect to Server successfully!

18:40:30.272 -> MQTT connection failed. Retrying...

18:40:30.272 -> WiFi connected

18:40:30.272 -> Attempting MQTT connection...

18:40:30.272 ->

您好 @zylee,

您需要在set up loop 裏,加入wifiClient.setNonBlockingMode();

可以先這樣試試看。

謝謝您。

您好 @Kelvin_Huang ,

可以連上了,謝謝您!

1 Like