guowenxue
2019-08-03 e6e79ca02d323e9a10f115ade7ba6ac9c0d7a3f0
mqttd/main.c
@@ -254,7 +254,8 @@
    mqtt_ctx_t             *ctx = (mqtt_ctx_t *)userdata;
    int                     rv = 0;
    char                    msg[128];  
    float                   temp = 0.0;
    float                   temp = 0.0; /* temperature */
    float                   rh = 0.0;   /* relative humidity */
    int                     retain = 0;
    if( result )
@@ -274,6 +275,26 @@
        else 
            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp);
        rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg), msg, ctx->pubQos, retain);
        if( rv )
        {
            log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv);
        }
        else
        {
            log_nrml("Publisher broadcast message '%s' ok\n", msg);
        }
    }
    if( ctx->hwconf.sht2x )
    {
        memset(msg, 0, sizeof(msg));
        if( sht2x_get_temp_humidity(&temp, &rh) < 0 )
            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\", \"RH\":\"error\" }", ctx->id);
        else
            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\", \"RH\":\"%.2f\" }", ctx->id, temp, rh);
        rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain);
        if( rv )
        {
@@ -284,6 +305,7 @@
            log_nrml("Publisher broadcast message '%s' ok\n", msg);
        }
    }
    
    log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); 
    mosquitto_disconnect(mosq);