Kniwwelino – Publish and subscribe to custom MQTT Broker

Kniwwelino and MQTT

Kniwwelino programming Board. Image courtesy of http://www.kniwwelino.lu

Kniwwelino programming Board. Image courtesy of http://www.kniwwelino.lu

 

Recently I had the chance to play around with a Kniwwelino , and wanted it to communicate with my custom MQTT Broker (Mosquitto). Although  there is a lot of Documentation on Kniwwelino’s Site, it is lacking some examples  for using a custom MQTT Broker.  In this post I share my experiences and findings with any Kniwwelino user.

 

 

 

Scenario

I have some Network Cameras linked to motioneye software.  When motioneye detects  significant change  in a series of frames it publishes a string to a MQTT Topic on a private Broker. The Kniwwelino is subscribed to this Topic, and turns the LED to RED (motion detected) or Green (motion clear). The Kniwwelino also has 2 clickable buttons,  if any Button  is clicked, it publishes a string to a MQTT Topic

Code

#include 

void setup() {
  //Initialize the Kniwwelino Board
  Kniwwelino.begin(true, true, false); // Wifi=true, Fastboot=true, MQTT Logging=false
  Kniwwelino.MQTTsetup("192.168.1.55", 1883, "username", "password");
  Kniwwelino.MQTTconnect();
  Kniwwelino.MQTTonMessage(messageReceived); 
  Kniwwelino.MQTTsubscribe("keller/cam1");
}

void loop() {
  if (Kniwwelino.BUTTONAclicked()) {
    Kniwwelino.MQTTpublish("keller/cam1", String("A"));
  }
  if (Kniwwelino.BUTTONBclicked()) {
    Kniwwelino.MQTTpublish("keller/cam1", String("B"));
  }
  Kniwwelino.loop(); // do background stuff...
}

static void messageReceived(String &topic, String &payload) {
  if (topic=="keller/cam1") {
    if (payload == "ON") {
      Kniwwelino.RGBsetColorEffect(String("FF0000"), RGB_ON, -1);
    } 
    else if (payload == "OFF") {
      Kniwwelino.RGBsetColorEffect(String("00FF00"), RGB_ON, -1);
    } 
    else {
      Kniwwelino.RGBsetColorEffect(String("0000FF"), RGB_ON, -1);
    }
  }
}

Explanation

The magic happens in the  setup() :

How to connect to your MQTT Broker:
Kniwwelino.MQTTsetup("192.168.1.55", 1883, "username", "password");
Tell Kniwwelino to connect:
Kniwwelino.MQTTconnect();
Initialise the MQTT subscription routine:
Kniwwelino.MQTTonMessage(messageReceived);
Setup the MQTT Topic to subscribe to:
Kniwwelino.MQTTsubscribe("keller/cam1");

Now, inside your main programm you can call different functions :

Publish  String “A” to Topic if Button A is clicked :
if (Kniwwelino.BUTTONAclicked()) {
  Kniwwelino.MQTTpublish("keller/cam1", String("A"));
}

Listen for incoming messages on topic
static void messageReceived(String &topic, String &payload) {
if (topic=="keller/cam1")
{
If message received is “ON” turn on LED to RED (#ff0000)
if (payload == "ON") {
Kniwwelino.RGBsetColorEffect(String("FF0000"), RGB_ON, -1);
}
If message received is “OFF”, turn the LED to GREEN  (#00ff00)
else if (payload == "OFF") {
Kniwwelino.RGBsetColorEffect(String("00FF00"), RGB_ON, -1);
}
else {
Any other message turns the LED to BLUE (#0000ff)
Kniwwelino.RGBsetColorEffect(String("0000FF"), RGB_ON, -1);
}
}
That’s it !  Thank you for reading, I hope this will help anyone in their future Projects.

Permanent link to this article: https://www.hiscorebob.lu/2018/05/kniwwelino-publish-subscribe-custom-mqtt-broker/

Hannerlosst eng Noriicht:

This site uses Akismet to reduce spam. Learn how your comment data is processed.