Reliable Power Switch

From Nearwiki
Jump to: navigation, search

The following code examples are intended to show you the simple way in which a NearBus enable device can be controlled from a simple Java Script code running on a browser (PC, or mobile device).


Because the NearBus system has been designed to control power devices through internet in a reliable way, these examples explain the different configurations that you can implement in order to increase the reliability in your controlled system.


Motor Switch Code - Basic Example


This simple example shows how to implement a basic Internet switch, to control a power load. The code implements the basic NearBus ACK call return in order to confirm that the remote agent has executed the command (at logical level)


The following video shows a working implementation of this example



In order to run this code you should copy the following code into a file, save it with html extension and run it in your browser. Additionally you should replace the device_ID, user and password in this file.


EXAMPLE CODE

<!DOCTYPE html>
<html>
<head>
<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_v01.js'></script>
<script>


//***********************************************************************
// USER CONFIGURATION
//***********************************************************************
var device_1     = "NB100***";         // Your device ID
var user         = "user";             // Your NearBus Web user
var pass         = "1234";             // Your NearBus Web password


//***********************************************************************
// SYSTEM CONFIGURATION
//***********************************************************************
var state_log    = "Waiting";
var motor_state  = 0;
var step_pointer = 10;
var ret = 0;
var LOOP_DELAY   = 2000;               // Main Loop Delay


//***********************************************************************
// Function: refreshDisplay()
//***********************************************************************	
function refreshDisplay()
{
	$('#field_1').text( "Step_Pointer = " 	+ step_pointer.toString() );
	$('#field_2').text( "Message = " 	+ state_log );			
	$('#field_3').text( "Return = " 	+ ret.toString() );		
	$('#field_4').text( "Motor = " 		+ motor_state.toString() );		
}


//********************************
// Function: MOTOR_ON
//********************************		
function Motor_On( )
{
	step_pointer = 100;
}

//********************************
// Function: MOTOR_OFF
//********************************	
function Motor_Off( )
{	
	step_pointer = 200;
}


//********************************
// Function: Main Loop
//********************************	
$(document).ready( function ()
{
	
	setInterval( function()
	{
		switch ( step_pointer )
		{
			//*******************************
			// WAITING
			//*******************************
			case 10:
				break;
				
					
			//*******************************
			//  MOTOR ON
			//*******************************				
			case 100:
				ret = NearAPIjs( "DIG_OUTPUT", device_1, 0, 1 );	

				if( ret == 1 ) {
					motor_state = "ON";
					step_pointer = 10;
				}
				break;
			
					
			//*******************************
			//  MOTOR OFF
			//*******************************			
			case 200:			
				ret = NearAPIjs( "DIG_OUTPUT", device_1, 0, 0 );	

				if( ret == 0 ) {
					motor_state = "OFF";
					step_pointer = 10;
				}
				break;
								
		}
		refreshDisplay();
		
	}, 1000 );	
				
});


</script>
</head>

<body>

<!-- *************************************************************************************** -->  
<!-- *  HTML CODE                                                                          * --> 	
<!-- *************************************************************************************** --> 

<p><b> MOTOR SWITCH DEMO </b></p>

<div id="field_1">field_1</div>
<br />
<div id="field_2">field_2</div>
<br />
<div id="field_3">field_3</div>
<br />
<div id="field_4">field_4</div>
<br />
<br />
<button onclick="Motor_On()">MOTOR ON</button>
<br />
<br />
<button onclick="Motor_Off()">MOTOR OFF</button>
</body>





Motor Switch Code - Advanced Example


This example implements two features in order to increase the system reliability. In this context the reliability should be understood as an improvement in the capacity of all system to guarantee that the remote power switch works as expected, or under failure it will trip to a well-defined state.


To accomplish this behaviour the system implements:

  • A Watch-Dog supervisor code.
  • A current feedback signal (using a non-invasive current transformer).


The two systems work independently to guarantee the system whole reliability. The Watch-Dog code is a simple register counter that increments by one with each state-machine cycle. The Watch-Dog counter is reset with each successful operation and put the system in a "turn-off" state when the counter reaches a threshold level.


The current transformer measures the real current that feeds the power load. This signal allows to the control system to know if the load is really working or not and then take the right action. The last versions of NearBus system offers enhanced features like the True RMS function that allows know the true RMS current value of the load. This feature allows calculate the active power that the load is consuming.


Current Feedback

To know if the Motor is running the system measured the current through the "RMS_INPUT" service. Before to measure the current, the system configures the remote ADC to a value of 1100mV calling the "RMS_INPUT_CONFIG" service (because this example uses a low power motor of only 15 Watts). The measured current is compared with a threshold reference of 10 in order to decide if the Motor is running or not.


Power Calculation

In order to calculate the active Power consumption we use the following parameters:

* V = 220Vac
* Power Factor = 0.75 (inductive motor)
* TI Ratio = 5:2 [A/V]
* Vref ADC 1100mV
* ADC resolution 10 bits (1023)

Power [Watts] = Irms * (1100 / 1023) / 1000 * (5 / 2) * 220 * 0.75


The following picture shows the prototype used to test this example.

Motor switch 01.png


Components:



EXAMPLE CODE

<!DOCTYPE html>
<html>
<head>
<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_v01.js'></script>

<script>

//***********************************************************************
// USER CONFIGURATION
//***********************************************************************
var device_1     = "NB100***";		// Your device ID
var user 	 = "user";		// Your NearBus Web user
var pass 	 = "1234";		// Your NearBus Web password


//***********************************************************************
// SYSTEM CONFIGURATION
//***********************************************************************
var state_log    = "Waiting";
var motor_state  = 0;
var step_pointer = 10;
var my_watch_dog = 0;
var ret          = 0;
var power        = 0;
var LOOP_DELAY   = 2000;		// Main Loop Delay
var WATCH_DOG    = 20;


//***********************************************************************
// Function: refreshDisplay()
//***********************************************************************	
function refreshDisplay()
{
	$('#field_1').text( "Step_Pointer = " 	+ step_pointer.toString() );
	$('#field_2').text( "Message = " 	+ state_log );			
	$('#field_3').text( "Return = " 	+ ret.toString() );		
	$('#field_4').text( "Watch_Dog = " 	+ my_watch_dog.toString() );		
	$('#field_5').text( "Motor = " 		+ motor_state.toString() );		
	$('#field_6').text( "Power = " 		+ power.toString() + " Watts" );			
}


//********************************
// Function: MOTOR_ON
//********************************		
function Motor_On( )
{
	step_pointer = 100;
}


//********************************
// Function: MOTOR_OFF
//********************************	
function Motor_Off( )
{	
	step_pointer = 200;
}


//********************************
// Function: Main Loop
//********************************	
$(document).ready( function ()
{
	
	setInterval( function()
	{

		//**************************************************
		// WATCH DOG CONTROL
		//**************************************************
		my_watch_dog++;
		if ( my_watch_dog > WATCH_DOG )	{
			my_watch_dog = 0;
			step_pointer = 200;
		}	

		switch ( step_pointer )
		{
			//*******************************
			// WAITING
			//*******************************
			case 10:
				my_watch_dog = 0;
				break;
					
					
			//*******************************
			//  MOTOR ON
			//*******************************				
			case 100:
				ret = NearAPIjs( "DIG_OUTPUT", device_1, 0, 1 );

				if( ret == 1 ) {
					my_watch_dog = 0;
					step_pointer = 110;
				}
				break;

				
			case 110:
				ret = NearAPIjs( "RMS_INPUT_CONFIG", device_1, 1, 1100 );				
				
				if( ret != "WAIT" ) {
					my_watch_dog = 0;					
					step_pointer = 120;
				}
				break;	
				
				
			case 120:
				ret = NearAPIjs( "RMS_INPUT", device_1, 1, 0 );	

				if( ret != "WAIT" && ret > 10 ) {
					motor_state = "ON";
					my_watch_dog = 0;					
					step_pointer = 130;
				}
				break;	
				
				
			case 130:
				ret = NearAPIjs( "RMS_INPUT", device_1, 1, 0 );
	
				if( ret != "WAIT" ) {
					power  =  Math.round( ret * (1100 / 1023) / 1000 * (5 / 2) * 220 * 0.75 );
					my_watch_dog = 0;					
					step_pointer = 130;
				}
				break;			
				
					
			//*******************************
			//  MOTOR OFF
			//*******************************			
			case 200:			
				ret = NearAPIjs( "DIG_OUTPUT", device_1, 0, 0 );
	
				if( ret == 0 ) {
					my_watch_dog = 0;
					step_pointer = 210;
				}
				break;
				
				
			case 210:
				ret = NearAPIjs( "RMS_INPUT", device_1, 1, 0 );	

				if( ret != "WAIT" && ret < 10 ) {
					motor_state = "OFF";
					my_watch_dog = 0;
					step_pointer = 10;
				}
				break;
				
		}
		refreshDisplay();
		
	}, 1000 );	
				
});



</script>
</head>

<body>

<!-- ******************************************************************** -->  
<!-- *  HTML CODE					 	        * --> 	
<!-- ******************************************************************** --> 

<p><b> MOTOR SWITCH DEMO </b></p>

<div id="field_1">field_1</div>
<br />
<div id="field_2">field_2</div>
<br />
<div id="field_3">field_3</div>
<br />
<div id="field_4">field_4</div>
<br />
<div id="field_5">field_5</div>
<br />
<div id="field_6">field_6</div>
<br />
<br />
<button onclick="Motor_On()">MOTOR ON</button>
<br />
<br />
<button onclick="Motor_Off()">MOTOR OFF</button>

</body>
</html>