• MQTT-C-PUB


    /*
     ============================================================================
     Name        : mqtest.c
     Author      :
     Version     :
     Copyright   : Your copyright notice
     Description : Hello World in C, Ansi-style
     ============================================================================
     */

    #include <stdio.h>
    #include <stdlib.h>
    #include <mosquitto.h>
    #include <string.h>
    void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
    {
        if(message->payloadlen){
            printf("%s %s ", message->topic, message->payload);
        }else{
            printf("%s (null) ", message->topic);

        }

        fflush(stdout);
    }

    void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
    {
        int i;
        if(!result){

            mosquitto_subscribe(mosq, NULL, "lgt", 2);
            /*mosquitto_publish(mosq,NULL,"lgt")*/
            /*int payloadlen, const void *payload, int qos, bool retain*/
        }else{
            fprintf(stderr, "Connect failed ");
        }
    }

    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<qos_count; i++){
            printf(", %d", granted_qos[i]);
        }

        printf(" ");
    }

    void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
    {
        /* Pring all log messages regardless of level.*/
        printf("%s ", str);
    }

    int main(void) {
        puts("!!!Hello World!!!");
    /*    prints !!!Hello World!!! */

        int i=0;
            char *host = "localhost";
            char dst[]="lgttest12321321";
            char dst1[20];
            char ddd[200];
            int port = 1883;
            int keepalive = 60;
            bool clean_session = true;
            struct mosquitto *mosq = NULL;
            mosquitto_lib_init();
            mosq = mosquitto_new("track01", clean_session, NULL);
                if(!mosq){
                fprintf(stderr, "Error: Out of memory. ");
                return 1;
            }
                mosquitto_log_callback_set(mosq, my_log_callback);
            /*mosquitto_connect_callback_set(mosq, my_connect_callback);*/
            mosquitto_message_callback_set(mosq, my_message_callback);
            mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);

            if(mosquitto_connect(mosq, host, port, keepalive)){
                fprintf(", %d", i);
                return 1;

            }


            for(;;)
                    {

                i++;
                /*itoa(i,dst1,10);*/
                sprintf(dst1, "%d", i);
                /**ddd=strcat(dst, dst1);*/
                //dst1=dst;
                mosquitto_publish(mosq, NULL, "lgt",strlen(dst1), dst1, 0, true);
                /*fprintf(", %d", i);*/
                    }

            mosquitto_loop_forever(mosq, -1, 1);

            mosquitto_destroy(mosq);
            mosquitto_lib_cleanup();

            return 0;
        /*return EXIT_SUCCESS;*/
    }

  • 相关阅读:
    解决beego运行程序报错问题:stderr: go: github.com/astaxie/beego@v1.12.1: missing go.sum entry
    Flutter-填平菜鸟和高手之间的沟壑
    百度地图的脑残设计,附上代码,为后来的码农们...
    迅捷Flutter图片浏览软件
    青峰Flutter视频播放软件
    Element UI 自定义Validator
    在C#中如何URL编码和解码
    Postman新手入门
    安装SSDT2017
    layer.prompt 输入值为空的时候点击confirm不能继续
  • 原文地址:https://www.cnblogs.com/dpf-learn/p/6963551.html
Copyright © 2020-2023  润新知