J L / Mbed OS MQTT_Integration

Dependencies:   X_NUCLEO_IKS01A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <math.h>
00003 
00004 #include <MQTTClientMbedOs.h>
00005 #include "SpwfSAInterface.h"
00006 
00007 
00008 #include "XNucleoIKS01A2.h"
00009 
00010 #define MQTT_MAX_PACKET_SIZE 250   
00011 #define MQTT_MAX_PAYLOAD_SIZE 300
00012 
00013 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
00014 
00015 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00016 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00017 
00018 int main()
00019 {
00020     uint8_t id;
00021     int16_t axes[3];
00022     int32_t axes_gyro[3];
00023     acc_gyro->enable_x();
00024     acc_gyro->enable_g();
00025     acc_gyro->read_id(&id);
00026     acc_gyro->set_x_fs(2.0f);
00027 
00028     printf("MQTT Integration\n\n");
00029 
00030     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00031     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00032     if (ret != 0) {
00033         printf("\nConnection error\n");
00034         return -1;
00035     }
00036     
00037     printf("Success\n\n");
00038     
00039 
00040     //NetworkInterface *net = NetworkInterface::get_default_instance();
00041     TCPSocket socket;
00042     MQTTClient client(&socket);
00043     socket.open(&wifi);
00044     int rc = socket.connect("192.168.178.21", 1883);
00045     
00046     if (rc == 0)
00047         printf("Connection Successful");
00048     else
00049         printf("Connection Failed");
00050     
00051     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00052     
00053     data.MQTTVersion = 4;
00054     data.struct_version=0;
00055     data.clientID.cstring = "clientId-ACC2";
00056     data.username.cstring = "jlenz";
00057     data.password.cstring = "123";
00058 
00059     rc = client.connect(data);
00060     
00061     if (rc == 0)
00062         printf("Client Connection Successful");
00063     else
00064         printf("Client Connection Failed");
00065     
00066     while(1) 
00067     {
00068         int con = client.isConnected();
00069         if (con == false)
00070             client.connect(data);
00071             
00072         acc_gyro->get_x_axes_raw(axes);
00073         MQTT::Message message;
00074         char* pubTopic = "SLG/ACC2";
00075         char buf[MQTT_MAX_PAYLOAD_SIZE];
00076         auto normAxe = axes[0]*axes[0] + axes[1]*axes[1] + axes[2]*axes[2];
00077         sprintf(buf,"%d",normAxe);
00078         message.qos = MQTT::QOS0;
00079         message.retained = false;
00080         message.dup = false;
00081         message.payload = (void*)buf;
00082         message.payloadlen = strlen(buf);
00083         client.publish(pubTopic, message);
00084         printf("%d \n",normAxe);
00085         printf("%d \n",axes[0]);
00086         
00087     }
00088 
00089 
00090 
00091 
00092     wifi.disconnect();
00093 
00094     printf("\nDone\n");
00095 }