As per the Welcome Topic…
- Do not distract community members by sending direct messages asking to solve a problem (i.e. write code for you or debug your code)
I have moved your request to your own topic… I recommend you post the code here (properly formatted as also mentioned in the Welcome Topic) so that others may be able to point out any issues in it that are probably the cause of your devices instability, if not your own network that is.