Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion sample/get_cpu_temp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
while true
do
# /usr/bin/powermetrics -b 1 -n 1 -s tasks --show-process-energy --show-process-io | sed -l "s/^/$(date +%Y-%m-%dT%H:%M:%S" "%Z) /" >> ./data/process_energy_io.dat
/usr/bin/powermetrics -b 1 -n 1 -s smc | grep "CPU Thermal level" | sed -l "s/^/$(date +%Y-%m-%dT%H:%M:%S" "%Z) /" >> ./data/cpu_thermal.dat
#/usr/bin/powermetrics -b 1 -n 1 -s smc
power
| grep "CPU Thermal level"
| sed -l "s/^/$(date +%Y-%m-%dT%H:%M:%S" "%Z) /"
>> ./data/cpu_thermal.dat
sleep 10
done
Binary file added work/.DS_Store
Binary file not shown.
Binary file added work/2023/.DS_Store
Binary file not shown.
Binary file added work/2023/ne211028/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file added work/2023/ne211028/src/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions work/2023/ne211028/src/cpu_temp.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2023-09-06T12:16:31 JST, temp=45.1'C
2023-09-06T12:16:34 JST, temp=44.5'C
2023-09-06T12:16:37 JST, temp=45.1'C
12 changes: 12 additions & 0 deletions work/2023/ne211028/src/get_cpu_temp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

while true
do
# /usr/bin/powermetrics -b 1 -n 1 -s tasks --show-process-energy --show-process-io | sed -l "s/^/$(date +%Y-%m-%dT%H:%M:%S" "%Z) /" >> ./data/process_energy_io.dat
# /usr/bin/powermetrics -b 1 -n 1 -s smc | grep "CPU Thermal level" | sed -l "s/^/$(date +%Y-%m-%dT%H:%M:%S" "%Z) /" >> ./data/cpu_thermal.dat

# Raspberry PiのCPU温度の取得と保存
vcgencmd measure_temp | sed "s/^/$(date +%Y-%m-%dT%H:%M:%S" "%Z), /" >> cpu_temp.dat

sleep 3
done
41 changes: 41 additions & 0 deletions work/2023/ne211028/src/get_volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from pydub import AudioSegment
import math
import time

def write_message(sound):
if sound >= 60:
print("基準値以上の音声を感知しました")

def get_audio_volume_transitions(m4a_file, interval=1):
try:
# M4Aファイルを読み込みます
audio = AudioSegment.from_file(m4a_file, format="m4a")

# サンプリング間隔(秒)を指定します
sample_interval = interval * 1000 # ミリ秒単位に変換

for t in range(0, len(audio), sample_interval):
# 指定した時間刻みでの音声データを切り出します
segment = audio[t:t + sample_interval]

# 音量を取得します
rms = segment.rms
# 音量をデシベル単位に変換します
volume_db = 20 * math.log10(rms)

# 音量と時間(秒)を出力します
print(f"時間: {t / 1000:.2f}秒, 音量(デシベル単位): {volume_db:.2f} dB")
#音量が基準を超えているか判定
write_message(volume_db)

# 次のサンプリングまで待機します
time.sleep(interval)

except KeyboardInterrupt:
# Ctrl+Cが押された場合、プログラムを終了します
pass

if __name__ == "__main__":
m4a_file = "sensorlog_20230908.m4a" # 分析したいM4Aファイルへのパスを指定してください
interval = 1 # サンプリング間隔(秒)を設定します
get_audio_volume_transitions(m4a_file, interval)
Binary file not shown.
Binary file added work/2023/ne211028/src/sensorlog_20230908.m4a
Binary file not shown.
82 changes: 82 additions & 0 deletions work/2023/ne211028/src/update_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import requests #httpリクエストを行うためのモジュール
import re #正規表現を使うためのモジュールをインポート
import time

api_key = 'V4S9V18KFNIR8QDI' # 作成したチャンネルのAPIキー
channle_id = '2261964' # 作成したチャンネルのID
data_file = "cpu_temp.dat"

_ts_base_url = "https://api.thingspeak.com"
ts_update_url = _ts_base_url + "/update"
# GET https://api.thingspeak.com/update?api_key=MSUJ80Z21B6XIS7G&field1=0

# HTTPでのデータ登録のための設定
headers = {'X-THINGSPEAKAPIKEY': api_key}


#------
# powermetricsで取得したCPU die tempartureの値を取得して、配列を返す
# 引数:データが入ったファイルのパス
# return : cpu_temp リスト(配列)
# 2021-09-07T23:59:36 JST CPU die temperature: 69.77 C
#------
def getCpuTempFromFile(filename):

