LightingNEST example (OP-AN-010)

From openPicus Wiki
Jump to: navigation, search

The application note, is almost a blank template where developer can begin to create their own application, since all the hardware aspects are just initialized for a general purpose use.

The input/output events can be attached to external events, like email sending, tweets, data exchange on servers or sites, etc...

Also the expansion with exernal boards or sensors can be made easily with the use of the expansion connector, using for example i2c bus enabled devices, or analog inputs, like potentiometers.



Application Note PDF

Source Code


In this specific application, a webserver is shown, using the jQuery Library. jQuery is a fast and concise Javascript Library that simplifies HTML document traversing, event handling, animating , and AJAX interactions. In addition to the jQuery lib, in the application note webserver are used the jQuery UI plugins; jQuery UI helps to develop great user interfaces with user-friendly design, event-driven architecture, focused on web standards.

The plug-ins are tested for compatibility in IE6.0+, Firefox 3+,Safari 3.1+, Opera 9.6+, and Google Chrome.

Required Hardware

Required Software and Libreries


  • OpenPicus Free IDE 2.2+



The taskFlyport.c processes very simple tasks. First of all the I/Os are initialized; the inputs are configured as input with internal pullup resistors, the 4 relays and the fifth solid state TRIAC ON/OFF (WARNING only for alternate voltage signals!) relay as output. After that, if the project is using the Flyport Wi-Fi, it connects to the network.

All the digital input and outputs are defined with different names, to help user to handle the LightingNEST expansion board:

#define LightingNEST
#ifdef LightingNEST
#define input1 p7
#define input2 p5
#define input3 p9
#define input4 p11
#define input5 p2
#define relay1 p6
#define relay2 p17
#define relay3 p19
#define relay4 p21
#define relay5 p4

At the first taskFlyport() execution, all the input and outputs are properly initialized using the IOInit function:

// Here we setup the IOs for the LightingNEST setup:
// Inputs:
IOInit(input1, inup); //input 1, p5
IOInit(input2, inup); //input 2, p7
IOInit(input3, inup); //input 3, p9, J6 pin1
IOInit(input4, inup); //input 4, p11, J6 pin2
IOInit(input5, inup); //input 5, p2, J6 pin3
IOInit(relay1, out); //Relay 1, p6
IOInit(relay2, out); //Relay 2, p17
IOInit(relay3, out); //Relay 3, p19
IOInit(relay4, out); //Relay 4, p21
IOInit(relay5, out); //Relay 5, p4

For demonstration purposes only, inside the run time loop, the firmware prints to UART1 the state of the 4 Analog inputs, if the input1 signal is tied to ground. The taskFlyport() code can be easily customized, and the read of the state of the input signal is just a starting point.

// Insert here your controlling code
// (for example activate Relay1 if ADVal(1)<250...)
i = IOButtonState(input1);
if( i == pressed)
for (i=1; i<5; i++)
value = ADCVal(i);//(WORD)ADC1BUF1;
uitoa(value, (BYTE*)ANValue);
sprintf(ANString, "AD value %d: %s\r\n", i, ANValue);
UARTWrite(1, ANString);

The HTTPApp.c processes all the webserver information exchanges. All the input and outputs state are handled in this file, in conjunction with the jQuery Javascript file.

The relays commands are handled with the cgi requests to the file leds.cgi inside the following function:

BYTE *ptr;
BYTE filename[20];
// Load the file name
// Make sure BYTE filename[] above is large enough for your longest name
MPFSGetFilename(curHTTP.file, filename, 20);
// If it's the LED updater file
if(!memcmppgm2ram(filename, "leds.cgi", 8))
// Determine which LED to toggle
ptr = HTTPGetROMArg(, (ROM BYTE *)"led");
// Toggle the specified LED
4 Application Note OP-AN-010 rev.1.1 24.04.2012 Page
case '0':
case '1':
case '2':
case '3':
case '4':
return HTTP_IO_DONE;

For the real time display of the state of FLYPORT inputs and outputs the functions HTTPPrint_xxx functions are used:

  • HTTPPrint_pot for the 4 analog inputs state
  • HTTPPrint_btn for the 5 digital inputs state
  • HTTPPrint_led for the 5 relays output state


The webserver page uses jQuery UI elements. The elements used are:

  • “button” for the relays control
  • “ui-icon” for the inputs state monitoring
  • “progressbar” for the analog inputs graphic rendering

The “button” allows to check the Relays status and to change them by pressing the buttons.

The “ui-icon” selects different images in function of the input state.

The “progressbar” changes itself with the analog input values returned by the FLYPORT.

Testing OP-AN-010 1.png

All the AJAX and Javascripts are handled by jQuery files. The scripting files are stored inside the “.\Web pages\scripts\” folder:

  1. jquery-1.5.1.min.js (the minimal jQuery Javascript Library)
  2. jquery-tasks.js (the AJAX related script handling file)
  3. jquery-ui-1.8.14.custom.min.js (a customization of the jQuery UI plugins, with only the UI elements used)

The jQuery UI plug-ins can be inserted or deleted with the needs of the specific application. This flexibility is a great feature for embedded devices, because of the reduced memory space available.

User can customize the UI events, simply editing the “jquery-tasks.js” file. The function “ready” sets the first values of the UI, and set a timed calling of the UpdateData() function by the “setInterval('UpdateData()',100);”, where 100 is the milliseconds of updating refreshes of the UI values. The “UpdateData()” function is responsible of the updating of the new values of all the variables of UI.


Personal tools