Skip to content

Python Script

Elizabeth Adams edited this page Sep 5, 2019 · 4 revisions

With our operating system installed along with our two Python modules for reading sensor data and sending data to Initial State, we are ready to write our Python script. The following script will create/append to an Initial State data bucket, read the DHT22 sensor data, and send that data to a real-time dashboard. All you need to do is modify lines 6–11.

import Adafruit_DHT
from ISStreamer.Streamer import Streamer
import time

# --------- User Settings ---------
SENSOR_LOCATION_NAME = "Office"
BUCKET_NAME = ":partly_sunny: Room Temperatures"
BUCKET_KEY = "rt0129"
ACCESS_KEY = "PLACE YOUR INITIAL STATE ACCESS KEY HERE"
MINUTES_BETWEEN_READS = 10
METRIC_UNITS = False
# ---------------------------------

streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
while True:
	humidity, temp_c = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
	if METRIC_UNITS:
		streamer.log(SENSOR_LOCATION_NAME + " Temperature(C)", temp_c)
	else:
		temp_f = format(temp_c * 9.0 / 5.0 + 32.0, ".2f")
		streamer.log(SENSOR_LOCATION_NAME + " Temperature(F)", temp_f)
	humidity = format(humidity,".2f")
	streamer.log(SENSOR_LOCATION_NAME + " Humidity(%)", humidity)
	streamer.flush()
	time.sleep(60*MINUTES_BETWEEN_READS)
  • Line 6 - This value should be unique for each node/temperature sensor. This could be your sensor node’s room name, physical location, unique identifier, or whatever. Just make sure it is unique for each node to ensure that the data from this node goes to its own data stream in your dashboard.
  • Line 7 - This is the name of the data bucket. This can be changed at any time in the Initial State UI.
  • Line 8 - This is your bucket key. It needs to be the same bucket key for every node you want displayed in the same dashboard.
  • Line 9 - This is your Initial State account access key. Copy and paste this key from your Initial State account.
  • Line 10 - This is the time between sensor reads. Change accordingly.
  • Line 11 - You can specify metric or imperial units on line 11.

After you have set lines 6–11 in your Python script on your Pi Zero WH, save and exit the text editor. Run the script with the following command:

$ python tempsensor.py

Repeat these steps for each sensor node. As long as each node is sending data to Initial State using the same access key and bucket key, all data will go into the same data bucket and show up on the same dashboard.

<< Software Setup - Dashboard >>

Clone this wiki locally