Monitoring and Power Control on a Dime Part II: Now with Humidity & API logging
In my last post, I detailed how to monitor your server room or entertainment room's temperature with a Web Power Switch Pro by DLI. (Also, check out API access, or CSV writing with part IV here!) While that worked quite well, there were a few things that I wanted to cover, including the setup of alerts and building better graphs through the energy monitor. While those are still goals of mine and something that I would like to cover, I would like to first show an alternative method of sensing the environment for changes in temperature as well as humidity. To do this, I am going to swap out the DS18B20 that I used as a temperature probe for a DHT11.
Why the change? What is a DHT11?
Simply put, the DHT11 is a very low-cost digital temperature that also measures humidity. It combines a thermistor with a capacitive humidity sensor and sends both measurements down a digital data pin. The downside of using this over the DS18B20 is its accuracy.
Sensor Type | Accuracy | Temperature | Humidity |
---|---|---|---|
DHT11 | ±2°C | Yes | Yes |
DS18B20 |
±0.5°C | Yes |
No |
Braun IRT6500 |
.1°C | Yes | N/A |
With the DHT11, measurements are down to a ±2°C margin of error, whereas the DS18B20 can get within a ±0.5°C margin of error. Humidity is accurate to ±5%. A temperature difference of 1 degree C is the equivalent of a temperature difference around 1.8°F. For my purposes, this accuracy level is fine, but if the intention is to get very accurate readings, another solution may be necessary. In the table above, I have included a handheld temperature thermometer to give an idea as to the scale and resolution offered by these sensors.
The first step is getting the DHT11 and figuring out the pinout. It is possible to buy the sensor without an attached PCB for an ultra-compact setup, but for simplicity’s sake, purchasing an already assembled unit with attached pull-up resistor can save assembly time and create a more finished unit.
For connections, the setup is similar to the DS18B20:
The tip of the jack will provide 5 volts DC.
The center ring of the jack will be for TTL data.
The sleeve (part closest to the wire of the plug) is ground.
Using a continuity meter, figure out which wires go to the segmented parts of the plug and solder accordingly. In most cases, left channel (W) will go to VCC, right channel (R) will go to Data, and the sleeve will be grounded.
Once everything is wired, it may be desirable to cover or reinforce the solder points. I used white Sugru and formed a layer over the connector to reinforce the junction and to provide some additional protection from the outside world. The end result looks like this:
With that, the sensor is done. Plugging it into the DLI Web Power Switch Pro will enable the unit to begin tracking humidity, but it may be necessary to configure graphs and meters to show that information, especially if the default ones were deleted at some point in time.
If you’re done at this point, great! But the thing is, the Web Power Switch Pro has an API. You can log this data with a little bit of Python, and put it into a local CSV for your own monitoring, plotting, and more. Below is the script I use to access the data and copy it to a local CSV file:
#!/usr/bin/python3 import requests import csv import time import urllib3 # disable SSL warnings, because this is dev, right guys? Right!? urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # IP address of the web power switch pro ip_address = x.x.x.x' # login credentials username = 'user' password = 'hey_maybe_erase_this_before_pushing' # disable SSL warnings urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # path to the CSV file csv_path = 'temperature_humidity_data.csv' while True: # make a request to the API to get the temperature and humidity response = requests.get(f'https:///restapi/meter/values/sensors.0.temperature/value/', auth=(username, password), verify=False) temp_text = response.text.strip() temp = float(temp_text) / 10.0 * 1.8 + 32.0 # convert to Fahrenheit response = requests.get(f'https:///restapi/meter/values/sensors.0.relative_humidity/value/', auth=(username, password), verify=False) hum_text = response.text.strip() hum = float(hum_text) * 100 # print the results to the console print(f'Temperature: F, Humidity: %') # save the results to the CSV file with open(csv_path, 'a', newline='') as csv_file: writer = csv.writer(csv_file) writer.writerow([time.time(), temp, hum]) # wait for 10 seconds before checking again time.sleep(10)