Difference between revisions of "Beaglebone Black/Mosquitto"
From Teknologisk videncenter
m |
m |
||
(18 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | See also [[MQTT/Mosquitto]] | ||
+ | |||
=Install from git= | =Install from git= | ||
#apt-get update # Better safe than sorry | #apt-get update # Better safe than sorry | ||
Line 6: | Line 8: | ||
#apt-get install libsystemd-dev | #apt-get install libsystemd-dev | ||
#apt install libcjson1 libcjson-dev | #apt install libcjson1 libcjson-dev | ||
+ | #cd | ||
#git clone https://github.com/eclipse/mosquitto.git | #git clone https://github.com/eclipse/mosquitto.git | ||
#cd mosquitto | #cd mosquitto | ||
#vi config.mk – set WITH_SYSTEMD:=yes | #vi config.mk – set WITH_SYSTEMD:=yes | ||
− | #make all | + | #make all (Der mangler sikkert en del dependicies - som skal installeres undervejs) |
#make install | #make install | ||
#adduser –system mosquitto # Bemærk det er ”-”-”system | #adduser –system mosquitto # Bemærk det er ”-”-”system | ||
#ln /usr/local/lib/libmosquitto.so.1 /usr/lib/arm-linux-gnueabihf/ | #ln /usr/local/lib/libmosquitto.so.1 /usr/lib/arm-linux-gnueabihf/ | ||
− | #Da vi er på debian en systemd implementation | + | #Da vi er på debian en systemd implementation ønsker vi at installere service under systemd |
− | |||
− | |||
##cp /home/debian/mosquitto/service/systemd/mosquitto.service.simple /etc/systemd/system/mosquitto.service | ##cp /home/debian/mosquitto/service/systemd/mosquitto.service.simple /etc/systemd/system/mosquitto.service | ||
##ln /usr/local/sbin/mosquitto /usr/sbin/ | ##ln /usr/local/sbin/mosquitto /usr/sbin/ | ||
Line 22: | Line 23: | ||
##service mosquitto start | ##service mosquitto start | ||
##service mosquitto status | ##service mosquitto status | ||
− | ##Genstart og se den er startet automatisk | + | ##Genstart og se den er startet automatisk ( service mosquitto restart ) |
##Check grundlæggende installation | ##Check grundlæggende installation | ||
##mosquitto –v # i en terminal (Brooker) | ##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_sub -v -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) | ##mosquitto_pub -h localhost -t "temperatur/sommerhus/stue" -m "24" # i tredje terminal (client/publisher) | ||
+ | |||
+ | |||
+ | =Example= | ||
+ | *MQTT Broker: 93.166.84.21 | ||
+ | <source lang=c> | ||
+ | //Author: heth@mercantec.dk | ||
+ | //Date..: 19. nov 2020 | ||
+ | //Ver...: 0.2 beta | ||
+ | // | ||
+ | //Modification: | ||
+ | // 01/04/2022 - Simplified: Only one mosquitto instance used in main() | ||
+ | // | ||
+ | //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 | ||
+ | #include <stdio.h> | ||
+ | #include <mosquitto.h> | ||
+ | #include <string.h> | ||
+ | #include <stdlib.h> | ||
+ | int number; | ||
+ | |||
+ | #define SUCCES 0 | ||
+ | void on_connect1(struct mosquitto *mosq, void *obj, int result) | ||
+ | { | ||
+ | int rc = MOSQ_ERR_SUCCESS; | ||
+ | |||
+ | if(result == SUCCES){ | ||
+ | mosquitto_subscribe(mosq, NULL, "henrik/cputemp/fahrenheit", 0); // NOTE: Change to published topic | ||
+ | }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/DIT_NAVN_HER_ELLER_WHATEVER/english", strlen(txtpoi2), txtpoi2, message->qos, message->retain); | ||
+ | free(txtpoi2); | ||
+ | free(txtpoi); | ||
+ | } | ||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | struct mosquitto *mosq1; | ||
+ | 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]); | ||
+ | |||
+ | mosq1 = mosquitto_new(NULL, true, NULL); | ||
+ | |||
+ | mosquitto_connect_callback_set(mosq1, on_connect1); | ||
+ | mosquitto_message_callback_set(mosq1, on_message1); | ||
+ | |||
+ | mosquitto_connect(mosq1, "127.0.0.1", 1883, 60); // NOTE: Replace localhost with IP address of broker | ||
+ | |||
+ | |||
+ | mosquitto_loop_forever(mosq1, -1, 1); | ||
+ | |||
+ | mosquitto_destroy(mosq1); | ||
+ | |||
+ | mosquitto_lib_cleanup(); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </source> | ||
+ | [[Category:Beagleboard Black]][[Category:MQTT]] |
Latest revision as of 09:39, 27 October 2023
See also MQTT/Mosquitto
Install from git
- apt-get update # Better safe than sorry
- apt-get install libssl-dev
- apt-get install docbook docbook-xml docbook-xsl
- apt-get install xsltproc
- apt-get install libsystemd-dev
- apt install libcjson1 libcjson-dev
- cd
- git clone https://github.com/eclipse/mosquitto.git
- cd mosquitto
- vi config.mk – set WITH_SYSTEMD:=yes
- make all (Der mangler sikkert en del dependicies - som skal installeres undervejs)
- make install
- adduser –system mosquitto # Bemærk det er ”-”-”system
- ln /usr/local/lib/libmosquitto.so.1 /usr/lib/arm-linux-gnueabihf/
- Da vi er på debian en systemd implementation ønsker vi at installere service under systemd
- cp /home/debian/mosquitto/service/systemd/mosquitto.service.simple /etc/systemd/system/mosquitto.service
- ln /usr/local/sbin/mosquitto /usr/sbin/
- > /etc/mosquitto/mosquitto.conf # Opret tom config fil (se eksempel fil)
- systemctl enable mosquitto.service
- service mosquitto start
- service mosquitto status
- Genstart og se den er startet automatisk ( service mosquitto restart )
- Check grundlæggende installation
- mosquitto –v # i en terminal (Brooker)
- mosquitto_sub -v -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
- MQTT Broker: 93.166.84.21
//Author: heth@mercantec.dk
//Date..: 19. nov 2020
//Ver...: 0.2 beta
//
//Modification:
// 01/04/2022 - Simplified: Only one mosquitto instance used in main()
//
//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
#include <stdio.h>
#include <mosquitto.h>
#include <string.h>
#include <stdlib.h>
int number;
#define SUCCES 0
void on_connect1(struct mosquitto *mosq, void *obj, int result)
{
int rc = MOSQ_ERR_SUCCESS;
if(result == SUCCES){
mosquitto_subscribe(mosq, NULL, "henrik/cputemp/fahrenheit", 0); // NOTE: Change to published topic
}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/DIT_NAVN_HER_ELLER_WHATEVER/english", strlen(txtpoi2), txtpoi2, message->qos, message->retain);
free(txtpoi2);
free(txtpoi);
}
int main(int argc, char *argv[])
{
struct mosquitto *mosq1;
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]);
mosq1 = mosquitto_new(NULL, true, NULL);
mosquitto_connect_callback_set(mosq1, on_connect1);
mosquitto_message_callback_set(mosq1, on_message1);
mosquitto_connect(mosq1, "127.0.0.1", 1883, 60); // NOTE: Replace localhost with IP address of broker
mosquitto_loop_forever(mosq1, -1, 1);
mosquitto_destroy(mosq1);
mosquitto_lib_cleanup();
return 0;
}