#!/usr/bin/env python
#coding:utf-8

import os
import RPi.GPIO as GPIO #
import json
from time import sleep #
from twython import Twython

f=open("tw_config.json",'r')
config=json.load(f)
f.close()

CONSUMER_KEY =config['consumer_key']
CONSUMER_SECRET =config['consumer_secret']
ACCESS_TOKEN =config['access_token']
ACCESS_SECRET =config['access_secret']

dist=config['dist']

def on_positive_edge(channel):
    #time stamp
    timestamp = 'date +%F_%H:%M:%S'
    current_time=os.popen(timestamp).readline().strip()


    # get CPU temperature
    cmd = '/opt/vc/bin/vcgencmd measure_temp'
    line = os.popen(cmd).readline().strip()
    temp = line.split('=')[1].split("'")[0]

    direct_message='CPU:'+temp+'deg @'+current_time+'  : by Python script'


    global ledstate
    if channel == trigger_input:
        ledstate = not ledstate
        GPIO.output(25, ledstate)

        api.send_direct_message(text=direct_message ,screen_name=dist)

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_TOKEN,ACCESS_SECRET)

trigger_input=21

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(trigger_input, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.add_event_detect(trigger_input, GPIO.RISING, callback=on_positive_edge, bouncetime=1000)

ledstate = GPIO.LOW

try:
    while True:
        sleep(0.01)

except KeyboardInterrupt: #
    pass

GPIO.cleanup() #

[((11, 7, 11, 19), 'json.load', 'json.load', ({(11, 17, 11, 18): 'f'}, {}), '(f)', False, 'import json\n'), ((42, 6, 42, 70), 'twython.Twython', 'Twython', ({(42, 14, 42, 26): 'CONSUMER_KEY', (42, 27, 42, 42): 'CONSUMER_SECRET', (42, 43, 42, 55): 'ACCESS_TOKEN', (42, 56, 42, 69): 'ACCESS_SECRET'}, {}), '(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET)', False, 'from twython import Twython\n'), ((46, 0, 46, 22), 'RPi.GPIO.setmode', 'GPIO.setmode', ({(46, 13, 46, 21): 'GPIO.BCM'}, {}), '(GPIO.BCM)', True, 'import RPi.GPIO as GPIO\n'), ((47, 0, 47, 24), 'RPi.GPIO.setup', 'GPIO.setup', ({(47, 11, 47, 13): '(25)', (47, 15, 47, 23): 'GPIO.OUT'}, {}), '(25, GPIO.OUT)', True, 'import RPi.GPIO as GPIO\n'), ((48, 0, 48, 60), 'RPi.GPIO.setup', 'GPIO.setup', (), '', True, 'import RPi.GPIO as GPIO\n'), ((50, 0, 50, 93), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (), '', True, 'import RPi.GPIO as GPIO\n'), ((61, 0, 61, 14), 'RPi.GPIO.cleanup', 'GPIO.cleanup', ({}, {}), '()', True, 'import RPi.GPIO as GPIO\n'), ((38, 8, 38, 33), 'RPi.GPIO.output', 'GPIO.output', ({(38, 20, 38, 22): '(25)', (38, 24, 38, 32): 'ledstate'}, {}), '(25, ledstate)', True, 'import RPi.GPIO as GPIO\n'), ((56, 8, 56, 19), 'time.sleep', 'sleep', ({(56, 14, 56, 18): '(0.01)'}, {}), '(0.01)', False, 'from time import sleep\n'), ((24, 17, 24, 36), 'os.popen', 'os.popen', ({(24, 26, 24, 35): 'timestamp'}, {}), '(timestamp)', False, 'import os\n'), ((29, 11, 29, 24), 'os.popen', 'os.popen', ({(29, 20, 29, 23): 'cmd'}, {}), '(cmd)', False, 'import os\n')]