guowenxue
2021-08-17 87742b9845b15e93f13a074b34ce7c9fa8ccbe48
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,7 +275,7 @@
        else 
            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp);
        rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain);
        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);
@@ -284,6 +285,27 @@
            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), 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);
        }
    }
    
    log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); 
    mosquitto_disconnect(mosq);
@@ -442,7 +464,15 @@
            {
                if( strcasestr(value, "on") )
                {
                    log_nrml("Turn buzzer on [%d] times\n", hwconf->beep_times);
          turn_passive_beep(ON, BEEP_FREQ);
                }
      else if( strcasestr(value, "off") )
                {
          turn_passive_beep(OFF, BEEP_FREQ);
                }
      else if( strcasestr(value, "music") )
                {
          play_little_star();
                }
            }