Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yamaryu0508
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaryu0508/2b8cb09565156b3bb33b to your computer and use it in GitHub Desktop.
Save yamaryu0508/2b8cb09565156b3bb33b to your computer and use it in GitHub Desktop.
Raspberry Pi上にアタッチした温度センサ(ADT7410)からの値をTypetalkに渡すサンプル
# 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