Commit 56d8c77a authored by Alexander David Hellwig's avatar Alexander David Hellwig
Browse files

Merge branch '8-handwrite-a-mqtt-adapter' into 'master'

First version of the project (completed issues 1-8)

See merge request !1
parents c1ce9e0d 73df7261
Pipeline #165402 passed with stage
in 42 seconds
/target/
/bin/
.settings
.project
.classpath
.DS_Store
.idea
\ No newline at end of file
stages:
- linux
BranchJobLinux:
stage: linux
image: registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emam2mqtt
script:
- mvn clean install -s settings.xml
except:
- master
# EMAM2MQTT
A project that generates MQTT-Adapters for EmbeddedMontiArcMath components.
\ No newline at end of file
A project that generates MQTT-Adapter for EmbeddedMontiArcMath components.
## Getting Started
These instructions will get MQTT up and running on your **Ubuntu** system
### Installing project
[Download](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/generators/emam2mqtt/-/archive/8-handwrite-a-mqtt-adapter/emam2mqtt-3-install-mqtt-linux.zip) this project to your computer
[Download](https://maven.apache.org/guides/getting-started/) and install Maven
[Download](https://www.eclipse.org/downloads/) and install Ecplise IDE
Import the project as "Maven Project" in Eclipse
Open terminal and switch to your project directory
In terminal run
`mvn clean install -s settings.xml`
### Installing MQTT broker (locally on Ubuntu)
Open terminal
Add new MQTT repository
`sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa`
Run update
`sudo apt-get update`
Install mosquitto and mosquitto-clients
`sudo apt-get install mosquitto`
`sudo apt-get install mosquitto-clients`
### Installing MQTT broker (locally on Windows)
[Download](https://mosquitto.org/files/binary/win64/mosquitto-1.6.2-install-windows-x64.exe) and install Mosquitto
Restart your computer
Mosquitto is now available and can be accessed via command line using `mosquitto -h`
### Installing MQTT libraries (locally on Ubuntu)
Open terminal and run
`sudo apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui`
#### Installing PahoMQTT C library
run
```bash
git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
git checkout v1.2.1
cmake -Bbuild -H. -DPAHO_WITH_SSL=ON
sudo cmake --build build/ --target install
sudo ldconfig
```
#### Installing PahoMQTT C++ library
**after** installation of C library run
```bash
git clone https://github.com/eclipse/paho.mqtt.cpp
cd paho.mqtt.cpp
cmake -Bbuild -H.
sudo cmake --build build/ --target install
```
## Installing MQTT Publisher/Subscriber demo (locally on Ubuntu)
open terminal, switch to **mqtt_demo** directroy inside the project and run
`cmake .`
compile the demo by running
`make`
run the demo afterwards with `./mqtt_demo`
---
additionaly, you can also use `mosquitto_pub` and `mosquitto_sub` commands from mosquitto broker to subscribe/publish messages
for example:
subscribe to topic **/hello** with `mosquitto_sub -t /hello` and publish a message to this topic with
`mosquitto_pub -t /hello -m "hi, how are you?"`
FROM ubuntu:latest
RUN apt-get update
RUN apt-get -y install software-properties-common
RUN apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get -y install mosquitto
RUN apt-get -y install mosquitto-clients
RUN apt-get -y install openjdk-8-jdk
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
RUN apt -y install maven
# Created by Georg Vinogradov on 28.05.19
# Setting cmake version
cmake_minimum_required(VERSION 3.1...3.14)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
# Using C++ version 11 with threads for compiling
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
# Setting search paths for cmake modules
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules")
# Setting project name and description
project(MqttAdapter VERSION 1.0
DESCRIPTION "MQTT adapter"
LANGUAGES CXX)
# Find mqtt packages using modules
find_package(MQTT REQUIRED)
# Adding library as a target
add_library(MqttAdapter_tests_a_compA MqttAdapter_tests_a_compA.cpp)
# Checking if everything was found
if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB)
message(FATAL_ERROR "MQTT libraries not found!")
elseif (NOT MQTT_INCLUDE_DIR)
message(FATAL_ERROR "MQTT includes not found!")
else()
message("MQTT includes found in "${MQTT_INCLUDE_DIR})
message("MQTT libraries found in "${MQTT_LIBS})
endif()
# Adding include directory to a target
target_include_directories(MqttAdapter_tests_a_compA PUBLIC ${MQTT_INCLUDE_DIR})
# Linking libraries to target
target_link_libraries(MqttAdapter_tests_a_compA PUBLIC ${MQTT_LIBS})
# Export target to a cmake module file for outside usage
export(TARGETS MqttAdapter_tests_a_compA FILE MqttAdapter_tests_a_compA.cmake)
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#include "MqttAdapter_tests_a_compA.h"
MqttAdapter_tests_a_compA::MqttAdapter_tests_a_compA() {}
void MqttAdapter_tests_a_compA::init(tests_a_compA *comp)
{
this->component = comp;
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true);
_clockSubscriber = new mqtt::client(SERVER_ADDRESS, SUB_ID);
_echoPublisher = new mqtt::client(SERVER_ADDRESS, PUB_ID);
try {
_clockSubscriber->connect(connOpts);
callback cb(*_clockSubscriber);
_clockSubscriber->set_callback(cb);
_clockSubscriber->subscribe(TOPIC, 1);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void MqttAdapter_tests_a_compA::publish_echoPublisher()
{
mqtt::message tmpMsg;
double value = component->rosOut;
uint8_t *buf = (uint8_t*)&value;
tmpMsg.set_payload(buf, 8); // pointer to byte buffer of length 8
try {
_echoPublisher->publish(tmpMsg);
} catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void MqttAdapter_tests_a_compA::tick()
{
publish_echoPublisher();
}
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#pragma once
#include "tests_a_compA.h"
#include "mqtt/client.h"
#include <iostream>
using namespace std;
class MqttAdapter_tests_a_compA {
public:
MqttAdapter_tests_a_compA();
void init(tests_a_compA* comp);
void publish_echoPublisher();
void tick();
private:
const string SERVER_ADDRESS = "tcp://localhost:1883";
const string PUB_ID = "publisher_cpp";
const string SUB_ID = "subscriber_cpp";
const string TOPIC = "/clock";
tests_a_compA* component = nullptr;
mqtt::client* _clockSubscriber = nullptr;
mqtt::client* _echoPublisher = nullptr;
};
class callback : public virtual mqtt::callback
{
mqtt::client& cli_;
// Callback for when connected
void connected(const std::string& cause) {}
// Callback for when the connection is lost.
void connection_lost(const std::string& cause) {
cout << "\nConnection lost";
if (!cause.empty())
cout << ": " << cause << endl;
}
// Callback for when message is received
void message_arrived(mqtt::const_message_ptr msg) {
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
}
public:
callback(mqtt::client& cli) : cli_(cli) {}
};
# Created by Georg Vinogradov on 28.05.19
message("Searching for installed MQTT on your system...")
# Searching for include directory
if (NOT MQTT_INCLUDE_DIR)
find_path(MQTT_INCLUDE_DIR MQTTAsync.h HINTS "/usr/include/" "/usr/local/include/")
endif()
# Searching for libraries (without SSL support)
if (NOT MQTT_LIBS)
find_library(
MQTT_C_LIB
NAMES paho-mqtt3c)
find_library(
MQTT_A_LIB
NAMES paho-mqtt3a)
find_library(
MQTT_PP_LIB
NAMES paho-mqttpp3)
set(MQTT_LIBS ${MQTT_A_LIB} ${MQTT_C_LIB} ${MQTT_PP_LIB})
endif()
// MqttAdapter
//
// Created by Georg Vinogradov on 05.06.19.
// Copyright © 2019 Georg Vinogradov. All rights reserved.
//
#ifndef tests_a_compA_h
#define tests_a_compA_h
#include <iostream>
using namespace std;
class tests_a_compA {
public:
double rosIn = 0;
double rosOut = 0;
};
#endif /* tests_a_compA_h */
# Created by Georg Vinogradov on 28.05.19
# Setting cmake version
cmake_minimum_required(VERSION 3.1...3.14)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
# Using C++ version 11 with threads for compiling
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
# Setting search paths for cmake modules
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules")
# Setting project name and description
project(MQTT VERSION 1.0
DESCRIPTION "MQTT demo"
LANGUAGES CXX)
# Find mqtt packages using modules
find_package(MQTT REQUIRED)
# Find Pthread package
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
# Adding executable target
add_executable(mqtt_demo main.cpp)
# Checking if everything was found
if (NOT MQTT_C_LIB OR NOT MQTT_A_LIB OR NOT MQTT_PP_LIB)
message(FATAL_ERROR "MQTT libraries not found!")
elseif (NOT MQTT_INCLUDE_DIR)
message(FATAL_ERROR "MQTT includes not found!")
else()
message("MQTT includes found in "${MQTT_INCLUDE_DIR})
message("MQTT libraries found in "${MQTT_LIBS})
endif()
# Adding include directory to a target
target_include_directories(mqtt_demo PUBLIC ${MQTT_INCLUDE_DIR})
# Linking libraries to target
target_link_libraries(mqtt_demo PUBLIC ${MQTT_LIBS})
// Created by Georg Vinogradov on 28.05.19.
//
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h> // Sleep on unix
#include "mqtt/client.h"
const std::string SERVER_ADDRESS = "tcp://localhost:1883";
const std::string PUB_ID = "publisher_cpp";
const std::string SUB_ID = "subscriber_cpp";
const std::string TOPIC = "topic/hello";
const std::string PAYLOAD = "265";
const int QOS = 1;
using namespace std;
class callback : public virtual mqtt::callback
{
mqtt::client& cli_;
// Callback for when connected
void connected(const std::string& cause) {}
// Callback for when the connection is lost.
// This will initiate the attempt to manually reconnect.
void connection_lost(const std::string& cause) {
cout << "\nConnection lost";
if (!cause.empty())
cout << ": " << cause << endl;
}
// Callback for when message is received
void message_arrived(mqtt::const_message_ptr msg) {
cout << "Message received "<< msg->get_topic() << ": " << msg->get_payload_str() << endl;
}
public:
callback(mqtt::client& cli) : cli_(cli) {}
};
void publish()
{
cout << "\nInitialzing publisher" << endl;
// Publisher
mqtt::client publisher(SERVER_ADDRESS, PUB_ID);
mqtt::connect_options pubConnOpts;
pubConnOpts.set_keep_alive_interval(20);
pubConnOpts.set_clean_session(true);
try {
cout << "\nPublisher connects to broker..." << endl;
publisher.connect(pubConnOpts);
cout << "\nPublisher prepares message: "<< PAYLOAD << endl;
auto pubmsg = mqtt::make_message(TOPIC, PAYLOAD);
sleep(2);
cout << "\nPublisher sends message: "<< PAYLOAD << endl;
pubmsg->set_qos(QOS);
publisher.publish(pubmsg);
cout << "\nPublisher disconnects..." << endl;
publisher.disconnect();
}
catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
}
void subscribe()
{
cout << "\nInitialzing subscriber" << endl;
//Subscriber
mqtt::client subscriber(SERVER_ADDRESS, SUB_ID);
callback cb(subscriber); // Subscriber needs callback
subscriber.set_callback(cb);
mqtt::connect_options subConnOpts;
subConnOpts.set_keep_alive_interval(20);
subConnOpts.set_clean_session(false);
subConnOpts.set_automatic_reconnect(true);
try {
cout << "\nSubscriber connects to broker..." << endl;
subscriber.connect(subConnOpts);
cout << "\nSubscriber subscribes to topic: "<< TOPIC << endl;
subscriber.subscribe(TOPIC, QOS);
}
catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
}
while (tolower(cin.get()) != 'q'); // Listens for messages until user interrupts
}
int main(int argc, char* argv[])
{
thread sub (subscribe);
publish();
sub.join();
return 0;
}
# Created by Georg Vinogradov on 28.05.19
message("Searching for installed MQTT on your system...")
# Searching for include directory
if (NOT MQTT_INCLUDE_DIR)
find_path(MQTT_INCLUDE_DIR MQTTAsync.h HINTS "/usr/include/" "/usr/local/include/")
endif()
# Searching for libraries (without SSL support)
if (NOT MQTT_LIBS)
find_library(
MQTT_C_LIB
NAMES paho-mqtt3c)
find_library(
MQTT_A_LIB
NAMES paho-mqtt3a)
find_library(
MQTT_PP_LIB
NAMES paho-mqttpp3)
set(MQTT_LIBS ${MQTT_A_LIB} ${MQTT_C_LIB} ${MQTT_PP_LIB})
endif()
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.emam2mqtt</groupId>
<artifactId>emam2mqtt</artifactId>
<version>1.0-SNAPSHOT</version>
<name>emam2mqtt</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- .. SE-Libraries .................................................. -->
<se-commons.version>1.7.7</se-commons.version>
<Embedded-MontiArc-Math.version>0.1.7-SNAPSHOT</Embedded-MontiArc-Math.version>
<Embedded-montiarc-math-rosmsg-generator.version>0.1.3-SNAPSHOT</Embedded-montiarc-math-rosmsg-generator.version>
<!-- .. Libraries .................................................. -->
<guava.version>18.0</guava.version>
<junit.version>4.12</junit.version>
<logback.version>1.1.2</logback.version>
<!-- .. Plugins ....................................................... -->
<assembly.plugin>2.5.4</assembly.plugin>
<compiler.plugin>3.3</compiler.plugin>
<source.plugin>2.4</source.plugin>
<shade.plugin>2.4.3</shade.plugin>
<jacoco.plugin>0.8.1</jacoco.plugin>
<!-- Classifiers -->
<grammars.classifier>grammars</grammars.classifier>
<cli.classifier>cli</cli.classifier>
<!-- .. Misc .......................................................... -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<github.global.server>github</github.global.server>
</properties>
<dependencies>
<dependency>
<groupId>de.se_rwth.commons</groupId>
<artifactId>se-commons-logging</artifactId>
<version>${se-commons.version}</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-rosmsg-generator</artifactId>
<version>${Embedded-montiarc-math-rosmsg-generator.version}</version>
</dependency>
<!-- MontiCore Dependencies -->
<dependency>
<groupId>de.se_rwth.commons</groupId>
<artifactId>se-commons-groovy</artifactId>
<version>${se-commons.version}</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math</artifactId>
<version>${Embedded-MontiArc-Math.version}</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math</artifactId>
<version>${Embedded-MontiArc-Math.version}</version>
<classifier>${grammars.classifier}</classifier>
<scope>provided</scope>
</dependency>
<!-- .. Test Libraries ............................................... -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<!-- == PROJECT BUILD SETTINGS =========================================== -->
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<!-- Other Configuration -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.plugin}</version>
<configuration>