{"id":1285,"date":"2012-10-08T13:26:31","date_gmt":"2012-10-08T17:26:31","guid":{"rendered":"http:\/\/lukemiller.org\/?p=1285"},"modified":"2012-10-08T13:26:31","modified_gmt":"2012-10-08T17:26:31","slug":"another-r-ibutton-script","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2012\/10\/another-r-ibutton-script\/","title":{"rendered":"Another R + iButton script"},"content":{"rendered":"<p><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/10\/ibutton_sm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-1289\" title=\"ibutton_sm\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2012\/10\/ibutton_sm.jpg\" alt=\"\" width=\"300\" height=\"259\" \/><\/a>Previously I&#8217;ve detailed R scripts that automate the <a href=\"https:\/\/lukemiller.org\/index.php\/2012\/03\/launching-ibutton-thermochrons-with-the-help-of-r\/\" target=\"_blank\">launching<\/a> and <a href=\"https:\/\/lukemiller.org\/index.php\/2012\/07\/r-scripts-for-downloading-ibutton-thermochron-dataloggers\/\" target=\"_blank\">downloading<\/a> Maxim iButton thermochron data loggers. I&#8217;m typically doing the launching and downloading at separate times in my workflow, since I have duplicate iButtons to swap out, so separate scripts work for me. Ryan Knowles recently contributed a combined version of these scripts that downloads and immediately re-launches each iButton. This is useful if you want to retrieve iButtons and then stick the same units back out in the field as soon as possible. As usual, this script saves the raw downloaded data from each iButton in one directory, and saves a collated csv file that contains the time and temperature information for the iButtons.<\/p>\n<p>If you haven&#8217;t read the previous posts that detail the additional software you&#8217;ll need, go back and <a href=\"https:\/\/lukemiller.org\/index.php\/2012\/03\/launching-ibutton-thermochrons-with-the-help-of-r\/\" target=\"_blank\">check it out<\/a>. The code below will make a little more sense once you do that.<\/p>\n<p>The new script, ibutton_download_launch_combined.R, is available at the <a href=\"http:\/\/github.com\/millerlp\/ibuttons\" target=\"_blank\">GitHub repository<\/a>. The code is also shown below.<\/p>\n<div style=\"overflow: auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family: monospace;\"><span style=\"color: #666666; font-style: italic;\"># Filename: ibutton_download_launch_combined.R<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Author: Luke Miller Jul 11, 2012 (as modified by Ryan Knowles Oct 04, 2012)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">###############################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># This script will allow the user to download iButtons, giving a unique name <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># to each file. It will parse the raw data file and extract the useful bits<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># (to me at least): date\/time, temperature (Celsius), user's ID for this <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># iButton, and the serial number for this iButton, and place all of these in a<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># comma-separated-value file that can be easily opened in Excel or R. Each <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># downloaded iButton data file has the current date and time included in the <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># filename to avoid overwriting older data files. Raw data files are stored in a <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory with today's date on it, while the parsed csv files go in a 2nd <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory.<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># The thermodl.exe file was originally downloaded as part of the Maxim iButton <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># 1-Wire Public Domain Kit. <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># There are several versions of the Kit available, including<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># versions with pre-compiled binaries (executables) for Windows\/Linux\/OSX.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/1wire\/wirekit.cfm<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># On my Windows 7 x64 computer using the DS9490B USB ibutton adapter, I used the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># precompiled binary build for Win64 USB (DS9490 + WinUSB) Preliminary Version <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># 3.11 Beta 2,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># filename: winusb64vc311Beta2_r2.zip, downloaded 2012-03-15<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Unzip this file and find the .exe files thermoms.exe and thermodl.exe in the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># builds\\winusb64vc\\release folder. Copy these to your R working directory.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The drivers for the DS9490 USB iButton adapter must also be downloaded and <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># installed: <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/tmex\/<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># I downloaded and installed the file \"install_1_wire_drivers_x64_v403.msi\"<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The Java OneWireViewer app can also be downloaded and used to verify that your<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># drivers work and that you can communicate with iButtons successfully through <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># the USB adapter. You can download this app here: <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># http:\/\/www.maxim-ic.com\/products\/ibutton\/software\/1wire\/OneWireViewer.cfm<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># NOTE: the Maxim program \"thermodl.exe\" must be present in the current R <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># working directory. Uncomment the setwd() line below to change the R working <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># directory. Enter your working directory location inside the quotes.<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/setwd\"><span style=\"color: #003399; font-weight: bold;\">setwd<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'C:\/Users\/Ryan\/Dropbox\/R\/'<\/span><span style=\"color: #009900;\">)<\/span> \r\n\r\ncur.date = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.Date\"><span style=\"color: #003399; font-weight: bold;\">Sys.Date<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Get current date<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Assemble a directory name to store raw downloaded data into<\/span>\r\ndir.name = <span style=\"color: #0000ff;\">\"C:\/Users\/Ryan\/Dropbox\/R\/temp\"<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Assemble a directory name to store the parsed csv output files in<\/span>\r\ndir.name2 = <span style=\"color: #0000ff;\">\"C:\/Users\/Ryan\/Dropbox\/Project\/Heat Stress\/Data\/ibutton\"<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Check to see if that folder already exists, if not then create it.<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file.info\"><span style=\"color: #003399; font-weight: bold;\">file.info<\/span><\/a><span style=\"color: #009900;\">(<\/span>dir.name<span style=\"color: #009900;\">)<\/span><span>$<\/span>isdir<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/dir.create\"><span style=\"color: #003399; font-weight: bold;\">dir.create<\/span><\/a><span style=\"color: #009900;\">(<\/span>dir.name<span style=\"color: #009900;\">)<\/span>\t\r\n<span style=\"color: #009900;\">}<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Check to see if the other folder already exists, if not then create it.<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file.info\"><span style=\"color: #003399; font-weight: bold;\">file.info<\/span><\/a><span style=\"color: #009900;\">(<\/span>dir.name2<span style=\"color: #009900;\">)<\/span><span>$<\/span>isdir<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/dir.create\"><span style=\"color: #003399; font-weight: bold;\">dir.create<\/span><\/a><span style=\"color: #009900;\">(<\/span>dir.name2<span style=\"color: #009900;\">)<\/span>\t\r\n<span style=\"color: #009900;\">}<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Enter a desired mission start time YYYY-MM-DD HH:MM<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Enter 0 for immediate start. Delay must be less than 45.5 days.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\ntime.delay = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> n = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">','<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The sep value in scan is necessary so that spaces are not interpreted as the<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># default record delimiter.<\/span>\r\n\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>time.delay <span>==<\/span> <span style=\"color: #0000ff;\">'0'<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\tlaunch = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span>\r\n<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Convert the date\/time into a POSIX time object<\/span>\r\n\ttime.delay = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXct\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXct<\/span><\/a><span style=\"color: #009900;\">(<\/span>strptime<span style=\"color: #009900;\">(<\/span>time.delay<span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">'%Y-%m-%d %H:%M'<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\"># Check to make sure that the delay time is usable<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">(<\/span>time.delay<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># If time.delay can't be interpreted, fail <\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Time could not be interpreted<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Quitting now<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\tlaunch = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># If time.delay is a valid POSIX time, check that it is within limits<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><span>!<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">(<\/span>time.delay<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\tcurr.time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXct\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXct<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.time\"><span style=\"color: #003399; font-weight: bold;\">Sys.time<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># get current time<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Check time difference between user's delay and current computer time<\/span>\r\n\t\tt.diff = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span>time.delay<span style=\"color: #009900;\">)<\/span> <span>-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span>curr.time<span style=\"color: #009900;\">)<\/span>\r\n\t\tt.diff = t.diff <span>\/<\/span> <span style=\"color: #cc66cc;\">60<\/span> <span style=\"color: #666666; font-style: italic;\"># convert to minutes<\/span>\r\n\r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>t.diff <span>&lt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Time delay is less than zero. Check your watch.<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Quitting now<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\tlaunch = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>t.diff <span>&gt;<\/span> <span style=\"color: #cc66cc;\">65535<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Time delay is longer than 45.5 days. You are bad at math.<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Quitting now<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\tlaunch = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>t.diff <span>&gt;<\/span> <span style=\"color: #cc66cc;\">0<\/span> <span>&amp;<\/span> t.diff <span>&lt;<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Time delay is being set to 0 for immediate launch.<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\tlaunch = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span> \r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># time.delay is a readable time, and t.diff is between 0 and 65535<\/span>\r\n\t\t\tlaunch = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># end of !is.na(time.delay) if-statement<\/span>\r\n<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># end of time.delay if-statement<\/span>\r\n\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Enter the desired sampling frequency in minutes (1 to 255):<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\nfreq = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/numeric\"><span style=\"color: #003399; font-weight: bold;\">numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> n = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span>\r\nfreq = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.character\"><span style=\"color: #003399; font-weight: bold;\">as.character<\/span><\/a><span style=\"color: #009900;\">(<\/span>freq<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># convert to character<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">#######################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># This main while loop will repeat continuously to download data and launch<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># multiple iButtons. The same parameters will be used to launch every iButton,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># except that the start delay (if &gt;0) will automatically adjust as time<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># elapses so that each iButton will start at the same absolute time.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Data is downloaded in a separate sequence from the launch so the launch may<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># fail even if the download is successful. The opposite situation is prevented<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># since launching a new iButton will delete the previous data before it can<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># be downloaded successfully.<\/span>\r\nCounter = <span style=\"color: #cc66cc;\">0<\/span>\r\nloop = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span>\r\n<span style=\"color: #000000; font-weight: bold;\">while<\/span><span style=\"color: #009900;\">(<\/span>loop<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\tCounter = Counter <span>+<\/span> <span style=\"color: #cc66cc;\">1<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Get current time to insert in filename so we don't overwrite old data<\/span>\r\n\tcurrTime = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/strftime\"><span style=\"color: #003399; font-weight: bold;\">strftime<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.time\"><span style=\"color: #003399; font-weight: bold;\">Sys.time<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%Y%m%d_%H%M%S\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Assemble filename<\/span>\r\n\tfname = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>dir.name<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\\\<\/span>'<\/span><span style=\"color: #339933;\">,<\/span>currTime<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'.dat'<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Call the thermodl.exe program provided by Maxim. This must be in the <\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># current R working directory. Start by assembling a command to be issued<\/span>\r\n\tsys.com = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'thermodl.exe ds2490-0 '<\/span><span style=\"color: #339933;\">,<\/span> fname<span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Now issue the command to the system using the system() function<\/span>\r\n\ttemp = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span>sys.com<span style=\"color: #339933;\">,<\/span> wait = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># The raw downloaded data should now be in the \"raw_downloads\" directory <\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># created at the start of the script.<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Output the status updates from thermodl.exe <\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>temp<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">7<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>temp<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">9<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>temp<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">18<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>temp<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">20<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Open the data file created by thermodl.exe<\/span>\r\n\tx <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/readLines\"><span style=\"color: #003399; font-weight: bold;\">readLines<\/span><\/a><span style=\"color: #009900;\">(<\/span>fname<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># read data into a character vector<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Extract serial number from line 4 of file<\/span>\r\n\tserialnum <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">(<\/span>x<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">37<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">39<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\"># Parse the iButton data file to extract the relevant temp\/time data<\/span>\r\n\tlog.start <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/grep\"><span style=\"color: #003399; font-weight: bold;\">grep<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Log Data'<\/span><span style=\"color: #339933;\">,<\/span> x<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Find what line the log data starts below<\/span>\r\n\tlog.start <span>&lt;-<\/span> log.start <span>+<\/span> <span style=\"color: #cc66cc;\">3<\/span> <span style=\"color: #666666; font-style: italic;\"># change value to first line of log data<\/span>\r\n\tlog.end <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/grep\"><span style=\"color: #003399; font-weight: bold;\">grep<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Debug Dump'<\/span><span style=\"color: #339933;\">,<\/span> x<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Find where the log data ends (roughly)<\/span>\r\n\tlog.end <span>&lt;-<\/span> log.end <span>-<\/span> <span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #666666; font-style: italic;\"># change value to last line of log data<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\"># Check if there were any logged temperature data<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><span>!<\/span><span style=\"color: #009900;\">(<\/span>log.end <span>-<\/span> log.start <span>&lt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\ttemps <span>&lt;-<\/span> x<span style=\"color: #009900;\">[<\/span>log.start<span>:<\/span>log.end<span style=\"color: #009900;\">]<\/span> <span style=\"color: #666666; font-style: italic;\"># extract log data, still as characters<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Convert time stamps to POSIX object<\/span>\r\n\t\ttimes <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXct\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXct<\/span><\/a><span style=\"color: #009900;\">(<\/span>strptime<span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">(<\/span>temps<span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">17<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">'%m\/%d\/%Y %H:%M'<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\ttemp.data <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/data.frame\"><span style=\"color: #003399; font-weight: bold;\">data.frame<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/time\"><span>Time<\/span><\/a> = times<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># stick into a new data frame<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># convert temps to numeric and insert into temp.data<\/span>\r\n\t\ttemp.data<span>$<\/span>TempC <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">(<\/span>temps<span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">20<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">26<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Insert column with iButton's unique serial number<\/span>\r\n\t\ttemp.data<span>$<\/span>Serial.number <span>&lt;-<\/span> serialnum\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Output temperature data to console<\/span>\r\n\r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>Counter <span>==<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># Copy times and temps for first ibutton<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># The times and number of readings of the first ibutton<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># will dictate how much is read from each subsequent button.<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># For example, if the first ibutton has 100 readings and the<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># second has 120 readings, only the first 100 readings of the<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># second ibutton will be saved in the combined csv file.<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># The remaining data will still be available in the .dat file.<\/span>\r\n\t\t\tcombdatah <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.data.frame\"><span style=\"color: #003399; font-weight: bold;\">as.data.frame<\/span><\/a><span style=\"color: #009900;\">(<\/span>temp.data<span>$<\/span>Time<span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>combdatah<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span> <span>&lt;-<\/span> <span style=\"color: #0000ff;\">\"Time\"<\/span>\r\n\t\t\ttemprow <span>&lt;-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/nrow\"><span style=\"color: #003399; font-weight: bold;\">nrow<\/span><\/a><span style=\"color: #009900;\">(<\/span>combdatah<span style=\"color: #009900;\">)<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of first button IF<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Only copy temps for all subsequent ibuttons (add *'s to ibuttons with<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># times that don't line up with the first ibutton)<\/span>\r\n\t\tColcounter <span>&lt;-<\/span> Counter <span>+<\/span> <span style=\"color: #cc66cc;\">1<\/span>\r\n\t\tCheckdiff = temp.data<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span> <span style=\"color: #666666; font-style: italic;\"># Extract time<\/span>\r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span><span style=\"color: #009900;\">(<\/span>Checkdiff <span>!<\/span>= combdatah<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\tserialnum<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>serialnum<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"*\"<\/span><span style=\"color: #339933;\">,<\/span>sep=<span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Add * to serialnum<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> \r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Add temperature data to combined data frame<\/span>\r\n\t\tcombdatah<span style=\"color: #009900;\">[<\/span>Colcounter<span style=\"color: #009900;\">]<\/span> <span>&lt;-<\/span> temp.data<span>$<\/span>TempC<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span>:<\/span>temprow<span style=\"color: #009900;\">]<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Rename new temp data with serial num.<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>combdatah<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">[<\/span>Colcounter<span style=\"color: #009900;\">]<\/span> <span>&lt;-<\/span> serialnum \r\n\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Temperature data downloaded <span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># Continue logged data check IF<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>*****No temperature data*****<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Would you still like to launch this iButton?<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Doing so will erase any data that has not been downloaded.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Enter y to continue.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Leave blank to skip launch and (re)download ibutton.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Ask user to continue<\/span>\r\n\t\tcont.launch = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> n = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span> \r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/length\"><span style=\"color: #003399; font-weight: bold;\">length<\/span><\/a><span style=\"color: #009900;\">(<\/span>cont.launch<span style=\"color: #009900;\">)<\/span> <span>&gt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># Allows user to not type anything<\/span>\r\n\t\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>cont.launch <span>==<\/span> <span style=\"color: #0000ff;\">'y'<\/span><span style=\"color: #009900;\">)<\/span> launch = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span>launch = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #009900;\">}<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Cancel launch if entry is anything but 'y'<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of logged data check IF<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">############################################################################<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">## Start the launch loop. <\/span>\r\n\r\n\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>launch<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># only do this part if launch == TRUE<\/span>\r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>time.delay <span>!<\/span>= <span style=\"color: #0000ff;\">'0'<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\tcurr.time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXct\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXct<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.time\"><span style=\"color: #003399; font-weight: bold;\">Sys.time<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Get current time<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># Calculate difference between current time and time.delay value<\/span>\r\n\t\t\ttime.diff = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span>time.delay<span style=\"color: #009900;\">)<\/span> <span>-<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span>curr.time<span style=\"color: #009900;\">)<\/span>\r\n\t\t\ttime.diff = time.diff <span>\/<\/span> <span style=\"color: #cc66cc;\">60<\/span> <span style=\"color: #666666; font-style: italic;\"># convert from seconds to minutes<\/span>\r\n\t\t\ttime.diff = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/floor\"><span style=\"color: #003399; font-weight: bold;\">floor<\/span><\/a><span style=\"color: #009900;\">(<\/span>time.diff<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># round down to nearest minute<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># iButtons only read out to the nearest minute. Rounding down to the<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># nearest minute should produce the proper delay to start at the <\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># desired time. <\/span>\r\n\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># If too much time elapses during this script, the time difference <\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># could eventually shrink to zero or less than zero. In that case, <\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># warn the user and set the iButton for immediate launch. <\/span>\r\n\t\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>time.diff <span>&lt;<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\ttime.diff = <span style=\"color: #cc66cc;\">0<\/span> <span style=\"color: #666666; font-style: italic;\"># set for immediate launch.<\/span>\r\n\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'*********************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Delay has shrunk to zero. Setting for immediate launch.'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'*********************************<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End time.diff &lt; 1 IF<\/span>\r\n\t\t\ttime.diff = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.character\"><span style=\"color: #003399; font-weight: bold;\">as.character<\/span><\/a><span style=\"color: #009900;\">(<\/span>time.diff<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># convert to character <\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># time.delay was originally set to 0, so keep that.<\/span>\r\n\t\t\ttime.diff <span>==<\/span> <span style=\"color: #0000ff;\">'0'<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End time delay != 0 IF<\/span>\r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Calculated delay: '<\/span><span style=\"color: #339933;\">,<\/span> time.diff<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">' minutes<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># The thermoms.exe program expects a series of inputs in order to establish the<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># mission parameters. Rgui doesn't work all that well with interactive command <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># line programs (Rterm is just fine, but our goal is to not have to interact), <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># so instead we'll create a character vector of answers to thermoms.exe's <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># queries and supply those via the input option of the system() function. <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># The parameters are as follows:<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Erase current mission (0) yes, (1) no. Answer: 0<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Enter start delay in minutes (0 to 65535). Answer: whatever you choose<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Enter sample rate in minutes (1 to 255). Answer: whatever you choose<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Enable roll-over (0) yes, (1) no. Answer: 1<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Enter high temperature threshold in Celsius (-40 to 70). Answer: 70<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Enter low temperature threshold in Celsius (-40 to 70). Answer: -40<\/span>\r\n\r\n\t\tmission.params = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'0'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #666666; font-style: italic;\"># erase current mission <\/span>\r\n\t\t\t\ttime.diff<span style=\"color: #339933;\">,<\/span> <span style=\"color: #666666; font-style: italic;\"># start delay in minutes (0 to 65535)<\/span>\r\n\t\t\t\tfreq<span style=\"color: #339933;\">,<\/span> <span style=\"color: #666666; font-style: italic;\"># sample rate in minutes (1 to 255)<\/span>\r\n\t\t\t\t<span style=\"color: #0000ff;\">'1'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #666666; font-style: italic;\"># enable roll-over? 1 = no roll over<\/span>\r\n\t\t\t\t<span style=\"color: #0000ff;\">'70'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #666666; font-style: italic;\"># high temperature threshold<\/span>\r\n\t\t\t\t<span style=\"color: #0000ff;\">'-40'<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># low temperature threshold<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Launch thermoms.exe<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># The 1st argument supplied to thermoms needs to be the location of the iButton <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># in the system. If using a DS9490B USB reader on Windows, you will probably get <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># away with using ds2490-0. The DS9490 USB reader uses a ds2490 chip internally, <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># so you need to tell thermoms.exe to look for a ds2490. <\/span>\r\n\t\tout = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'thermoms.exe ds2490-0'<\/span><span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span> wait = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t\t\tinput = mission.params<span style=\"color: #009900;\">)<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># Check the output from the mission launch to ensure that the correct parameters<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># were registered. Occasionally the time delay will not be properly registered <\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># on the first launch, so the loops below will immediately relaunch the mission<\/span>\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># to get the time delay to register properly.<\/span>\r\n\r\n\t\t<span style=\"color: #666666; font-style: italic;\"># If no iButton is plugged in, this should be the failure message<\/span>\r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>out<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">7<\/span><span style=\"color: #009900;\">]<\/span> <span>==<\/span> <span style=\"color: #0000ff;\">'Thermochron not present on 1-Wire'<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'******************************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>out<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">7<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'******************************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span><span style=\"color: #000099; font-weight: bold;\">\\a<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># if out[7] is blank, a mission was probably uploaded<\/span>\r\n\t\t\t<span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">(<\/span>i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #cc66cc;\">73<\/span><span>:<\/span><span style=\"color: #cc66cc;\">90<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># Display read-back from mission upload<\/span>\r\n\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>out<span style=\"color: #009900;\">[<\/span>i<span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># Make sure the delay was actually entered correctly if it's &gt;0<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># This while loop will run a maximum of 3 times. Each time through <\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># it will compare the output in out[73] to make sure the correct <\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># delay was returned from the iButton. If not, it will re-launch the<\/span>\r\n\t\t\t<span style=\"color: #666666; font-style: italic;\"># mission up to two more times before sending a failure message<\/span>\r\n\t\t\tretry = <span style=\"color: #cc66cc;\">0<\/span>\r\n\t\t\t<span style=\"color: #000000; font-weight: bold;\">while<\/span> <span style=\"color: #009900;\">(<\/span>retry <span>&lt;<\/span> <span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\tsetting = out<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">79<\/span><span style=\"color: #009900;\">]<\/span>\r\n\t\t\t\tnums = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/gregexpr\"><span style=\"color: #003399; font-weight: bold;\">gregexpr<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'[0-9]'<\/span><span style=\"color: #339933;\">,<\/span> setting<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Find numbers in the string<\/span>\r\n\t\t\t\tdigs = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">(<\/span>setting<span style=\"color: #339933;\">,<\/span> nums<span style=\"color: #009900;\">[<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t\t\t\t\tnums<span style=\"color: #009900;\">[<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">[<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/length\"><span style=\"color: #003399; font-weight: bold;\">length<\/span><\/a><span style=\"color: #009900;\">(<\/span>nums<span style=\"color: #009900;\">[<\/span><span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\"># Extract delay value<\/span>\r\n\t\t\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>digs <span>!<\/span>= time.diff <span>&amp;<\/span> retry <span>&lt;<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># If delay value returned by iButton doesn't match the <\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># programmed delay, warn the user and re-launch the mission<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'*****************************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'****Launch did not work, re-launching****<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'*****************************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\tout = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'thermoms.exe ds2490-0'<\/span><span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\t\t\t\t\twait = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span> input = mission.params<span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">(<\/span>i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #cc66cc;\">73<\/span><span>:<\/span><span style=\"color: #cc66cc;\">90<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># Display the newly returned values<\/span>\r\n\t\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>out<span style=\"color: #009900;\">[<\/span>i<span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\t\t\t\t\tretry = retry <span>+<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #666666; font-style: italic;\"># increment the loop counter<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'---------------------------<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>digs <span>!<\/span>= time.diff <span>&amp;<\/span> retry <span>==<\/span> <span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># If the returned delay still doesn't match the programmed<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># delay after two more iterations, send a failure message<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># to the user and let them deal with this issue. <\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># A common failure mode is due to a dead battery, which will<\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># keep returning a clock time of 01\/01\/1970 00:00:00<\/span>\r\n\t\t\t\t\tretry = <span style=\"color: #cc66cc;\">3<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'****************************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'*****Uploaded failed, check iButton*****<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'****************************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<span style=\"color: #000000; font-weight: bold;\">for<\/span> <span style=\"color: #009900;\">(<\/span>i <span style=\"color: #000000; font-weight: bold;\">in<\/span> <span style=\"color: #cc66cc;\">73<\/span><span>:<\/span><span style=\"color: #cc66cc;\">90<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>out<span style=\"color: #009900;\">[<\/span>i<span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t\t\t\t\tanswer = out<span style=\"color: #009900;\">[<\/span><span style=\"color: #cc66cc;\">85<\/span><span style=\"color: #009900;\">]<\/span> <span style=\"color: #666666; font-style: italic;\"># Check the iButton's internal clock<\/span>\r\n\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># Find the location of the date in this line (if present)<\/span>\r\n\t\t\t\t\td1 = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/regexpr\"><span style=\"color: #003399; font-weight: bold;\">regexpr<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'[0-9]{2}\/[0-9]{2}\/[0-9]{4}'<\/span><span style=\"color: #339933;\">,<\/span> answer<span style=\"color: #009900;\">)<\/span>\r\n\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># Extract the date as a character string<\/span>\r\n\t\t\t\t\tbutton.date = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">(<\/span>answer<span style=\"color: #339933;\">,<\/span> d1<span style=\"color: #339933;\">,<\/span> \r\n\t\t\t\t\t\t\td1 <span>+<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/attr\"><span style=\"color: #003399; font-weight: bold;\">attr<\/span><\/a><span style=\"color: #009900;\">(<\/span>d1<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'match.length'<\/span><span style=\"color: #009900;\">)<\/span> <span>-<\/span> <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># If the iButton date returns 01\/01\/1970, the iButton <\/span>\r\n\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># battery is probably dead<\/span>\r\n\t\t\t\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>button.date <span>==<\/span> <span style=\"color: #0000ff;\">'01\/01\/1970'<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'********************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'The iButton battery may be dead.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'********************************<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\r\n\t\t\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>digs <span>==<\/span> time.diff<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># iButton mission launch worked<\/span>\r\n\t\t\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>----------Success---------<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t\t\t\tretry = <span style=\"color: #cc66cc;\">3<\/span>\r\n\t\t\t\t<span style=\"color: #009900;\">}<\/span>\r\n\t\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of retry while-loop<\/span>\r\n\t\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of if (out[7]... if-else statements<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of launch IF <\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'<span style=\"color: #000099; font-weight: bold;\">\\a<\/span><span style=\"color: #000099; font-weight: bold;\">\\n<\/span>---------------------<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Swap in next iButton and hit enter. Enter q to quit.<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span>\r\n\tuser.input = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/file\"><span style=\"color: #003399; font-weight: bold;\">file<\/span><\/a> = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> n = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/length\"><span style=\"color: #003399; font-weight: bold;\">length<\/span><\/a><span style=\"color: #009900;\">(<\/span>user.input<span style=\"color: #009900;\">)<\/span> <span>&gt;<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\"># Allows user to not type anything<\/span>\r\n\t\t<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>user.input <span>==<\/span> <span style=\"color: #0000ff;\">'q'<\/span><span style=\"color: #009900;\">)<\/span> loop = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span> <span style=\"color: #666666; font-style: italic;\"># quit out of while loop<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t\tloop = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span> <span style=\"color: #666666; font-style: italic;\"># return to start of while loop to download next iButton<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of \"q\" IF<\/span>\r\n\r\n<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\"># End of main loop<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Write the contents of combdatah to a comma-separated-values file.<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/write.csv\"><span style=\"color: #003399; font-weight: bold;\">write.csv<\/span><\/a><span style=\"color: #009900;\">(<\/span>combdatah<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">\"horizontal.csv\"<\/span><span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/row.names\"><span style=\"color: #003399; font-weight: bold;\">row.names<\/span><\/a> = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Finished<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>'<\/span><span style=\"color: #009900;\">)<\/span><\/pre>\n<\/div>\n<\/div>\n<p><a title=\"Created by Pretty R at inside-R.org\" href=\"http:\/\/www.inside-r.org\/pretty-r\">Created by Pretty R at inside-R.org<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I&#8217;ve detailed R scripts that automate the launching and downloading Maxim iButton thermochron data loggers. I&#8217;m typically doing the launching and downloading at separate times in my workflow, since I have duplicate iButtons to swap out, so separate scripts work for me. Ryan Knowles recently contributed a combined version of these scripts that downloads [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[125,137,130],"class_list":["post-1285","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-ibutton","tag-r-script","tag-thermochron"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=1285"}],"version-history":[{"count":7,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1285\/revisions"}],"predecessor-version":[{"id":1370,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1285\/revisions\/1370"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=1285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=1285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=1285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}