DennyP
Posts: 4
Joined: Sun Dec 29, 2013 7:47 am

Using Arduno with the Pi as a Analoginput datalogger withcsv

Sun Dec 29, 2013 8:48 am

Hello there,

For my first project on the Raspberry Pi, I'd like to couple it with Arduino using a interconnection shield (like gertuino or chipkit32Pi)
to record analog data. However I want it to start recording by pressing a button (digital_input =True) and stop recording when I press the same button again (if digital_input = True; digital_input = False) and once the button is pressed the second time, it should save all the data that was accumulated in the time-frame, to a .csv or .xls file (whatever is easier I guess).

The .csv will be saved into a directory in the Pi, which is connected to a network, where I'll drag it out from the main computer in the network.

My 3 month experience Python (with syntax errors) Psudocode will be of the form:

Code: Select all


"""" Setup """"
import RPi.GPIO as GPIO
CHANNEL = 7                                   ## Channel 7 constant
GPIO.setup(CHANNEL, GPIO.IN)   ## Set GPIO pin 7 

""" Boolean Generator helper function """" ##  should integrate a debounce code
def boolean_generator():
press_incrementer = 0      ##
    if GPIO.input(CHANNEL) = True:           ## current flows into the channel when button is pushed
           press_incrementer += 1
   return boolean_var = press_incrementer % 2     ## Take modulus 2 to create 1,0,1, etc to create the numerical boolean state
       
if boolean_generator() = 1:
       "Record serial data from Arduino"
else:
      "Stop recording data from Arduino"
      "Automatically save the accumulated data into a .csv file in a directory of the RPi.
I need help for me to enable me to do this though:). Would anybody guide me?

Cheers,
Denny P

DennyP
Posts: 4
Joined: Sun Dec 29, 2013 7:47 am

Re: Using Arduno with the Pi as a Analoginput datalogger wit

Sun Dec 29, 2013 9:44 pm

I realised I can use Pyserial to record the data:), but saving the data to excel will be the hardest thing for me.

fixitsan
Posts: 58
Joined: Sun Sep 29, 2013 4:28 pm

Re: Using Arduno with the Pi as a Analoginput datalogger wit

Mon Dec 30, 2013 8:21 am

Because Excel accepts CSV then it should be easy.

Often, data which is collected needs to be scaled or manipulated before it is written to an output file. There are at least two ways to do this - either capture all data in one go into a large buffer (Python is good for this), and then manipulate the data before writing it to the output file - or scale and manipulate the data as it arrives, live, and write it immediately to the output file in real time.

To create the output file do sometihing like f = open(/pi/mydata/capture.csv,'w') (where 'w' means write mode) and then write data to it with something like f.write("data to be written,"). If using write mode be prepared to lose the data from the last time yoiu ran the program, if yoiu want to add data to that file open it with 'a' mode, which means 'append' (add to the end of...)

My syntax is probably a bit wrong but the basic file handling and writing commands are easily good enough for what you want.

You could save directly to the SD card if you provide the correct path, or transmit the file over the network. You could also just run BaseHTTPServer and dish out the newly collected data to anyone who browses to the Pi's network address.

Chris

User avatar
DougieLawson
Posts: 37069
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Using Arduno with the Pi as a Analoginput datalogger wit

Mon Dec 30, 2013 6:33 pm

DennyP wrote:I realised I can use Pyserial to record the data:), but saving the data to excel will be the hardest thing for me.
I've got a php library (yes I know the evils of php) that reads a MySQL database and writes an excel spreadsheet (without going through CSV).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

DennyP
Posts: 4
Joined: Sun Dec 29, 2013 7:47 am

Re: Using Arduno with the Pi as a Analoginput datalogger wit

Fri Jan 03, 2014 6:38 am

Thank you guys for the feedback!

Here is my code atm:

Code: Select all

################## DATALOGGER ####################

##########
#Version 1
##########


#######SETUP########

'''Import Modules'''


import RPi.GPIO as GPIO
import serial
import csv
import time
import random

'''CONSTANTS'''

SWITCH_CHANNEL = 7                    #GPIO channel for input toggle switch 
RECORDING_LED_SWITCH_CHANNEL = 11     #GPIO channel for recording LED 

#SAMPLING_FREQUENCY = 10              #Used to average 1second values per every 10 seconds that has passed in the program

##CONVERTER_CONSTANT_A = 1              #A in A*x+B to convert Arduino serial to the required unit
##CONVERTER_CONSTANT_B = 0              #B in A*x+B to convert Arduino serial to the required unit       


'''GLOBAL VARIABLES'''

data_list = []                       #Initial empty list
counter = 0                          #Initial count
switch_state = False                 #Initial switch state

'''GPIO SETUP'''

GPIO.setmode(GPIO.BOARD)     #Use raspberry pi board numbering system
GPIO.setup(SWITCH_CHANNEL, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  #Set channel 7 as an input 



#######ACTION########


''' helper function to convert arduino serial to the desired unit'''

def serial_converter(serial_value):  # use this later
    return (CONVERTER_CONSTANT_A * serial_value) + CONVERTER_CONSTANT_B
     
''' helper function that changes the switch_state global variable via the switch'''

def boolean_generator(SWITCH_CHANNEL):
     global counter, switch_state
     counter += 1
     switch_state = bool(counter % 2)
    
''' recording function ''''

def recording_function():
      global switch_state, data_list
      while True:
          while switch_state == True:
              data_list.append(random.randrange(0,9))
              print data_list
              time.sleep(5)
              if switch_state == False:
                  data_list = []
                  print 'wiped'

      
''' program startup '''          
GPIO.add_event_detect(SWITCH_CHANNEL,  GPIO.RISING, boolean_generator, bouncetime = 300)
recording_function()

    
At the moment I'm just logging a random number between 0 and 8 as a stub before I use Pyserial. Some sections of the code are
commented out atm until it is needed, such as serial_converter function.

The code uses threaded callback to call a boolean generator function every-time the switched is pressed: it changes the global variable
switch_state from its original False value, to True and vica versa.

This gets used in the self activated recording_function, the first while loop forces the recording function to continuously seek when switch_state = True.
When switch_state becomes True from the button switch, the second while loop then gets used which adds a random number between 0 and 8 every 5 seconds (due to time.sleep(5)) to the original empty data_list global variable continuously until the button gets pressed that evaluates the if switch_state == False clause, which empties the list and prints for me 'data wiped' as a stub; this finishes the second while loop and so the program goes back to the first while loop constantly seeking another button press.

The biggest downside now (and the biggest problem for me implementing this program) is that when the program hangs on the first while loop, it hangs the CPU bigtime! as it constantly loops around every millisecond or so.

I know there would be a logic structure to bypass a constantly looping while loop, would anyone tip me off to the right direction?

Cheers,
DennyP

broo0ose
Posts: 317
Joined: Wed Dec 14, 2011 3:59 pm
Location: Wirral, UK

Re: Using Arduno with the Pi as a Analoginput datalogger wit

Fri Jan 03, 2014 4:16 pm

have you considered logging the data to a Google spreadsheet.

http://learn.adafruit.com/dht-humidity- ... g/overview

Return to “Advanced users”