The IotDevice class

The IotDevice class will contain the specific behavior for our sensor node. This class will contain the methods necessary to sample our sensors, control external devices, and so on. For our example, the IotDevice class will perform several functions, including the following:

  • Manage the state of the LEDs (which represent the external devices to control)
  • Manage sensor variables, such as temperature and humidity
  • Hold the IoT device's ID
  • Include methods for sampling and controlling the IoT device

The first step is to create the IotDevice class and build the constructor. An example of what this might look like can be seen in the following code:

class IotDevice:
def __init__(self):
self.LED1 = "Off"
self.LED2 = "Off"
self.LED3 = "Off"
self.Temperature = 21.1
self.Humidity = 63.4
self.ID = "14-3826"

We can then create a method that would manage sampling the onboard sensors. Now, we don't have any sensors in our example so we will use the temperature and humidity variables we created in our constructor and just create a pattern for it to generate, such as incrementing every time the sample method is called. An example sample method can be seen here:

    def sample(self):
self.Temperature = self.Temperature + 0.1
if self.Temperature >= 30.0:
self.Temperature = 15
self.Humidity = self.Humidity + 0.5
if self.Humidity >= 100:
self.Humidity = 25.0

If you are following along, a great extension to this code would be to integrate a real temperature and humidity sensor that could then be sampled to provide real value. I'll leave that as an exercise for you to try out.

Let's now look at how we can implement our command parsing function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.129.19