Skip to content

Commit 64101b9

Browse files
authored
Merge pull request contiki-ng#723 from g-oikonomou/contrib/tests/mqtt-native
Add MQTT client execution test
2 parents cc6398d + aba1aa9 commit 64101b9

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

examples/mqtt-client/mqtt-client.c

+5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
#include <strings.h>
4848
/*---------------------------------------------------------------------------*/
4949
#define LOG_MODULE "mqtt-client"
50+
#ifdef MQTT_CLIENT_CONF_LOG_LEVEL
51+
#define LOG_LEVEL MQTT_CLIENT_CONF_LOG_LEVEL
52+
#else
5053
#define LOG_LEVEL LOG_LEVEL_NONE
54+
#endif
5155
/*---------------------------------------------------------------------------*/
5256
/* Controls whether the example will work in IBM Watson IoT platform mode */
5357
#ifdef MQTT_CLIENT_CONF_WITH_IBM_WATSON
@@ -305,6 +309,7 @@ pub_handler(const char *topic, uint16_t topic_len, const uint8_t *chunk,
305309
}
306310

307311
if(strncmp(&topic[10], "leds", 4) == 0) {
312+
LOG_DBG("Received MQTT SUB\n");
308313
if(chunk[0] == '1') {
309314
leds_on(LEDS_RED);
310315
} else if(chunk[0] == '0') {

examples/mqtt-client/project-conf.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
* devices, set your Org ID here and then make sure you set the correct token
5353
* through MQTT_CLIENT_CONF_AUTH_TOKEN.
5454
*/
55+
#ifndef MQTT_CLIENT_CONF_ORG_ID
5556
#define MQTT_CLIENT_CONF_ORG_ID "quickstart"
57+
#endif
5658

5759
/*
5860
* The MQTT username.
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
source ../utils.sh
3+
4+
# Contiki directory
5+
CONTIKI=$1
6+
7+
# Example code directory
8+
CODE_DIR=$CONTIKI/examples/mqtt-client/
9+
CODE=mqtt-client
10+
11+
CLIENT_LOG=$CODE.log
12+
CLIENT_TESTLOG=$CODE.testlog
13+
CLIENT_ERR=$CODE.err
14+
MOSQ_SUB_LOG=mosquitto_sub.log
15+
MOSQ_SUB_ERR=mosquitto_sub.err
16+
17+
# Start mosquitto server
18+
echo "Starting mosquitto daemon"
19+
mosquitto &> /dev/null &
20+
MOSQID=$!
21+
sleep 2
22+
23+
# Start mosquitto_sub client. Subscribe
24+
echo "Starting mosquitto subscriber"
25+
mosquitto_sub -t iot-2/evt/status/fmt/json > $MOSQ_SUB_LOG 2> $MOSQ_SUB_ERR &
26+
MSUBID=$!
27+
sleep 2
28+
29+
# Starting Contiki-NG native node
30+
echo "Starting native node"
31+
make -C $CODE_DIR TARGET=native \
32+
DEFINES=MQTT_CLIENT_CONF_ORG_ID=\\\"travis-test\\\",MQTT_CLIENT_CONF_LOG_LEVEL=LOG_LEVEL_DBG \
33+
> make.log 2> make.err
34+
sudo $CODE_DIR/$CODE.native > $CLIENT_LOG 2> $CLIENT_ERR &
35+
CPID=$!
36+
37+
# The mqtt-client will publish every 30 secs. Wait for 45
38+
sleep 45
39+
40+
# Send a publish to the mqtt client
41+
mosquitto_pub -m "1" -t iot-2/cmd/leds/fmt/json
42+
43+
echo "Closing native node"
44+
sleep 2
45+
kill_bg $CPID
46+
47+
echo "Stopping mosquitto daemon"
48+
kill_bg $MOSQID
49+
50+
echo "Stopping mosquitto subscriber"
51+
kill_bg $MSUBID
52+
53+
# Success criteria:
54+
# * mosquitto_sub output not empty
55+
# * mqtt-client.native output contains "MQTT SUB"
56+
SUB_RCV=`grep "MQTT SUB" $CLIENT_LOG`
57+
if [ -s "$MOSQ_SUB_LOG" -a -n "$SUB_RCV" ]
58+
then
59+
cp $CLIENT_LOG $CODE.testlog
60+
printf "%-32s TEST OK\n" "$CODE" | tee $CODE.testlog;
61+
else
62+
echo "==== make.log ====" ; cat make.log;
63+
echo "==== make.err ====" ; cat make.err;
64+
echo "==== $CLIENT_LOG ====" ; cat $CLIENT_LOG;
65+
echo "==== $CLIENT_ERR ====" ; cat $CLIENT_ERR;
66+
echo "==== $MOSQ_SUB_LOG ====" ; cat $MOSQ_SUB_LOG;
67+
echo "==== $MOSQ_SUB_ERR ====" ; cat $MOSQ_SUB_ERR;
68+
69+
printf "%-32s TEST FAIL\n" "$CODE" | tee $CODE.testlog;
70+
fi
71+
72+
rm make.log
73+
rm make.err
74+
rm $CLIENT_LOG $CLIENT_ERR
75+
rm $MOSQ_SUB_LOG $MOSQ_SUB_ERR
76+
77+
# We do not want Make to stop -> Return 0
78+
# The Makefile will check if a log contains FAIL at the end
79+
exit 0

0 commit comments

Comments
 (0)