使用mosquitto开发MQTT客户端

1. 下载并安装openSSL, mosquitto 应用程序运行时会用到

http://slproweb.com/products/Win32OpenSSL.html

 

2. 下载并安装mosquitto

http://mosquitto.org/download/

3. 使用

1)在cmd窗口中,启动Mosquito的MQTT服务

C:\Program Files\mosquitto>mosquitto

2)在另一个cmd窗口中,启动侦听通道程序

c:\Program Files\mosquitto> mosquitto_sub -t mytopic

3)在另一个cmd启动发布程序

c:\Program Files\mosquitto>mosquitto_pub -t mytopic -m 111

此时在第2个窗口中会收到111

4. 开发代码sample

以下代码来自

http://mosquitto.org/man/libmosquitto-3.html

 

Examples

#include 
#include 
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
          if(message->payloadlen){
                    printf("%s %s\n", message->topic, message->payload);
          }else{
                    printf("%s (null)\n", message->topic);
          }
          fflush(stdout);
}

void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
          int i;
          if(!result){
                    /* Subscribe to broker information topics on successful connect. */
                    mosquitto_subscribe(mosq, NULL, "$SYS/#", 2);
          }else{
                    fprintf(stderr, "Connect failed\n");
          }
}

void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
          int i;
          printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
          for(i=1; i

 

你可能感兴趣的:(物联网)