MQTT client for IBM Bluemix

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL MQTT VL53L0X

Getting started with IBM Bluemix

This is the MQTT Client example for mbed OS. It demonstrates how to connect a device with IBM Bluemix, how to get values from sensors, and how to see them on the IBM quickstart website. If you are unfamiliar with IBM Bluemix, we recommend that you read the introduction starting here.

Demo workflow:

  • Connection to the local network with 6LoWPAN ND connection.
  • Connection to the Quickstart Bluemix.
  • Read data sensors.
  • Sends data sensors to the Quickstart Bluemix

You can compile this project in three ways:

1. Using the Online compiler. Just clicking here:

/media/uploads/rspelta/import-mqtt.jpg

Information

Learn how to use the Online compiler reading https://docs.mbed.com/docs/mbed-os-handbook/en/latest/dev_tools/online_comp/ page.

2. Using the compiler on your PC:

Information

Learn how to use the mbed-cli reading https://docs.mbed.com/docs/mbed-os-handbook/en/latest/dev_tools/cli/ page.
The name of the machine is SILICA_SENSOR_NODE.

3. Exporting to 3rd party tools (IDE) like eclipse, IAR, uvision 5 and more others

Information

Learn how to use the mbed-cli reading https://docs.mbed.com/docs/mbed-os-handbook/en/latest/dev_tools/third_party/ page.

Warning

This example requires a Border Router board. For more details please read the Border Router paragraph from this page.

Please read carefully the next pages:

  • What to do before to compile the project: read here. This step is indipendent from the way you compile the project.
