Last active
August 29, 2015 14:02
-
-
Save yamaryu0508/2b8cb09565156b3bb33b to your computer and use it in GitHub Desktop.
Raspberry Pi上にアタッチした温度センサ(ADT7410)からの値をTypetalkに渡すサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import smbus | |
import time | |
import requests | |
# config for I2C and ADT7410 | |
i2c = smbus.SMBus(1) | |
address = 0x48 | |
# config for typetalk | |
clientId = 'your clientId' | |
clientSecret = 'clientSecret' | |
topicId = 'topicId' | |
while True: | |
# process of temperature sensoring | |
block = i2c.read_i2c_block_data(address, 0x00, 12) | |
temp = (block[0] << 8 | block[1]) >> 3 | |
if(temp >= 4096): | |
temp -= 8192 | |
value = temp / 16.0 | |
msg = "Current temperature:%6.2f [Deg. C.] " % value | |
print(msg) | |
# access token | |
res = requests.post("https://typetalk.in/oauth2/access_token", { | |
'client_id': clientId, | |
'client_secret': clientSecret, | |
'grant_type': 'client_credentials', | |
'scope': 'topic.post' | |
}) | |
accessToken = res.json()['access_token'] | |
# post to Typetalk | |
requests.post('https://typetalk.in/api/v1/topics/'+topicId,{ | |
'message': msg | |
}, headers={ | |
'Authorization':'Bearer '+accessToken | |
}) | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment