Smart Traffic

Dependencies:   MQTT_G_SENSOR

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WISEAgent.cpp Source File

WISEAgent.cpp

00001 #include "WISEAgent.h"
00002 
00003 void messageCbf(MQTT::MessageData& md)
00004 {
00005     MQTT::Message &message = md.message;
00006     printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
00007     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
00008 }
00009 
00010 MQTT::Client<MQTTEthernet, Countdown> *pMQTTClient;
00011 
00012 void Init(MQTTEthernet ipstack)
00013 {
00014     if( !pMQTTClient )
00015         pMQTTClient = new MQTT::Client<MQTTEthernet, Countdown>(ipstack);    
00016 }
00017 
00018 int WISEAgentConnect(const char *ip, char *uid)
00019 {
00020     int rc = 0;
00021     MQTT::Message message;
00022         
00023     // Register
00024     char topic[128]={0};
00025     char buf[256]={0};
00026         
00027     //snprintf(buf,sizeof(topic),RegistJson,uid, uid, uid, 0, uid, 1436160081000);
00028         
00029     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00030     data.MQTTVersion = 3;   
00031     data.clientID.cstring= uid;//mac;
00032     data.username.cstring = "ral";
00033     data.password.cstring = "123";
00034     // willmessage
00035     data.will.topicName.cstring = DEF_WILLMSG_TOPIC;
00036     data.will.message.cstring = buf;
00037     
00038     memset(topic, 0, sizeof(topic));
00039     memset(buf, 0, sizeof(buf)); 
00040         
00041     if ((rc = pMQTTClient->connect(data)) != 0) {
00042        printf("rc from MQTT connect is %d\n", rc);
00043        return rc;
00044     }
00045            
00046     snprintf(topic,sizeof(topic),WA_PUB_CONNECT_TOPIC,uid);
00047     //snprintf(buf,sizeof(topic),RegistJson,uid, uid, uid, 1, uid, 1436160081020);
00048     
00049     message.qos = MQTT::QOS0;
00050     message.retained = false;
00051     message.dup = false;
00052     message.payload = (void*)buf;
00053     message.payloadlen = strlen(buf)+1;
00054             
00055     if( rc = pMQTTClient->publish(topic, message) != 0 ) {
00056        printf("rc from MQTT publish topic=%s rc= %d\n", topic, rc);
00057        return rc;        
00058     }
00059     
00060     memset(topic, 0, sizeof(topic));
00061     memset(buf, 0, sizeof(buf)); 
00062 
00063     snprintf(topic,sizeof(topic),WA_PUB_ACTION_TOPIC,uid);
00064     //snprintf(buf,sizeof(topic),OSInfoJson,ip, uid, 1436160081030);   
00065 
00066     message.payloadlen = strlen(buf)+1;
00067             
00068     if( rc = pMQTTClient->publish(topic, message) != 0 ) {
00069        printf("rc from MQTT publish topic=%s rc= %d\n", topic, rc);
00070        return rc;        
00071     }        
00072     
00073     memset(topic, 0, sizeof(topic));
00074     memset(buf, 0, sizeof(buf));     
00075     snprintf(topic,sizeof(topic),WA_SUB_CBK_TOPIC,uid);    
00076 
00077     if ((rc = pMQTTClient->subscribe(topic, MQTT::QOS1, messageCbf)) != 0) {
00078         printf("rc from MQTT subscribe is %d\n", rc);     
00079         return rc;
00080     }
00081     
00082     return rc;
00083 }