7.4 KiB
Measure temperature - Virtual IoT Hardware
For dis part of di lesson, you go add temperature sensor to your virtual IoT device.
Virtual Hardware
Di virtual IoT device go use simulated Grove Digital Humidity and Temperature sensor. Dis one go make di lab be like say you dey use Raspberry Pi wey get physical Grove DHT11 sensor.
Di sensor dey combine temperature sensor wit humidity sensor, but for dis lab, na di temperature sensor part you go focus on. For physical IoT device, di temperature sensor go be thermistor wey dey measure temperature by sensing di change wey dey happen for resistance as temperature dey change. Temperature sensors na usually digital sensors wey dey internally convert di resistance wey dem measure into temperature for degrees Celsius (or Kelvin, or Fahrenheit).
Add di sensors to CounterFit
To use virtual humidity and temperature sensor, you go need add di two sensors to di CounterFit app.
Task - add di sensors to CounterFit
Add di humidity and temperature sensors to di CounterFit app.
-
Create new Python app for your computer inside folder wey you go call
temperature-sensorwit one file wey you go callapp.pyand Python virtual environment, then add di CounterFit pip packages.⚠️ You fit check di instructions for how to create and setup CounterFit Python project for lesson 1 if you need am.
-
Install extra Pip package to install CounterFit shim for di DHT11 sensor. Make sure say you dey install am from terminal wey get di virtual environment activated.
pip install counterfit-shims-seeed-python-dht -
Make sure say di CounterFit web app dey run.
-
Create humidity sensor:
-
For di Create sensor box wey dey di Sensors pane, drop down di Sensor type box and select Humidity.
-
Leave di Units as Percentage.
-
Make sure say di Pin dey set to 5.
-
Click di Add button to create di humidity sensor for Pin 5.
Di humidity sensor go dey created and e go show for di sensors list.
-
-
Create temperature sensor:
-
For di Create sensor box wey dey di Sensors pane, drop down di Sensor type box and select Temperature.
-
Leave di Units as Celsius.
-
Make sure say di Pin dey set to 6.
-
Click di Add button to create di temperature sensor for Pin 6.
Di temperature sensor go dey created and e go show for di sensors list.
-
Program di temperature sensor app
Now you fit program di temperature sensor app using di CounterFit sensors.
Task - program di temperature sensor app
Program di temperature sensor app.
-
Make sure say di
temperature-sensorapp dey open for VS Code. -
Open di
app.pyfile. -
Add dis code to di top of
app.pyto connect di app to CounterFit:from counterfit_connection import CounterFitConnection CounterFitConnection.init('127.0.0.1', 5000) -
Add dis code to di
app.pyfile to import di libraries wey you need:import time from counterfit_shims_seeed_python_dht import DHTDi
from seeed_dht import DHTstatement dey import diDHTsensor class to interact wit virtual Grove temperature sensor using shim from dicounterfit_shims_seeed_python_dhtmodule. -
Add dis code after di code wey dey above to create instance of di class wey dey manage di virtual humidity and temperature sensor:
sensor = DHT("11", 5)Dis one dey declare instance of di
DHTclass wey dey manage di virtual Digital Humidity and Temperature sensor. Di first parameter dey tell di code say di sensor wey dem dey use na virtual DHT11 sensor. Di second parameter dey tell di code say di sensor dey connect to port5.💁 CounterFit dey simulate dis combined humidity and temperature sensor by connecting to 2 sensors, humidity sensor for di pin wey dem give when dem create di
DHTclass, and temperature sensor wey dey run for di next pin. If di humidity sensor dey pin 5, di shim go expect say di temperature sensor dey pin 6. -
Add infinite loop after di code wey dey above to dey check di temperature sensor value and print am for di console:
while True: _, temp = sensor.read() print(f'Temperature {temp}°C')Di call to
sensor.read()dey return tuple of humidity and temperature. Na only di temperature value you need, so di humidity dey ignored. Di temperature value go then dey printed for di console. -
Add small sleep of ten seconds for di end of di
loopbecause di temperature levels no need dey checked every time. Sleep dey help reduce di power wey di device dey use.time.sleep(10) -
From di VS Code Terminal wey get activated virtual environment, run dis one to run your Python app:
python app.py -
From di CounterFit app, change di value of di temperature sensor wey di app go read. You fit do dis one in two ways:
-
Enter number for di Value box for di temperature sensor, then click di Set button. Di number wey you enter go be di value wey di sensor go return.
-
Check di Random checkbox, then enter Min and Max value, then click di Set button. Every time wey di sensor dey read value, e go read random number between Min and Max.
You go see di values wey you set dey show for di console. Change di Value or di Random settings to see di value dey change.
(.venv) ➜ temperature-sensor python app.py Temperature 28.25°C Temperature 30.71°C Temperature 25.17°C -
💁 You fit find dis code for di code-temperature/virtual-device folder.
😀 Your temperature sensor program don work well!
Disclaimer:
Dis dokyument don use AI translet service Co-op Translator do di translet. Even as we dey try make am correct, abeg make you sabi say AI translet fit get mistake or no dey accurate well. Di original dokyument wey dey for im native language na di one wey you go take as di correct source. For important mata, e good make you use professional human translet. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translet.



