1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nature Remo 複数台用温度計

Last updated at Posted at 2022-06-24

MicrosoftTeams-image (2).png
↑これは iframeで埋め込んだものです

はじめに

なんとなくできたので

複数台温度計がある理由

  • 室温1、湿度1
  • 室温2
  • 実質外気温1、実質外湿度2
  • それらの温度の最大、最小

を記録したかったので

これの環境

  • Raspberry Pi 4B 8Gモデル FreeBSD (SSD運用)

もう高かったんだからぁ、ロシアさん)

  • Remo = Nature Remo 3W1 (3世代)
  • Remo Mini = Nature Remo Mini (1世代)
  • RemoOut = Nature Remo W1 (1世代)

お古になったラズパイには、raspi-os が入っています

必須なもの

  • jqコマンド
  • curlコマンド

ソース

remo.pl
#!/usr/bin/perl
$ =1;

#実行時刻の取得
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$yyyy = $year + 1900;
$month = $mon + 1;
 
#ファイル指定
$dir = '/home/dirname/htdocs/hometemp';
$ym=sprintf("%04d%02d",$yyyy,$month);
mkdir($ymdir);

$log = "$dir/log/$ym.csv";
$tmp = "$dir/tmp.json";
$html = "$dir/kion.html";

#アクセストークンを指定
$apikey = "アクセストークン";
 
#コマンドと引数を指定
$curl = "/usr/local/bin/curl";
$curlopt = "-X GET";
$url = "https://api.nature.global/1/devices";
$header1 = "-H \"accept: application/json\"";
$header2 = "-H \"Authorization: Bearer $apikey\"";
$jq = "/usr/local/bin/jq";
 
#APIアクセスを実行
print STDERR "[exec]$curl $curlopt $url $header1 $header2\n";
$remoapi = `$curl $curlopt $url $header1 $header2`;

#センサー値の取り出し
my @remo;

# 最大ループ数はここで決め打ちで書いておく
for($loop=0; $loop<=2; $loop++) {

	$name = `echo '$remoapi' |$jq -r .[$loop].name`;
	chomp($name);
	continue if($name eq "");

	$firm = `echo '$remoapi' |$jq -r .[$loop].firmware_version`;
	$te = `echo '$remoapi' |$jq .[$loop].newest_events.te.val`;
	$hu = `echo '$remoapi' |$jq .[$loop].newest_events.hu.val`;
	$il = `echo '$remoapi' |$jq .[$loop].newest_events.il.val`;
	$mo = `echo '$remoapi' |$jq .[$loop].newest_events.mo.val`;

	chomp($firm);
	chomp($te);
	chomp($hu);
	chomp($il);
	chomp($mo);
	push(@remo, "$name\t$te\t$hu");

	#ログを出力
	open(LOG, ">>$log");
	flock(LOG, 2);
	print(LOG "$yyyy/$month/$mday $hour:$min:$sec,name=$name,firm=$firm,te=$te,hu=$hu,il=$il,mo=$mo\n");
	close(LOG);
}

#最高、最低気温の取得
foreach(@remo) {
	($name, $te, $hu)=split(/\t/,$_);
	open(LOG, "$log");
	foreach(<LOG>) {
		chomp;
		if(/^$yyyy\/$month\/$mday /) {
			if(/name\=$name,/) {
				next if(length($_) < 10);
				$tetmp=$_;
				$tetmp=~s/.*te=//g;
				$tetmp=~s/,.*//g;
				if($temax{$name} eq "") {
					$temax{$name}=-1000;
				}
				if($temin{$name} eq "") {
					$temin{$name}=1000;
				}
				if($tetmp > $temax{$name}+0) {
					$temax{$name}=$tetmp;
				}
				if($tetmp < $temin{$name}+0) {
					$temin{$name}=$tetmp;
				}
			}
		}
	}
	close(LOG);
}

#HTMLファイル生成
open(FILE, ">$html");
flock(FILE, 2);
print FILE <<EOM;
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="Refresh" content="60;./kion.html">
<title>kion</title>
<style>
body { background:#eec; color:#000}
span.font1 {font-size: 100%; }
span.font2 {font-size: 100%; }
span.max {color:#f00}
span.min {color:#00f}
</style>
</head>
<body>
<span class="font1">@{[sprintf("%02d:%02d",$hour,$min)]}</span>
EOM
foreach(@remo) {
	($name, $te, $hu)=split(/\t/,$_);
	if($hu eq "null") {
		print FILE <<EOM;
<span class="font2">$name=$te℃</span><span class="max">$temax{$name}℃</span><span class="min">$temin{$name}℃</span>
EOM
	} else {
		print FILE <<EOM;
<span class="font2">$name=$te℃/$hu%</span><span class="max">$temax{$name}℃</span><span class="min">$temin{$name}℃</span>
EOM
	}
}
print FILE <<EOM;
</body>
</html>
EOM

close(FILE);

exit:

最高温度、最低温度について

  • 月別にデータをファイル格納します。ラズパイの処理能力ですと、それらをパースするのに10数秒程度かかります
  • 日がはじまったばかりだと、以下のようになってしまいます
    image.png

さいごに

ちょっとした電子工作でRaspberryPiに温度計取り付けることも考えたのですが、
なんだかめんどくさくなってしまった。

その結果、こうなってしまったという結論です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?