Exemple #1
0
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
    static int count1 = 0;
    switch (ev) {
    case MG_EV_CONNECT:
        if (*(int *) p != 0) {
            SYS_PRINT("Failed to connect to %s\r\n", MQTT_BROKER_ADDRESS);
        } else {
            SYS_PRINT("Connected to %s\r\n", MQTT_BROKER_ADDRESS);
        }
        struct mg_send_mqtt_handshake_opts opts;
        memset(&opts, 0, sizeof(opts));
        opts.user_name = MQTT_USER_NAME;
        opts.password = MQTT_USER_PWD;
        mg_set_protocol_mqtt(nc);
        mg_send_mqtt_handshake_opt(nc, "PIC32", opts);
        break;
    case MG_EV_MQTT_CONNACK:
        if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
            SYS_PRINT("Got mqtt connection error %d\n\r", msg->connack_ret_code);
        } else {
            SYS_PRINT("Connected to broker\n\r");
        }
        SYS_PRINT("Subscribing to /test\n\r");
        mg_mqtt_subscribe(nc, topic_expressions,
                          sizeof(topic_expressions) / sizeof(*topic_expressions),
                          ++msg_id);
        nc->flags |= MG_F_USER_1;
        break;
    case MG_EV_MQTT_SUBACK:
        SYS_PRINT("Subscription acknowledged\r\n");
        break;
    case MG_EV_MQTT_PUBLISH:
        SYS_PRINT("Got incoming message %s: %.*s\r\n", msg->topic,
                  (int) msg->payload.len, msg->payload.p);
        break;
    case MG_EV_POLL: {
        if (nc->flags & MG_F_USER_1) {
            char msg[100];
            static uint32_t prev_send = 0;
            uint32_t now = DRV_RTCC_TimeGet();
            if (now - prev_send > 1000) {
                int len = snprintf(msg, sizeof(msg), "Current RTC value: %u", now);
                SYS_PRINT("Publishing message with RTC value=%u\r\n", now);
                mg_mqtt_publish(nc, "/stuff", ++msg_id, MG_MQTT_QOS(0), msg, len);
                prev_send = now;
            }
        }
        break;
    }
    case MG_EV_CLOSE:
        SYS_PRINT("Connection to broker is closed\r\n");
        appData.state = APP_STATE_DONE;
        break;
    default:
        break;
    }
}
Exemple #2
0
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
    struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
    (void) nc;

#if 0
    if (ev != MG_EV_POLL)
        printf("USER HANDLER GOT %d\n", ev);
#endif

    switch (ev) {
    case MG_EV_CONNECT: {
        struct mg_send_mqtt_handshake_opts opts;
        memset(&opts, 0, sizeof(opts));
        opts.user_name = s_user_name;
        opts.password = s_password;

        mg_set_protocol_mqtt(nc);
        mg_send_mqtt_handshake_opt(nc, "dummy", opts);
        break;
    }
    case MG_EV_MQTT_CONNACK:
        if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
            printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
            exit(1);
        }
        s_topic_expr.topic = s_topic;
        printf("Subscribing to '%s'\n", s_topic);
        mg_mqtt_subscribe(nc, &s_topic_expr, 1, 42);
        break;
    case MG_EV_MQTT_PUBACK:
        printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
        break;
    case MG_EV_MQTT_SUBACK:
        printf("Subscription acknowledged, forwarding to '/test'\n");
        break;
    case MG_EV_MQTT_PUBLISH: {
#if 0
        char hex[1024] = {0};
        mg_hexdump(nc->recv_mbuf.buf, msg->payload.len, hex, sizeof(hex));
        printf("Got incoming message %.*s:\n%s", (int)msg->topic.len, msg->topic.p, hex);
#else
        printf("Got incoming message %.*s: %.*s\n", (int) msg->topic.len,
               msg->topic.p, (int) msg->payload.len, msg->payload.p);
#endif

        printf("Forwarding to /test\n");
        mg_mqtt_publish(nc, "/test", 65, MG_MQTT_QOS(0), msg->payload.p,
                        msg->payload.len);
        break;
    }
    case MG_EV_CLOSE:
        printf("Connection closed\n");
        exit(1);
    }
}