Last active
August 7, 2019 01:43
-
-
Save astrophysik928/d4206a9f46d68fd8504e1d2e68a70c19 to your computer and use it in GitHub Desktop.
登山情報発信システム
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from gps3 import gps3 | |
from time import sleep | |
import base64 | |
import datetime | |
import subprocess | |
import sys | |
import time | |
import commands | |
import ambient | |
import RPi.GPIO as GPIO | |
import os | |
# twitterの接続定義 | |
CONSUMER_KEY = "**************" | |
CONSUMER_SECRET = "**************" | |
ACCESS_TOKEN = "**************" | |
ACCESS_TOKEN_SECRET = "**************" | |
url_text = 'https://api.twitter.com/1.1/statuses/update.json' | |
url_media = 'https://upload.twitter.com/1.1/media/upload.json' | |
twitter = OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) | |
twitter_post_number = "初期値" | |
#気温センサーの定義 | |
SENSOR_ID = "28-030197793676" | |
SENSOR_W1_SLAVE = "/sys/bus/w1/devices/" + SENSOR_ID + "/w1_slave" | |
ERR_VAL = 85000 | |
#GPSの接続定義 | |
gps_socket = gps3.GPSDSocket() | |
data_stream = gps3.DataStream() | |
gps_socket.connect() | |
gps_socket.watch() | |
#プログラム起動中にシャットダウン | |
def shutdown(channel): | |
print("shutdown!!") | |
os.system("sudo shutdown -h now") | |
#GPIOの定義(タクトスイッチが押されたイベントを検出するため) | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.add_event_detect(18, GPIO.FALLING, callback=shutdown, bouncetime=300) | |
#画像の撮影 | |
def do_camera(): | |
check = commands.getoutput("sudo bash /home/pi/action/camera.sh") | |
print check | |
print "taking picture is completed!!" | |
#撮影した画像を取得 | |
def get_image(): | |
picture_path = "/home/pi/imageBox/image.jpg" | |
return picture_path | |
#DS18B20センサーの気温データを取得 | |
def get_temperature(): | |
try: | |
res = subprocess.check_output(["cat", SENSOR_W1_SLAVE]) | |
except: | |
res = None | |
if res is not None: | |
temp_val = res.split("=") | |
if temp_val[-1] == ERR_VAL: | |
print "Got value:85000. Circuit is ok, but something wrong happens..." | |
sys.exit(1) | |
temp_val = round(float(temp_val[-1]) / 1000, 1) | |
return temp_val | |
else: | |
return "nothing" | |
sys.exit(1) | |
#GPSデータを取得 | |
def get_gps_data(): | |
for new_data in gps_socket: | |
if new_data: | |
data_stream.unpack(new_data) | |
time = data_stream.TPV['time'] | |
lat = data_stream.TPV['lat'] | |
lon = data_stream.TPV['lon'] | |
alt = data_stream.TPV['alt'] | |
#緯度lat 経度lon 海抜alt | |
gps_dictionary = { | |
'time': time, | |
'lat' : lat, | |
'lon' : lon, | |
'alt' : alt | |
} | |
return gps_dictionary | |
#twitterに投稿 | |
def do_twitter(text): | |
#twitter_post_numberはグローバル変数である | |
global twitter_post_number | |
#画像投稿 | |
picture = get_image() | |
files = {"media" : open(picture, 'rb')} | |
req_media = twitter.post(url_media, files = files) | |
# レスポンスを確認 | |
if req_media.status_code != 200: | |
print ("画像アップデート失敗: %s", req_media.text) | |
exit() | |
# Media ID を取得 | |
media_id = json.loads(req_media.text)['media_id'] | |
#print ("Media ID: %d" % media_id) | |
# Media ID を付加してテキストを投稿 | |
params = {'status': text, "media_ids": [media_id]} | |
req_media = twitter.post(url_text, params = params) | |
#スプレッドシートに書き込むための代入 | |
twitter_post_number = str(media_id) | |
# 再びレスポンスを確認 | |
if req_media.status_code != 200: | |
print ("テキストアップデート失敗: %s", req_text.text) | |
exit() | |
print ("OK") | |
#main関数 | |
if __name__ == '__main__': | |
count = 1 | |
while True: | |
sw_status = GPIO.input(18) | |
gps_data = get_gps_data() | |
if sw_status == 0: | |
print("finished") | |
break | |
print('日付') | |
print(gps_data['time']) | |
print('') | |
print('緯度') | |
print(gps_data['lat']) | |
print('') | |
print('経度') | |
print(gps_data['lon']) | |
print('') | |
print('海抜') | |
print(gps_data['alt']) | |
print('') | |
print('') | |
#画像を撮影 | |
do_camera() | |
tweet_text = "日時 " + str(gps_data['time']) + " 気温 " + str(get_temperature()) + "度" + " 緯度 " + str(gps_data['lat']) + " 経度 " + str(gps_data['lon']) + " 高度 " + str(gps_data['alt']) | |
#twitterにアップ | |
sleep(20) | |
do_twitter(tweet_text) | |
#ambientにデータをアップ | |
sleep(20) | |
ambi = ambient.Ambient(123456, "***********") # 自分のチャネルID、ライトキー | |
r = ambi.send({'created': 'YYYY-MM-DD HH:mm:ss.sss','d1': get_temperature(),'d2': gps_data['alt'],'lat': gps_data['lat'],'lng': gps_data['lon']}) | |
r.close() | |
print str(count) + "updateing ambient is finished!!" | |
count = count + 1 | |
#4分おきに作動 | |
sleep(240) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment