Announcement

Collapse
No announcement yet.

Logging data to a file from Portal

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Logging data to a file from Portal

    Quoting the SNAP Reference Manual: "Since Portal runs on a PC, its script executes in a full Python environment with access to the many libraries, services, and capabilities there."

    One example of a capability that you can make use of from Portal is Python's logging capabiility. The attached script shows a quick example of one way to do this. Put logSupport.py in your Portal directory, and then in any script that needs to log something to a file, do:

    import logSupport

    ...
    ...
    ...

    logSupport.logToFile(root, "MYLOG", "some text goes here")

    The first parameter is some object that can be considered the "owner" of the log file (this allows having multiple active log files, just give them distint owner objects).

    The second parameter is the "base name" for the log file.

    The third parameter is the text to be logged. If you want to include numeric data in the log, simply format up a string containing all the component data.

    formattedTemperature = "Temperature is " + str(temperature) + " degrees"
    logSupport.logToFile(root, "LOGFILE", formattedTemperature)

    In this example, files are rolled over at midnight, and given names that include the date. Each entry into the file is prefixed by a timestamp.

    All of these formatting decisions could be changed by customizing logSupport.py
    Attached Files

  • #2
    python errors

    def plotval(who,y):
    logData(who,y,1024)


    gives an error
    name "logData" not defined ??

    could you attach a sample bit of code that uses
    logSupport.logToFile(root, "LOGFILE", formattedTemperature)
    for portal and a rf unit

    I cant get anyting to run on portal
    PortalManyMeter and anything else gives errors. I set the python lib location to program files/portal as well.

    rpc("\x00\x00\x01","plotval","ADC",aa) on a node is working is the chart data only graphic? Is there a way to save the data sent to the chart using the above code in text format?

    I am using portal 2.1.22

    Thanks
    Martin

    Comment


    • #3
      From our phone call just now (after a few wrong turns) we realized what you were running into - you were running the SNAPpy validator on your Portal scripts.

      (From reading your post originally, I thought you were getting an error when you tried to LOAD the Portal scripts), which is why I was mistakenly looking into your LIB settings.

      The Test SNAPpy Script button is documented at the bottom of page 119 of the SNAP Reference Manual. I've made a note to try and explain this button more clearly in a future version of the docs. We also will consider disabling the button if you are editing a script from the Portal directory.

      Other points from the phone call:

      If you want to use functions from a file like logSupport.py, be sure to do something like

      Code:
      from logSupport import *
      at the top of your script.

      No, the graph does not support logging to a text file (that is done by logSupport). The chart only allows saving the picture of the graph.

      Keep in mind that your nodes can call a single Portal script function, and that single function can then do multiple things with the data, like logging it to a file, putting it in the Portal Event Log, and graphing it on a chart.

      Comment


      • #4
        portal logging

        I have this code in a node.
        ====================
        rpc("\x00\x00\x01","logToFile", "test_LOG", "aa") #logToFile("test_LOG", "temp 1=",aa))
        cls()
        writeChars(str(aa)[:])
        writeChars("t=")
        cc = readAdc(6)
        print "t",cc
        writeChars(str(cc)[:])
        rpc("\x00\x00\x01","plotval","ADC1",cc)
        rpc("\x00\x00\x01","logToFile", "test_LOG",("b="))
        rpc("\x00\x00\x01","logToFile", "test_LOG_2", cc)
        ===============================


        I have this loaded in potral
        ======================
        import logSupport

        def plotval(who,y):
        ........logData(who,y,1024)

        def logToFile(logname, logdata):
        ........logSupport.logToFile("c:", logname, logdata
        ======================


        only the test_LOG file is generated not thetest_LOG_2 ????

        also the data logged

        is each entry repeated 172 or 173 times?

        16:51:11.655 aa #repeared 172 times then
        16:51:11.687 b= #repeared 172 times

        How do I debug this (or any python program for portal)?

        ???????????????????

        Ok I was trying to erase the test_LOG file and couldnot as poratal was using it.

        I closed portal. renamed the file, restarted portal ... and its logging correctly. mabye this is usefull to you, once I had typed this all I though I would send it anyway.

        martin

        Comment


        • #5
          more things I cant work out

          somtimes the file is created in ...\My Documents\Portal\snappyImages
          other times in C:\Program Files\Portal.

          How is the desicion made? (...ok probably the root thing..)

          also why wont both log file get created?
          test_LOG .... is created
          LOG2test ......is not

          see code below
          ===================================
          formattedTemperature = "Temperature aa " + str(aa) + " units"
          rpc("\x00\x00\x01","logToFile", "test_LOG", formattedTemperature)
          .
          .
          .
          .
          cc = readAdc(6)
          formattedTemperature = "Temperature cc " + str(cc) + " units"
          rpc("\x00\x00\x01","logToFile", "LOG2test",formattedTemperature)


          Martin

          Comment


          • #6
            Taking a quick look at logSupport.py, I don't think it was intended to support multiple simultaneous log files. The intent of the "baseName" parameter was to let you choose what the name of the ONE log file would be.

            Example scripts we post on the forum (like logSupport.py) are just that - examples. If they don't do exactly what you want, feel free to change them.

            Comment


            • #7
              I take part of that back... looking at the very first post of this thread, I see

              The first parameter is some object that can be considered the "owner" of the log file (this allows having multiple active log files, just give them distint owner objects).
              Are you providing a distinct owner object (some object that does exist, and is not object "root") in the function call?

              (It still is possible that the example script needs further modifications to do what you want...)

              Comment


              • #8
                Looking more closely at your posted code, I see my suspicion was correct.

                You are only able to create one log file because you are only providing one root object.

                Code:
                def logToFile(logname, logdata):
                ........logSupport.logToFile("c:", logname, logdata
                Judging by your use of the value "c:" for the first parameter, I think you thought the parameter was "root as in root directory" (it's not). It's "root as in root object".

                In other words, the parent/containing object. If you are using logSupport.py functions from within Portal, there is a pre-existing object NAMED root that can be used (once! as in for one log file only) for this.

                It's important to note that you can only use each "root" object once per run of Portal. (Once you has used "file1" with root, you cannot use "file2" - the association has already been made).

                To have more than one log file, you will need to create some other global object, and pass it in to the logToFile() function.

                When you do this, be sure to use the right "root/parent/container" objects with the right filenames (in other words, be consistent). A Python dictionary might be useful for this purpose (see for example "Core Python Programming" by Wesley J. Chun, page 40).
                Last edited by kbanks; 10-30-2008, 08:34 AM. Reason: age -> page

                Comment


                • #9
                  I'm not sure how your post relates to any products that Synapse Wireless makes. Do you currently own or are looking at purchasing any SNAP based products? Unfortunately we deal daily with automated programs trying to post on our forums so if you make another unrelated post we will have to ban you.
                  --Mark
                  Synapse Design Engineer

                  Comment


                  • #10
                    I'm having problems using this method for some reason but I am unable to figure out why. I'm wondering if I placed the Logsupport.py file in the wrong place. Also if I do test the portal script it errors not allowing me to load the logToFile function. I'm using the protoboard from the eval kit to monitor a simple temperature sensor and I need it to log the data and save it to a file.

                    Comment


                    • #11
                      Ok, I am trying to use, a slightly different logging script from Synapse called PortalNode_simpleTemrLog.py.

                      I load it into the portal. then I click on logTofile() function.

                      a box pops up and I enter a file name and a parameter to log from the other
                      remote node running manyMeter.py.

                      It gives me an error. What am I missing??

                      Kosta

                      Comment


                      • #12
                        You are more than likely not providing it with the correct data-types. It requires two strings. The first is a filename for the log to use and the second the string data to write to the file.

                        Ex.

                        In box 1 => "myFile"
                        In box 2 => "This is my string to print"

                        This can be seen if you look at the code of the logSupport.py file.

                        BTW, PortalNode_simpleTemrLog.py was just a quick example sent to you. It is not publicly accessible.
                        sigpic
                        Proven Solutions for the Internet of Things
                        www.synapse-wireless.com

                        Comment


                        • #13
                          I try to log measured rssi data to a file by using "logSupport.logToFile" . When I try to build my script , portal gives error of " unsupported opcode " on event log and logToFile tab is opened on portal after receiving "file LogtoFile does not exist " error. Any idea about this error will be appreciated. Thanks.
                          Last edited by sbkaya; 01-21-2014, 12:57 PM.

                          Comment


                          • #14
                            That only works in your Portal script, not a script uploaded to a radio module.

                            Comment


                            • #15
                              Thanks for reply. However, i could not even built my script file so I could not upload to module . I open new script file and copy/paste code cointainig logSupport.logToFile but it is not built. Can it be a problem about not importing logging module of python ?

                              Comment

                              X