Committer:
rspelta
Date:
Thu Nov 02 11:01:13 2017 +0100
Revision:
0:e673387d5add
release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rspelta 0:e673387d5add 1 /**
rspelta 0:e673387d5add 2 ******************************************************************************
rspelta 0:e673387d5add 3 * @file main.cpp
rspelta 0:e673387d5add 4 * @version V1.0.0
rspelta 0:e673387d5add 5 * @date 27-October-2017
rspelta 0:e673387d5add 6 * @brief Simple Example application for using SILICA_SENSOR_NODE with IBM Bluemix
rspelta 0:e673387d5add 7 * project ported from X_NUCLEO_IKS01A2 ST project
rspelta 0:e673387d5add 8 *
rspelta 0:e673387d5add 9 ******************************************************************************
rspelta 0:e673387d5add 10 * @attention
rspelta 0:e673387d5add 11 *
rspelta 0:e673387d5add 12 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
rspelta 0:e673387d5add 13 *
rspelta 0:e673387d5add 14 * Redistribution and use in source and binary forms, with or without modification,
rspelta 0:e673387d5add 15 * are permitted provided that the following conditions are met:
rspelta 0:e673387d5add 16 * 1. Redistributions of source code must retain the above copyright notice,
rspelta 0:e673387d5add 17 * this list of conditions and the following disclaimer.
rspelta 0:e673387d5add 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
rspelta 0:e673387d5add 19 * this list of conditions and the following disclaimer in the documentation
rspelta 0:e673387d5add 20 * and/or other materials provided with the distribution.
rspelta 0:e673387d5add 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
rspelta 0:e673387d5add 22 * may be used to endorse or promote products derived from this software
rspelta 0:e673387d5add 23 * without specific prior written permission.
rspelta 0:e673387d5add 24 *
rspelta 0:e673387d5add 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rspelta 0:e673387d5add 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rspelta 0:e673387d5add 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rspelta 0:e673387d5add 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
rspelta 0:e673387d5add 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rspelta 0:e673387d5add 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rspelta 0:e673387d5add 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
rspelta 0:e673387d5add 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
rspelta 0:e673387d5add 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
rspelta 0:e673387d5add 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rspelta 0:e673387d5add 35 *
rspelta 0:e673387d5add 36 ******************************************************************************
rspelta 0:e673387d5add 37 */
rspelta 0:e673387d5add 38
rspelta 0:e673387d5add 39 /* Includes */
rspelta 0:e673387d5add 40 #include "mbed.h"
rspelta 0:e673387d5add 41 #include "mbed-trace/mbed_trace.h"
rspelta 0:e673387d5add 42
rspelta 0:e673387d5add 43 #include "HTS221Sensor.h"
rspelta 0:e673387d5add 44 #include "LPS22HBSensor.h"
rspelta 0:e673387d5add 45 #include "LSM6DSLSensor.h"
rspelta 0:e673387d5add 46
rspelta 0:e673387d5add 47 #include "easy-connect/easy-connect.h"
rspelta 0:e673387d5add 48
rspelta 0:e673387d5add 49 #include "MQTTNetwork.h"
rspelta 0:e673387d5add 50 #include "MQTT/MQTTmbed.h"
rspelta 0:e673387d5add 51 #include "MQTT/MQTTClient.h"
rspelta 0:e673387d5add 52
rspelta 0:e673387d5add 53 #define MAX_MQTT_PACKET_SIZE 250
rspelta 0:e673387d5add 54
rspelta 0:e673387d5add 55 #define logMessage printf
rspelta 0:e673387d5add 56
rspelta 0:e673387d5add 57 #include "LSM303AGRMagSensor.h"
rspelta 0:e673387d5add 58 #include "LSM303AGRAccSensor.h"
rspelta 0:e673387d5add 59
rspelta 0:e673387d5add 60 /* Retrieve the composing elements of the expansion board */
rspelta 0:e673387d5add 61
rspelta 0:e673387d5add 62 /* Interface definition */
rspelta 0:e673387d5add 63 #define SPI_TYPE_LPS22HB LPS22HBSensor::SPI3W
rspelta 0:e673387d5add 64 #define SPI_TYPE_LSM6DSL LSM6DSLSensor::SPI3W
rspelta 0:e673387d5add 65 SPI devSPI(PB_15, NC, PB_13); // 3-wires SPI on SensorTile
rspelta 0:e673387d5add 66 static Serial ser(PC_12,PD_2); // Serial with SensorTile Cradle Exp. Board + Nucleo
rspelta 0:e673387d5add 67 #define printf(...) ser.printf(__VA_ARGS__)
rspelta 0:e673387d5add 68
rspelta 0:e673387d5add 69 /* Environmental sensors */
rspelta 0:e673387d5add 70 static LPS22HBSensor press_temp(&devSPI, PA_3, NC, SPI_TYPE_LPS22HB);
rspelta 0:e673387d5add 71
rspelta 0:e673387d5add 72 /* Motion sensors */
rspelta 0:e673387d5add 73 static LSM6DSLSensor acc_gyro(&devSPI,PB_12, NC, PA_2, SPI_TYPE_LSM6DSL);
rspelta 0:e673387d5add 74 static LSM303AGRMagSensor magnetometer(&devSPI, PB_1);
rspelta 0:e673387d5add 75 static LSM303AGRAccSensor accelerometer(&devSPI, PC_4);
rspelta 0:e673387d5add 76
rspelta 0:e673387d5add 77
rspelta 0:e673387d5add 78 /* Simple main function */
rspelta 0:e673387d5add 79 int main( void )
rspelta 0:e673387d5add 80 {
rspelta 0:e673387d5add 81 char buf[500];
rspelta 0:e673387d5add 82 char* topic = MQTT_TOPIC;
rspelta 0:e673387d5add 83 unsigned int index = 0;
rspelta 0:e673387d5add 84 uint8_t id;
rspelta 0:e673387d5add 85 float temp, press;
rspelta 0:e673387d5add 86 int32_t axes[3];
rspelta 0:e673387d5add 87 const char* hostname = MQTT_URL_BROKER;
rspelta 0:e673387d5add 88 int port = 1883;
rspelta 0:e673387d5add 89
rspelta 0:e673387d5add 90
rspelta 0:e673387d5add 91 MQTT::Message message;
rspelta 0:e673387d5add 92
rspelta 0:e673387d5add 93 mbed_trace_init();
rspelta 0:e673387d5add 94
rspelta 0:e673387d5add 95 printf("Avnet-Silica Sensor Node\n\rmbed-os-mqtt-client demo\r\n");
rspelta 0:e673387d5add 96
rspelta 0:e673387d5add 97 NetworkInterface* network = easy_connect(true);
rspelta 0:e673387d5add 98 if(network == NULL) {
rspelta 0:e673387d5add 99 tr_err("\nConnection to Network Failed - exiting application...\n");
rspelta 0:e673387d5add 100 return -1;
rspelta 0:e673387d5add 101 }
rspelta 0:e673387d5add 102
rspelta 0:e673387d5add 103 MQTTNetwork mqttNetwork(network);
rspelta 0:e673387d5add 104 MQTT::Client<MQTTNetwork, Countdown, MAX_MQTT_PACKET_SIZE> client(mqttNetwork);
rspelta 0:e673387d5add 105 printf("Connecting to %s:%d\r\n", hostname, port);
rspelta 0:e673387d5add 106 int rc = mqttNetwork.connect(hostname, port);
rspelta 0:e673387d5add 107 if (rc != 0) {
rspelta 0:e673387d5add 108 tr_err("rc from TCP connect is %d\r\n", rc);
rspelta 0:e673387d5add 109 return -1;
rspelta 0:e673387d5add 110 }
rspelta 0:e673387d5add 111 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
rspelta 0:e673387d5add 112 data.MQTTVersion = 3;
rspelta 0:e673387d5add 113 data.clientID.cstring = MQTT_CLIENT_ID;
rspelta 0:e673387d5add 114 data.username.cstring = MQTT_USERNAME;
rspelta 0:e673387d5add 115 data.password.cstring = MQTT_PASSWORD;
rspelta 0:e673387d5add 116 tr_debug( "Connecting with Client Id: %s\r\n", MQTT_CLIENT_ID );
rspelta 0:e673387d5add 117 if ((rc = client.connect(data)) != 0) {
rspelta 0:e673387d5add 118 tr_err("rc from MQTT connect is %d\r\n", rc);
rspelta 0:e673387d5add 119 return -1;
rspelta 0:e673387d5add 120 }
rspelta 0:e673387d5add 121
rspelta 0:e673387d5add 122 tr_debug("Sending [QoS0], topic: %s\n\r", topic );
rspelta 0:e673387d5add 123
rspelta 0:e673387d5add 124
rspelta 0:e673387d5add 125 /* Init all sensors with default params */
rspelta 0:e673387d5add 126 press_temp.init(NULL);
rspelta 0:e673387d5add 127 magnetometer.init(NULL);
rspelta 0:e673387d5add 128 acc_gyro.init(NULL);
rspelta 0:e673387d5add 129 accelerometer.init(NULL);
rspelta 0:e673387d5add 130
rspelta 0:e673387d5add 131 /* Enable all sensors */
rspelta 0:e673387d5add 132 press_temp.enable();
rspelta 0:e673387d5add 133 magnetometer.enable();
rspelta 0:e673387d5add 134 accelerometer.enable();
rspelta 0:e673387d5add 135 acc_gyro.enable_x();
rspelta 0:e673387d5add 136 acc_gyro.enable_g();
rspelta 0:e673387d5add 137
rspelta 0:e673387d5add 138 press_temp.read_id(&id);
rspelta 0:e673387d5add 139 tr_debug("LPS22HB pressure & temperature = 0x%X\r\n", id);
rspelta 0:e673387d5add 140 magnetometer.read_id(&id);
rspelta 0:e673387d5add 141 tr_debug("LSM303AGR magnetometer = 0x%X\r\n", id);
rspelta 0:e673387d5add 142 accelerometer.read_id(&id);
rspelta 0:e673387d5add 143 tr_debug("LSM303AGR accelerometer = 0x%X\r\n", id);
rspelta 0:e673387d5add 144 acc_gyro.read_id(&id);
rspelta 0:e673387d5add 145 tr_debug("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
rspelta 0:e673387d5add 146
rspelta 0:e673387d5add 147 while( rc == 0 ) {
rspelta 0:e673387d5add 148 press_temp.get_temperature(&temp);
rspelta 0:e673387d5add 149 press_temp.get_pressure(&press);
rspelta 0:e673387d5add 150 tr_debug("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", temp, press);
rspelta 0:e673387d5add 151 accelerometer.get_x_axes(axes);
rspelta 0:e673387d5add 152 tr_debug("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
rspelta 0:e673387d5add 153 #if 0
rspelta 0:e673387d5add 154 magnetometer.get_m_axes(axes);
rspelta 0:e673387d5add 155 tr_debug("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
rspelta 0:e673387d5add 156 acc_gyro.get_x_axes(axes);
rspelta 0:e673387d5add 157 tr_debug("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
rspelta 0:e673387d5add 158 acc_gyro.get_g_axes(axes);
rspelta 0:e673387d5add 159 tr_debug("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
rspelta 0:e673387d5add 160 #endif
rspelta 0:e673387d5add 161
rspelta 0:e673387d5add 162 printf("Sending payload #%d\n\r", index);
rspelta 0:e673387d5add 163 sprintf(buf, "{\"%s\": {\"temp\":%.0f,\"humidity\":0,\"pressure\":%.0f,\"ambient\":0,\"uv\":0,\"accel_X\":0,\"accel_Y\":0,\"accel_Z\":0}}", MQTT_SENSOR_ID, temp, press );
rspelta 0:e673387d5add 164 message.qos = MQTT::QOS0;
rspelta 0:e673387d5add 165 message.retained = false;
rspelta 0:e673387d5add 166 message.dup = false;
rspelta 0:e673387d5add 167 message.payload = (void*)buf;
rspelta 0:e673387d5add 168 message.payloadlen = strlen(buf);
rspelta 0:e673387d5add 169 if( (message.payloadlen + strlen(topic)+1) >= MAX_MQTT_PACKET_SIZE )
rspelta 0:e673387d5add 170 logMessage("message too long!\r\n");
rspelta 0:e673387d5add 171 rc = client.publish(topic, message);
rspelta 0:e673387d5add 172 printf( "Sent: %d, rc: %d, payload: %s\r\n", index, rc, buf );
rspelta 0:e673387d5add 173 index++;
rspelta 0:e673387d5add 174
rspelta 0:e673387d5add 175 wait(10);
rspelta 0:e673387d5add 176 }
rspelta 0:e673387d5add 177
rspelta 0:e673387d5add 178 tr_warning("Server connection closed. (rc = %d)\n", rc);
rspelta 0:e673387d5add 179 client.disconnect();
rspelta 0:e673387d5add 180 mqttNetwork.disconnect();
rspelta 0:e673387d5add 181 tr_info("Demo ended\r\n");
rspelta 0:e673387d5add 182 return -1;
rspelta 0:e673387d5add 183 }