The follwing script is just an example
/* Set dio status if the topic will get subscribed If value "0" will get subscribed it will set dio out1 OFF If value "1" will get subscribed it will set dio out1 ON*/ HOST = "localhost"; PORT = 1883; KEEPALIVE = 60; PROTOCOL = "V31"; USERNAME = "user"; PASSWORD = "password"; CLIENT_ID = "1"; TOPIC = "dio/status"; QOS = 0; RETAIN = 0; MESSAGE = ""; TIMEOUT = 1000; CLEAN_SESSION = true; MQTT_HANDLE = 0; counter = 0; /*create new mqtt instance*/ MQTT_HANDLE = nb_mqttlib_new(CLIENT_ID, CLEAN_SESSION); if (nb_mqttlib_set_protocol_version(MQTT_HANDLE, PROTOCOL) < 0) { printf("Unable to set Protocol version\n"); exit(1); } if (nb_mqttlib_set_user_pw(MQTT_HANDLE, USERNAME, PASSWORD) < 0) { printf("Unable to set Username and Passsword\n"); exit(1); } if (nb_mqttlib_connect(MQTT_HANDLE, HOST, PORT, KEEPALIVE) < 0) { printf("Unable to connect\n"); exit(1); } if (nb_mqttlib_subscribe(MQTT_HANDLE, TOPIC, QOS) < 0) { printf("Unable to subscribe\n"); exit(1); } while (1) { ret = nb_mqttlib_get_callback_message(MQTT_HANDLE, TIMEOUT); buffer = (string)ret.msg.msg; if (buffer == "1") { printf("switch DIO 1 on %d \n", counter); counter++; if (nb_dio_set("out1", 1) < 0) { printf("DIO switch on failed\n"); exit(1); } } else if (buffer == "0") { printf("switch DIO 1 off\n"); if (nb_dio_set("out1", 0) < 0) { printf("DIO switch off failed\n"); exit(1); } } if (ret < 0) { nb_syslog("Failed to publish mqtt message"); } sleep(1); } exit(0);
To subscribe to the topic /dio/status we are useing mosquitto.