Madowatt Meter: a current transformer based internet connected meter

Bill Of Materials:
1. Atmel ATmega328PU with Arduino Optiboot
2. Crystal: 1 x 16MHz
3. Voltage regulators: 1 x LD7805, 1 x LD33V
4. Stripboard
5. XBee S2 6. XBee USB Adapter 7. XBee Module Breakout.
7. Capacitors: 3 x 10uF, 2 x 22pF
8. Resistors: 2 x 1.7k, 2 x 2.2k, 2 x 3.3k, 2 x 10k
9. 12VDC 500mA Power Adapter
10. LEDs: 2
11. SCT 030 current transformer
12. TRRS 3.5mm Jack Breakout

Circuit Diagram
The circuit diagram is shown below. Solder all the components onto stripboard. Use a socket for the ATMEGA328-PU and a breadboard adapter for the XBee S2, i.e. don't solder these directly onto the stripboard as they're sensitive devices and may be destroyed while soldering. The rest of the devices can be soldered directly onto the board.

Circuit Diagram

Current transformer circuit: the current transformer circuit is the simplest part of the meter. Simply connect the +ive end of the SCT-030 to A1 on the Uno and the -ive to GND. Then connect a 10kohm resistor and a 10uF capacitor in series from 5V on the Uno to GND on the Uno. Then connect another 10kohm resistor in parallel with the capacitor and in series with the first resistor. See diagram above and prototype below:

Atmel / Arduino circuit: an Arduino Uno board can be replicated on stripboard as described here.

XBee circuit: a 5V power supply is used with the Atmel ATMEGA328 while the XBee has a reference voltage of 3.3V. So the connections from the Atmel RX/TX to the XBee Dout/Din have to made using a voltage divider to correspond with the XBee's Vref=3.3V.

Atmel ATMEGA328PU
1. Burn bootloader: if the ATMEGA doesn't already have a Arduino bootloader on it it will have to be loaded.
2. Upload the following sketch (current only) onto the micro-controller using an Arduino Uno. Power is calculated assuming voltage of 230V and written to serial port.

#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
void setup(){
Serial.begin(9600);
emon1.current(1, 30); // Current: input pin, calibration.
}
void loop(){
double Irms = emon1.calcIrms(1480); // Calculate Irms only
Serial.println(Irms*230.0); // Apparent power
delay(10000);
}

XBee RF wireless network

  1. Prepare the radios for configuration: use X-CTU on Windows or Wine on Linux. See p33 in WirelessSensorNetworks for instructions on how to install Wine and run X-CTU in Wine on Linux:
  2. If Linux, create a symbolic link in ~/.wine/dosdevices to /dev/ttyUSB0 and call it something like com10: ln -s /dev/ttyUSB0 ~/.wine/dosdevices/com10
  3. Then create a 'User COM Port' in X-CTU by choosing the appropriate com port number (10 in this case).

  4. Use an XBee USB adapter to connect a radio to the USB port selected as com10. Test/Query the radio to ensure it's working. A dialog box with the type and firmware version should appear.

  5. Pressing 'Read' will give current parameter values. For a Digi International XBee S2 choose XB24-ZB as 'Modem'. Configure one co-ordinator radio by choosing the 'Modem Configuration' tab and choosing 'ZIGBEE COORDINATOR AT' function set. Values can be changed by either entering values into the box next to the parameter name and pressing 'Write' OR by clicking on the 'Terminal' tab and manually setting the parameter values using AT commands as follows:

    1. +++ (3 pluses without pressing enter) will result in OK from the radio and set it in AT mode. eg. +++OK
    2. ATID will read and output the PAN ID of the network. This can be left 0 if there are no other co-ordinator radios around. The co-ordinator will randomly choose a PAN ID which can be read using ATOI.
    3. ATMY will read and output the 16-bit network address of the module. A value of 0xFFFE means the module hasn't joined a ZigBee network. The co-ordinator will have an ATMY of 0.
    4. ATBD will read and output the baud rate. Set it to 9600 using ATBD3.
    5. ATOI reads and outputs the 16-bit PAN ID. The OI value reflects the actial 16-bit PAN ID the module is running on.
    6. ATCN exits command mode.
  6. Configure one or more router radios by inserting another XBee S2 into the USB adapater and choose XB24-ZB as 'Modem'. Configure a router radio by choosing 'Zigbee Router AT' under 'Modem Configuration':

    1. If ATID is not set (left at 0), a router/end device will join any extended PAN ID.
    2. If ATID has been set on the co-ordinator radio then the same value should be set as ATID on the router radio so the router can join the network created by the co-ordinator.
    3. Changes to ID, eg. ATID2612 should be written to non-volatile memory using ATWR command to preserve the ID setting if a power cycle occurs.
    4. More details can be found in the product manual on Digi's website.
  7. In case X-CTU is not at hand the radios can also be configured using an Arduino Uno and an XBee wireless shield.

    1. Upload a 'blank' sketch onto the ATMega328. A black sketch (bare minimum) is one with nothing in the loop() function.
    2. Connect the XBee radio to the XBee shield and connect the shield to the Arduino Uno (Uno).
    3. To configure the radio, the XBee shield should be in USB mode. In this mode, commands from your serial port on the computer are sent to the XBee radio.
    4. The XBee radio can be configured using either the Arduino XBee shield or a XBee adapter.
    5. Connect the XBee adapter to the USB port of a computer using a USB to USB mini B cable.
    6. Use a serial emulator like screen. After finding the port that the XBee adapter has been connected, type screen /dev/ttyUSB0 9600
    7. The XBee radio can be put into configuration mode by typing +++ and waiting for a second. The expected response from the XBee radio is OK.
    8. Use the commands described in the previous section to change parameters of the radios. Note the radios must already be configured using X-CTU at some point in the past.
  8. Notes

  9. If the XBee shield is being used in a production meter then it should be in MICRO mode.
  10. Uploading the current sketch: if you want to upload the current sketch without removing the XBee shield, disconnect the Uno+XBee shield from the power supply and switch the XBee shield to MICRO mode. In this mode, you can upload sketches to the ATMega328. Note that in this mode whatever is sent to the ATMega328 is also transmitted by the XBee radio if it's already configured. So you're likely to see a stream of rubbish if there's another XBee radio with the same (or no) PAN ID.

Prototype

Madowatt Portal
1. Client: the MadowattClient connects to the MadowattServer, reads the serial port of the computer and transmits the readings to the MadowattServer. The MadowattClient is run by downloading the jar and running: java -jar Madowatt.jar
2. Server: The MadowattServer is a multi-threaded Java application which listens for connections from clients on a single port and stores the data the clients send in a postgresql database. Complile and run the server using:
javac -d . -cp .:postgresql-9.3-1100.jdbc41.jar:c3p0-0.9.1.jar MadowattServer.java MadowattServerThread.java MadowattProtocol.java MadowattData.java
java -cp .:postgresql-9.3-1100.jdbc41.jar:c3p0-0.9.1.jar madowatt.MadowattServer
3. Servlet: the ChartServlet is a Java HttpServlet which displays the data sent by the Client to the Server. See this for instance.