Raspberry Pi & openDAQ (II)

24 February 2013

In the previous article we have seen that Raspberry Pi can work with directly with openDAQ, without any more than plugging each other through the USB port. In this article we will discuss a simple example of some interesting things that could be done by connecting the two instruments.

One limitation that openDAQ presents is its connectivity, which is limited to serial communications through the USB port. You can not control and extract data from openDAQ if you do not have a computer nearby. With the aid of a Raspberry such limitation can be overcome easily. In fact, in this example we will see how to create a basic Web application in Raspberry Pi, in order to access data and control openDAQ from the Internet, from any other location where the user was.

Webserver with Raspberry Pi

On this occasion, we will use the Python module called Bottle. Bottle is a fast, simple and lightweight Web Server Gateway Interface for Python. It is a web micro-framework which is distributed as a single file module. It has no dependencies other than the Python Standard Library.

For this example we will use the module itself and the templates. In the Bottle site can help find multiple examples of how to create simple web servers.

Following it is the code from the sample program:

from bottle import route, run, static_file, request, template

import bottle

import socket

import sys

import struct

from daq import *

 

 

@route('/analog', method='GET')

def analog():

#get voltage value and set it in openDAQ

value = request.GET.get('valueDAC','').strip()

try:

valueF = int(value)

except:

valueF = 0

if valueF>=4096:

valueF = 4095

if valueF<=-4096:

valueF = -4095

valueF = float(valueF)

valueF /=1000

dq.set_analog(valueF)

#get configuration and configure ADC channel

try:

ch = int(request.GET.get('channel','').strip())

gain = int(request.GET.get('gain','').strip())

dq.conf_adc(pinput=ch,gain=gain)

#get adc value, gains and offset

adcValue = dq.read_analog()

print adcValue

 

except:

adcValue = 0

#run template

return template('analog',value = adcValue)

 

#connect to de device and run bottle webserver

dq=DAQ("/dev/ttyUSB1")

bottle.TEMPLATE_PATH.insert(0,'/home/pi/.views/')

run(host='192.168.0.26', port=8080, debug=True)

 

You can copy it and just make any modification to suit your needs.

Analog function returns a template. This template should be stored in the PATH indicated in Bottle: TEMPLATE_PATH.  The template must be passed with the value of ADC to insert it into the HTML.

Below there it is the template code.

 

<HTML> 

<HEAD> 

<TITLE>openDAQ server control</TITLE> 

</HEAD> 

<BODY> 

<H1 align="center" >openDAQ server </H1> 

<HR> 

<P> 

<form name="input" action="analog" method="get"> 

Set voltage (mv): <input type="text" name="valueDAC"><br> 

Select positive channel<select name="channel"> 

<option value="0" >(please select:)</option> 

<option value="1" >A1</option> 

<option value="2" >A2</option> 

<option value="3" >A3</option> 

<option value="4" >A4</option> 

<option value="5" >A5</option> 

<option value="6" >A6</option> 

<option value="7" >A7</option> 

<option value="8" selected>A8</option> 

</select><br> 

Select gain<select name="gain"> 

<option value="0" selected>(please select:)</option> 

<option value="1">+- 12 V</option> 

<option value="2">+- 4 V</option> 

<option value="3">+- 2 V</option> 

<option value="4">+- 0.4 V</option> 

<option value="5">+- 0.04 V</option> 

</select><br> 

<input type="submit" value="Refresh"> 

</form> 

</P> 

<h1>ADC value {{value} V}</h1> 

</BODY> 

</HTML>

 

Below it is shown the result of this example. When connecting to the RaspBerry Pi through a web browser (e.g. through ip 192.168.0.26:8080 / Analog), it displays a web form that allows:

• Setting analog output voltage

• Reading the ADC value by configuration (channel gain)