OneM2M / Mbed OS MQTTSN_AE_v1

Dependencies:   MQTTSN mbed-http

Client-AE.cpp

Committer:
m_ahsan
Date:
2019-03-11
Revision:
15:557d0008dd2d

File content as of revision 15:557d0008dd2d:

#define MQTTSNCLIENT_QOS2 1

#include "mbed.h"
#include "stdio.h"
#include "MQTT_server_setting.h"
#include "MQTTSNUDP.h"
#include "MQTTSNClient.h"
#include "MbedJSONValue.h"
#include "Enumeration.h"
#include "sensor.h"
#include "easy-connect.h"
#include "MQTTSN_func.h"
#include "registration.h"

NetworkInterface* network = NULL;

int main() {
       
    float version = 0.47;
    MQTTSNUDP* ipstack = NULL;
    MQTTSN::Client<MQTTSNUDP, Countdown> *client = NULL;
    
    /////////////////////////////////////////////////////////////////////////////////////////
    printf("Nai binary hai ye theek hai Opening network interface...\r\n");
    {
        network = easy_connect(true);    // If true, prints out connection details.
        if (!network) {
            printf("Unable to open network interface.\r\n");
            return -1;
        }
    }
    printf("Network interface opened successfully.\r\n");
    printf("\r\n");
JUMP:
    ipstack = new MQTTSNUDP(network);
    //MQTTSNUDP ipstack = MQTTSNUDP(network);  

    const char* Local_IP = network->get_ip_address();
    printf("IP Address of Device is: %s\n", Local_IP);
    
    const char* Local_MAC = network->get_mac_address();
    printf("MAC Address of Device is: %s\n", Local_MAC);
    
    client = new MQTTSN::Client<MQTTSNUDP, Countdown>(*ipstack);
    //////////////////////////////////UDP Connect///////////////////////////////////////////    
    printf("Connecting to %s:%d\n", MQTT_SERVER_HOST_NAME, PORT);
    rc = ipstack->connect(MQTT_SERVER_HOST_NAME, PORT);
    if (rc != 0)
         printf("rc from UDP connect is %d\n", rc);
    else
        printf("UDP connected\n"); 
    //////////////////////////////////MQTT Connect///////////////////////////////////////////
    attemptConnect(client, ipstack);
    Registration(client, ipstack);
    while(1) 
     {
        // Check connection //
        if(!client->isConnected()){
            break;
        }
        // Received a control message. //
        if(isMessageArrived) {
            isMessageArrived = false;
            // Just print it out here.
            //printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer);
            //process_msg();
            //free(messageBuffer);
        }
        // Publish data //
            //PUB_Count++;
                     
            client->yield(4000);
            //wait(4);
        
    } 
    
    //if ((rc = client->unsubscribe(topicid2)) != 0)
    //    printf("rc from unsubscribe was %d\n", rc);
    
    if ((rc = client->disconnect()) != 0)
        printf("rc from disconnect was %d\n", rc);
    
    ipstack->disconnect();
    
    delete ipstack;
    delete client;
    
    printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
    printf("Finishing with %d messages received\n", arrivedcount);
goto JUMP;
    
    return 0;

}