Categorygithub.com/hnw/go-smartmeter
modulepackage
0.0.0-20200515154420-789ba447f197
Repository: https://github.com/hnw/go-smartmeter.git
Documentation: pkg.go.dev

# README

go-smartmeter

Wi-SUNモジュールを使って電力スマートメーターにアクセスするGoライブラリです。

特徴

  • 致命的エラーとリトライ可能エラーを区別して可能ならリトライする(Wi-SUNの通信は不安定なので実用上はリトライ実装が重要)
  • SKSCANで指定チャンネルだけスキャンできるようにしたので、チャンネルがわかっていれば再スキャンが高速
  • ECHONET Liteの複数プロパティを1コマンドにまとめられるので、920MHz帯の節約になる
  • Bルート専用モジュールとデュアルスタックモジュール両対応
  • 実行ファイルが外部ライブラリ依存のない小さいバイナリになるので、Raspberry Piなど低スペック環境でも動作させやすい

サンプルコード

次のようなコードでスマートメーターの瞬時電力計測値にアクセスできます。(Wi-SUNモジュールの入手とBルートサービスの申し込みは必須です)

package main

import (
	"fmt"

	smartmeter "github.com/hnw/go-smartmeter"
)

func main() {
	dev, err := smartmeter.Open("/dev/ttyACM0",
		smartmeter.DualStackSK(), // Bルート専用モジュールを使う場合はコメントアウト
		smartmeter.ID("00000000000000000000000000000000"), // Bルート認証ID
		smartmeter.Password("AB0123456789"),               // パスワード
		smartmeter.Channel("33"))                          // チャンネル。各環境でScan()で取得した値に書き換える。

	if err != nil {
		fmt.Printf("%+v", err)
		return
	}

	err = dev.Authenticate()
	if err != nil {
		fmt.Printf("%+v\n", err)
		return
	}

	request := smartmeter.NewFrame(smartmeter.LvSmartElectricEnergyMeter, smartmeter.Get, []*smartmeter.Property{
		smartmeter.NewProperty(smartmeter.LvSmartElectricEnergyMeter_InstantaneousElectricPower, nil),
	})
	response, err = dev.QueryEchonetLite(request, smartmeter.Retry(3))
	if err != nil {
		fmt.Printf("%+v\n", err)
		return
	}

	for _, p := range response.Properties {
		fmt.Print(p.Desc())
	}
}

これを実行すると、次のように自宅の消費電力がわかります。

Instantaneous Electric Power: 389.000000 [W]

examples/以下に利用例がありますので参考にしてください。

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewFrame は Frame構造体のコンストラクタ関数.
NewProperty は Property構造体のコンストラクタ関数.
No description provided by the author
No description provided by the author
ParseFrame は ECHONET Liteフレームのバイト列を受け取り、Frame構造体として返す.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

コントローラ.
No description provided by the author
No description provided by the author
0x10=ECHONET Lite, 0x81=電文形式1.
低圧スマート電力量メータ.
係数(作者の環境では1).
瞬時電流計測値.
瞬時電力計測値.
積算電力量(正方向).
定時積算電力量(正方向).
積算電力量(逆方向).
定時積算電力量(逆方向).
積算電力量単位(作者の環境では0.1kWh).
ノードプロファイル.
事業場コード.
No description provided by the author
No description provided by the author
識別番号.
No description provided by the author
メーカコード.
自ノードクラス数(作者の環境では2).
商品コード.
製造年月日.
製造番号.
自ノードクラスリストS.
自ノードインスタンスリストS.
個体識別情報.
Version情報.

# Variables

No description provided by the author

# Structs

Device.
Frame はECHONET Liteのフレームに対応する構造体 複数のプロパティの操作を1フレームにまとめて送信することができる.
Property はECHONET Liteのプロパティに対応する構造体.

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author