Mozilla IoT Framework complete!

After three long days of work, the framework and API is finally complete. Today I restarted my code from scratch with the fresh thoughts I had to work from. I knew I needed to run the device server in a thread. It took a bit of tinkering but I finally figured out why that error was being passed and it was as simple as adding a single line of code to the top fo the thread. Basically just telling the tornado package that it was a loop running in the background. Now that I had this running in a thread all I had to do was control my socket in the main thread and depending on what strings I received from Unity I could start and stop the server thread giving me a dynamic python IoT server that can support whatever devices are present in the Unity environment.

I also used a new package on the Unity side called JSONObject that can dynamically decode JSON data which I need because I have no idea how many keys or what keys will be present in the JSON data as it changes based on how many and what types of objects I have.

Running the code with these changes it worked extremely well and I got the server starting and stopping with my Unity code. However, something weird kept happening. Let’s say I had two devices UnityLightOne and UnityLightTwo if I removed UnityLightOne and reran the code it would think UnityLightOne is present. I spent some time reviewing code on how these ids are assigned and found that they just use simple indexes but no special tag for the device. I asked about it on their Github and received a response in an hour. You can see the solution here. I had to change some of the URL paths and the id assignment code to assign the name of the device as the ID rather than a general index number. If I change device names this system will not work but its better than the current solution. I may be able to add code to use a special id for each device that remains attached to it.

I ended the day with three balls all tagged as a UnityLight. I got these devices created by sending their names to the python code. I then was able to toggle them on and off from the gateway. I wrote a small script that takes data coming in from the socket in Unity which was being sent from the python code and decodes the JSON data and then changes the color of the ball if its on or off. I was able to get all the balls changing color and was able to create rules in the gateway to tell other balls to turn on when ball two turns on. This is a great framework for the RDL game and can have things added and changed to match our needs.

One thought on “Mozilla IoT Framework complete!

Leave a comment