Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. #****************************************************
  2. # Import Package
  3. #****************************************************
  4.  
  5. import time
  6. import RPi.GPIO as GPIO
  7. import http.client
  8. import urllib
  9. import json
  10. import sys
  11. sys.path.append('/home/pi/rpi/code/Package')
  12.  
  13. #****************************************************
  14. # Set GPIO Pin, MediaTek Cloud Sandbox (MCS) Key
  15. #****************************************************
  16.  
  17. GPIO.setmode(GPIO.BOARD)
  18. GPIO.setup(7, GPIO.IN)
  19. GPIO.setup(11, GPIO.IN)
  20. GPIO.setup(12, GPIO.IN)
  21. GPIO.setup(13, GPIO.IN)
  22. GPIO.setup(15, GPIO.IN)
  23. GPIO.setup(16, GPIO.IN)
  24. GPIO.setup(18, GPIO.IN)
  25. GPIO.setup(22, GPIO.IN)
  26.  
  27. deviceId = "DwmmhQZ7"
  28. deviceKey = "y1TM6og32xfG8e0t"
  29.  
  30. #****************************************************
  31. # Set MediaTek Cloud Sandbox (MCS) Connection
  32. #****************************************************
  33. def post_to_mcs(payload):
  34. headers = {"Content-type": "application/json", "deviceKey": deviceKey}
  35. not_connected = 1
  36. while (not_connected):
  37. try:
  38. conn = http.client.HTTPConnection("api.mediatek.com:80")
  39. conn.connect()
  40. not_connected = 0
  41. except (http.client.HTTPException, socket.error) as ex:
  42. print("Error: %s" % ex)
  43. time.sleep(10) # sleep 10 seconds
  44.  
  45. conn.request("POST", "/mcs/v2/devices/" + deviceId + "/datapoints", json.dumps(payload), headers)
  46. response = conn.getresponse()
  47. print( response.status, response.reason, json.dumps(payload), time.strftime("%c"))
  48. data = response.read()
  49. conn.close()
  50.  
  51. #****************************************************
  52. # Post MediaTek Cloud Sandbox (MCS)
  53. #****************************************************
  54. fi = open("resistance.dat","w")
  55. try:
  56. for x in range(0, 50):
  57. a0 = GPIO.input(7)
  58. a1 = GPIO.input(11)
  59. a2 = GPIO.input(12)
  60. a3 = GPIO.input(13)
  61. a4 = GPIO.input(15)
  62. a5 = GPIO.input(16)
  63. a6 = GPIO.input(18)
  64. a7 = GPIO.input(22)
  65.  
  66. total=a0+(a1*2)+(a2*4)+(a3*8)+(a4*16)+(a5*32)+(a6*64)+(a7*128)
  67. print(a7,a6,a5,a4,a3,a2,a1,a0,"[",total,"]")
  68. resistance = total * 1000/255 #1K ohm resistor transform.
  69. fi.write(str(resistance)+'\n')
  70. payload = {"datapoints":[{"dataChnId":"variable_resistor","values":{"value":resistance}}]}
  71. post_to_mcs(payload)
  72. time.sleep(1)
  73. except:
  74. print('stop')
  75. GPIO.cleanup()
  76.  
  77. fi.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement