MQTT on Bluz DK


#1

Hey all,

I’m new to Bluz.io and wanted to start playing around with MQTT on the Bluz DK - Is this function supported? the MQTT server lives on my LAN and I have the Photon + Gateway shield.
Basically want to start by turn on/off an LED when receiving a message from the MQTT subscribe function

Also is there a way to access the serial monitor on the DK?

Cheers


#2

MQTT is no currently supported on bluz. We use the Particle ecosystem which uses a fundamentally different type of IoT architecture called CoAP. The two are not compatible.

There have been efforts to port MQTT to the Photon, I am not sure if it is working or not, but you can possibly use that to make it work. Ultimately, you can send down data to the bluz device through the gateway shield, so if the Photon on the Gateway shield can talk MQTT, then you can proxy messages through it to bluz using local communication.

By serial monitor, do you just mean serial output? Perhaps this will help? http://docs.bluz.io/tutorials/serial/


#3

I’m doing that for a switch to turn a lifx light on and off. The bluz monitors a button press, that then sends a message via BLE.sendData function to the photon attached to the gateway, which then sends an mqtt message to the local mqtt server:

My code on the bluz:

#include "application.h"
SYSTEM_MODE(AUTOMATIC);
const uint8_t buttonPin = 2;     // the number of the pushbutton pin
volatile int push = false;
String myID;
char data[28] = {};
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 100;    // the debounce time; increase if the output flickers


void interruptus(void);

void setup() {
    RGB.control(true);
    RGB.color(0, 0, 0);
    pinMode(buttonPin, INPUT);
    attachInterrupt(buttonPin,interuptus, RISING);
    myID = System.deviceID();
    Particle.publish("Bluz DeviceID", myID);
    memcpy(data,myID.c_str(),24);
    
}


void loop() {
    if (push){
        if ((millis() - lastDebounceTime) > debounceDelay) {
            data[24] = '|';
            data[25] = 'B';
            data[26] = '0' + buttonPin;
            BLE.sendData((uint8_t*)data,28);
        }
        push = false;
    }
    System.sleep(SLEEP_MODE_CPU);
}

void interuptus(){
        // read the state of the switch into a local variable:
        push = true;
}

Code on the Photon:

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

// This #include statement was automatically added by the Particle IDE.
#include "bluz_gateway/bluz_gateway.h"
#define POLL_CONNECTIONS_INTERVAL 30000

SYSTEM_MODE(AUTOMATIC);
bluz_gateway gateway;

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

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

char server[] = "mqtt.home.local";
String deviceID = "bluzgateway";
MQTT client(server, 1883, callback);
int lastButtonState[5] = {LOW,LOW,LOW,LOW,LOW};
int connected = FALSE;
int lastconnected = FALSE;
int lifxswitch[5] = {D1,D1,D2,D3,D4};
char name[] = "bluzgateway.home.local";
unsigned long t;
unsigned long deltat = 1000;        //keep alive time for mqtt queue
std::string message = "";
std::string topic = "";
char* chrtopic;
std::string pressed = "";
int debouncefix;
int debounce = 200;



void handle_custom_data(uint8_t *data, uint16_t length) {
    //if you use BLE.send from any connected DK, the data will end up here
    Particle.publish("Bluz Custom Data", String((char*)data));
    pressed = String((char*)data);
}

void handle_gateway_event(uint8_t event, uint8_t *data, uint16_t length) {
    //will return any polling queries from the gateway here
}

void setup() {
    gateway.init();

    //register the callback functions
    gateway.register_data_callback(handle_custom_data);
    gateway.register_gateway_event(handle_gateway_event);
    // Connect mqtt to broker
    while(client.isConnected() == FALSE){
        client.connect(name);
        delay(10);
    }
    debouncefix = millis();


}

long timeToNextPoll = POLL_CONNECTIONS_INTERVAL;
void loop() {
    if (client.isConnected()){
        client.loop();
        connected = TRUE;
        if (connected != lastconnected){
            Spark.publish("reconnected", NULL, 60, PRIVATE);    //publish the event for IFTTT to consume
            lastconnected = connected;
        }
    }
    gateway.loop();
    if (pressed != ""){
        if (millis()-debouncefix > debounce){
            topic = "particle/" + deviceID + "/buttons";                    //build topic
            char* chrtopic = strdup(topic.c_str());                         //need to convert string to char
            message = "Button " + pressed + " Pressed";
            char* chrmessage = strdup(message.c_str());
            client.publish(chrtopic, chrmessage);                               //publish click to MQTT.
            Particle.publish("Pressed",chrmessage, 60, PRIVATE);      //publish button press to particle cloud for IFTTT to consume.
            free(chrmessage);
            free(chrtopic);                                                 //free memory. Not sure if this is needed.
            debouncefix = millis();
        }
        pressed = "";
    }
    if (millis() > timeToNextPoll) {
        timeToNextPoll = POLL_CONNECTIONS_INTERVAL + millis();
        gateway.poll_connections();
    }
    if(client.isConnected() == FALSE){
        client.connect(name);
    }

}

can’t seem to get the blue blinking light to turn off though so a battery is lasting me about a week. :frowning:

edit: False alarm on not controlling the RGB… works now. :slight_smile: