you dont have to add the publish lines in the main loop. have a look at Sending MQTT messages within rate limits