Cloud Bluemix Demo
Demonstrates IBM Bluemix communication over the MQTT protocol
- SDK directory:
apps/cloud/bluemix
API Features Demonstrated
Platforms
This app works on:
- AMW004-E03 - Zentri Mackerel evaluation board
- AMW106-E03 - Zentri Moray evaluation board
- NXP-SCCK - NXP Secure Cloud Connectivity Kit - Note that for this device the
periodic_publish
function must be modified to use a different sensor. - Avnet BCM4343W - Avnet IoT Starter Kit - Note that for this device the
periodic_publish
function must be modified to use a different sensor.
Requirements and Prerequisites
This app requires ZentriOS 3.1+.
The application is written to transmit thermistor data. The Zentri AMW004-E03 and AWM106-E03 evaluation boards have a thermistor connected to a standard ADC and GPIO.
For other platforms you can substitute another data source, by modifying the periodic_publish
function.
Resources
The manifest.json file is configured to download two lookup tables to the device:
resources/lut_10bit.csv
resources/lut_12bit.csv
Description
This demonstration does not require a user account or security certificates.
The application connects to the Bluemix Quickstart broker:
quickstart.messaging.internetofthings.ibmcloud.com
After connection is established, the app periodically reads the thermistor and converts the value to temperature, using lookup tables. It also reads the signal strength of the Access Point. The result is then published to the quickstart
topic. The period is by default 5 seconds.
You can view a graph of the transmitted values on the IBM Quickstart IoT site:
https://quickstart.internetofthings.ibmcloud.com
Usage Instructions
Open a ZentriOS serial terminal to the device. See Getting Started, Opening a ZentriOS Terminal.
This app assumes your ZentriOS device is set up with the credentials to join your local network. If you have not already set up network credentials, on the ZentriOS terminal, run the commands:
network_up -s
save
See the Wi-Fi Command API documentation for network_up and save.
On starting up, the app displays messages similar to the following on the ZentriOS terminal:
> Network is down, restarting...
[Associating to Zentri_Guest]
> Security type from probe: WPA2-AES
Obtaining IPv4 address via DHCP
IPv4 address: 192.168.6.113
[Associated]
>
IBM Bluemix MQTT Demo Application Started
- Broker: quickstart.messaging.internetofthings.ibmcloud.com
- Client ID: d:quickstart:type:zentri_4422
- Topic/Queue: iot-2/evt/zentri/fmt/json
Opening connection with broker quickstart.messaging.internetofthings.ibmcloud.com:1883
[Opening: quickstart.messaging.internetofthings.ibmcloud.com:1883]
> Resolving host: quickstart.messaging.internetofthings.ibmcloud.com
Connecting (TCP): 184.172.124.189:1883
[2016-03-29 | 06:14:35: Opened: 0]
> Connection established
Connecting...
Send CONNECT frame
If the connection is successful, the app displays messages similar to the following:
Received CONNACK
CONNECTED
Publishing to topic: 'iot-2/evt/zentri/fmt/json', message: '{"d":{"signal strength": -72, "temperature": 25.7693}}'
Send PUBLISH frame
MESSAGE PUBLISHED
Publishing to topic: 'iot-2/evt/zentri/fmt/json', message: '{"d":{"signal strength": -67, "temperature": 25.7436}}'
Send PUBLISH frame
MESSAGE PUBLISHED
Publishing to topic: 'iot-2/evt/zentri/fmt/json', message: '{"d":{"signal strength": -75, "temperature": 25.7693}}'
Send PUBLISH frame
...
To view the transmitted values on the IBM Quickstart IoT site, open the following URL in a browser:
https://quickstart.internetofthings.ibmcloud.com
In the Quickstart page Device ID box, enter the client ID type
displayed in the ZentriOS terminal when the app starts up. For example if the app displays:
- Client ID: d:quickstart:type:zentri_4422
then enter zentri_4422
in the Device ID box
The device ID is unique to the device, in the form zentri_xxxx
where xxxx
represents the least significant 4 bytes of the device WLAN MAC address.
Implementation
Configuration values and constants
Sets up global variables:
settings
- settings structuremqtt_connection
- object to hold MQTT connection detailscallback
-mqtt_connection_event_cb
connection handler callback
zn_app_init
- Starts up the network if required with zn_network_restart
- Initalises the thermistor ADC
- Initialises the lookup table
- Allocates memory for an MQTT connection object with zn_malloc
- Initialises the MQTT connection object with mqtt_init
- Calls
mqtt_app_connect
to connect to the IBM cloud service
mqtt_app_connect
- calls mqtt_open to open the connection to the IBM Watson IoT platform Quickstart site.
- If unsuccessful, logs an error and returns.
- The connection handler callback function supplied is the static function
mqtt_connection_event_cb
. This function is called on any subsequent connection event.
- On successful open, calls mqtt_connect to send the CONNECT message requesting a connection.
- Note how memory for the
conninfo
is allocated - The connection info includes a keepalive period and the client ID.
- Note how memory for the
mqtt_connection_event_cb
- Callback to handle connection events
- On successful connection event, issues a one-off event
periodic_publish
- On subsequent publish events, issues a timed
periodic_publish
event
- On successful connection event, issues a one-off event
periodic_publish
- Sends a message with the ADC temperature value and the RSSI reading. On a platform with other sensors, you can modify this function to send an appropriate value.
- calls zn_issue_command_return_data to issue the wlan_get_rssi command and return the value.
- gets the ADC value with zn_adc_gpio_to_peripheral. Logs error if unsuccessful.
- reads ADC value as temperature using the previously set up lookup table with zn_adc_sample_and_convert. Logs error if unsuccessful.
- converts the floating point temperature to a string and formats as an MQTT message in the global
settings.message
. - calls
mqtt_app_publish
to publish the message
mqtt_app_publish
- calls mqtt_publish to publish
settings.message
to the topicsettings.topic
using the connectionmqtt.connection
. Logs an error if unsuccessful.
Source
See: