Publish changed in the State of the Digital IN or OUT to a MQTT Broker.

Please us it with the rigger “sdk-startup”

/* desc: this script will publish router canges of the Digital in or Out 
 * to a mqtt Broker.
 * 
 * for details about status infos check api documentation about nb_status();
 * copyright (c) 2020 netmodule ag, switzerlan
 */
 
host = "mz.mqttbroker.io"; 
port = 1883;
username = "test";
password = "test";
topic = "switch/location/";
/* 	qos values */
/*	0 = publish at most one (fire & forget) */
/*	1 = publish at least once */
/*	2 = publish exactly once */	
	qos = 0;
/* 	retain values */
/*	0 = the message will not be stored */
/*	1 = the message will be stored and sent to a new subscriber of that topic */
retain = 0; 
message = "";
 
uinterval=500;
 
lst=nb_status("dio");
while (true) {
 
	st=nb_status("dio");
	st_fields=struct_fields(st);
	// only update last field if we had a diff
	st_diff=false;
		for (i=0;i<length(st_fields);i++) {
		if (struct_get(st,st_fields[i]) != struct_get(lst,st_fields[i])) {
 
			/* create the message */
			message=sprintf("%s",struct_get(st,st_fields[i]));	
			ltopic=sprintf("%s%s",topic,st_fields[i]);
				/* publish the message to mqtt broker */
			nb_syslog("mqtt pub: topic: %s, msg: %s to %s:",ltopic, message,host);
			ret = nb_mqtt_publish (host, port, username, password, topic, qos, retain, message);
			if(ret<0){
				  nb_syslog("failed to publish mqtt message");
			} 
			st_diff=true;
		}
	}
	//update last state so we can check next time. 
	// but only if something changed
	if (st_diff) lst=st;
	usleep(uinterval);
 
 
}
 
exit(0);