Using csv file in diddy web scipt

Hi, Im currently using a modified Diddyborg web script to control me ROV. I have attached a ms 5837 02B depth sensor that allows me to know what depth my ROV is at. Max 10m. I save the results to data.csv file which I can access through an appache2 webserver. this is fie but It meas I must have a seperate webpage open and flick between pages or link it as a source on the diddy web page which takes up space on the screen.

Is it possible to use the csv file directly in the diddy web page similar to displaying distances from an ultraborg?
Also if it is possible could multiple files be displayed such as compass readings,temp,depth.

I have included my working script (its a bit rough! )

Thanks.

Attachments: 
piborg's picture

Yes, you can read CSV files into the Python script and have values shown on the web page.

We already have an example of adding a value display here. All you will need to do is replace WiFiSignalStrength() with the text or HTML you want to show.

For reading the CSV files you can use the standard csv module. There is a quick explanation of how to use it here.

Thanks for your help. I followed your advice and used the wifi signal strength example and modified it by putting in a call for my CSV.text file.
This works and I'm able to see the sensor results in my web page. unfortunatly the results flicker between "Not conected" and the result. I suspect this is because the results in the CSV file are constantly being over written and this part of the script is seeing that. :-

# See if we are connected
if len(wifiResult) == 0:
#Not connected
wifiStrength = 'Not connected'

How can I stop this?

Also I'm not sure what parts I can omit regarding the wifi percentage and how I should label things Ive left all the wifi labels which works but would like it to reflect My sensor as it reads the depth of the ROV.

Ive included both scripts.

On a side note could the sensor depth be used with the distance once parts and stop the motors when a certain depth is reached ?

Thanks in advance

piborg's picture

I think you are right, the flickering is caused because the script cannot always access the CSV file.

There are two things we need to handle to stop the flickering:

  1. A try .. except block around the file access to catch when it failed to open the file
  2. Save off the previous answer and use that when we cannot get CSV data

Most of the other file processing in that function was specific to the WiFi use and can be left out.

I think this basically covers what you need to get a clean display from the file:

# Read the sensor values from CSV file, use last result if we couldn't get any data
global latestSensorValues
latestSensorValues = 'NO DATA'
def SensorValues():
    global latestSensorValues

    # Read in the CSV data file
    try:
        csvQuery = open("data.csv", "r")
        csvData = csvQuery.readlines()
        csvQuery.close()
    except:
        # File not found or could not be opened
        csvData = ""

    # Update the sensor values only if we got any data
    if len(csvData) > 0:
        # Get the first line of data from the file and remove the '\n' from the end
        line1 = csvData[0]
        latestSensorValues = line1[:-1]

    # Return the updated or previous value
    return latestSensorValues
Subscribe to Comments for "Using csv file in diddy web scipt"