## ##########################################################
## @title: GrovePi+ sensors example
## @author: Milos Viktorovic
## @email: m.viktorovic@tue.nl
## @version: 2.0b [13-11-19]
## ##########################################################
import grovepi
import math
from time import sleep, gmtime, strftime
import paho.mqtt.client as mqtt_client
import atexit
# Connect the Grove Dust Sensor to digital port D4 (Only works on D4 !)
dust_sensor = 4
grovepi.dust_sensor_en(dust_sensor) # enable dust sensor
sleep (5)
# Connect the Grove Temperature & Humidity Sensor to digital port D3
# This example uses the blue colored sensor.
# SIG,NC,VCC,GND
dht_sensor = 3 # The Temp & humidity Sensor goes on digital port 3.
# temp_humidity_sensor_type
# Grove Base Kit comes with the blue sensor.
blue = 0 # The Blue colored sensor.
white = 1 # The White colored sensor.
loudness_sensor = 0 # The Loudness Sensor goes on analog port 0.
uv_sensor = 1 # The UV Sensor goes on analog port 1.
moisture_sensor = 2 # The Moisture Sensor goes on analog port 2.
print("### Script Started! ###")
while True: #try to connect to MQTT server, until connection is established
try:
#creating MQTT client
mqtt = mqtt_client.Client("Student_1")
#connecting to MQTT server
mqtt.connect(host="broker.hivemq.com", port=1883)
mqtt.loop_start()
break # exit loop if connection established
except:
print("MQTT connection error!")
exit()
# Pre-initialize variables for payload
msg_0 = ""
msg_1 = ""
msg_2 = ""
msg_3 = ""
msg_4 = ""
while True:
try:
print("### Connecting to the dust sensor! ###")
# Read values from dust sensor
[lowpulseoccupancy, lpo_percentage, concentration] = grovepi.dust_sensor_read(dust_sensor)
if lowpulseoccupancy > 0 or True:
msg_0 = "Concentration = %.02f pcs/0.01cf "%(lowpulseoccupancy)
mqtt.publish(topic="testtopic/my-office/pm",
payload = lowpulseoccupancy,
qos=2
)
print("### Connecting to the Temp&Humid sensor! ###")
# This example uses the blue colored sensor.
# The first parameter is the port, the second parameter is the type of sensor.
[temp,humidity] = grovepi.dht(dht_sensor,blue)
if math.isnan(temp) == False and math.isnan(humidity) == False:
msg_1 = "temp = %.02f°C humidity =%.02f%% "%(temp, humidity)
mqtt.publish(topic="testtopic/my-office/temp",
payload = temp,
qos=2
)
mqtt.publish(topic="testtopic/my-office/humidity",
payload = str(humidity),
qos=2
)
print("### Connecting to the loudness sensor! ###")
# Read the sound level from loudness sensor
loudness = grovepi.analogRead(loudness_sensor)
msg_2 = "Loudness level = %.02f "%(loudness)
mqtt.publish(topic="testtopic/my-office/sound",
payload = loudness,
qos=2
)
print("### Connecting to the UV sensor! ###")
# Read the UV radiation level from the UV sensor
uv_level = grovepi.analogRead(uv_sensor)
msg_3 = "UV level = %.02f "%(uv_level)
mqtt.publish(topic="testtopic/my-office/uv",
payload = uv_level,
qos=2
)
print("### Connecting to the soild moisture sensor! ###")
# Read the soil moisture level from the moisture sensor
moisture_level = grovepi.analogRead(moisture_sensor)
msg_4 = "Soil moisture level = %.02f/900 "%(moisture_level)
#combine messages
full_msg = strftime("%Y-%m-%d %H:%M:%S", gmtime())+" > "+msg_0+msg_1+msg_2+msg_3+msg_4
#publist to MQTT
## single message
mqtt.publish(topic="testtopic/my-office",
payload = full_msg,
qos=2
)
print(full_msg)
print("_______________________________")
except Exception as e:
print ("Error: "+str(e))
# Wait N seconds
sleep(10)