Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temp_sensor.py #123

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion wireless_modules_py/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/wind_module/config.py

/wind_module/config.py
/temp_module/config.py

config.py

38 changes: 38 additions & 0 deletions wireless_modules_py/sensors/temp_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from sensor_base import Sensor
import time
import sys
import Adafruit_DHT
class TempSensor(Sensor):
"""
A class for the DHT_22 temp sensor
"""
def __init__(self, pin):
"""
Getting the DHT_22 temp sensor and pin for readings
"""
self.pin=pin
self.sensor=Adafruit_DHT.DHT22

def read(self):
"""
Read the temperature (deg in Cel) and humidity (%) from the temperature sensor
:return: An array of length 2 containing a temperature dictionary and a humidity dictionary.
Each dictionary contains a "type" key associated with the sensor type (str) and a "value" key associated with
another dictionary containing key-value pairs of the measurement method (str) and its relevant data (float).
"""
readings=[]
humidity, temperature=Adafruit_DHT.read_retry(self.sensor,self.pin)
if humidity is not None and temperature is not None:
temperature=round(float(temperature),2)
humidity=round(float(humidity),2)
readings = [
{
"temp_in_deg": temperature,
},
{
"humidity_in_percent": humidity,
}
]
else:
print('Failed to obtain data')
return readings
3 changes: 1 addition & 2 deletions wireless_modules_py/sensors/wind_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,5 @@ def read(self):
]

# update the last time of read() being called
self.query_time = time.time_ns() * self.NS_TO_MS


return self.readings
41 changes: 41 additions & 0 deletions wireless_modules_py/temp_module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Wind Module
This directory contains the scripts to collect temperature and humidity from the DHT22 sensor, and publish these data to the MQTT broker under the topic `/v3/wireless_module/6/data`.

- [Temp Module](#temp-module)
- [Basic Setup and Usage](#basic-setup-and-usage)
- [Testing](#testing)

## Basic Setup and Usage
1. Install the [Vaisala USB Instrument Finder driver](https://go.vaisala.com/software/WXT530/Vaisala_WXT530_Configuration_Tool_Weather_Measurement.zip?_ga=2.138439603.1803271655.1674458831-1555859295.1674458831) using these [installation instructions](https://docs.vaisala.com/r/M211840EN-F/en-US/GUID-6D206CCD-21E9-4E9A-98C9-760C90EA90BF/GUID-AE6CDFA9-16A5-4E47-B354-37C04534C558).

2. Connect the DHT22 to the raspberry pi and note down which pin it is connected to.

3. In the `temp_module` directory, create a local version of `config.py` using the `config.example.py` file.

4. Get inside the `temp_module` directory using:
```
cd wireless_mdoules_py/temp_module
```

5. Ensuring that you have [poetry](https://python-poetry.org/) installed, spawn into the poetry environment using:
```
poetry shell
```

6. Run the main program using:
```
python main.py
```

After completing these steps, the anemometer should be collecting wind speed and direction data, and publishing these data to the MQTT broker under the topic `/v3/wireless_module/6/data`.

<br>

## Testing
1. Open 2 terminals:
- One to subscribe to the topic `/v3/wireless_module/6/data`.
- Another to publish to the topic `v3/start`.

2. Publish a true start message to the topic `v3/start`. Streams of data should be shown in the terminal subscribed to the topic `/v3/wireless_module/6/data`.

3. While the data is being published, mess around with the sensor to ensure values are changing accordingly.
5 changes: 5 additions & 0 deletions wireless_modules_py/temp_module/config.example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MQTT_BROKER = "<Broker domain address or IP address>"

USERNAME = "<Username to connect to MQTT broker>"
PASSWORD = "<Password to connect to MQTT broker>"
PIN = "<Pin to connect to the temp sensor>"
38 changes: 38 additions & 0 deletions wireless_modules_py/temp_module/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys
sys.path.insert(1,'../sensors')
from temp_sensor import TempSensor
sys.path.insert(2, '../')
import config
from wireless_module import WirelessModule
import time
import os
os.environ['PI_HOST']='127.0.0.1'
os.environ['PI_PORT']='8888'
sys.path.append('home/pi/data-acquisition-system/wireless_modules_py/sensors/Adafruit_Python_DHT/Adafruit_DHT')
import Adafruit_DHT
import logging
import asyncio

DHT_SENSOR=Adafruit_DHT.DHT22
MODULE_NUM=6

async def main():
logging.basicConfig(
format="%(levelname)-8s [%(filename)s] %(message)s", level=logging.DEBUG
)

temp_sensor_pin = config.PIN
my_temp_sensor = TempSensor(temp_sensor_pin)

temp_module = WirelessModule(MODULE_NUM)
sensors = [my_temp_sensor]
temp_module.add_sensors(sensors)

logging.debug("Start asyncio loop")
asyncio.create_task(temp_module.run())

while True:
await asyncio.sleep(1)


asyncio.run(main())
Loading