====== Mqtt subscribe DIO Status ====== The following script will send a subscribe message to the topic, if DIO status has been changed. HOST = "123.123.123.123"; PORT = 1883; KEEPALIVE = 60; PROTOCOL = "V31"; USERNAME = "mqtt-username"; PASSWORD = "mqtt-password"; CLIENT_ID = "mqtt-client-id"; TOPIC = "1/io"; QOS = 0; RETAIN = 0; MESSAGE = ""; TIMEOUT = 1000; CLEAN_SESSION = true; MQTT_HANDLE = 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\n"); 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); } } } //For debugging //dump(buffer); if(ret<0){ nb_syslog("Failed to publish mqtt message"); } //sleep(10); } exit(0);