Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. '''
  2. This program reads sensor data from the GrovePI sensor kit on a Raspberry PI 3
  3. and pushes the data into the SAP Cloud Platform IoT Services 4.0.
  4.  
  5. It needs to be run with Python 3 interpreter and the script needs to be
  6. placed under following path on your Raspberry PI:
  7. /home/pi/Desktop/GrovePi/Software/Python
  8.  
  9. @author: Matthias Allgaier, SAP SE
  10. '''
  11. import requests
  12. import json
  13. import time
  14. import math
  15. import time
  16. import grovepi
  17. import os
  18.  
  19. import paho.mqtt.client as mqtt
  20. import ssl
  21.  
  22. # Definition of the message type for sensor data including initial values
  23. [valueTemp, valueLight, valueUltrasonic, valueSound, valueRotary, valueActivity] = [10, 20, 30, 40, 50, 60]
  24. '''
  25. Sensor & Measure Definitions
  26. ====================================
  27. Sensor 1: Temperature -> Slot D2
  28. Sensor 2: Light -> Slot A1
  29. Sensor 3: Ultrasonic -> Slot D4
  30. Sensor 4: Sound -> Slot A2
  31. Sensor 5: Rotary -> Slot A0
  32. Sensor 6: Activity / Humidity -> Slot D2
  33. Note: field "activity" (can also be used as a custom field)
  34. '''
  35.  
  36. # D2 (Combined sensor for temperature and humidity)
  37. # Blue sensor (Version 1.2)
  38. sensor_temperature = 2
  39. blue = 0
  40. # A1
  41. sensor_light = 1
  42. # D4
  43. sensor_ultrasonic = 4
  44. # A2
  45. sensor_sound= 2
  46. # A0
  47. sensor_rotary = 0
  48.  
  49. # Time interval for polling the sensor data
  50. timeIntervall = 3;
  51.  
  52. def on_connect(client, userdata, rc):
  53. print("connected")
  54. mqttc.publish(topic='measures/920e5bee6b1e7fdd', payload='{"measureIds":["641"], "values":[100], "logNodeAddr":"33a203a8bc05c0c2"}', qos=0)
  55.  
  56. def on_message(client, userdata, msg):
  57. print(msg.topic+" "+str(msg.payload))
  58.  
  59. def on_publish(client, userdata, mid):
  60. print("published")
  61. mqttc.disconnect()
  62.  
  63. while True:
  64. try:
  65. print("")
  66. print("============================================")
  67. print("Reading sensor data ...")
  68.  
  69. # Read light
  70. valueLight = grovepi.analogRead(sensor_light)
  71. print("Light value = %d" %valueLight)
  72.  
  73. # Read ultrasonic
  74. valueUltrasonic = grovepi.ultrasonicRead(sensor_ultrasonic)
  75. print("Ultrasonic value = %d" %valueUltrasonic)
  76.  
  77. # Read sound
  78. valueSound = grovepi.analogRead(sensor_sound)
  79. print("Sound value = %d" %valueSound)
  80.  
  81. # Read rotary
  82. valueRotary = grovepi.analogRead(sensor_rotary)
  83. print("Rotary value = %d" %valueRotary)
  84.  
  85. # Read temperature & humiditiy
  86. [temp,humidity] = grovepi.dht(sensor_temperature,blue)
  87. if math.isnan(temp) == False and math.isnan(humidity) == False:
  88. valueTemp = temp
  89. valueActivity = humidity
  90. print("Temperature value = %d" %valueTemp)
  91. print("Hummidity value = %d" %valueActivity)
  92.  
  93. '''
  94. Create the HTTP POST message and send it to the SAP IoT service
  95. Adjust MAC address in the URL according to your physical node in the IoT service
  96. and begin with 0000
  97.  
  98. Example:
  99. ========
  100. MAC adress without '-' or ':' b827eb9d3f4c
  101. URL: https://iotae-ekt.eu10.cp.iot.sap/iot/gateway/rest/measures/0000920e5bee6b1e7fdd'
  102. '''
  103.  
  104. data = json.dumps({"measureIds":["641"], "values":[100], "logNodeAddr":"33a203a8bc05c0c2"})
  105. # headers = {'content-type': 'application/json'}
  106. # print os.getcwd()
  107. # r = requests.post('http://iotae-ekt.eu10.cp.iot.sap/iot/gateway/rest/measures/920e5bee6b1e7fdd', data=data, headers=headers, cert='keyStore.pem', timeout=5)
  108.  
  109. # responseCode = r.status_code
  110. # print ("==> HTTP Response: %d %s" %(responseCode, r.reason))
  111.  
  112. mqttc = mqtt.Client(client_id="920e5bee6b1e7fdd", clean_session=True, userdata=None, transport="tcp")
  113. mqttc.on_connect = on_connect
  114. #mqttc.on_disconnect = on_disconnect
  115. mqttc.on_message = on_message
  116. mqttc.on_publish = on_publish
  117. mqttc.tls_set(certfile="keyStore.pem", tls_version=ssl.PROTOCOL_TLSv1_2)
  118. mqttc.connect(host='iotae-ekt.eu10.cp.iot.sap', port=8883)
  119. print("jest")
  120. mqttc.publish("measures/920e5bee6b1e7fdd", json.dumps({"measureIds":["644", "642", "645", "646", "643", "641"], "values":[valueTemp,valueLight,valueUltrasonic,valueSound,valueRotary,valueActivity], "logNodeAddr":"33a203a8bc05c0c2"}))
  121.  
  122.  
  123. # wait some time to read again sensor values
  124. time.sleep(timeIntervall)
  125.  
  126. except IOError:
  127. print ("Error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement