Shop OBEX P1 Docs P2 Docs Learn Events
insert or attach txt file in email message — Parallax Forums

insert or attach txt file in email message

lfreezelfreeze Posts: 174
edited 2013-06-17 17:07 in Accessories
Thanks to the excellent help from the forum, I have made a lot of progress with my project, but
have gotten stuck with an SMTP component. I have been successful with sending an email message
of a single sensor reading. I would like to routinely email the entire file that has been created on
The sd card . I started by attempting to read the sdcard file, one line at a time. If successful,
I would then add the code to send each line after it was read. I was not successful with the
read attempts. Here is the snippet of code I am trying to use for the reads.
PUB SendTestEmail(id) | size, tempMask, wait
  
  waitcnt(clkfreq/10 + cnt)     
 
  SDCard.changeDirectory(@approot) 
  
          if(FileExists(@TESTfile))
               SER.STR(string("   LINE 1063 FILE EXISTS",13))              
               sdcard.openFile(string("test.txt"), "R") 
               
          else
               SER.STR(string("   LINE 1066 FILE DOES NOT EXIST",13))

   repeat until(sdcard.filetell == sdcard.filesize)  'repeat until end of file 
           SER.str(string("  filesize =  ")) 
           ser.dec(sdcard.filesize)
           SER.str(string(13,"    "))  

           SER.str(string("  filetell =  ")) 
           ser.dec(sdcard.filetell)
           SER.str(string(13,"    "))  

          ableX:=sdcard.readbyte
          waitcnt(clkfreq/10 + cnt)  
          SER.str(ableX)
          SER.dec(ableX)
          SER.str(string("   LINE 1072   "))


  waitcnt(clkfreq/10 + cnt)
  sdcard.unmountPartition                            'CLOSES THE FILE
  waitcnt(clkfreq + cnt)
  sdcard.fatEngineStop                            ' Give the block driver a second to finish up.


As an alternative to sending the file a line at time, is it possible to simply send the file
as an attachment to the email message? This would simplify the process,
But I don’t know how to accomplish this.

Thanks in advance for any help.

Larry

