Muhammad Afzal
Published © Apache-2.0

Warehouse/Control Shed/Greenhouse Monitoring

This project is an example of industrial IoT that demonstrates how we can monitor our warehouse/production hall conditions.

IntermediateFull instructions provided10 hours422

Things used in this project

Story

Read more

Code

nxp-rapid-iot-cayenne-mydevices.py

Python
########################################################################################
#
# NXP Rapid IoT Prototyping KiT + Raspberry Pi + Cayenne Mydevices Cloud.
# Written by @Muhammad Afzal 
# hackster.io/mafzal
# This Project is only for Educational Purpose. use at own risk for Commerical Purpose.
#
#########################################################################################

import time
import pygatt
import cayenne.client

#NXP Mac Address which is located at the back of the device 
#Starting With M Just Take First 6 Digit and 6 Digit From Last Omit the Center Values.
mac = '00:60:37:0A:B1:1D'

#BLE Characteristic UUID (128-bit) of Sensors
char_uuids = {
    "temperature": "016c5f53-4dbf-4419-9b80-beb3bf6a5893",
    "humidity" :"016c5f53-4dbf-4419-9b80-beb3bf6a5894",
    "air_quality_tvoc":"c285e3a4-c9c0-43ed-956c-a15cc1690b77",
    "air_quality_co2": "c285e3a4-c9c0-43ed-956c-a15cc1690b7b",
    "pressure": "c285e3a4-c9c0-43ed-956c-a15cc1690b76",
    "ambient-light": "c285e3a4-c9c0-43ed-956c-a15cc1690b7a"  ,
    "battery-level": "c285e3a4-c9c0-43ed-956c-a15cc1690b78",
    "charging-status": "c285e3a4-c9c0-43ed-956c-a15cc1690b79"
}
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "==================UserName========================"
MQTT_PASSWORD  = "==================Password========================"
MQTT_CLIENT_ID = "==================ClientID========================"

# The callback for when a message is received from Cayenne.
def on_message(message):
  print("message received: " + str(message))
# If there is an error processing the message return an error string, otherwise return nothing.
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883)

def send_values_to_cayenne_cloud(sensors_data=[]):
        #print(sensors_data)
        client.loop() 
        client.virtualWrite(1,sensors_data['temperature'],"temp","c")      				#Temperature
        client.virtualWrite(2,sensors_data['humidity'],"rel_hum","p")         			#Humidity
        client.luxWrite(3,sensors_data['ambient-light'])        						#Light Level
        client.virtualWrite(4,sensors_data['air_quality_tvoc'],"analog_sensor","null")  #TVOC
        client.virtualWrite(5,sensors_data['air_quality_co2'],"co2","ppm")  			#CO2
        client.virtualWrite(6,sensors_data['pressure'],"bp","hpa")         				#Pressure
        client.virtualWrite(7,sensors_data['battery-level'],"batt","p")    				#Batter Level
        client.virtualWrite(8,sensors_data['charging-status'],"digital_sensor","d") 	#Charging Status



def save_sensor_values(): #Get Sensor Data Fro NXP Rapid IOT Kit
	adapter = pygatt.GATTToolBackend()
	try:
		adapter.start()
	    conn = adapter.connect(mac)
		
        sensor_values = {}
		for sensor in char_uuids:
			value = conn.char_read(char_uuids[sensor])
			sensor_values[sensor] = float(value.decode("utf-8").rstrip('\00'))
		return (sensor_values)

	except Exception as e:
		print("Error: ", e)

	finally:
		adapter.stop()
		

if __name__ == '__main__':
	while True:
			senosrs_values = {} 
			sensors_values = save_sensor_values()
			if sensors_values is not None and len(sensors_values)>=8 :
					send_values_to_cayenne_cloud(sensors_values)
			time.sleep(60) #Delay of 1 Minute

Credits

Muhammad Afzal

Muhammad Afzal

18 projects • 67 followers
I am Software Eng having 9+ Years of experience. Hackster.io & Cayenne Mydevices Ambassador in Pakistan.

Comments