-2

I have app that I wrote last year reading data into a byte array for time, date and temperature from an external device. The external device stores the temperature ever hour and has six weeks worth of temperatures stored. The external device is constantly overwriting the oldest data so I always have the last six weeks worth of temperatures. My App has a string array 1008 long to store six weeks worth of temperatures as below

private static final String[] fullDataArray = new String[1008];

My app reads the raw data from the byte array then writes each element of the string array until it has done all 1008 as below,

fullDataArray[arrayCount] = (nowDataString + " Temp = " + tempString + "c");

This then displays the data in the string array in a list view as below,

private void printList(){
        // create array adaptor and assign array to list view
        final ArrayAdapter adapter = new ArrayAdapter(this,R.layout.data_layout,R.id.textItem,fullDataArray);
        setListAdapter(adapter);
    }

I'm then reading this list view which has six weeks worth of data for the date, time and temperature and manually writing to an excel file to save the logged data.

Is there some way to export either the raw byte array or the string array to save me manually copying all the data to excel before it is lost ?

I've not done any android programming prior to this App and have not done any since, so I'm a bit rusty.

Any ideas or suggestions on how to go about this would be most appreciated.

fatmanming
  • 165
  • 1
  • 3
  • 12
  • Define "from an external device" What is this for an external device? What capabilities does it have? WiFi? Do you have a server to send the data to store? – Barns Mar 29 '18 at 15:18
  • Its a pic microcontroller based unit controlling my Pond equipment. It's reading temperatures and storing data in an EEPROM. It sends the data back as a byte stream over Wifi using an ESP8266. I did it originally with Bluetooth but the ESP is connected to my home Wifi so has more range. – fatmanming Mar 29 '18 at 15:34
  • So the data is sent from the micro controller via WiFi to your Android device? From the Android device you want to export the date and temp data to a PC? – Barns Mar 29 '18 at 15:38
  • Yes, at the moment I'm reading from the list view and manually typing into excel, if I leave it a week or two it takes ages ! – fatmanming Mar 29 '18 at 15:43
  • The ESP is set up as ESP link and the data is in 6 byte chunks, day, month, year, hour and hi and low temp data from a DS18B20 temp sensor. – fatmanming Mar 29 '18 at 15:45
  • I think the easiest solution (maybe not the most elegant) would be: save the date and Temp data in a CSV file (excel can read them and sort data into columns) on your Android device. Then use Android to send an email to your email address using `Intents` here is a link how to do it :: https://stackoverflow.com/questions/9974987/how-to-send-an-email-with-a-file-attachment-in-android :: Another solution would be to use sockets and to connect to your PC, but that requires a whole different skill set. – Barns Mar 29 '18 at 15:51

1 Answers1

0

Thanks Barns for the suggestion.

I have created a CSV file in the internal storage of my phone, I can read that onto my PC to copy the data, no need to e-mail.

Have this on a old phone on API 18 as well as a new phone so struggled with creating a file to work on both API's, settled on the solution below.

Set the permissions in code

if(Build.VERSION.SDK_INT>22){
            requestPermissions(new String[] {"android.permission.WRITE_EXTERNAL_STORAGE"}, 1);
        }

set permissions in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

created a new file and opened the output stream

File file;
FileOutputStream fos;
OutputStreamWriter myOutWriter = null;

// create file and output stream to save data to csv file
String csvFile = "Temp Log.csv";
String path = Environment.getExternalStorageDirectory() + "/Documents";
file = new File(path, csvFile);
try {
    // create csv file stream
    Log.d(TAG,"Creating File Stream!");
    fos = new FileOutputStream(file);
    myOutWriter = new OutputStreamWriter(fos);
}catch(Exception e){
    e.printStackTrace();
}

created my string for the CSV file using the data for the list view

String csvData = new String(dayStr + "/" + monthStr + "/" + yearStr + "," + hourStr + ":00,");
csvData = (csvData + tempString);
csvData = (csvData + "\n");

write to CSV file

try {
     myOutWriter.write(csvData);
}catch(Exception e){
     e.printStackTrace();
}

close the stream

try {
    // close the csv output stream
    myOutWriter.close();
}catch(Exception e){
     e.printStackTrace();
}

This adds a line to the CSV file for each set of data as it creates the list view, and the CSV file is saved to the documents folder on my phones. Tried loads of different ways to get the path to the documents folder and file right, but the one above worked for both of the API's on my phones. Tried creating a xml file first but struggled to write data to it ?

Works well for what I want, and saves me loads of time but any suggestions for improvement would be appreciated.

fatmanming
  • 165
  • 1
  • 3
  • 12