As Ega tolds, it could be fine if you share the hole code. However, calling “write bucket” each loop is not the best idea, because the server is only going to get the data each 60s (30s in your case that have a maker account) and this is creating a lot of work and trafic in your processor.
So I recomend you includding a limitation algorithm as the one below or change the configuration to sampling interval from device resource.
finally, I’m seeing that you have written “BucketId” in your code, this instruction will only write data in a platform bucket with the ID=bucketId, be careful with that!!