#!/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() #

[((175, 187), 'json.load', 'json.load', (['f'], {}), '(f)\n', (184, 187), False, 'import json\n'), ((954, 1021), 'twython.Twython', 'Twython', (['CONSUMER_KEY', 'CONSUMER_SECRET', 'ACCESS_TOKEN', 'ACCESS_SECRET'], {}), '(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET)\n', (961, 1021), False, 'from twython import Twython\n'), ((1038, 1060), 'RPi.GPIO.setmode', 'GPIO.setmode', (['GPIO.BCM'], {}), '(GPIO.BCM)\n', (1050, 1060), True, 'import RPi.GPIO as GPIO\n'), ((1061, 1085), 'RPi.GPIO.setup', 'GPIO.setup', (['(25)', 'GPIO.OUT'], {}), '(25, GPIO.OUT)\n', (1071, 1085), True, 'import RPi.GPIO as GPIO\n'), ((1086, 1146), 'RPi.GPIO.setup', 'GPIO.setup', (['trigger_input', 'GPIO.IN'], {'pull_up_down': 'GPIO.PUD_UP'}), '(trigger_input, GPIO.IN, pull_up_down=GPIO.PUD_UP)\n', (1096, 1146), True, 'import RPi.GPIO as GPIO\n'), ((1148, 1245), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (['trigger_input', 'GPIO.RISING'], {'callback': 'on_positive_edge', 'bouncetime': '(1000)'}), '(trigger_input, GPIO.RISING, callback=on_positive_edge,\n    bouncetime=1000)\n', (1169, 1245), True, 'import RPi.GPIO as GPIO\n'), ((1344, 1358), 'RPi.GPIO.cleanup', 'GPIO.cleanup', ([], {}), '()\n', (1356, 1358), True, 'import RPi.GPIO as GPIO\n'), ((849, 874), 'RPi.GPIO.output', 'GPIO.output', (['(25)', 'ledstate'], {}), '(25, ledstate)\n', (860, 874), True, 'import RPi.GPIO as GPIO\n'), ((1293, 1304), 'time.sleep', 'sleep', (['(0.01)'], {}), '(0.01)\n', (1298, 1304), False, 'from time import sleep\n'), ((477, 496), 'os.popen', 'os.popen', (['timestamp'], {}), '(timestamp)\n', (485, 496), False, 'import os\n'), ((601, 614), 'os.popen', 'os.popen', (['cmd'], {}), '(cmd)\n', (609, 614), False, 'import os\n')]