Reputation: 1
My setup :
Ubuntu22.04 VM running IPv6 network enabled docker - 2001:db8:2::/64
Contiki-ng container on the docker with ifconfig -
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.2 netmask 255.255.0.0 broadcast 172.18.255.255
inet6 fe80::42:acff:fe12:2 prefixlen 64 scopeid 0x20<link>
inet6 2001:db8:2::2 prefixlen 64 scopeid 0x0<global>
ether 02:42:ac:12:00:02 txqueuelen 0 (Ethernet)
RX packets 208 bytes 182016 (182.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 95 bytes 7465 (7.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 370 bytes 33910 (33.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 370 bytes 33910 (33.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
allow_anonymous true
command to run mqttbroker - mosquitto -c /etc/mosquitto/mosquitto.conf -v
my cooja mote codes for node A, B, C -
Basically what I'm trying to achieve is a cyclic connection between 3 nodes where Node A publishes to Node B and subscribes to Node C via topics. The same is the case with Node B and Node C.
Node A Code is below :
#include "contiki.h"
#include "os/net/app-layer/mqtt/mqtt.h"
#include <string.h>
#define BROKER_IP "2001:db8:2::2"
#define PUBLISH_TOPIC "A2B" // Node A publishes to "A2B"
#define SUBSCRIBE_TOPIC "C2A" // Node A subscribes to "C2A"
#define CLIENT_ID "node-a" // Unique client ID for Node A
#define MQTT_PORT 1883
#define PUBLISH_INTERVAL (CLOCK_SECOND * 10) // Adjust the interval as needed
#define MQTT_MESSAGE_BUFFER_SIZE 128 // Defines the buffer size for MQTT messages
static struct mqtt_connection conn;
static char broker_ip[] = BROKER_IP;
static char client_id[] = CLIENT_ID;
PROCESS(node_a_process, "Node A Process");
AUTOSTART_PROCESSES(&node_a_process);
static void mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) {
switch (event) {
case MQTT_EVENT_CONNECTED: {
printf("Connected to the MQTT broker.\n");
// Subscribe to "C2A" upon successful connection
mqtt_subscribe(m, NULL, SUBSCRIBE_TOPIC, MQTT_QOS_LEVEL_1);
break;
}
case MQTT_EVENT_DISCONNECTED: {
printf("Disconnected from the MQTT broker.\n");
break;
}
case MQTT_EVENT_PUBLISH: {
// This is where Node A would handle incoming messages from "C2A"
printf("Received a message on A.\n");
break;
}
case MQTT_EVENT_SUBACK: {
printf("Subscription successful.\n");
break;
}
default:
printf("Unhandled MQTT event: %d\n", event);
break;
}
}
PROCESS_THREAD(node_a_process, ev, data) {
static struct etimer publish_timer;
PROCESS_BEGIN();
mqtt_status_t status = mqtt_register(&conn, &node_a_process, client_id, mqtt_event, MQTT_MESSAGE_BUFFER_SIZE);
if(status != MQTT_STATUS_OK) {
printf("MQTT register failed: %d\n", status);
PROCESS_EXIT();
}
status = mqtt_connect(&conn, broker_ip, MQTT_PORT, 60, 1 /* Clean Session */);
if(status != MQTT_STATUS_OK) {
printf("MQTT connect failed: %d\n", status);
PROCESS_EXIT();
}
etimer_set(&publish_timer, PUBLISH_INTERVAL);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&publish_timer));
etimer_reset(&publish_timer);
// Node A publishes its message to "A2B"
static char msg[] = "Message from A to B";
status = mqtt_publish(&conn, NULL, PUBLISH_TOPIC, (uint8_t *)msg, strlen(msg), MQTT_QOS_LEVEL_1, MQTT_RETAIN_OFF);
if(status != MQTT_STATUS_OK) {
printf("MQTT publish failed: %d\n", status);
}
}
PROCESS_END();
}
Node B Code is below :
#include "contiki.h"
#include "os/net/app-layer/mqtt/mqtt.h"
#include <string.h>
#define BROKER_IP "2001:db8:2::2"
#define PUBLISH_TOPIC "B2C"
#define SUBSCRIBE_TOPIC "A2B"
#define CLIENT_ID "node-b"
#define MQTT_PORT 1883
#define PUBLISH_INTERVAL (CLOCK_SECOND * 12) // Slightly offset from Node A for demonstration
#define MQTT_MESSAGE_BUFFER_SIZE 128 // This line defines the buffer size
static struct mqtt_connection conn;
static char broker_ip[] = BROKER_IP;
static char client_id[] = CLIENT_ID;
PROCESS(node_b_process, "Node B Process");
AUTOSTART_PROCESSES(&node_b_process);
static void mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) {
switch (event) {
case MQTT_EVENT_CONNECTED: {
printf("Connected to the MQTT broker.\n");
mqtt_subscribe(m, NULL, SUBSCRIBE_TOPIC, MQTT_QOS_LEVEL_1);
break;
}
case MQTT_EVENT_DISCONNECTED: {
printf("Disconnected from the MQTT broker.\n");
break;
}
case MQTT_EVENT_PUBLISH: {
printf("Received a message on B.\n");
// Node B could process the message here before sending to C
break;
}
case MQTT_EVENT_SUBACK: {
printf("Subscription successful.\n");
break;
}
default:
printf("Unhandled MQTT event: %d\n", event);
break;
}
}
PROCESS_THREAD(node_b_process, ev, data) {
static struct etimer publish_timer;
PROCESS_BEGIN();
mqtt_status_t status = mqtt_register(&conn, &node_b_process, client_id, mqtt_event, MQTT_MESSAGE_BUFFER_SIZE);
if(status != MQTT_STATUS_OK) {
printf("MQTT register failed: %d\n", status);
PROCESS_EXIT();
}
status = mqtt_connect(&conn, broker_ip, MQTT_PORT, 60, 1 /* Clean Session */);
if(status != MQTT_STATUS_OK) {
printf("MQTT connect failed: %d\n", status);
PROCESS_EXIT();
}
etimer_set(&publish_timer, PUBLISH_INTERVAL);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&publish_timer));
etimer_reset(&publish_timer);
static char msg[] = "Message from B to C";
status = mqtt_publish(&conn, NULL, PUBLISH_TOPIC, (uint8_t *)msg, strlen(msg), MQTT_QOS_LEVEL_1, MQTT_RETAIN_OFF);
if(status != MQTT_STATUS_OK) {
printf("MQTT publish failed: %d\n", status);
}
}
PROCESS_END();
}
Node C code is below :
#include "contiki.h"
#include "os/net/app-layer/mqtt/mqtt.h"
#include <string.h>
#define BROKER_IP "2001:db8:2::2"
#define PUBLISH_TOPIC "C2A"
#define SUBSCRIBE_TOPIC "B2C"
#define CLIENT_ID "node-c"
#define MQTT_PORT 1883
#define PUBLISH_INTERVAL (CLOCK_SECOND * 14) // Slightly offset from Node B for demonstration
#define MQTT_MESSAGE_BUFFER_SIZE 128 // This line defines the buffer size
static struct mqtt_connection conn;
static char broker_ip[] = BROKER_IP;
static char client_id[] = CLIENT_ID;
PROCESS(node_c_process, "Node C Process");
AUTOSTART_PROCESSES(&node_c_process);
static void mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) {
switch (event) {
case MQTT_EVENT_CONNECTED: {
printf("Connected to the MQTT broker.\n");
mqtt_subscribe(m, NULL, SUBSCRIBE_TOPIC, MQTT_QOS_LEVEL_1);
break;
}
case MQTT_EVENT_DISCONNECTED: {
printf("Disconnected from the MQTT broker.\n");
break;
}
case MQTT_EVENT_PUBLISH: {
printf("Received a message on C.\n");
// Node C could process the message here before sending to A
break;
}
case MQTT_EVENT_SUBACK: {
printf("Subscription successful.\n");
break;
}
default:
printf("Unhandled MQTT event: %d\n", event);
break;
}
}
PROCESS_THREAD(node_c_process, ev, data) {
static struct etimer publish_timer;
PROCESS_BEGIN();
mqtt_status_t status = mqtt_register(&conn, &node_c_process, client_id, mqtt_event, MQTT_MESSAGE_BUFFER_SIZE);
if(status != MQTT_STATUS_OK) {
printf("MQTT register failed: %d\n", status);
PROCESS_EXIT();
}
status = mqtt_connect(&conn, broker_ip, MQTT_PORT, 60, 1 /* Clean Session */);
if(status != MQTT_STATUS_OK) {
printf("MQTT connect failed: %d\n", status);
PROCESS_EXIT();
}
etimer_set(&publish_timer, PUBLISH_INTERVAL);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&publish_timer));
etimer_reset(&publish_timer);
static char msg[] = "Message from C to A";
status = mqtt_publish(&conn, NULL, PUBLISH_TOPIC, (uint8_t *)msg, strlen(msg), MQTT_QOS_LEVEL_1, MQTT_RETAIN_OFF);
if(status != MQTT_STATUS_OK) {
printf("MQTT publish failed: %d\n", status);
}
}
PROCESS_END();
}
00:10.382 ID:1 MQTT publish failed: 129
00:12.236 ID:2 MQTT publish failed: 129
00:14.899 ID:3 MQTT publish failed: 129
00:20.382 ID:1 MQTT publish failed: 129
00:24.236 ID:2 MQTT publish failed: 129
00:28.899 ID:3 MQTT publish failed: 129
00:30.382 ID:1 MQTT publish failed: 129
00:36.236 ID:2 MQTT publish failed: 129
00:40.382 ID:1 MQTT publish failed: 129
00:42.899 ID:3 MQTT publish failed: 129
00:48.236 ID:2 MQTT publish failed: 129
00:50.236 ID:2 Disconnected from the MQTT broker.
00:50.382 ID:1 Disconnected from the MQTT broker.
00:50.382 ID:1 MQTT publish failed: 129
00:50.899 ID:3 Disconnected from the MQTT broker.
00:56.899 ID:3 MQTT publish failed: 129
01:00.236 ID:2 MQTT publish failed: 129
01:00.382 ID:1 MQTT publish failed: 129
01:10.382 ID:1 MQTT publish failed: 129
01:10.899 ID:3 MQTT publish failed: 129
I couldn't solve the networking behind it so require help from both communities. The logs of mqtt broker also doesn't reflect any topics connection activity but when i try from terminal with mosquitto_pub and mosquitto_sub, i'm able to create topics and connect with broker.
Need to solve this? Please help.
Upvotes: 0
Views: 67