Activity Forums Tutorials MQTT Library

  • MQTT Library

     jasin updated 1 year, 2 months ago 3 Members · 4 Posts
  • jasin

    Member
    March 7, 2019 at 4:26 pm

    There is an MQTT library compatible with Controllino MAXI and MEGA (Must provide a network implementation).

    It can be installed via Library Manager in Arduino IDE see:

    [img]https://i.postimg.cc/YqgLcmNd/MQTT-Library.png[/img]

    After installing the library it is possible to use MQTTClient class:

    Code:

    MQTTClient client;

    MQTT client has to be initialized ie:

    Code:

    client.begin(“broker.shiftr.io”, net);

    broker.shiftr.io is a free service which provides a rich publish & subscribe communication infrastructure.

    net is initialized EthernetClient connection.

    Then it is possible to Register a handler for incoming messages

    Code:

    client.onMessage(messageReceived);

    messageReceived is a function, that will be called whenever a message is received from the topic. The function must have two string parameters topic and message ie:

    Code:

    void messageReceived(String &topic, String &payload)
    {
    Serial.println(“incoming: ” + topic + ” – ” + payload);
    }

    Method for connecting the client with a username and password specified:

    Code:

    client.connect(“Controllino”, “try”, “try”)

    Methods for Publish/Subscribe to broker are:

    Code:

    client.publish(“/controllino.biz”, “Hello Controllino”);
    client.subscribe(“/controllino.biz”);

    When Example sketch is used, the communication can be seen on web pages of Shiftr.io:

    [img]https://i.postimg.cc/GpnQwCsy/shiftr-io-web.png[/img]

    And on Controllino terminal:

    [img]https://i.postimg.cc/CKwqsLqm/terminal.png[/img]

  • mfathany-psi

    Member
    April 23, 2019 at 6:43 am

    Can I request the Full Example Code MQTT ? :)

  • ifi_raj

    Member
    August 6, 2020 at 8:30 am

    Hello,

    I think the “MQTT” Lib from Joel Gaehwiler is used here. On the Github Site you can see some examples and information to the lib: https://github.com/256dpi/arduino-mqtt

  • jasin

    Member
    August 10, 2020 at 11:23 am

    Hello

    Here it is.

    It was tested as PlatformIO project with this config (.ini)

    ; PlatformIO Project Configuration File
    ;
    ;   Build options: build flags, source filter
    ;   Upload options: custom upload port, speed and extra flags
    ;   Library options: dependencies, extra library storages
    ;   Advanced options: extra scripting
    ;
    ; Please visit documentation for the other options and examples
    ; https://docs.platformio.org/page/projectconf.html
    
    [env:controllino_maxi]
    platform = atmelavr
    board = controllino_maxi
    framework = arduino
    lib_deps = controllino, ethernet, SPI, MQTT

    Code:

    #include <SPI.h>

    #include <Controllino.h> /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */

    #include <Ethernet.h>

    #include <MQTT.h> ​/* by Joël Gähwiler. Have to be installed via Library Manager. See https://github.com/256dpi/arduino-mqtt */

    /*

    ​CONTROLLINO - Demonstration of MQTT protocol usage, Version 01

    ​Based on example code by Joël Gähwiler. See https://github.com/256dpi/arduino-mqtt .

    ​You can check on your device after a successful

    ​connection here: https://shiftr.io/try .

    ​Compatible with CONTROLLINO MAXI, MAXI Automation and MEGA.

    ​IMPORTANT INFORMATION!

    ​Please, select proper target board in Tools-&gt;Board-&gt;Controllino MAXI/MEGA before Upload to your CONTROLLINO.

    ​(Refer to https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library if you do not see the CONTROLLINOs in the Arduino IDE menu Tools-&gt;Board.)

    ​Created 29 Jan 2019

    ​by Jasin

    ​https://controllino.biz/

    ​Check https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library for the latest CONTROLLINO related software stuff.

    ​Visit our forum here: https://forum.controllino.biz/

    */

    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

    byte ip[] = {192, 168, 16, 177}; // &lt;- change to match your network

    EthernetClient net;

    MQTTClient client;

    unsigned long lastMillis = 0;

    void connect() {

    ​Serial.print("connecting...");

    ​while (!client.connect("Controllino", "try", "try")) {

    ​Serial.print(".");

    ​delay(1000);

    ​}

    ​Serial.println("nconnected!");

    ​client.subscribe("/controllino.biz");

    ​// client.unsubscribe("/hello");

    }

    void messageReceived(String &amp;topic, String &amp;payload) {

    ​Serial.println("incoming: " + topic + " - " + payload);

    }

    void setup() {

    ​Serial.begin(9600);

    ​Ethernet.begin(mac, ip);

    ​// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.

    ​// You need to set the IP address directly.

    ​client.begin("broker.shiftr.io", net);

    ​client.onMessage(messageReceived);

    ​connect();

    }

    void loop() {

    ​client.loop();

    ​if (!client.connected()) {

    ​connect();

    ​}

    ​// publish a message roughly every second.

    ​if (millis() - lastMillis &gt; 1000) {

    ​lastMillis = millis();

    ​client.publish("/controllino.biz", "Hello Controllino");

    ​}

    }

    /* 2019-01-29: The sketch was successfully tested with Arduino 1.8.5, Controllino Library 3.0.4 and CONTROLLINO MAXI. */

Viewing 1 - 4 of 4 replies

Log in to reply.

Original Post
0 of 0 posts June 2018
Now