Skip to main content

Measure temperature - Virtual IoT Hardware

In this part of the lesson, you will add a temperature sensor to your virtual IoT device.

Virtual Hardware

The virtual IoT device will use a simulated Grove Digital Humidity and Temperature sensor. This keeps this lab the same as using a Raspberry Pi with a physical Grove DHT11 sensor.

The sensor combines a temperature sensor with a humidity sensor, but in this lab you are only interested in the temperature sensor component. In a physical IoT device, the temperature sensor would be a thermistor that measures temperature by sensing a change in resistance as temperature changes. Temperature sensors are usually digital sensors the internally convert the resistance measured into a temperature in degrees Celsius (or Kelvin, or Fahrenheit).

Add the sensors to CounterFit

To use a virtual humidity and temperature sensor, you need to add the two sensors to the CounterFit app

Task - add the sensors to CounterFit

Add the humidity and temperature sensors to the CounterFit app.

  1. Create a new Python app on your computer in a folder called temperature-sensor with a single file called app.py and a Python virtual environment, and add the CounterFit pip packages.

    ⚠️ You can refer to the instructions for creating and setting up a CounterFit Python project in lesson 1 if needed.

  2. Install an additional Pip package to install a CounterFit shim for the DHT11 sensor. Make sure you are installing this from a terminal with the virtual environment activated.

    pip install counterfit-shims-seeed-python-dht
  3. Make sure the CounterFit web app is running

  4. Create a humidity sensor:

    1. In the Create sensor box in the Sensors pane, drop down the Sensor type box and select Humidity.

    2. Leave the Units set to Percentage

    3. Ensure the Pin is set to 5

    4. Select the Add button to create the humidity sensor on Pin 5

    The humidity sensor settings

    The humidity sensor will be created and appear in the sensors list.

    The humidity sensor created

  5. Create a temperature sensor:

    1. In the Create sensor box in the Sensors pane, drop down the Sensor type box and select Temperature.

    2. Leave the Units set to Celsius

    3. Ensure the Pin is set to 6

    4. Select the Add button to create the temperature sensor on Pin 6

    The temperature sensor settings

    The temperature sensor will be created and appear in the sensors list.

    The temperature sensor created

Program the temperature sensor app

The temperature sensor app can now be programmed using the CounterFit sensors.

Task - program the temperature sensor app

Program the temperature sensor app.

  1. Make sure the temperature-sensor app is open in VS Code

  2. Open the app.py file

  3. Add the following code to the top of app.py to connect the app to CounterFit:

    from counterfit_connection import CounterFitConnection
    CounterFitConnection.init('127.0.0.1', 5000)
  4. Add the following code to the app.py file to import the required libraries:

    import time
    from counterfit_shims_seeed_python_dht import DHT

    The from seeed_dht import DHT statement imports the DHT sensor class to interact with a virtual Grove temperature sensor using a shim from the counterfit_shims_seeed_python_dht module.

  5. Add the following code after the code above to create an instance of the class that manages the virtual humidity and temperature sensor:

    sensor = DHT("11", 5)

    This declares an instance of the DHT class that manages the virtual Digital Humidity and Temperature sensor. The first parameter tells the code the sensor being used is a virtual DHT11 sensor. The second parameter tells the code the sensor is connected to port 5.

    💁 CounterFit simulates this combined humidity and temperature sensor by connecting to 2 sensors, a humidity sensor on the pin given when the DHT class is created, and a temperature sensor that runs on the next pin. If the humidity sensor is on pin 5, the shim expects the temperatures sensor to be on pin 6.

  6. Add an infinite loop after the code above to poll the temperature sensor value and print it to the console:

    while True:
    _, temp = sensor.read()
    print(f'Temperature {temp}°C')

    The call to sensor.read() returns a tuple of humidity and temperature. You only need the temperature value, so the humidity is ignored. The temperature value is then printed to the console.

  7. Add a small sleep of ten seconds at the end of the loop as the temperature levels don't need to be checked continuously. A sleep reduces the power consumption of the device.

    time.sleep(10)
  8. From the VS Code Terminal with an activated virtual environment, run the following to run your Python app:

    python app.py
  9. From the CounterFit app, change the value of the temperature sensor that will be read by the app. You can do this in one of two ways:

    • Enter a number in the Value box for the temperature sensor, then select the Set button. The number you enter will be the value returned by the sensor.

    • Check the Random checkbox, and enter a Min and Max value, then select the Set button. Every time the sensor reads a value, it will read a random number between Min and Max.

    You should see the values you set appearing in the console. Change the Value or the Random settings to see the value change.

    (.venv) ➜  temperature-sensor python app.py
    Temperature 28.25°C
    Temperature 30.71°C
    Temperature 25.17°C

💁 You can find this code in the code-temperature/virtual-device folder.

😀 Your temperature sensor program was a success!