Created
March 7, 2017 22:05
-
-
Save andyleap/01601cc9cdf7d3708bb63d5867afc484 to your computer and use it in GitHub Desktop.
Power Monitoring
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
// powermon project main.go | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"time" | |
"github.com/bemasher/rtlamr/parse" | |
"github.com/bemasher/rtlamr/scm" | |
"github.com/bemasher/rtltcp" | |
"github.com/influxdata/influxdb/client/v2" | |
) | |
var receiver = &rtltcp.SDR{} | |
func main() { | |
receiver.RegisterFlags() | |
flag.Parse() | |
p, _ := parse.NewParser("scm", 72, 1) | |
receiver.Connect(nil) | |
receiver.HandleFlags() | |
cfg := p.Cfg() | |
c, _ := client.NewHTTPClient(client.HTTPConfig{ | |
Addr: "http://localhost:8086", | |
Username: "", | |
Password: "", | |
}) | |
receiver.SetCenterFreq(cfg.CenterFreq) | |
receiver.SetSampleRate(uint32(cfg.SampleRate)) | |
block := make([]byte, p.Cfg().BlockSize2) | |
lastRead := map[uint32]uint32{} | |
for { | |
// Read new sample block. | |
_, err := io.ReadFull(receiver, block) | |
if err != nil { | |
log.Fatal("Error reading samples: ", err) | |
} | |
indices := p.Dec().Decode(block) | |
for _, pkt := range p.Parse(indices) { | |
if pkt.MeterID() == 44141527 || pkt.MeterID() == 26333031 { | |
data := pkt.(scm.SCM) | |
log.Println(pkt.MeterID(), data.Consumption) | |
last := lastRead[pkt.MeterID()] | |
if last != data.Consumption { | |
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ | |
Database: "power", | |
Precision: "s", | |
}) | |
p, _ := client.NewPoint(fmt.Sprint(pkt.MeterID()), nil, map[string]interface{}{"consumption": float64(data.Consumption) / 100}, time.Now()) | |
bp.AddPoint(p) | |
err := c.Write(bp) | |
if err != nil { | |
log.Println(err) | |
} | |
lastRead[pkt.MeterID()] = data.Consumption | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have rtlamr working on my Raspberry Pi 3 can you tell me how I can run this code on the RBP3