_cpu_temps = []

# ファイルの存在を確認
is_file = os.path.exists(filename)
if not is_file:
print("正しいファイル名を指定してください。")
sys.exit(1)

# ファイルを開いてデータを取得
with open(filename) as f:
_lines = f.readlines()
for _line in _lines: # 配列を1行ずつ取り出す
_data = _line.split() # ファイルの1行を空白で区切って配列にする
print(_data) # 1行の中身を確認
# ここで、1行のデータのどの部分をどう使うか考えて処理する
res = re.match(r'temp=([0-9.]*)\'C', _data[2]) #正規表現を使って温度を取得
if res:
print(res.group(1)) #マッチしたパターンのうち、()の部分を取り出し
_cpu_temps.append(res.group(1)) # 取得したパターンを結果用の配列に追記する

return _cpu_temps

#------
# 指定したデータをThingSpeakに登録
# 引数:req_url, headers, post_data
#------
def post2ThingSpeak(req_url, headers, post_data):
while True:
response = requests.post(req_url, headers=headers, data=post_data)
if response.text != '0':
break
# time.sleep(1)


# メイン処理
cpu_temps = []

print(data_file + " のデータをThingSpeakに登録します。")
# CPU温度の情報をファイルから取得
cpu_temps = getCpuTempFromFile(data_file)

print("CPU温度のデータが " + str(len(cpu_temps)) + " 件あります。")
# データの中身をすべて表示
print(cpu_temps)

# 最新のデータ(一番最後)をThingSpeakに登録
# 登録するデータを設定

for i in range(len(cpu_temps)):
post_data = {'field1': cpu_temps[i]}
post2ThingSpeak(ts_update_url, headers, post_data)
print("CPU温度:" + str(cpu_temps[i]) + " を登録しました。")
sys.exit(0)
82 changes: 82 additions & 0 deletions work/2023/ne211028/src/volume_to_Thingspeak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import requests #httpリクエストを行うためのモジュール
import re #正規表現を使うためのモジュールをインポート
import time

api_key = '' # 作成したチャンネルのAPIキー
channle_id = '' # 作成したチャンネルのID
data_file = ""

_ts_base_url = "https://api.thingspeak.com"
ts_update_url = _ts_base_url + "/update"
# GET https://api.thingspeak.com/update?api_key=MSUJ80Z21B6XIS7G&field1=0

# HTTPでのデータ登録のための設定
headers = {'X-THINGSPEAKAPIKEY': api_key}


#------
# powermetricsで取得したCPU die tempartureの値を取得して、配列を返す
# 引数:データが入ったファイルのパス
# return : cpu_temp リスト(配列)
# 2021-09-07T23:59:36 JST CPU die temperature: 69.77 C
#------
def getCpuTempFromFile(filename):

_cpu_temps = []

# ファイルの存在を確認
is_file = os.path.exists(filename)
if not is_file:
print("正しいファイル名を指定してください。")
sys.exit(1)

# ファイルを開いてデータを取得
with open(filename) as f:
_lines = f.readlines()
for _line in _lines: # 配列を1行ずつ取り出す
_data = _line.split() # ファイルの1行を空白で区切って配列にする
print(_data) # 1行の中身を確認
# ここで、1行のデータのどの部分をどう使うか考えて処理する
res = re.match(r'temp=([0-9.]*)\'C', _data[2]) #正規表現を使って温度を取得
if res:
print(res.group(1)) #マッチしたパターンのうち、()の部分を取り出し
_cpu_temps.append(res.group(1)) # 取得したパターンを結果用の配列に追記する

return _cpu_temps

#------
# 指定したデータをThingSpeakに登録
# 引数:req_url, headers, post_data
#------
def post2ThingSpeak(req_url, headers, post_data):
while True:
response = requests.post(req_url, headers=headers, data=post_data)
if response.text != '0':
break
# time.sleep(1)


# メイン処理
cpu_temps = []

print(data_file + " のデータをThingSpeakに登録します。")
# CPU温度の情報をファイルから取得
cpu_temps = getCpuTempFromFile(data_file)

print("CPU温度のデータが " + str(len(cpu_temps)) + " 件あります。")
# データの中身をすべて表示
print(cpu_temps)

# 最新のデータ(一番最後)をThingSpeakに登録
# 登録するデータを設定

for i in range(len(cpu_temps)):
post_data = {'field1': cpu_temps[i]}
post2ThingSpeak(ts_update_url, headers, post_data)
print("CPU温度:" + str(cpu_temps[i]) + " を登録しました。")
sys.exit(0)