Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This program reads sensor data from the GrovePI sensor kit on a Raspberry PI 3
- and pushes the data into the SAP Cloud Platform IoT Services 4.0.
- It needs to be run with Python 3 interpreter and the script needs to be
- placed under following path on your Raspberry PI:
- /home/pi/Desktop/GrovePi/Software/Python
- @author: Matthias Allgaier, SAP SE
- '''
- import requests
- import json
- import time
- import math
- import time
- import grovepi
- import os
- import paho.mqtt.client as mqtt
- import ssl
- # Definition of the message type for sensor data including initial values
- [valueTemp, valueLight, valueUltrasonic, valueSound, valueRotary, valueActivity] = [10, 20, 30, 40, 50, 60]
- '''
- Sensor & Measure Definitions
- ====================================
- Sensor 1: Temperature -> Slot D2
- Sensor 2: Light -> Slot A1
- Sensor 3: Ultrasonic -> Slot D4
- Sensor 4: Sound -> Slot A2
- Sensor 5: Rotary -> Slot A0
- Sensor 6: Activity / Humidity -> Slot D2
- Note: field "activity" (can also be used as a custom field)
- '''
- # D2 (Combined sensor for temperature and humidity)
- # Blue sensor (Version 1.2)
- sensor_temperature = 2
- blue = 0
- # A1
- sensor_light = 1
- # D4
- sensor_ultrasonic = 4
- # A2
- sensor_sound= 2
- # A0
- sensor_rotary = 0
- # Time interval for polling the sensor data
- timeIntervall = 3;
- def on_connect(client, userdata, rc):
- print("connected")
- mqttc.publish(topic='measures/920e5bee6b1e7fdd', payload='{"measureIds":["641"], "values":[100], "logNodeAddr":"33a203a8bc05c0c2"}', qos=0)
- def on_message(client, userdata, msg):
- print(msg.topic+" "+str(msg.payload))
- def on_publish(client, userdata, mid):
- print("published")
- mqttc.disconnect()
- while True:
- try:
- print("")
- print("============================================")
- print("Reading sensor data ...")
- # Read light
- valueLight = grovepi.analogRead(sensor_light)
- print("Light value = %d" %valueLight)
- # Read ultrasonic
- valueUltrasonic = grovepi.ultrasonicRead(sensor_ultrasonic)
- print("Ultrasonic value = %d" %valueUltrasonic)
- # Read sound
- valueSound = grovepi.analogRead(sensor_sound)
- print("Sound value = %d" %valueSound)
- # Read rotary
- valueRotary = grovepi.analogRead(sensor_rotary)
- print("Rotary value = %d" %valueRotary)
- # Read temperature & humiditiy
- [temp,humidity] = grovepi.dht(sensor_temperature,blue)
- if math.isnan(temp) == False and math.isnan(humidity) == False:
- valueTemp = temp
- valueActivity = humidity
- print("Temperature value = %d" %valueTemp)
- print("Hummidity value = %d" %valueActivity)
- '''
- Create the HTTP POST message and send it to the SAP IoT service
- Adjust MAC address in the URL according to your physical node in the IoT service
- and begin with 0000
- Example:
- ========
- MAC adress without '-' or ':' b827eb9d3f4c
- URL: https://iotae-ekt.eu10.cp.iot.sap/iot/gateway/rest/measures/0000920e5bee6b1e7fdd'
- '''
- data = json.dumps({"measureIds":["641"], "values":[100], "logNodeAddr":"33a203a8bc05c0c2"})
- # headers = {'content-type': 'application/json'}
- # print os.getcwd()
- # r = requests.post('http://iotae-ekt.eu10.cp.iot.sap/iot/gateway/rest/measures/920e5bee6b1e7fdd', data=data, headers=headers, cert='keyStore.pem', timeout=5)
- # responseCode = r.status_code
- # print ("==> HTTP Response: %d %s" %(responseCode, r.reason))
- mqttc = mqtt.Client(client_id="920e5bee6b1e7fdd", clean_session=True, userdata=None, transport="tcp")
- mqttc.on_connect = on_connect
- #mqttc.on_disconnect = on_disconnect
- mqttc.on_message = on_message
- mqttc.on_publish = on_publish
- mqttc.tls_set(certfile="keyStore.pem", tls_version=ssl.PROTOCOL_TLSv1_2)
- mqttc.connect(host='iotae-ekt.eu10.cp.iot.sap', port=8883)
- print("jest")
- mqttc.publish("measures/920e5bee6b1e7fdd", json.dumps({"measureIds":["644", "642", "645", "646", "643", "641"], "values":[valueTemp,valueLight,valueUltrasonic,valueSound,valueRotary,valueActivity], "logNodeAddr":"33a203a8bc05c0c2"}))
- # wait some time to read again sensor values
- time.sleep(timeIntervall)
- except IOError:
- print ("Error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement