Power Monitor

From Nearwiki
Jump to: navigation, search

This example shows how to implement a real time Power Monitor in JavaScript for a mobile device. This implementation require an Xively account (www.xively.com) in order to get the historical data sequence in a chart format.

Mobile power meter.png

smartphone interface example

System Implementation

This implementation works in manual mode asking the remote agent (Arduino) the power reading in a periodic way. The system use the Cosm Connector to send the reading to Xively in order to show it in a graphical way. On the other hand a independently javascript code running from a mobile device ask to the NearBus system in a periodic way the last power reading to show it on the mobile device.

Only two hadware components are needed to implement this real time Power Monitor: An Arduino Board (Ethernet / WiFi) and an AC Current Sensor [1] as showed in the following picture.

Arduino power monitor.png

The AC Current Sensor is connected to the A0 analog inpur on the Arduino board.

IMPORTANT: the current sensor only should be installed on only one power wire (of two). If you place both power wires into the sensor,
then the reading will be 0 (zero).

Configuring the Manual Mode

In order to get samples from the sensor in a continous way, you should configure the RMS_INPUT service in manual mode for the selected channel (the port in which the sensor will be connected). In this example we will use the Channel 0 (A0 3 in an Arduino platform). The following picture shows this configuration.

Manual mode power.png

Xively Configuration (formerly Cosm)

This screen allows you send to Xively (www.xively.com) the values received from or transmitted to the remote device over each channel.

Xively config power.png

  • Stream ID: This field allows you to set the Xvely's (Cosm) datastream where you want to send the signal.
  • IN / OUT: This option allows you to indicate if you want to send a readed value (input) from the remote device or the written value (output) to the remote device for each channel.
  • Constant and Offset: The main advantage of this feature is that it allows you to apply a basic transfer function to your signal before sending it to Xively (Cosm):
     f(x) = x * K + offset

  • ON: This option allows you to turn on or off the data feed to Xively (Cosm).
  • Xively Feed and API Key: This option allows you to configure the required parameters to connect succesfully to www.xively.com.

In this example we use a constant factor of 6.6. This factor is obtained in the following way:

  • Power Line tension: 220 Vac
  • Current Sensor Ratio: 30:1 (30Aac= 1Vac)
  • RMS_INPUT scale: mV (RMS)
K = 220 * 30 / 1000  = 6.6 [W/mV]

Note: this is a calibration example. In order to calibrate your Power Monitor you should use a well-known resistive load (and adjust the K factor to match its value).

The following picture shows a real system power chart (Xively). It is important to remark the amount of information that can be obtained from it. In this example the chart represents a measure of our lab enviroment, where an electric heater (about 1400W) and a small refrigerator (about 175 W) are working together, each with its own power fingerprint, as indicated in red on the Xively output chart (note that a residual power consumption of 196W is always present).

Xively power chart.png

Javascript Code

The following code will allow you to display the sensed power in a graphical way from a mobile device or desktop computer. In order to run this example, download the following file in you mobile device or computer, change the parameters indicated with "****" and execute it. Double click onto the downloaded "html" file in order to run it on your default browser.

This example is calibrated to use a 30A AC Current Sensor [2] but you can use any other current sensor sensor that you want (you only should change conversion factor). In this example a conversion factor of 6.6 is used.

Note: that the Xively transfer function does not affect the value read with the RMS_INPUT service.

Download: Power_Monitor_0v1.zip [3]

In order to run this example you should edit it (eg. notepad) and replace the following parameters:
var device_id  = "NB100xxx";		// You NearBus Device ID
var user       = "****";            	// Your NearBus Web user
var pass       = "****";            	// Your NearBus Web password

<!DOCTYPE html>        
<!-- ****************************************************************************************************************************** -->  
<!-- * NEARBUS JAVASCRIPT EXAMPLE - www.nearbus.net                                                                               * --> 
<!-- * Description: This simple code shows how to implement a power monitor on a Smartphone                                       * --> 
<!-- * This example requires an Xively account (www.xively.com) in order to record the power registers in a historical way        * --> 
<!-- * The system is configured to work with a AC Current Sensor                                                                  * --> 
<!-- * Support: info@nearbus.net                                                                                                  * --> 
<!-- ****************************************************************************************************************************** -->  
<!-- * REVISION HISTORY                                                                                                           * -->
<!-- * v0.1 - 28-11-13 - Initial Release                                                                                          * --> 
<!-- ****************************************************************************************************************************** --> 
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="PAGE" ></script>
<script type='text/javascript' src='http://nearbus.net/downloads/js_apps/nearapi_v05.js'></script>

// +--------------------------------------------------------------------+
// | To run this example you should replace the following parameters    |
// | device_id         Your device ID, or.. devices ID :)               |
// | user              Your NearBus Web user                            |
// | pass              Your NearBus Web password                        |
// +--------------------------------------------------------------------+

var device_id     = "NB100xxx";     // Your device ID
var user          = "****";         // Your NearBus Web user
var pass          = "****";         // Your NearBus Web password
var LOOP_DELAY    = 2000;
var power         = 0;
    window.onload = function ()
        // Function: Main Loop
        setInterval( function()
            ret = NearAPIjs( "RMS_INPUT", device_id , 0, 0, "RONLY" );
            if( ret != "WAIT" && !isNaN(ret) ) {
                power = Math.round( ret * 6.6 ); 
                $('#field_1').text( power.toString() );    
        }, LOOP_DELAY );                


<body bgcolor="#000">
<br />
<p><div style="text-align:center; width:950px"><img src="http://nearbus.net/images/logo_nearbus.png" width="140" heisgth="78"</div></p>
<br />
<p><div style="text-align:center; width:950px; font: bold 60px Arial; color:orange;">POWER MONITOR</div></p>
<br />

<!-- this line embed a 750 x 400px feed chart form www.xively.com (former cosm.com) --> 
<!-- you should replace the "feed" and the "zone" in order to get the chart from www.xively.com  --> 

<div id="div_temp_chart_cm"> <img src="https://api.cosm.com/v2/feeds/****YOUR_FEED_ID_HERE****/datastreams/****YOUR_CHANNEL_NAME_HERE****.png?width=750&height=400&colour=%23f15a24&duration=3hours&show_axis_labels=true&detailed_grid=true&timezone=****YOUR_ZONE_HERE****" > </div>

<br />
<br />
<p><div style="text-align:center; width:950px; font: bold 60px Arial; color:orange;">watts</div></p>
<p><div id="field_1" style="text-align:center; width:950px; font: bold 300px Arial; color:orange;">. . . .</div></p>