Comments

  • RforbesRforbes Posts: 281
    edited 2013-06-14 14:16
    Hey Larry,

    What are you actually doing with the SER object? Is this being displayed on a terminal? Or something else? I'm assuming you're sending it to a terminal to view, but just wanted to check.

    Your method is writing each byte of data from the sd card file to ableX ... so the value changes with each iteration of the loop. Not sure why you're doing this- more code snippet might clarify that.

    If you're able to send an email with ONE sensor value, you should be able to do the same with any number of them (within reason.) So, read your data from the sd card file to a buffer, and then include the entire buffer instead of the individual sensor value in your successful email code.
  • Mike GMike G Posts: 2,702
    edited 2013-06-14 18:35
    Hi Larry, Rforbes nailed it. Write values to a file and when ready read the file contents into RAM and send the data to the SMTP server.

    I built a little test to verify functionality, which you can find below.
        ' Open the file for reading
        if(FileExists(string("email.txt")))
          SDCard.openFile(string("email.txt"), "r")
          fileSize := SDCard.getFileSize
          pst.char(13)
          pst.str(string("File size: "))
          pst.dec(fileSize)
          pst.char(13)
                
          if fileSize < MAX_PACKET_SIZE
            ' send the file in one packet
            SDCard.readFromFile(@txdata, fileSize)
            byte[@txdata][fileSize] := 13
            byte[@txdata][fileSize+1] := 10
            byte[@txdata][fileSize+2] := 0 
            pst.str(@txdata)
            StringSend(id, @txdata)
            pause(wait)
         
          SDCard.closeFile
          SDCard.changeDirectory(@approot)
    

    The code snippet above was copied from the StaticFileHandler and it replaces the hardcoded StringSend(id, string("Hello from the Spinneret.", 13, 10)). I also replaced socket.txTCP with StringSend(id, @txdata). One thing you'll notice, the code snippet does not handle files larger than MAX_PACKET_SIZE. You should be able to handle the deficiency by visiting the StaticFileHander and doing another copy and paste from StaticFileHandler.

    I have to ask, why are you sending email? What is the goal? It seems to me, email is not the best solution but I'm not sure what gap email fills. Please step us through your thought process we (community) might be able to provide an alternative solution.
  • lfreezelfreeze Posts: 174
    edited 2013-06-15 04:49
    Robert, Mike,

    Thanks for the responses. The SER statements are for viewing progress on the screen (debugging).
    The variable “ableX” is always showing as “0”

    I will plug in Mike code, (thanks again) and see if it flies with my modified “Httpserv”

    Here is the objective and what I have accomplished so far:

    I am remotely monitoring a baseboard hot water heating system. This includes capturing the water temperature going into the
    Boiler and water temperature leaving the boiler. It also includes capturing interior and exterior temperature..
    In the event of a boiler overheating situation, the program will shut the boiler down (AC off) and generate and
    Alarm email message. There are a bunch of future enhancements that I have in mind. Besides the monitoring
    And alarm function, I believe I can reduce fuel consumption by carefully controlling the water pump on
    The boiler, and doing some smart thermostat functions using tod and exterior temperature measurements.

    Here is where I am at”

    Write sensor data from four temperature sensors along with a time stamp to the SD card on the
    Spinneret board. The sensors are connected to an external prop which sends the data via
    A serial connection to the spinneret. The data is written once each minute. The SD card is written too for
    24 hours. The file is then erased and the process starts over again. The program was sending an email message once an hour
    With a single capture of the four sensor values. All of this works, I can access the file on the sd card via an
    Internet connection and I am receiving the hourly updates.

    I have now eliminated the hourly email message and am attempting a daily email message of 24 hours of sensor values
    From the sd card. My purpose is to use that email message in excel spreadsheet for further processing. I know I can
    download the file at any time or at the 24 hour mark, but, having it automatically sent will eliminate the need to babysit the file.


    Larry
  • Don MDon M Posts: 1,647
    edited 2013-06-15 05:22
    I wonder if you can just send your data directly to Google Docs (spreadsheet) and have it available for viewing at any time?
  • lfreezelfreeze Posts: 174
    edited 2013-06-16 18:07
    Thanks for the suggestion.
    Google Docs would be an excellent method to save the file. I can only
    Imagine the coding difficulty I would have getting the Spinneret to do
    A handshaking login to Google. Can anyone comment about whether
    This is possible. For the present, I will continue my quest in getting the
    File sent via email.
    Larry
  • Mike GMike G Posts: 2,702
    edited 2013-06-17 05:02
    lfreeze, it looks like you want to do two things. 1) send email alerts. 2) store sensor reading on a remote PC.

    I believe number one is handled. The second item is very doable but you must write an application on the remote system that requests and stores the sensor data. FTP is a type of application running on a remote system.

    The easiest thing to do, which I mentioned previously, is to HTTP GET the current sensor cache on the SD card. Build SPIN logic such that each time the cached data is requested a new file is generated. On the HTTP GET side, schedule the GET once a day. Pragmatically, copy the file to your Google Docs folder to expose the file to Google Docs.

    An alternative is to create a hosted web app on the remote PC. Post data to the hosted service when the Spinneret is ready.

    Either way, an application running of the remote system is required, email client, FTP, or custom app.
  • lfreezelfreeze Posts: 174
    edited 2013-06-17 13:55
    Thanks Mike. I’m thinking of keeping it simple, I think I understand your suggestion. On the remote computer, I will use
    windows task scheduler and access the file once a day. I need to write a batch file that will then save the file with
    a different name each time it is accessed.

    I’m confused about:

    “. Build SPIN logic such that each time the cached data is requested a new file is generated”

    A new file is already being generated each time I access it, this function may already be built into HTTPserv.

    Larry
  • Mike GMike G Posts: 2,702
    edited 2013-06-17 14:31
    I’m confused about:

    “. Build SPIN logic such that each time the cached data is requested a new file is generated”

    A new file is already being generated each time I access it, this function may already be built into HTTPserv.
    ok... I did not realize you already added the feature or I do not understand what you mean.
  • lfreezelfreeze Posts: 174
    edited 2013-06-17 14:45
    I did not build the feature, I get the current version of the .txt file almost everytime I access it. I can tell because each line of sensor values has a time stamp.
    on occasion, when I access .txt, I get a signifcantly older version. I'm not sure why, if I try again I get the current version. Heres what the current version
    looks like. Please give me a hint on what needs to be written to get a new file generated each time it's accessed.

    74.3 74.75 73.85 73.96 17:35:0 6/17/2013
    74.86 73.85 73.96 74.3 17:36:0 6/17/2013
    73.96 74.08 74.41 74.86 17:37:0 6/17/2013
    74.41 74.98 73.96 74.08 17:38:0 6/17/2013
    74.08 74.19 74.53 75.09 17:39:0 6/17/2013
    74.3 74.64 75.09 74.19 17:40:0 6/17/2013
    74.98 74.08 74.19 74.53 17:41:0 6/17/2013
    74.19 74.3 74.64 75.31 17:42:0 6/17/2013
    74.64 75.2 74.08 74.3 17:43:0 6/17/2013

    Thanks
    Larry
  • Mike GMike G Posts: 2,702
    edited 2013-06-17 15:57
    on occasion, when I access .txt, I get a significantly older version. I'm not sure why
    The file is probably cached in the browser.
    Please give me a hint on what needs to be written to get a new file generated each time it's accessed.
    The requested resource is available during the request/response process. Just check the requested file property. If the file was MyData.txt then delete and create the file. You might consider sending a querystring flag like ?delete=1 or ?delete=0 so the cache file can be viewed without being deleted.

    I built a basic RESTful service to log stuff. I imagine a lot of folks want to save data on a remote system. Anyway, the service is not ready for release. I'll publish the service and source code here on the forum once I get to an Alpha release.
  • lfreezelfreeze Posts: 174
    edited 2013-06-17 17:07
    Thanks Mike, I believe I now have enough info to complete my project. I will maintain the email message for alarm conditions
    And will capture the file once each day to a remote PC. I look forward to your logging application.

    I have to comment, that when I started this journey with the spinneret, I thought a socket was something you put a light bulb in.
    Thanks to your help and the others who contributed. I am near completion on a project that given my small abilities would
    Have seemed impossible a few short months ago.

    The Spinneret is an amazing device that in my opinion is not recognized for its capabilities,
    and could stand a substantial Amount of marketing by Parallax.

    Larry
Sign In or Register to comment.