MQTT with Boron 2G/3G

hey,
I am new to particle and was trying out the MQTT program. I set up a local mosquitto sever in my pc and was trying to connect Boron to my broker. I am using the basic MQTT library from the web ide. Mosquitto broker is working, as I checked with MQTT fx and the local client also but the boron doesn’t seem to connect to the broker. I have searched the forum but found most posts on the photon, is it any different from boron (apart from boron being a cellular module, any difference in the MQTT program?). Below is the code I have been using. would love someone’s help on this.

// This #include statement was automatically added by the Particle IDE.
#include "MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started
 * MQTT client("iot.eclipse.org", 1883, callback);
 * 
 **/
//MQTT client("server_name", 1883, callback);


byte server[] = { 182,108,1,144 }; 
 MQTT client(server, 1883, callback,);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
        
    delay(1000);
}


void setup() {
    RGB.control(true);
    Serial.begin(9600);
    // connect to the server
    client.connect("sparkclient");

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("outTopic/message","hello world");
        client.subscribe("inTopic/message");
        Serial.println("inside if");
    }
}

void loop() {
    Serial.println("inside loop");
    if (client.isConnected())
    {   Serial.println("connected");
        client.loop();
}}

@rohan_vamshiG, the Boron is cellular, putting its communications outside your home network. In order for the Boron to “see” your MQTT server, you would need to open the ports used by MQTT on your how firewall/gateway. Have you done this?

@peekay , Yes i have put 1883 as an open port in my firewall.

You’d not only open the port but setup port forwarding too.

2 Likes