Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #****************************************************
- # Import Package
- #****************************************************
- import time
- import RPi.GPIO as GPIO
- import http.client
- import urllib
- import json
- import sys
- sys.path.append('/home/pi/rpi/code/Package')
- #****************************************************
- # Set GPIO Pin, MediaTek Cloud Sandbox (MCS) Key
- #****************************************************
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(7, GPIO.IN)
- GPIO.setup(11, GPIO.IN)
- GPIO.setup(12, GPIO.IN)
- GPIO.setup(13, GPIO.IN)
- GPIO.setup(15, GPIO.IN)
- GPIO.setup(16, GPIO.IN)
- GPIO.setup(18, GPIO.IN)
- GPIO.setup(22, GPIO.IN)
- deviceId = "DwmmhQZ7"
- deviceKey = "y1TM6og32xfG8e0t"
- #****************************************************
- # Set MediaTek Cloud Sandbox (MCS) Connection
- #****************************************************
- def post_to_mcs(payload):
- headers = {"Content-type": "application/json", "deviceKey": deviceKey}
- not_connected = 1
- while (not_connected):
- try:
- conn = http.client.HTTPConnection("api.mediatek.com:80")
- conn.connect()
- not_connected = 0
- except (http.client.HTTPException, socket.error) as ex:
- print("Error: %s" % ex)
- time.sleep(10) # sleep 10 seconds
- conn.request("POST", "/mcs/v2/devices/" + deviceId + "/datapoints", json.dumps(payload), headers)
- response = conn.getresponse()
- print( response.status, response.reason, json.dumps(payload), time.strftime("%c"))
- data = response.read()
- conn.close()
- #****************************************************
- # Post MediaTek Cloud Sandbox (MCS)
- #****************************************************
- fi = open("resistance.dat","w")
- try:
- for x in range(0, 50):
- a0 = GPIO.input(7)
- a1 = GPIO.input(11)
- a2 = GPIO.input(12)
- a3 = GPIO.input(13)
- a4 = GPIO.input(15)
- a5 = GPIO.input(16)
- a6 = GPIO.input(18)
- a7 = GPIO.input(22)
- total=a0+(a1*2)+(a2*4)+(a3*8)+(a4*16)+(a5*32)+(a6*64)+(a7*128)
- print(a7,a6,a5,a4,a3,a2,a1,a0,"[",total,"]")
- resistance = total * 1000/255 #1K ohm resistor transform.
- fi.write(str(resistance)+'\n')
- payload = {"datapoints":[{"dataChnId":"variable_resistor","values":{"value":resistance}}]}
- post_to_mcs(payload)
- time.sleep(1)
- except:
- print('stop')
- GPIO.cleanup()
- fi.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement