OneM2M / Mbed OS MQTTSN_AE_ESP_copy-1

Dependencies:   MQTTSN

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 "sensor.h"
00009 #include "Enumeration.h"
00010 #include "easy-connect.h"
00011 #include "MQTTSN_func.h"
00012 #include "registration.h"
00013 #include "container.h"
00014 #include "contentInstance.h"
00015 #include "Subscription.h"
00016 
00017 NetworkInterface* network = NULL;
00018 
00019 int main() {
00020        
00021     float version = 1.0;
00022     MQTTSNUDP* ipstack = NULL;
00023     MQTTSN::Client<MQTTSNUDP, Countdown> *client = NULL;
00024     
00025     /////////////////////////////////////////////////////////////////////////////////////////
00026     printf("Opening network interface...\r\n");
00027     {
00028         network = easy_connect(true);    // If true, prints out connection details.
00029         if (!network) {
00030             printf("Unable to open network interface.\r\n");
00031             return -1;
00032         }
00033     }
00034     printf("Network interface opened successfully.\r\n");
00035     printf("\r\n");
00036 
00037     const char* Local_IP = network->get_ip_address();
00038     printf("IP Address of Device is: %s\n", Local_IP);
00039     
00040     const char* Local_MAC = network->get_mac_address();
00041     printf("MAC Address of Device is: %s\n", Local_MAC);
00042     /////////////////////////////////////////////////////////////////////////////////////////
00043     ipstack = new MQTTSNUDP(network);
00044     client = new MQTTSN::Client<MQTTSNUDP, Countdown>(*ipstack);
00045     
00046     //////////////////////////////////UDP Connect///////////////////////////////////////////    
00047     printf("Connecting to %s:%d\n", MQTT_SERVER_HOST_NAME, PORT);
00048     rc = ipstack->connect(MQTT_SERVER_HOST_NAME, PORT);
00049     if (rc != 0)
00050         printf("rc from UDP connect is %d\n", rc);
00051     else
00052         printf("UDP connected\n"); 
00053        
00054     //////////////////////////////////MQTT Connect///////////////////////////////////////////
00055     attemptConnect(client, ipstack);
00056     
00057     //////////////////////////////////MQTT Subscribe//////////////////////////////////////////
00058     
00059     //Set Topic to /oneM2M/reg_resp/Sensor01/CSE_01
00060     create_Topic("reg_resp", MQTT_CLIENT_ID, "CSE_01");
00061     
00062     MQTTSN_topicid topicid;
00063     if ((rc = subscribe(client, ipstack, topicid)) != 0)
00064         printf("rc from MQTT subscribe is %d\n", rc);
00065     else
00066         printf("Subscribed to Topic %s\n", MQTT_TOPIC);
00067 
00068     //Set Topic to /oneM2M/reg_req/Sensor01/CSE_01
00069     create_Topic("reg_req", MQTT_CLIENT_ID, "CSE_01");
00070     
00071     MQTTSN_topicid topicid2;    
00072     if ((rc = subscribe(client, ipstack, topicid2)) != 0)
00073         printf("rc from MQTT subscribe is %d\n", rc);
00074     else
00075         printf("Subscribed to Topic %s\n", MQTT_TOPIC);
00076     
00077     //////////////////////////////////Registration///////////////////////////////////////////
00078     if(Registration(client, ipstack, topicid2) != 0 )
00079         printf("Registration failed with response: %s\n", response.c_str());
00080     else printf("Registration Done wth response: %s\n", response.c_str());
00081   
00082     ////////////////////////////////Req-Resp Topics//////////////////////////////////////////
00083     c_aei =  new char [aei.length()+1];;
00084     printf("\nAEI in main: %s\r\n\n\n", aei.c_str());
00085     strcpy(c_aei, aei.c_str());
00086     printf("C_AEI: %s\r\n", c_aei);
00087     
00088     //Set Topic to /oneM2M/resp/SAE01/CSE_01
00089     create_Topic("resp", c_aei, "CSE_01");
00090     
00091     MQTTSN_topicid topicid3;
00092     if ((rc = subscribe(client, ipstack, topicid3)) != 0)
00093          printf("rc from MQTT subscribe is %d\n", rc);
00094     else
00095          printf("Subscribed to Topic %s\n", MQTT_TOPIC);
00096         
00097     //Set Topic to /oneM2M/req/SAE01/CSE_01
00098     create_Topic("req", c_aei, "CSE_01");
00099     
00100     MQTTSN_topicid topicid4;
00101     if ((rc = subscribe(client, ipstack, topicid4)) != 0)
00102          printf("rc from MQTT subscribe is %d\n", rc);
00103     else
00104          printf("Subscribed to Topic %s\n", MQTT_TOPIC);
00105     wait(3);    
00106     
00107     ////////////////////////////////// Container ////////////////////////////////////////////
00108     if(Container(client, ipstack, topicid4) != 0 )
00109         printf("Container Creation failed with response: %s\n", response.c_str());
00110     else printf("Container Created wth response: %s\n", response.c_str());
00111     wait(3);
00112     ////////////////////////////////// Subscription ////////////////////////////////////////////
00113     if(Subscription(client, ipstack, topicid4) != 0 )
00114         printf("Subscription Creation failed with response: %s\n", response.c_str());
00115     else printf("Subscription Created wth response: %s\n", response.c_str());
00116     wait(3);
00117     ////////////////////////////////// ContentInstance //////////////////////////////////////
00118     if(contentInstance(client, ipstack, topicid4) != 0 )
00119         printf("ContentInstance Creation failed with response: %s\n", response.c_str());
00120     else printf("ContentInstance Created wth response: %s\n", response.c_str());
00121     
00122     rc = delete_cnt(client, ipstack, topicid4);
00123     rc = delete_sub(client, ipstack, topicid4);
00124     
00125  
00126     while(1) 
00127     {
00128         // Check connection //
00129         if(!client->isConnected()){
00130             break;
00131         }
00132         // Received a control message. //
00133         if(isMessageArrived) {
00134             isMessageArrived = false;
00135             // Just print it out here.
00136             printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer);
00137             
00138             //generate notification response
00139             if(op == 5)
00140             {
00141                 Response Resp;
00142                 RES.resourceName = rn;
00143                 Resp.Request_Identifier = rqi;
00144                 Resp.To = From;
00145                 lcl = false;
00146                 Resp.From = CSE_ID;
00147                 buf = Notify_Resp(Resp);
00148                 if ((rc = publish(client, ipstack, topicid3)) != 0)
00149                     printf("rc from MQTT Publish is %d\n", rc);
00150                 else
00151                     printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC);
00152             }
00153             
00154         }          
00155             client->yield(4000);
00156             //wait(4);
00157     } 
00158     
00159     //if ((rc = client->unsubscribe(topicid2)) != 0)
00160     //    printf("rc from unsubscribe was %d\n", rc);
00161     
00162     if ((rc = client->disconnect()) != 0)
00163         printf("rc from disconnect was %d\n", rc);
00164  
00165     ipstack->disconnect();
00166     
00167     delete ipstack;
00168     delete client;
00169     
00170     printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
00171     printf("Finishing with %d messages received\n", arrivedcount);
00172     
00173     return 0;
00174 
00175 }