Skip to content
Miloš Viktorović
  • Home
  • Coaching
    • Coaching
    • Introduction to Agile
    • Practical (Remote) Agile
  • Posts & Publications
    • Publications
    • Flying
    • Blog
  • Projects
    • Research Project
    • Student IoT kit
    • CODES
  • About
  • Contact

[CODE] GrovePi_plus-Example.py

## ##########################################################
## @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) 
  • [CODE] GrovePi_plus-Example.py
  • [CODE] iot_simulator.py
  • [CODE] GrovePi_plus-startup
  • [CODE] mqtt_and_csv.py
  • [CODE] mqtt_messenger_example.py
  • [CODE] test_IoT.py

Recent Posts

  • Schnitzel flight! EHEH – EDRK
    Schnitzel flight! EHEH – EDRK
    03-Sep-2021
  • Private Pilot License – My journey to PPL!
    Private Pilot License – My journey to PPL!
    22-Apr-2021
  • Solo Cross-country Navigation flight (Big Triangle) EHEH ⇒ EHMZ ⇒ EHSE ⇒ EHEH
    Solo Cross-country Navigation flight (Big Triangle) EHEH ⇒ EHMZ ⇒ EHSE ⇒ EHEH
    13-Dec-2020
  • Spin & acrobatics on Super Decathlon
    Spin & acrobatics on Super Decathlon
    19-Oct-2020
  • Flight log EHEH-EHSE (via Rotterdam)
    Flight log EHEH-EHSE (via Rotterdam)
    19-Oct-2020

Recent Posts

  • Schnitzel flight! EHEH – EDRK
  • Private Pilot License – My journey to PPL!
  • Solo Cross-country Navigation flight (Big Triangle) EHEH ⇒ EHMZ ⇒ EHSE ⇒ EHEH

Archives

  • September 2021
  • April 2021
  • December 2020
  • October 2020
  • August 2020
  • June 2020
  • May 2020
  • January 2020
  • May 2019
Milos Viktorovic © 2023
  • Home
  • Coaching
    • Coaching
    • Introduction to Agile
    • Practical (Remote) Agile
  • Posts & Publications
    • Publications
    • Flying
    • Blog
  • Projects
    • Research Project
    • Student IoT kit
    • CODES
  • About
  • Contact