LabVIEW: Web Services Part 2

After making a basic webpage in my previous post, I wanted to expand on it by adding some control. I have used a TI MSP430 LaunchPad as my connected hardware.

In this version, I added two radio buttons to the main page and two normal buttons to submit the selection. I then used an html form with a GET method to pass the selection from the webpage into LabVIEW. Depending on the selection made, LabVIEW controlled the state change of the LaunchPad LED’s through VISA functions.

The following instructions add on from the project in Part 1.

Editing the html page:

Start off by adding the <form></form> code to index.html. This will allow you to send data from the webpage back into LabVIEW at the press of a button. The method being used is GET and the action points to a VI which will handle the data.

This code will create two radio buttons and two normal buttons. When a button is pressed, the status of the radio button selected and which button pressed will be sent to the VI called ToggleLED in the WebService folder.

Creating the Web Resource:

We need to create a VI which will receive the data from the form. Right click on the Web Resources folder in the LabVIEW project and select New VI.

Save this file as ToggleLED.vi in a folder called ‘Web Resources’ in the root directory and close it. We will come back to it later.

Adding Private Content:

The private content that we need to add are the VI’s that communicate with the external hardware. Make a folder called ‘private content’ in the root directory. Right click on WebService in the project and select Add Private Content Folder. Navigate into the created folder and select Select Folder.

Copy over the VI’s from my download project into this Private Folder. These VI’s include the library to communicate with the LaunchPad, a Variable Constants library and the VI that sends the commands.

Adding a Startup VI:

A Startup VI will run when the server is started and continue to run until the server is stopped. We will be using this VI to open and close the connection to the LaunchPad as the server is started and stopped.

Save the VI in a ‘Startup VI’ folder. Either create a new VI or copy mine and add it in the project. The VI should look like this.

Create the GET VI:

Now that we have added all the dependencies, we can go back and complete ToggleLED.vi. This VI needs to read the form data, pass the data to CommunicateToLaunchPad.vi and then redirect back to the main page.

We use the function Read Form Data.vi located in Functions>>Connectivity>>Web Services. The data is then passed into a subVI where the decision is made on what command to send to the LaunchPad. Notice that I have used a Variable Constant for the VISA Resource Name.

Once the hardware communication is complete, I redirect back to the index page. To get the original URL, use the Read Request Variable.vi function with the variable name REFERER. This will return the URL which can be passed into Set HTTP Redirect.vi.

Complete project:

Now that we have added all the files, we should have a complete project that looks like this.

We should now be ready to test our website. Save the entire project and Start the server. Navigate to the URL and you should see the following page.

Once the server is running and the page is open in the browser, open ToggleLED.vi front panel from the LabVIEW project. Select one of the radio buttons and then press a button. You will now be able to see what commands were received by LabVIEW by inspecting the indicators in ToggleLED.vi.

I have disabled the VISA functions in the uploaded project. If you want to try them, all you need to do is remove the Diagram Disable Structures from Startup.vi and ToggleLED.vi. This will then communicate with the LaunchPad; however if there is no hardware connected, the redirect will not work and an XML page will be returned after a button is pressed. You will also need to change the VISA Resource in Constants.lvlib:VisaPortNumber.vi.

That’s all for Part 2. If you have any questions, comments or advice for me, please leave a comment below. Part 3 should be up once I have decided on what to add and figured out how to do it.

Download LabVIEW Web Services part 2

Greg

Raspberry PI: Bottlepy and Twitter Bootstrap

I have been using my Raspberry Pi for the last few months quite a lot. I am currently working on a temperature/environment monitor that logs temperature, using a DS18B20, and light conditions using an optical sensor. I also have two LED’s and two buttons to test basic input and output.
I am developing the application in python using the BottlePy framework. I chose the BottlePy framework as it is a single file that you add, it is very small and it works with Python 3. To control my GPIO’s, I am using the Quick2Wire library. To display the temperature data, I am using Google Charts.

This is a picture of the main temperature logging page so far.
However, the purpose of this post is to show how to integrate BottlePy and Bootstrap by Twitter. According to their website, Bootstrap is a “Sleek, intuitive, and powerful front-end framework for faster and easier web development.”
To display this, I am going to use my GPIO code which reads buttons and turns on LED’s through a webpage. 

To start off we need python file that will run as the server. Below is a snippet of code. The @route shows that this particular function will be run when /gpio us entered into the browser. The webpage is broken up into four sections. The first controls LED 1, the second controls LED 2, the third gets the state of both buttons and light sensor and then the fourth runs a .c file to flash the LED’s.
Now we need to have a look at the html code that creates the webpage. By default, Bootstrap works with the user downloading the .css and .js file. These are then statically linked to the html code. When using the BottlePy framework, I haven’t found a way to return a webpage template, AND a static file. Therefore I have found a link where the .css and .js files have been hosted. By using these links, you only have access to the default settings, but they are enough for what I am wanting to do.
You have to link to the .js and .css file and then by using certain classes that have been defined and explained on the Bootstrap website, you can get nice looking buttons, navigation bars, and tables. They have many options to use but these are the three that I used here for my site. 
When a button on the website is pressed, it sends the value back to the python function and the python code will perform the task. With the Flash LED button, the python code calls a .c file that has been compiled and runs when called. 
When requesting the button state, I return hex formatted colours to show the state of the buttons. These the colour in the table. 

The red shows that Button 1 has been pressed, the Green shows that Button 2 is not pressed and the Blue shows that the light level is currently light.
There are many different elements that can be added by using the Bootstrap framework. The documentation is really clear and easy to follow with many examples. 
If you get stuck or need some more explanations give me a shout.
Greg