MQTT SUBSCRIBE没有提供任何更新 - OPENPicus

时间:2014-08-11 09:23:59

标签: c++ c tcp mqtt xively

我正在开发一个OpenPicus WiFi专业版,我正在尝试使用mqtt工作。这是我的代码:

我只是“检测到SUBACK”,但是当服务器上的数据发生变化时,picus就不会发生任何事情。

此处提供更多信息:

https://github.com/OpenPicus/lib_mqtthttp://wiki.openpicus.com/index.php/MQTT

#include "taskFlyport.h"
#include "mqtt.h"

#define APIKEY      "xxx"
#define FEED_ID     xxx
#define XIVELY_API  "api.xively.com"
#define XIVELY_PORT "1883"

void FlyportTask()
{
    char buffer[250];
    char str[250];
    int len         = 0;
    TCP_SOCKET sock = INVALID_SOCKET;

    WFConnect(WF_DEFAULT);
    while(WFGetStat() != CONNECTED);
    vTaskDelay(25);

    while(WFGetStat() == CONNECTED)
    {
        UARTWrite(1,"Flyport Wi-fi G connected...hello world!\r\n");

        sock = TCPClientOpen(XIVELY_API, XIVELY_PORT);
        int cnt = 0;
        while(!TCPisConn(sock))
        {
            if(cnt==30)
            {
                UARTWrite(1,"Xively timeout, try again in 5s..");
                sock = INVALID_SOCKET;
                vTaskDelay(500);
                break;
            }
            vTaskDelay(25);
            cnt++;
        }
        if(TCPisConn(sock))
        {
            UARTWrite(1,"TCP is connected to the Server, logging in..");
            len = MQTT_Connect(buffer, FEED_ID, 120, APIKEY, NULL, NULL, NULL, 1, NULL, NULL);
            TCPWrite(sock, buffer, len);
            len = 0;
        }
        while(TCPisConn(sock))
        {
            switch (MQTT_Response_Sniffer(sock))
            {
                case MQTT_CONNACK:
                    UARTWrite(1,"\r\nCONNACK detected, subscribing datastreams..\r\n");
                    sprintf(str, "%s/v2/feeds/%d.json", APIKEY, FEED_ID);
                    len = MQTT_Subscribe(buffer, str, 50, MQTT_QOS_0);
                    TCPWrite(sock, buffer, len);
                    len = 0;
                break;
                case MQTT_PUBLISH:
                {
                    UARTWrite(1,"\r\nPUBLISH detected\r\n");
                    QWORD lenmess=MQTT_Last_Response_Length();
                    char message[lenmess];
                    MQTT_Last_Response_Message(message);
                    QWORD i=0;
                    for(i=0;i<lenmess;i++)
                        UARTWriteCh(1,message[i]);
                }
                break;
                case MQTT_PUBACK:               
                    UARTWrite(1,"\r\nPUBACK detected\r\n");
                break;
                case MQTT_PUBREC:               
                    UARTWrite(1,"\r\nPUBREC detected\r\n");
                break;
                case MQTT_PUBCOMP:              
                    UARTWrite(1,"\r\nPUBCOMP detected\r\n");
                break;
                case MQTT_SUBACK:               
                    UARTWrite(1,"\r\nSUBACK detected\r\n");
                break;
                case MQTT_UNSUBACK:             
                    UARTWrite(1,"\r\nUNSUBACK detected\r\n");
                break;
                case MQTT_PINGRESP:             
                    UARTWrite(1,"\r\nPINGRESP detected\r\n");
                break;
            }
        }
    }
}

0 个答案:

没有答案