Skip to content

kbhuyan/emu

Repository files navigation

EMU-2 Go Library

Go Reference Go Report Card Build Status License: MIT

A pure Go library for interfacing with the EMU-2 (Energy Monitoring Unit) device through USB-serial communication. This library provides a clean API to query power consumption data from smart meters connected to the EMU-2.

Features

  • Serial port communication with EMU-2 device
  • Real-time power consumption monitoring
  • Support for multiple EMU-2 commands
  • Thread-safe message handling
  • Configurable logging
  • Type-safe data structures for power and energy readings

Installation

go get github.com/kbhuyan/emu

Usage

Basic Example

package main

import (
    "log"
    "time"
    "github.com/kbhuyan/emu"
)

func main() {
    // Connect to EMU-2 device
    device, err := emu.NewEmu("/dev/ttyACM1",
        emu.WithBaudRate(115200),
        emu.WithTimeOut(15*time.Second))
    if err != nil {
        log.Fatalf("Connection failed: %v", err)
    }
    defer device.Close()
    
    // Start the device communication
    device.Start()
    
    // Process the power consumption data
    if power, err := device.GetInstantaneousPowerConsumption(); err == nil {
        log.Printf("Power Consumption: %v kW", power.Power)
    }
}

Configuration Options

emu.NewEmu("/dev/ttyACM1",
    emu.WithBaudRate(115200),    // Default: 115200
    emu.WithTimeOut(15*time.Second), // Default: 15s
    emu.WithLogWriter(os.Stdout), // Default: os.Stdout
    emu.WithLoggingLevel(emu.LOG_WARNING) //Default: emu.LOG_WARNING
)

Data Structures

InstantaneousPowerConsumption

type InstantaneousPowerConsumption struct {
    TimeStamp   int64   // Unix timestamp
    Power       float64 // Power in kW
    DeviceMacId string  // EMU-2 MAC address
    MeterMacId  string  // Smart meter MAC address
}

if power, err := device.GetInstantaneousPowerConsumption(); err == nil {
    log.Printf("Power Consumption: %.3f kW", power.Power)
}

CumulativeEnergyConsumption

type CumulativeEnergyConsumption struct {
    TimeStamp   int64   // Unix timestamp
    Energy      float64 // Energy in kWh
    DeviceMacId string  // EMU-2 MAC address
    MeterMacId  string  // Smart meter MAC address
}
if energy, err := device.GetCumulativeEnergyConsumption(); err == nil {
    log.Printf("Current Cumulative Energy Delivered: %.3fkWh\n", energy.Energy)
}

Available Commands

  • emu.RESTART - restarts the emu-2 device
  • emu.GET_DEVICE_INFO - gets the basic emu-2 device info HW/SW version, make/model etc.
  • emu.GET_TIME - gets the time (local and UTC) on the emu-2 as sync with the smart energy meter
  • emu.GET_CONN_STATUS - gets the current connection status with the smart energy meter`
    if cmd, err := emu.NewCommand(emu.RESTART); err == nil {
        if err := device.SendCommand(cmd); err == nil {
            // Get response
            if rsp, err := device.GetResponse(); err == nil {
               fmt.Printf("Response %+v", rsp)
            }
	    }
    }

Asyncronous Message Reception

	sub := []emu.MessageName{emu.InstantaneousPower, emu.CumulativeEnergy}
	var handler = func(m emu.Message) {
		log.Printf("Received %+v", m)
	}
	device.Subscribe(sub, &handler)

Acknowledgements

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A pure Go library for interfacing with the Rainforest Automation EMU-2 Energy Monitoring Unit via USB-serial. Provides a thread-safe, type-safe API to monitor real-time power consumption and energy data from smart meters.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages