Difference between revisions of "Beaglebone Black/Mosquitto"

From Teknologisk videncenter
Jump to: navigation, search
m
m
Line 21: Line 21:
 
##systemctl enable mosquitto.service
 
##systemctl enable mosquitto.service
 
##service mosquitto start
 
##service mosquitto start
 +
##service mosquitto status
 +
##Genstart og se den er startet automatisk
 +
##Check grundlæggende installation
 +
##mosquitto –v # i en terminal (Brooker)
 +
##mosquitto_sub -h localhost –t "temperatur/sommerhus/stue" # i anden terminal (client/subscriber) brug evt -d for debug (Se alle feltet)
 +
##mosquitto_pub -h localhost -t "temperatur/sommerhus/stue" -m "24" # i tredje terminal (client/publisher)
 +
  
 
=Example=
 
=Example=
Line 114: Line 121:
 
}
 
}
 
</source>
 
</source>
 
##service mosquitto status
 
##Genstart og se den er startet automatisk
 
##Check grundlæggende installation
 
##mosquitto –v # i en terminal (Brooker)
 
##mosquitto_sub -h localhost –t "temperatur/sommerhus/stue" # i anden terminal (client/subscriber) brug evt -d for debug (Se alle feltet)
 
##mosquitto_pub -h localhost -t "temperatur/sommerhus/stue" -m "24" # i tredje terminal (client/publisher)
 

Revision as of 13:49, 1 April 2022

Install from git

  1. apt-get update # Better safe than sorry
  2. apt-get install libssl-dev
  3. apt-get install docbook docbook-xml docbook-xsl
  4. apt-get install xsltproc
  5. apt-get install libsystemd-dev
  6. apt install libcjson1 libcjson-dev
  7. git clone https://github.com/eclipse/mosquitto.git
  8. cd mosquitto
  9. vi config.mk – set WITH_SYSTEMD:=yes
  10. make all
  11. make install
  12. adduser –system mosquitto # Bemærk det er ”-”-”system
  13. ln /usr/local/lib/libmosquitto.so.1 /usr/lib/arm-linux-gnueabihf/
  14. Da vi er på debian en systemd implementation øsnker vi at installere service under systemd
    1. (DONE)Apt-get install libsystemd-dev
    2. (DONE)vi config.mk – set WITH_SYSTEMD:=yes
    3. cp /home/debian/mosquitto/service/systemd/mosquitto.service.simple /etc/systemd/system/mosquitto.service
    4. ln /usr/local/sbin/mosquitto /usr/sbin/
    5. > /etc/mosquitto/mosquitto.conf # Opret tom config fil (se eksempel fil)
    6. systemctl enable mosquitto.service
    7. service mosquitto start
    8. service mosquitto status
    9. Genstart og se den er startet automatisk
    10. Check grundlæggende installation
    11. mosquitto –v # i en terminal (Brooker)
    12. mosquitto_sub -h localhost –t "temperatur/sommerhus/stue" # i anden terminal (client/subscriber) brug evt -d for debug (Se alle feltet)
    13. mosquitto_pub -h localhost -t "temperatur/sommerhus/stue" -m "24" # i tredje terminal (client/publisher)


Example

  • MQTT Broker: 188.177.229.190
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mosquitto.h>

//Author: heth@mercantec.dk
//Date..: 19. nov 2020
//Ver...: 0.1 beta
//
//
//INFORMATION
//Mosquitto docementation:
//   - https://mosquitto.org/documentation/
//   - https://github.com/eclipse/mosquitto (Se under exanples)
//Compile with : gcc mqtt_ex1.c -lmosquitto -o mqtt_ex1

int number;

void on_connect1(struct mosquitto *mosq, void *obj, int result)
{
    int rc = MOSQ_ERR_SUCCESS;

    if(!result){
        mosquitto_subscribe(mosq, NULL, "#", 0);
    }else{
        fprintf(stderr, "%s\n", mosquitto_connack_string(result));
    }
}

void on_message1(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
    struct mosquitto *mosq2 = (struct mosquitto *)obj;
        char *txtpoi, *txtpoi2;
        printf("struct mosquitto_message contains:\n");
        printf("  mid (int).............: %i\n", message->mid );
        printf("  topic (* char)........: %s\n", message->topic );
        printf("  payload (* void)......: %p", message->payload );
        txtpoi = malloc(message->payloadlen + 1);
        if ( txtpoi == 0 ) {
                fprintf( stderr, "Malloc error\n");
        } else {
                strncpy(txtpoi, message->payload, message->payloadlen);
                txtpoi[message->payloadlen] = 0;
                printf("  Message: [%s]\n", txtpoi);
        }
        printf("  payloadlen (int)......: %i\n", message->payloadlen );
        printf("  qos (int).............: %i\n", message->qos );
        printf("  retain (int)..........: %i\n", message->retain );

        txtpoi2 = malloc( message->payloadlen + 20);
        sprintf(txtpoi2, "#%i:%s", number, txtpoi);
        number++;

        //mosquitto_publish(mosq, NULL, "user/henrik/english", strlen(txtpoi2), txtpoi2,  message->qos, message->retain);
        free(txtpoi2);
        free(txtpoi);
}

int main(int argc, char *argv[])
{
    struct mosquitto *mosq1, *mosq2;
    int version[3];
    number = 1; // Init mesage number

    mosquitto_lib_init();
    mosquitto_lib_version(&version[0],&version[1],&version[2]);
    printf("Mosquitto library version. %i.%i.%i\n", version[0], version[1], version[2]);

    mosq2 = mosquitto_new(NULL, true, NULL);
    mosq1 = mosquitto_new(NULL, true, mosq2);

    mosquitto_connect_callback_set(mosq1, on_connect1);
    mosquitto_message_callback_set(mosq1, on_message1);

    mosquitto_connect(mosq2, "localhost", 1883, 60);  // Replace localhost with IP address of broker
    mosquitto_connect(mosq1, "localhost", 1883, 60);  // Replace localhost with IP address of broker

    mosquitto_loop_start(mosq2);

    mosquitto_loop_forever(mosq1, -1, 1);

    mosquitto_destroy(mosq1);
    mosquitto_destroy(mosq2);

    mosquitto_lib_cleanup();

    return 0;
}