|
| 1 | +/* Usage example for: https://github.com/michalmonday/CSV-Parser-for-Arduino */ |
| 2 | + |
| 3 | +#include <CSV_Parser.h> |
| 4 | + |
| 5 | +/* |
| 6 | +Python script used to send the file through serial (from PC to arduino) can be found at: |
| 7 | +https://github.com/michalmonday/CSV-Parser-for-Arduino/blob/master/examples/reading_from_computer_python/arduino_serial.py |
| 8 | +
|
| 9 | +The csv file I used for this particular example can be found at: |
| 10 | +https://github.com/michalmonday/CSV-Parser-for-Arduino/blob/master/examples/reading_from_computer_python/hurricanes.csv |
| 11 | +
|
| 12 | +This example shows how Arduino can receive and parse csv file from a computer (sent using a python script). |
| 13 | +It first sends the file from PC to Arduino, Arduino parses it, for verification purposes it sends values |
| 14 | +from 2 columns ("Month", "Average") back to PC (python scripts reads them and prints them in console window). |
| 15 | +In this particular example the csv file I used was containing information about hurricanes. The csv file contained: |
| 16 | +
|
| 17 | +Month,Average,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015 |
| 18 | +May,0.1,0,0,1,1,0,0,0,2,0,0,0 |
| 19 | +Jun,0.5,2,1,1,0,0,1,1,2,2,0,1 |
| 20 | +Jul,0.7,5,1,1,2,0,1,3,0,2,2,1 |
| 21 | +Aug,2.3,6,3,2,4,4,4,7,8,2,2,3 |
| 22 | +Sep,3.5,6,4,7,4,2,8,5,2,5,2,5 |
| 23 | +Oct,2.0,8,0,1,3,2,5,1,5,2,3,0 |
| 24 | +Nov,0.5,3,0,0,1,1,0,1,0,1,0,1 |
| 25 | +Dec,0.0,1,0,1,0,0,0,0,0,0,0,1 |
| 26 | +
|
| 27 | +After uploading this code to arduino and running the python script (command: python --csv hurricanes.csv), |
| 28 | +the python script outputs the following: |
| 29 | +
|
| 30 | +All ports: |
| 31 | +1. device=COM1 name=COM1 description=Communications Port (COM1) manufacturer=(Standard port types) |
| 32 | +2. device=COM4 name=COM4 description=Arduino Leonardo (COM4) manufacturer=Arduino LLC (www.arduino.cc) |
| 33 | +
|
| 34 | +A single arduino port was found and will be used |
| 35 | +COM4 - Arduino Leonardo (COM4) |
| 36 | +Received: |
| 37 | +0. month = May |
| 38 | +0. average = 0.10 |
| 39 | +1. month = Jun |
| 40 | +1. average = 0.50 |
| 41 | +2. month = Jul |
| 42 | +2. average = 0.70 |
| 43 | +3. month = Aug |
| 44 | +3. average = 2.30 |
| 45 | +4. month = Sep |
| 46 | +4. average = 3.50 |
| 47 | +5. month = Oct |
| 48 | +5. average = 2.00 |
| 49 | +6. month = Nov |
| 50 | +6. average = 0.50 |
| 51 | +7. month = Dec |
| 52 | +7. average = 0.00 |
| 53 | +
|
| 54 | +Received: |
| 55 | +end |
| 56 | +Closing port |
| 57 | +
|
| 58 | +
|
| 59 | +Note: I suggest to close Serial Monitor window (if it's open) before running the python script, |
| 60 | + otherwise the port may be busy. |
| 61 | +*/ |
| 62 | + |
| 63 | +void setup() { |
| 64 | + Serial.begin(115200); |
| 65 | + CSV_Parser cp(/*format*/ "sfccccccccccc"); |
| 66 | + |
| 67 | + // wait for python script to start sending the csv file |
| 68 | + while(!Serial.available()) {} |
| 69 | + |
| 70 | + // receive the file and pass it to CSV_Parser object |
| 71 | + while(Serial.available()) { |
| 72 | + cp << Serial.read(); |
| 73 | + } |
| 74 | + |
| 75 | + cp.parseLeftover(); // just in case if the csv file doesn't end with "\n" |
| 76 | + |
| 77 | + // get "Month" and "Average" columns |
| 78 | + char **months = (char**)cp["Month"]; |
| 79 | + float *averages = (float*)cp["Average"]; |
| 80 | + |
| 81 | + // Send "Month" and "Average" columns back to the PC |
| 82 | + // (python script will read and print them in console window) |
| 83 | + for(int row = 0; row < cp.getRowsCount(); row++) { |
| 84 | + Serial.print(row, DEC); |
| 85 | + Serial.print(". month = "); |
| 86 | + Serial.println(months[row]); |
| 87 | + Serial.print(row, DEC); |
| 88 | + Serial.print(". average = "); |
| 89 | + Serial.println(String(averages[row])); |
| 90 | + } |
| 91 | + Serial.println(); |
| 92 | + |
| 93 | + // the python script is set to terminate after receiving "end" message |
| 94 | + delay(500); |
| 95 | + Serial.print("end"); |
| 96 | +} |
| 97 | + |
| 98 | +void loop() { |
| 99 | + |
| 100 | +} |
0 commit comments