upgrade to use parameters
add round function
This commit is contained in:
		
							parent
							
								
									f5b06bcafb
								
							
						
					
					
						commit
						02e6c84a44
					
				@ -1,2 +1,3 @@
 | 
				
			|||||||
# raspberrypi-temp-sensors
 | 
					# raspberrypi-temp-sensors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					run the script with "/usr/bin/python /home/pi/mqtt/mqtt_publish_temp.py <roomName> <mqtt.username> <mqtt.password>
 | 
				
			||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
import time
 | 
					import time
 | 
				
			||||||
import datetime
 | 
					import datetime
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Import Adafruit_DHT
 | 
					# Import Adafruit_DHT
 | 
				
			||||||
import Adafruit_DHT
 | 
					import Adafruit_DHT
 | 
				
			||||||
@ -12,7 +13,7 @@ import paho.mqtt.client as paho
 | 
				
			|||||||
# MQTT credentials
 | 
					# MQTT credentials
 | 
				
			||||||
#mqtt_username  = 'homeassistant' # MQTT client username
 | 
					#mqtt_username  = 'homeassistant' # MQTT client username
 | 
				
			||||||
#mqtt_password = '3355' # MQTT client password
 | 
					#mqtt_password = '3355' # MQTT client password
 | 
				
			||||||
#client_id = 'temperature' # unique client_id
 | 
					client_id = sys.argv[1] # unique client_id
 | 
				
			||||||
mqtt_broker = "raspberrypi" # broker address, usually your HASS IP address
 | 
					mqtt_broker = "raspberrypi" # broker address, usually your HASS IP address
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# DHT config
 | 
					# DHT config
 | 
				
			||||||
@ -27,10 +28,10 @@ def on_connect(client, data, flags, rc):
 | 
				
			|||||||
client = paho.Client(protocol=paho.MQTTv31)  # * set a random string (max 23 chars)
 | 
					client = paho.Client(protocol=paho.MQTTv31)  # * set a random string (max 23 chars)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Command topic
 | 
					# Command topic
 | 
				
			||||||
temperature_topic  = 'weathersensors/emily/temperature'
 | 
					temperature_topic  = 'weathersensors/' + client_id + '/temperature'
 | 
				
			||||||
humidity_topic = 'weathersensors/emily/humidity'
 | 
					humidity_topic = 'weathersensors/' + client_id + '/humidity'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# client connection
 | 
					#client connection
 | 
				
			||||||
#client.username_pw_set(mqtt_username, mqtt_password)  # MQTT server credentials
 | 
					#client.username_pw_set(mqtt_username, mqtt_password)  # MQTT server credentials
 | 
				
			||||||
client.connect(mqtt_broker)  # MQTT server address
 | 
					client.connect(mqtt_broker)  # MQTT server address
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -40,9 +41,10 @@ while True:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        # Get Temperature from DHT
 | 
					        # Get Temperature from DHT
 | 
				
			||||||
        _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
 | 
					        _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
 | 
				
			||||||
 | 
					        temperature = round(temperature, 2)
 | 
				
			||||||
        # Get Humidity from DHT
 | 
					        # Get Humidity from DHT
 | 
				
			||||||
        humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
 | 
					        humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
 | 
				
			||||||
 | 
					        humidity = round(humidity, 2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Publish our data
 | 
					        # Publish our data
 | 
				
			||||||
        client.publish(temperature_topic, temperature)
 | 
					        client.publish(temperature_topic, temperature)
 | 
				
			||||||
@ -53,4 +55,3 @@ while True:
 | 
				
			|||||||
        # Output data to screen
 | 
					        # Output data to screen
 | 
				
			||||||
        # print temperature
 | 
					        # print temperature
 | 
				
			||||||
        # print humidity
 | 
					        # print humidity
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user