Last active
March 24, 2016 17:38
-
-
Save yoshidaken1/e04c8b9da2b974c3228f 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
#coding: utf-8 | |
# spi, time ライブラリをインポート | |
import spidev | |
import time | |
# コマンド実行ライブラリ | |
import subprocess | |
# センサーデータのJSON化のためにインポート | |
import json | |
from datetime import datetime | |
# SpiDev オブジェクトのインスタンスを生成 | |
spi = spidev.SpiDev() | |
# ポート0、デバイス0のSPI をオープン | |
spi.open(0, 0) | |
# 最大クロックスピードを1MHz に設定 | |
spi.max_speed_hz=1000000 | |
# 1 ワードあたり8ビットに設定 | |
spi.bits_per_word=8 | |
# ダミーデータを設定(1111 1111) | |
dummy = 0xff | |
# スタートビットを設定(0100 0111) | |
start = 0x47 | |
# シングルエンドモードを設定 (0010 0000) | |
sgl = 0x20 | |
# ch0 を選択(0000 0000) | |
ch0 = 0x00 | |
# ch1 を選択(0001 0000) | |
ch1 = 0x10 | |
# MSB ファーストモードを選択(0000 1000) | |
msbf = 0x08 | |
# IC からデータを取得する関数を定義 | |
def measure(ch): | |
# SPI インターフェイスでデータの送受信を行う | |
ad = spi.xfer2( [ (start + sgl + ch + msbf), dummy ] ) | |
# | |
val = ((ad[0] & 0x03) << 8) + ad[1] | |
# 受信した2バイトのデータを10 ビットデータにまとめる | |
voltage = ( val * 3.3 ) / 1023 | |
# 結果を返す | |
return val, voltage | |
# 宛先 000の部分を自分の番号に変更する | |
topic='spinning/3f/brightness000' | |
# 例外を検して終了 | |
try: | |
# 無限ループ | |
while 1: | |
# 関数を呼び出してch1 のデータを取得 | |
ch1_val, ch1_voltage = measure(ch1) | |
# 結果を表示 | |
#print 'ch1 = {:4d}, {:2.2f}[V]'.format(ch1_val, ch1_voltage) | |
# センサーデータをJSON化するため辞書に入れる。後々のデータ処理に用いる | |
# JSONデータ構造 {"brightness": ch1_val, "ID": "id000", "time_sensor": "2015-10-15 16:21:56"} | |
# "ID": "id000"の000の部分の自分の番号に変える | |
dict = {"time_sensor": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "ID": "id000", "brightness": ch1_val} | |
message = json.dumps(dict, ensure_ascii=False) | |
#JSONデータを出力。リダイレクトしてファイルに保存可能 | |
#コマンド例 | |
# $ sudo python pub_light.py >> bightness000.json | |
print message | |
# 以下の設定はローカルのMosquittoに送信する場合に使用します。 | |
# mosquitto_pub -t $topic -m $val | |
args = ['mosquitto_pub','-t', topic, '-m', message] | |
#実行コマンドの表示(配列を結合) | |
#print " ".join(args) | |
subprocess.call(args) | |
# 10秒待つ | |
time.sleep(10) | |
# キーボード例外を検出 | |
except KeyboardInterrupt: | |
# 何も処理をしない | |
pass | |
# SPI を開放 | |
spi.close() |
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 | |
# spi, time ライブラリをインポート | |
import spidev | |
import time | |
# コマンド実行ライブラリ | |
import subprocess | |
# センサーデータのJSON化のためにインポート | |
import json | |
from datetime import datetime | |
# SpiDev オブジェクトのインスタンスを生成 | |
spi = spidev.SpiDev() | |
# ポート0、デバイス0のSPI をオープン | |
spi.open(0, 0) | |
# 最大クロックスピードを1MHz に設定 | |
spi.max_speed_hz=1000000 | |
# 1 ワードあたり8ビットに設定 | |
spi.bits_per_word=8 | |
# ダミーデータを設定(1111 1111) | |
dummy = 0xff | |
# スタートビットを設定(0100 0111) | |
start = 0x47 | |
# シングルエンドモードを設定 (0010 0000) | |
sgl = 0x20 | |
# ch0 を選択(0000 0000) | |
ch0 = 0x00 | |
# ch1 を選択(0001 0000) | |
ch1 = 0x10 | |
# MSB ファーストモードを選択(0000 1000) | |
msbf = 0x08 | |
# IC からデータを取得する関数を定義 | |
def measure(ch): | |
# SPI インターフェイスでデータの送受信を行う | |
ad = spi.xfer2( [ (start + sgl + ch + msbf), dummy ] ) | |
# | |
val = ((ad[0] & 0x03) << 8) + ad[1] | |
# 受信した2バイトのデータを10 ビットデータにまとめる | |
voltage = ( val * 3.3 ) / 1023 | |
# 結果を返す | |
return val, voltage | |
# 宛先 00の部分を自分の番号に変更する | |
topic='spinning/3f/brightness000' | |
# sangoへの接続設定 | |
#username='yourid@github' | |
#password='yourpssword' | |
#hostname='lite.mqtt.shiguredo.jp' | |
sango_topic= username + '/' + topic | |
# 例外を検して終了 | |
try: | |
# 無限ループ | |
while 1: | |
# 関数を呼び出してch1 のデータを取得 | |
ch1_val, ch1_voltage = measure(ch1) | |
# 結果を表示 | |
#print 'ch1 = {:4d}, {:2.2f}[V]'.format(ch1_val, ch1_voltage) | |
# JSONデータ構造 {"brightness": ch1_val, "ID": "id000", "time_sensor": "2015-10-15 16:21:56"} | |
# "ID": "id000"の000の部分の自分の番号に変える | |
dict = {"time_sensor": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "ID": "id000", "brightness": ch1_val} | |
message = json.dumps(dict, ensure_ascii=False) | |
# 以下の設定はsangoに送信する場合に使用してください。 | |
# username, passwordは事前に上部にある変数宣言部分を書き換えてください。 | |
# mosquitto_pub -t $sango_topic -m $temp -u $username -P $password -h $hostname | |
#args = ['mosquitto_pub', '-t', sango_topic, '-m', message, '-u', username, '-P', password, '-h', hostname] | |
# 以下の設定はSORACOM Beam経由でsangoに送信する場合に使用してください。 | |
# 上部にある変数宣言部分で、username内のaccount_nameをgithubのユーザー名に書き換えてください。 | |
# mosquitto_pub -t $sango_topic -m $temp -h beam.soracom.io | |
args = ['mosquitto_pub', '-t', sango_topic, '-m', message, '-h', 'beam.soracom.io'] | |
#実行コマンドの表示(配列を結合) | |
print " ".join(args) | |
subprocess.call(args) | |
# 15秒待つ | |
time.sleep(15) | |
# キーボード例外を検出 | |
except KeyboardInterrupt: | |
# 何も処理をしない | |
pass | |
# SPI を開放 | |
spi.close() |
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 | |
# spi, time ライブラリをインポート | |
import spidev | |
import time | |
# コマンド実行ライブラリ | |
import subprocess | |
# センサーデータのJSON化のためにインポート | |
import json | |
from datetime import datetime | |
# SpiDev オブジェクトのインスタンスを生成 | |
spi = spidev.SpiDev() | |
# ポート0、デバイス0のSPI をオープン | |
spi.open(0, 0) | |
# 最大クロックスピードを1MHz に設定 | |
spi.max_speed_hz=1000000 | |
# 1 ワードあたり8ビットに設定 | |
spi.bits_per_word=8 | |
# ダミーデータを設定(1111 1111) | |
dummy = 0xff | |
# スタートビットを設定(0100 0111) | |
start = 0x47 | |
# シングルエンドモードを設定 (0010 0000) | |
sgl = 0x20 | |
# ch0 を選択(0000 0000) | |
ch0 = 0x00 | |
# ch1 を選択(0001 0000) | |
ch1 = 0x10 | |
# MSB ファーストモードを選択(0000 1000) | |
msbf = 0x08 | |
# IC からデータを取得する関数を定義 | |
def measure(ch): | |
# SPI インターフェイスでデータの送受信を行う | |
ad = spi.xfer2( [ (start + sgl + ch + msbf), dummy ] ) | |
# | |
val = ((ad[0] & 0x03) << 8) + ad[1] | |
# 受信した2バイトのデータを10 ビットデータにまとめる | |
voltage = ( val * 3.3 ) / 1023 | |
# 結果を返す | |
return val, voltage | |
# 宛先 000の部分を自分の番号に変更する | |
topic='spinning/3f/brightness000' | |
# sangoへの接続設定 | |
username='yourid@github' | |
password='yourpssword' | |
hostname='lite.mqtt.shiguredo.jp' | |
sango_topic= username + '/' + topic | |
# 例外を検して終了 | |
try: | |
# 無限ループ | |
while 1: | |
# 関数を呼び出してch1 のデータを取得 | |
ch1_val, ch1_voltage = measure(ch1) | |
# 結果を表示 | |
#print 'ch1 = {:4d}, {:2.2f}[V]'.format(ch1_val, ch1_voltage) | |
# JSONデータ構造 {"brightness": ch1_val, "ID": "id000", "time_sensor": "2015-10-15 16:21:56"} | |
# "ID": "id000"の000の部分の自分の番号に変える | |
dict = {"time_sensor": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "ID": "id000", "brightness": ch1_val} | |
message = json.dumps(dict, ensure_ascii=False) | |
# 以下の設定はsangoに送信する場合に使用してください。 | |
# username, passwordは事前に上部にある変数宣言部分を書き換えてください。 | |
# mosquitto_pub -t $sango_topic -m $temp -u $username -P $password -h $hostname | |
args = ['mosquitto_pub', '-t', sango_topic, '-m', message, '-u', username, '-P', password, '-h', hostname] | |
#実行コマンドの表示(配列を結合) | |
print " ".join(args) | |
subprocess.call(args) | |
# 15秒待つ | |
time.sleep(15) | |
# キーボード例外を検出 | |
except KeyboardInterrupt: | |
# 何も処理をしない | |
pass | |
# SPI を開放 | |
spi.close() |
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 ruby | |
require 'soracom' | |
# SORACOM APIアクセス用クライアントの初期化方法 | |
# 1. client = Soracom::Client.new(email: '登録メールアドレス', password: 'パスワード') | |
# 2. client = Soracom::Client.new | |
# (環境変数 SORACOM_EMAIL & SORACOM_PASSWORD を参照) | |
client = Soracom::Client.new | |
# サブスクライバー(SIM)の一覧を取得 | |
##sims = client.list_subscribers | |
##puts "found #{sims.count} SIMs." | |
# 操作対象のIMSI配列を用意 | |
##imsis = sims.map { |sim| sim['imsi'] } | |
##puts 'change plan to s1.fast' | |
# プラン変更のためのAPIをコールする | |
##client.update_subscriber_speed_class(imsis, 's1.slow') | |
myImsi = '123456789012345'; # ご自身のSIMのIMSI番号(15ケタの番号)を文字列として記述 | |
client.update_subscriber_speed_class(myImsi, 's1.standard'); | |
# activate_subscriberメソッドで使用開始にする | |
client.activate_subscriber(myImsi); | |
puts 'done' |
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 | |
# 参考:「みんなのRaspberry Pi入門」リックテレコム 石井もルナ・江崎徳秀 著 | |
# spi, time ライブラリをインポート | |
import spidev | |
import time | |
# SpiDev オブジェクトのインスタンスを生成 | |
spi = spidev.SpiDev() | |
# ポート0、デバイス0のSPI をオープン | |
spi.open(0, 0) | |
# 最大クロックスピードを1MHz に設定 | |
spi.max_speed_hz=1000000 | |
# 1 ワードあたり8ビットに設定 | |
spi.bits_per_word=8 | |
# ダミーデータを設定(1111 1111) | |
dummy = 0xff | |
# スタートビットを設定(0100 0111) | |
start = 0x47 | |
# シングルエンドモードを設定 (0010 0000) | |
sgl = 0x20 | |
# ch0 を選択(0000 0000) | |
ch0 = 0x00 | |
# ch1 を選択(0001 0000) | |
ch1 = 0x10 | |
# MSB ファーストモードを選択(0000 1000) | |
msbf = 0x08 | |
# IC からデータを取得する関数を定義 | |
def measure(ch): | |
# SPI インターフェイスでデータの送受信を行う | |
ad = spi.xfer2( [ (start + sgl + ch + msbf), dummy ] ) | |
# | |
val = ((ad[0] & 0x03) << 8) + ad[1] | |
# 受信した2バイトのデータを10 ビットデータにまとめる | |
voltage = ( val * 3.3 ) / 1023 | |
# 結果を返す | |
return val, voltage | |
# 例外を検出 | |
try: | |
# 無限ループ | |
while 1: | |
# 関数を呼び出してch0 のデータを取得 | |
ch0_val, ch0_voltage = measure(ch0) | |
# 関数を呼び出してch1 のデータを取得 | |
ch1_val, ch1_voltage = measure(ch1) | |
# 結果を表示 | |
print 'ch0 = {:4d}, {:2.2f}[V], ch1 = {:4d}, {:2.2f}[V]'.format(ch0_val, ch0_voltage, ch1_val, ch1_voltage) | |
# 0.5 秒待つ | |
time.sleep(0.5) | |
# キーボード例外を検出 | |
except KeyboardInterrupt: | |
# 何も処理をしない | |
pass | |
# SPI を開放 | |
spi.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment