Skip to content

Commit

Permalink
mqtt-paho: added pkg MQTT-paho
Browse files Browse the repository at this point in the history
Using Lwip, added example to run on native RIOT. Example not working yet
  • Loading branch information
Javier FILEIV authored and javierfileiv committed Jan 19, 2020
1 parent 97a71bc commit 20d6cd2
Show file tree
Hide file tree
Showing 16 changed files with 764 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/mqtt-paho/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
APPLICATION = mqtt-paho-example

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 0
DEBUG ?= 0
# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

#define to use MQTT Paho as a task using MQTTStartTask()
CFLAGS += -DMQTT_TASK=1
#####################

LWIP_IPV6 = 1

USEPKG += mqtt-paho
USEMODULE += ipv6_addr
USEMODULE += ipv4_addr
USEMODULE += lwip_ipv6
USEMODULE += lwip_ipv6_autoconfig
# including lwip_ipv6_mld would currently break this test on at86rf2xx radios
USEMODULE += lwip lwip_sock_ip lwip_netdev
USEMODULE += lwip_tcp lwip_sock_tcp
USEMODULE += inet_csum
USEMODULE += shell
USEMODULE += od
USEMODULE += netdev_default
USEMODULE += ps

CFLAGS += -DSO_REUSE
CFLAGS += -DLWIP_SO_RCVTIMEO
CFLAGS += -DLWIP_SOCK_TCP_ACCEPT_TIMEOUT=500
CFLAGS += -DLWIP_NETIF_LOOPBACK=0
CFLAGS += -DLWIP_HAVE_LOOPIF=0
ifeq ($(BOARD),native)
USEMODULE += lwip_ethernet
endif

include $(RIOTBASE)/Makefile.include
191 changes: 191 additions & 0 deletions examples/mqtt-paho/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Copyright (C) 2019
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup examples
* @{
*
* @file
* @brief MQTT-paho example
*
* @author Javier FILEIV <javier.fileiv@gmail.com>
*
* @}
*/

#include <stdio.h>
#include <string.h>
#include <thread.h>
#include <mqtt.h>
#include <log.h>
#include "msg.h"
#include "lwip.h"
#include "lwip/netif.h"
#include "net/ipv6/addr.h"
#include "net/ipv4/addr.h"
#include "MQTTClient.h"
#include "shell.h"

#define REMOTE_IP_ADDRESS "fe80::18cf:f9ff:fe1c:afd2"
#define MAX_MSGS 12

static char* topic = "test_from_riot";
MQTTClient client;
static char _sender_stack[THREAD_STACKSIZE_DEFAULT];

static void _messageArrived(MessageData* data)
{
printf("mqtt_example: message arrived on topic"
" %.*s: %.*s\n\n", (int)data->topicName->lenstring.len, data->topicName->lenstring.data,
(int)data->message->payloadlen, (char *)data->message->payload);
}

static int ifconfig(int argc, char **argv)
{
(void)argc;
(void)argv;
for (struct netif *iface = netif_list; iface != NULL; iface = iface->next) {
printf("%s_%02u: ", iface->name, iface->num);
#ifdef MODULE_LWIP_IPV6
char addrstr[IPV6_ADDR_MAX_STR_LEN];
for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
if (!ipv6_addr_is_unspecified((ipv6_addr_t *)&iface->ip6_addr[i])) {
printf(" inet6 %s\n", ipv6_addr_to_str(addrstr, (ipv6_addr_t *)&iface->ip6_addr[i],
sizeof(addrstr)));
}
}
#endif
#ifdef MODULE_LWIP_IPV4
char addrstr[IPV4_ADDR_MAX_STR_LEN];
printf(" inet %s\n", ipv4_addr_to_str(addrstr, (ipv4_addr_t *)&iface->ip_addr,
sizeof(addrstr)));
#endif
puts("");
}
return 0;
}

static void *_mqtt_sender(void *arg)
{
(void) arg;
int count = 1;
for (;;) {
MQTTMessage message;
char payload[30];

message.qos = 1;
message.retained = 0;
message.payload = payload;
sprintf(payload, "message number %d", count);
message.payloadlen = strlen(payload);

int rc;
if ((rc = MQTTPublish(&client, topic, &message)) != 0) {
printf("mqtt_example: return code from MQTT publish is %d\n", rc);
} else {
printf("Message (%s) has been published to topic:%s\n", (char *)message.payload, topic);
}
if (count >= MAX_MSGS) {
/* kill thread */
printf("mqtt_example: finishing example, check that %d messages were sent/received\n",
count);
return NULL;
}
count++;
}
return NULL;
}

static void usage(void)
{
puts("Usage:");
puts(" help: this help");
puts(" pub msg: publish 'msg' to some random 'topic'");
}

