Getting started with emonCMS

One of the things I set out to do was monitor (and then affect) the amount of energy I use. To help me to do that I’ve started using EmonCMS, this is part of a larger project Open Energy Monitor.

Open Energy Monitor is a series of projects:

  • emonTX which takes energy readings (there are modules for all sorts of meters including gas, electricity and water), and transmits them wirelessly to a base station.
  • emonBase which takes data from emonTX then pushes it to a CMS (Content Management System)
  • emonGLCD a low cost LCD for visualising your current status.
  • emonCMS the content management system itself.

Whilst you may be familiar with CMS’s in general, emonCMS is a bit special as it’s been written solely to store energy usage across time. Unfortunately, like many open source projects, there is a steep learning curve involved and quite a lot of the documentation assumes you already know what you are doing. This coupled with the problems of debugging hardware and software at the same time, can sometimes leave you frustrated.

So I decided to write a quick app to feed the CMS with known “good” data. That way I could play around with the software in isolation, learn how it works, and have something to compare against when I’m debugging my sensors.

[code language=”java” toolbar=”false”]
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* Copyright 2013, Graham Evans
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For more information, see <http://www.gnu.org/licenses/>.
*
* @author Graham Evans
*/
public class TestInput {

static final String EMONCMS_URL = "http://localhost/emoncms";
static final String RW_API = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

private void runDataTest() {
// create a randomizer
long startTime = System.currentTimeMillis();
Random rand = new Random(System.currentTimeMillis());
try {
// main loop
while (true) {
// generate a "reading"
double SINPower = (1.0 + Math.sin(Math.PI * System.currentTimeMillis() / 60000.0))*300.0;
long CountTicks = (System.currentTimeMillis() – startTime) / 18000;
double COSVolts = rand.nextInt(200) + 1;
double COSAmps = (1.0 + Math.cos(Math.PI * System.currentTimeMillis() / 54321.0))*300.0 / COSVolts;
// post it as input "SINTEST" to CMS
StringBuilder urlString = new StringBuilder(EMONCMS_URL);
urlString.append("input/post.json?node=0&json={SINPower:");
urlString.append(SINPower);
urlString.append(",CountTicks:");
urlString.append(CountTicks);
urlString.append(",COSVolts:");
urlString.append(COSVolts);
urlString.append(",COSAmps:");
urlString.append(COSAmps);
urlString.append("}&apikey=");
urlString.append(RW_API);
URL url = new URL(urlString.toString());
System.out.println("Posting "+url.toExternalForm());
url.getContent();

// wait a few seconds
synchronized (this) {
wait(rand.nextInt(10000));
}
}
} catch (MalformedURLException ex) {
Logger.getLogger(TestInput.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestInput.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(TestInput.class.getName()).log(Level.SEVERE, null, ex);
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestInput test = new TestInput();
test.runDataTest();
}

}
[/code]

Quick Instructions:

  1. Save the code to a file called TestInput.java
  2. Make sure you have a recent version of the Java JDK installed (if not go to http://www.oracle.com/technetwork/java/javase/downloads/index.html)
  3. Edit the file to point to your installation (change the URL on line 28).
  4. Copy the “Read & Write” API key from your installation to the program (line 29).
  5. compile the app with javac TestInput.java
  6. run the app with java TestInput
  7. press ctrl-C to quit the program when you are finished

Challenge

Once you have the program running, it should load emonCMS with four new data inputs:

  • SINPower – This simulates a electricity clamp, measuring the power (measured in W) consumed by an appliance.
  • COSVolts / COSAmps – These are two separate readings for a second appliance, which varies the voltage it uses as well as the current. You will have to combine the two pieces of information to measure the power.
  • CountTicks – This is a bit harder; it represents the number of “ticks” on a electricity meter. Each tick represents 1/200 of a kWh.

So the challenge is, how much power is being used?

Be Sociable, Share!

    Leave a Reply

    Your email address will not be published. Required fields are marked *