Merge pull request #471 from g-oikonomou/contrib/mqtt-client-with-watson

Easy switch to IBM Watson mode
This commit is contained in:
Simon Duquennoy 2018-05-11 21:10:12 +02:00 committed by GitHub
commit 6acac2aa91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 35 deletions

View File

@ -13,25 +13,34 @@ The demo will give some visual feedback with a LED (configurable):
This example is known to work with all platforms that support the new button
API.
This example can operate in two modes: A default mode to be used with the
mosquitto MQTT broker and a second mode to be used with the IBM Watson IoT
platform.
To enable Watson mode, define `MQTT_CLIENT_CONF_WITH_IBM_WATSON` as 1 in the
example's `project-conf.h`.
Publishing
----------
By default the example will attempt to publish readings to an MQTT broker
running on the IPv6 address specified as `MQTT_DEMO_BROKER_IP_ADDR` in
running on the IPv6 address specified as `MQTT_CLIENT_CONF_BROKER_IP_ADDR` in
`project-conf.h`. This functionality was tested successfully with
[mosquitto](http://mosquitto.org/).
[mosquitto](http://mosquitto.org/). This define will be ignored in IBM Watson
mode.
The publish messages include sensor readings but also some other information,
such as device uptime in seconds and a message sequence number. The demo will
publish to topic `iot-2/evt/status/fmt/json`. The device will connect using
client-id `d:contiki-ng:mqtt-client:<device-id>`, where `<device-id>` gets
constructed from the device's IEEE address.
client-id `d:<org-id>:mqtt-client:<device-id>`, where `<device-id>` gets
constructed from the device's IEEE address. `<org-id>` can be controlled
through the `MQTT_CLIENT_CONF_ORG_ID` define.
Subscribing
-----------
You can also subscribe to topics and receive commands, but this will only
work if you use "Org ID" != 'quickstart'. To achieve this, you will need to
change 'Org ID' (`DEFAULT_ORG_ID`). In this scenario, the device will subscribe
to:
change `MQTT_CLIENT_CONF_ORG_ID` in `project-conf.h`. In this scenario, the
device will subscribe to:
`iot-2/cmd/+/fmt/json`
@ -53,13 +62,18 @@ messages, outgoing publish messages use proper json payload.
IBM Quickstart Service
----------------------
It is also possible to publish to IBM's quickstart service. To do so, you need
to undefine `MQTT_DEMO_BROKER_IP_ADDR`.
to enable this mode by setting `MQTT_CLIENT_CONF_WITH_IBM_WATSON` to 1 in
`project-conf.h`.
The device will then try to connect to IBM's quickstart over NAT64, so you will
need a NAT64 gateway in your network to make this work. A guide on how to
setup NAT64 is out of scope here.
setup NAT64 is out of scope here, but you can find one in the
[Contiki-NG wiki](https://github.com/contiki-ng/contiki-ng/wiki/NAT64-for-Contiki%E2%80%90NG).
If you want to use IBM's cloud service with a registered device, change
'Org ID' (`DEFAULT_ORG_ID`) and provide the 'Auth Token' (`DEFAULT_AUTH_TOKEN`),
which acts as a 'password', but bear in mind that it gets transported in clear
text.
If you want to use IBM's cloud service with a registered device, you will need
to set `MQTT_CLIENT_CONF_ORG_ID` and then also to provide the 'Auth Token'
(`MQTT_CLIENT_CONF_AUTH_TOKEN`), which acts as a 'password'. You will also
need to configure your Organisation / Registered device on Watson such that
TLS is optional.
Note: The token will be transported in cleartext.

View File

@ -49,22 +49,61 @@
#define LOG_MODULE "mqtt-client"
#define LOG_LEVEL LOG_LEVEL_NONE
/*---------------------------------------------------------------------------*/
/*
* IBM server: messaging.quickstart.internetofthings.ibmcloud.com
* (184.172.124.189) mapped in an NAT64 (prefix 64:ff9b::/96) IPv6 address
* Note: If not able to connect; lookup the IP address again as it may change.
*
* Alternatively, publish to a local MQTT broker (e.g. mosquitto) running on
* the node that hosts your border router
*/
#ifdef MQTT_CLIENT_CONF_BROKER_IP_ADDR
static const char *broker_ip = MQTT_CLIENT_CONF_BROKER_IP_ADDR;
#define DEFAULT_ORG_ID "contiki-ng"
/* Controls whether the example will work in IBM Watson IoT platform mode */
#ifdef MQTT_CLIENT_CONF_WITH_IBM_WATSON
#define MQTT_CLIENT_WITH_IBM_WATSON MQTT_CLIENT_CONF_WITH_IBM_WATSON
#else
static const char *broker_ip = "0064:ff9b:0000:0000:0000:0000:b8ac:7cbd";
#define DEFAULT_ORG_ID "quickstart"
#define MQTT_CLIENT_WITH_IBM_WATSON 0
#endif
/*---------------------------------------------------------------------------*/
/* MQTT broker address. Ignored in Watson mode */
#ifdef MQTT_CLIENT_CONF_BROKER_IP_ADDR
#define MQTT_CLIENT_BROKER_IP_ADDR MQTT_CLIENT_CONF_BROKER_IP_ADDR
#else
#define MQTT_CLIENT_BROKER_IP_ADDR "fd00::1"
#endif
/*---------------------------------------------------------------------------*/
/*
* MQTT Org ID.
*
* If it equals "quickstart", the client will connect without authentication.
* In all other cases, the client will connect with authentication mode.
*
* In Watson mode, the username will be "use-token-auth". In non-Watson mode
* the username will be MQTT_CLIENT_USERNAME.
*
* In all cases, the password will be MQTT_CLIENT_AUTH_TOKEN.
*/
#ifdef MQTT_CLIENT_CONF_ORG_ID
#define MQTT_CLIENT_ORG_ID MQTT_CLIENT_CONF_ORG_ID
#else
#define MQTT_CLIENT_ORG_ID "quickstart"
#endif
/*---------------------------------------------------------------------------*/
/* MQTT token */
#ifdef MQTT_CLIENT_CONF_AUTH_TOKEN
#define MQTT_CLIENT_AUTH_TOKEN MQTT_CLIENT_CONF_AUTH_TOKEN
#else
#define MQTT_CLIENT_AUTH_TOKEN "AUTHTOKEN"
#endif
/*---------------------------------------------------------------------------*/
#if MQTT_CLIENT_WITH_IBM_WATSON
/* With IBM Watson support */
static const char *broker_ip = "0064:ff9b:0000:0000:0000:0000:b8ac:7cbd";
#define MQTT_CLIENT_USERNAME "use-token-auth"
#else /* MQTT_CLIENT_WITH_IBM_WATSON */
/* Without IBM Watson support. To be used with other brokers, e.g. Mosquitto */
static const char *broker_ip = MQTT_CLIENT_BROKER_IP_ADDR;
#ifdef MQTT_CLIENT_CONF_USERNAME
#define MQTT_CLIENT_USERNAME MQTT_CLIENT_CONF_USERNAME
#else
#define MQTT_CLIENT_USERNAME "use-token-auth"
#endif
#endif /* MQTT_CLIENT_WITH_IBM_WATSON */
/*---------------------------------------------------------------------------*/
#ifdef MQTT_CLIENT_CONF_STATUS_LED
#define MQTT_CLIENT_STATUS_LED MQTT_CLIENT_CONF_STATUS_LED
#else
@ -122,18 +161,12 @@ static uint8_t state;
#define CONFIG_CMD_TYPE_LEN 8
#define CONFIG_IP_ADDR_STR_LEN 64
/*---------------------------------------------------------------------------*/
#define RSSI_MEASURE_INTERVAL_MAX 86400 /* secs: 1 day */
#define RSSI_MEASURE_INTERVAL_MIN 5 /* secs */
#define PUBLISH_INTERVAL_MAX 86400 /* secs: 1 day */
#define PUBLISH_INTERVAL_MIN 5 /* secs */
/*---------------------------------------------------------------------------*/
/* A timeout used when waiting to connect to a network */
#define NET_CONNECT_PERIODIC (CLOCK_SECOND >> 2)
#define NO_NET_LED_DURATION (NET_CONNECT_PERIODIC >> 1)
/*---------------------------------------------------------------------------*/
/* Default configuration values */
#define DEFAULT_TYPE_ID "mqtt-client"
#define DEFAULT_AUTH_TOKEN "AUTHZ"
#define DEFAULT_EVENT_TYPE_ID "status"
#define DEFAULT_SUBSCRIBE_CMD_TYPE "+"
#define DEFAULT_BROKER_PORT 1883
@ -423,9 +456,10 @@ init_config()
/* Populate configuration with default values */
memset(&conf, 0, sizeof(mqtt_client_config_t));
memcpy(conf.org_id, DEFAULT_ORG_ID, strlen(DEFAULT_ORG_ID));
memcpy(conf.org_id, MQTT_CLIENT_ORG_ID, strlen(MQTT_CLIENT_ORG_ID));
memcpy(conf.type_id, DEFAULT_TYPE_ID, strlen(DEFAULT_TYPE_ID));
memcpy(conf.auth_token, DEFAULT_AUTH_TOKEN, strlen(DEFAULT_AUTH_TOKEN));
memcpy(conf.auth_token, MQTT_CLIENT_AUTH_TOKEN,
strlen(MQTT_CLIENT_AUTH_TOKEN));
memcpy(conf.event_type_id, DEFAULT_EVENT_TYPE_ID,
strlen(DEFAULT_EVENT_TYPE_ID));
memcpy(conf.broker_ip, broker_ip, strlen(broker_ip));
@ -568,7 +602,7 @@ state_machine(void)
state = STATE_ERROR;
break;
} else {
mqtt_set_username_password(&conn, "use-token-auth",
mqtt_set_username_password(&conn, MQTT_CLIENT_USERNAME,
conf.auth_token);
}
}

View File

@ -35,8 +35,40 @@
/* Enable TCP */
#define UIP_CONF_TCP 1
/* If undefined, the demo will attempt to connect to IBM's quickstart */
/* Change to 1 to use with the IBM Watson IoT platform */
#define MQTT_CLIENT_CONF_WITH_IBM_WATSON 0
/*
* The IPv6 address of the MQTT broker to connect to.
* Ignored if MQTT_CLIENT_CONF_WITH_IBM_WATSON is 1
*/
#define MQTT_CLIENT_CONF_BROKER_IP_ADDR "fd00::1"
/*
* The Organisation ID.
*
* When in Watson mode, the example will default to Org ID "quickstart" and
* will connect using non-authenticated mode. If you want to use registered
* devices, set your Org ID here and then make sure you set the correct token
* through MQTT_CLIENT_CONF_AUTH_TOKEN.
*/
#define MQTT_CLIENT_CONF_ORG_ID "quickstart"
/*
* The MQTT username.
*
* Ignored in Watson mode: In this mode the username is always "use-token-auth"
*/
#define MQTT_CLIENT_CONF_USERNAME "mqtt-client-username"
/*
* The MQTT auth token (password) used when connecting to the MQTT broker.
*
* Used with as well as without Watson.
*
* Transported in cleartext!
*/
#define MQTT_CLIENT_CONF_AUTH_TOKEN "AUTHTOKEN"
/*---------------------------------------------------------------------------*/
#endif /* PROJECT_CONF_H_ */
/*---------------------------------------------------------------------------*/