MQTT client for IBM Bluemix

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL MQTT VL53L0X

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @version V1.0.0
00005  * @date    27-October-2017
00006  * @brief   Simple Example application for using SILICA_SENSOR_NODE with IBM Bluemix
00007  *          project ported from X_NUCLEO_IKS01A2 ST project
00008  *          
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */ 
00038 
00039 /* Includes */
00040 #include "mbed.h"
00041 #include "mbed-trace/mbed_trace.h"
00042 
00043 #include "HTS221Sensor.h"
00044 #include "LPS22HBSensor.h"
00045 #include "LSM6DSLSensor.h"
00046 
00047 #include "easy-connect/easy-connect.h"
00048 
00049 #include "MQTTNetwork.h"
00050 #include "MQTT/MQTTmbed.h"
00051 #include "MQTT/MQTTClient.h"
00052 
00053 #define MAX_MQTT_PACKET_SIZE 250
00054 
00055 #define logMessage printf
00056 
00057 #include "LSM303AGRMagSensor.h"
00058 #include "LSM303AGRAccSensor.h"
00059 
00060 /* Retrieve the composing elements of the expansion board */
00061 
00062 /* Interface definition */
00063 #define SPI_TYPE_LPS22HB   LPS22HBSensor::SPI3W
00064 #define SPI_TYPE_LSM6DSL   LSM6DSLSensor::SPI3W
00065 SPI devSPI(PB_15, NC, PB_13);  // 3-wires SPI on SensorTile  
00066 static Serial ser(PC_12,PD_2); // Serial with SensorTile Cradle Exp. Board + Nucleo   
00067 #define printf(...) ser.printf(__VA_ARGS__)     
00068 
00069 /* Environmental sensors */
00070 static LPS22HBSensor press_temp(&devSPI, PA_3, NC, SPI_TYPE_LPS22HB); 
00071 
00072 /* Motion sensors */
00073 static LSM6DSLSensor acc_gyro(&devSPI,PB_12, NC, PA_2, SPI_TYPE_LSM6DSL); 
00074 static LSM303AGRMagSensor magnetometer(&devSPI, PB_1);
00075 static LSM303AGRAccSensor accelerometer(&devSPI, PC_4);
00076 
00077 
00078 /* Simple main function */
00079 int main( void )
00080 {
00081   char buf[500];
00082   char* topic = MQTT_TOPIC;
00083   unsigned int index = 0;
00084   uint8_t id;
00085   float temp, press;
00086   int32_t axes[3];
00087   const char* hostname = MQTT_URL_BROKER;
00088   int port = 1883;
00089 
00090 
00091   MQTT::Message message;
00092   
00093   mbed_trace_init();
00094 
00095   printf("Avnet-Silica Sensor Node\n\rmbed-os-mqtt-client demo\r\n");
00096 
00097   NetworkInterface* network = easy_connect(true);
00098   if(network == NULL) {
00099     tr_err("\nConnection to Network Failed - exiting application...\n");
00100     return -1;
00101   }
00102 
00103   MQTTNetwork mqttNetwork(network);
00104   MQTT::Client<MQTTNetwork, Countdown, MAX_MQTT_PACKET_SIZE> client(mqttNetwork);
00105   printf("Connecting to %s:%d\r\n", hostname, port);
00106   int rc = mqttNetwork.connect(hostname, port);
00107   if (rc != 0) {
00108     tr_err("rc from TCP connect is %d\r\n", rc);
00109     return -1;
00110   }
00111   MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00112   data.MQTTVersion = 3;
00113   data.clientID.cstring = MQTT_CLIENT_ID;
00114   data.username.cstring = MQTT_USERNAME;
00115   data.password.cstring = MQTT_PASSWORD;
00116   tr_debug( "Connecting with Client Id:  %s\r\n", MQTT_CLIENT_ID );
00117   if ((rc = client.connect(data)) != 0) {
00118     tr_err("rc from MQTT connect is %d\r\n", rc);
00119     return -1;
00120   }
00121 
00122   tr_debug("Sending [QoS0], topic: %s\n\r", topic );
00123 
00124     
00125   /* Init all sensors with default params */
00126   press_temp.init(NULL);
00127   magnetometer.init(NULL);
00128   acc_gyro.init(NULL);
00129   accelerometer.init(NULL);
00130   
00131   /* Enable all sensors */
00132   press_temp.enable();
00133   magnetometer.enable();
00134   accelerometer.enable();
00135   acc_gyro.enable_x();
00136   acc_gyro.enable_g();
00137 
00138   press_temp.read_id(&id);
00139   tr_debug("LPS22HB pressure & temperature    = 0x%X\r\n", id);
00140   magnetometer.read_id(&id);
00141   tr_debug("LSM303AGR magnetometer            = 0x%X\r\n", id);
00142   accelerometer.read_id(&id);
00143   tr_debug("LSM303AGR accelerometer           = 0x%X\r\n", id);
00144   acc_gyro.read_id(&id);
00145   tr_debug("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
00146  
00147   while( rc == 0 ) {    
00148     press_temp.get_temperature(&temp);
00149     press_temp.get_pressure(&press);
00150     tr_debug("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", temp, press);
00151     accelerometer.get_x_axes(axes);
00152     tr_debug("LSM303AGR [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);        
00153 #if 0
00154     magnetometer.get_m_axes(axes);
00155     tr_debug("LSM303AGR [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);        
00156     acc_gyro.get_x_axes(axes);
00157     tr_debug("LSM6DSL [acc/mg]:        %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00158     acc_gyro.get_g_axes(axes);
00159     tr_debug("LSM6DSL [gyro/mdps]:     %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00160 #endif
00161 
00162     printf("Sending payload #%d\n\r", index);
00163     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 );
00164     message.qos = MQTT::QOS0;
00165     message.retained = false;
00166     message.dup = false;
00167     message.payload = (void*)buf;
00168     message.payloadlen = strlen(buf);
00169     if( (message.payloadlen + strlen(topic)+1) >= MAX_MQTT_PACKET_SIZE )
00170         logMessage("message too long!\r\n");
00171     rc = client.publish(topic, message);
00172     printf( "Sent: %d, rc: %d, payload: %s\r\n", index, rc, buf );
00173     index++;
00174 
00175     wait(10);
00176   }
00177   
00178   tr_warning("Server connection closed. (rc = %d)\n", rc);
00179   client.disconnect();
00180   mqttNetwork.disconnect(); 
00181   tr_info("Demo ended\r\n");
00182   return -1;
00183 }