OneM2M / Mbed OS MQTTSN_AE_v1

Dependencies:   MQTTSN mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Client-AE.cpp Source File

Client-AE.cpp

00001 #define MQTTSNCLIENT_QOS2 1
00002 
00003 #include "mbed.h"
00004 #include "stdio.h"
00005 #include "MQTT_server_setting.h"
00006 #include "MQTTSNUDP.h"
00007 #include "MQTTSNClient.h"
00008 #include "MbedJSONValue.h"
00009 #include "Enumeration.h"
00010 #include "sensor.h"
00011 #include "easy-connect.h"
00012 #include "MQTTSN_func.h"
00013 #include "registration.h"
00014 
00015 NetworkInterface* network = NULL;
00016 
00017 int main() {
00018        
00019     float version = 0.47;
00020     MQTTSNUDP* ipstack = NULL;
00021     MQTTSN::Client<MQTTSNUDP, Countdown> *client = NULL;
00022     
00023     /////////////////////////////////////////////////////////////////////////////////////////
00024     printf("Nai binary hai ye theek hai Opening network interface...\r\n");
00025     {
00026         network = easy_connect(true);    // If true, prints out connection details.
00027         if (!network) {
00028             printf("Unable to open network interface.\r\n");
00029             return -1;
00030         }
00031     }
00032     printf("Network interface opened successfully.\r\n");
00033     printf("\r\n");
00034 JUMP:
00035     ipstack = new MQTTSNUDP(network);
00036     //MQTTSNUDP ipstack = MQTTSNUDP(network);  
00037 
00038     const char* Local_IP = network->get_ip_address();
00039     printf("IP Address of Device is: %s\n", Local_IP);
00040     
00041     const char* Local_MAC = network->get_mac_address();
00042     printf("MAC Address of Device is: %s\n", Local_MAC);
00043     
00044     client = new MQTTSN::Client<MQTTSNUDP, Countdown>(*ipstack);
00045     //////////////////////////////////UDP Connect///////////////////////////////////////////    
00046     printf("Connecting to %s:%d\n", MQTT_SERVER_HOST_NAME, PORT);
00047     rc = ipstack->connect(MQTT_SERVER_HOST_NAME, PORT);
00048     if (rc != 0)
00049          printf("rc from UDP connect is %d\n", rc);
00050     else
00051         printf("UDP connected\n"); 
00052     //////////////////////////////////MQTT Connect///////////////////////////////////////////
00053     attemptConnect(client, ipstack);
00054     Registration(client, ipstack);
00055     while(1) 
00056      {
00057         // Check connection //
00058         if(!client->isConnected()){
00059             break;
00060         }
00061         // Received a control message. //
00062         if(isMessageArrived) {
00063             isMessageArrived = false;
00064             // Just print it out here.
00065             //printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer);
00066             //process_msg();
00067             //free(messageBuffer);
00068         }
00069         // Publish data //
00070             //PUB_Count++;
00071                      
00072             client->yield(4000);
00073             //wait(4);
00074         
00075     } 
00076     
00077     //if ((rc = client->unsubscribe(topicid2)) != 0)
00078     //    printf("rc from unsubscribe was %d\n", rc);
00079     
00080     if ((rc = client->disconnect()) != 0)
00081         printf("rc from disconnect was %d\n", rc);
00082     
00083     ipstack->disconnect();
00084     
00085     delete ipstack;
00086     delete client;
00087     
00088     printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
00089     printf("Finishing with %d messages received\n", arrivedcount);
00090 goto JUMP;
00091     
00092     return 0;
00093 
00094 }