static int mqtt_publish_cmd(int argc, char **argv)
{
(void) argc;
(void) argv;

if (argc < 2) {
usage();
return 1;
}

#define STR_LEN 25
MQTTMessage msg;
msg.qos = QOS1;
if (strcmp(argv[0], "pub") == 0) {
msg.payload = argv[1];
msg.payloadlen = strlen(argv[1]);
MQTTPublish(&client, topic, &msg);
printf("%s message to topic %s\n", argv[1], topic);
}
return 0;
}

int main(void)
{
#define BUF_SIZE 250
int ret = -1;

static const shell_command_t shell_commands[] = {
{ "pub", "publish MQTT 'msg' to 'topic'", mqtt_publish_cmd},
{ "ifconfig", "Shows assigned IPv6 addresses", ifconfig },
{ NULL, NULL, NULL}
};

char* remote_ip = REMOTE_IP_ADDRESS;
int port = 8808;
char* clientid = "myid";
char* username = "test";
char *passwd = "pass";
Network n;

printf("mqtt_example: trying to connect to %s , port: %d\n", remote_ip, port);
NetworkInit(&n);
if (NetworkConnect(&n, remote_ip, port)) {
printf("mqtt_example: Couldn't connect\n");
ret = -1;
}
else {
unsigned char buf[BUF_SIZE];
unsigned char readbuf[BUF_SIZE];
char* topic = "test_topic";

MQTTClientInit(&client, &n, 1000, buf, 100, readbuf, BUF_SIZE);

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = clientid;
data.username.cstring = username;
data.password.cstring = passwd;
data.keepAliveInterval = 3600;
data.cleansession = 1;
printf("Connecting to %s %d\n", remote_ip, port);

ret = MQTTConnect(&client, &data);
printf("mqtt_example: Connected %d\n", ret);
printf("Subscribing to %s\n", topic);
ret = MQTTSubscribe(&client, topic, QOS1, _messageArrived);
printf("mqtt_example: Subscribed %d\n", ret);
if (!ret) {
MQTTStartTask(&client);
thread_create(_sender_stack, sizeof(_sender_stack),
THREAD_PRIORITY_MAIN + 4, THREAD_CREATE_STACKTEST,
_mqtt_sender, NULL, "_mqtt_sender");
} else {
printf("mqtt_example: Couldn't run, type 'reboot'\n");
}
}

char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}
16 changes: 16 additions & 0 deletions pkg/mqtt-paho/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PKG_NAME = mqtt-paho
PKG_URL = https://github.com/eclipse/paho.mqtt.embedded-c.git
PKG_VERSION = 29ab2aa29c5e47794284376d7f8386cfd54c3eed
PKG_LICENSE = EDL

include $(RIOTBASE)/pkg/pkg.mk

TARGETS = mqtt-paho-packet

.PHONY: all

all: prepare mqtt-paho

mqtt-paho:
"$(MAKE)" -C $(PKG_BUILDDIR)/MQTTPacket/src/ -f $(CURDIR)/Makefile.$(PKG_NAME)-packet
"$(MAKE)" -C $(PKG_BUILDDIR)/MQTTClient-C/src/ -f $(CURDIR)/Makefile.$(PKG_NAME)
5 changes: 5 additions & 0 deletions pkg/mqtt-paho/Makefile.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
USEMODULE += xtimer
USEMODULE += ipv4_addr
USEMODULE += ipv6_addr
USEMODULE += mqtt-paho-contrib
USEMODULE += mqtt-paho-packet
7 changes: 7 additions & 0 deletions pkg/mqtt-paho/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
INCLUDES += -I$(PKGDIRBASE)/mqtt-paho/MQTTClient-C/src/
INCLUDES += -I$(PKGDIRBASE)/mqtt-paho/MQTTPacket/src
INCLUDES += -I$(RIOTBASE)/pkg/mqtt-paho/include
ifneq (,$(filter mqtt-paho-contrib,$(USEMODULE)))
DIRS += $(RIOTBASE)/pkg/mqtt-paho/contrib
endif
CFLAGS += -DSOCK_HAS_IPV6
10 changes: 10 additions & 0 deletions pkg/mqtt-paho/Makefile.mqtt-paho
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MODULE = mqtt-paho

RIOT_MQTT_IFACE_H = mqtt.h

CFLAGS += -DMQTT_TASK=1
CFLAGS += -DMQTTCLIENT_PLATFORM_HEADER="$(RIOT_MQTT_IFACE_H)"
CFLAGS += -Wno-sign-compare
CFLAGS += -Wno-unused-parameter

include $(RIOTBASE)/Makefile.base
5 changes: 5 additions & 0 deletions pkg/mqtt-paho/Makefile.mqtt-paho-packet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MODULE = mqtt-paho-packet

CFLAGS += -Wno-unused-parameter

include $(RIOTBASE)/Makefile.base
3 changes: 3 additions & 0 deletions pkg/mqtt-paho/contrib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE = mqtt-paho-contrib

include $(RIOTBASE)/Makefile.base
Loading

0 comments on commit 20d6cd2

Please sign in to comment.