Python, email temperature

Discussion in 'Python' started by Alexander Ranstam, Dec 22, 2012.

  1. Hi!

    Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.

    The two programs work very well on their own, but this doesnt work.

    this works: server.sendmail(fromaddr, toaddrs, msg)
    but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)

    despite the command "print cputemp" working in the same program.

    When i run the program i get the error:

    Traceback (most recent call last):
    File "sendcpu.py", line 36, in <module>
    msg = cpu_temperature
    NameError: name 'cpu_temperature' is not defined

    Does anyone know why the program claims that cpu_temperature isnt defined, when it is?

    Thanx!

    //Alexander
     
    Alexander Ranstam, Dec 22, 2012
    #1
    1. Advertising

  2. Alexander Ranstam

    KarlE Guest

    On Saturday, December 22, 2012 9:36:41 PM UTC+1, Alexander Ranstam wrote:
    > Hi!
    >
    >
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >
    >
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    >
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    >
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    >
    >
    > despite the command "print cputemp" working in the same program.
    >
    >
    >
    > When i run the program i get the error:
    >
    >
    >
    > Traceback (most recent call last):
    >
    > File "sendcpu.py", line 36, in <module>
    >
    > msg = cpu_temperature
    >
    > NameError: name 'cpu_temperature' is not defined
    >
    >
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >
    >
    >
    > Thanx!
    >
    >
    >
    > //Alexander


    Typo: "print cputemp" should say "print cpu_temperature"
     
    KarlE, Dec 22, 2012
    #2
    1. Advertising

  3. On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <>wrote:

    > Hi!
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a
    > program that sends an email, and one that checks the temperature of my CPU,
    > but i cant seem to combine the to into the funktion that i want, sending me
    > the CPU temp via Email.
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    > despite the command "print cputemp" working in the same program.
    >
    > When i run the program i get the error:
    >
    > Traceback (most recent call last):
    > File "sendcpu.py", line 36, in <module>
    > msg = cpu_temperature
    > NameError: name 'cpu_temperature' is not defined
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined,
    > when it is?
    >


    You should copy and paste the code here including the context around the
    error. You say print cputemp works, but cpu_temperature is not defined.
    They are spelled differently. Start there

    >
    > Thanx!
    >
    > //Alexander
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Joel Goldstick
     
    Joel Goldstick, Dec 22, 2012
    #3
  4. Alexander Ranstam

    Gary Herron Guest

    On 12/22/2012 12:36 PM, Alexander Ranstam wrote:
    > Hi!
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    > despite the command "print cputemp" working in the same program.
    >
    > When i run the program i get the error:
    >
    > Traceback (most recent call last):
    > File "sendcpu.py", line 36, in <module>
    > msg = cpu_temperature
    > NameError: name 'cpu_temperature' is not defined
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >
    > Thanx!
    >
    > //Alexander
    >


    Could it be this easy? In one spot you refer to it as "cpu_temperature"
    and in another as "cputemp".

    If that's not it, you'd probably better show us your *real* code,
    otherwise we're just guessing.

    Gary Herron
     
    Gary Herron, Dec 22, 2012
    #4
  5. Alexander Ranstam

    KarlE Guest

    On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
    > On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <> wrote:
    >
    > Hi!
    >
    >
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending methe CPU temp via Email.
    >
    >
    >
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    >
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    >
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    >
    >
    > despite the command "print cputemp" working in the same program.
    >
    >
    >
    > When i run the program i get the error:
    >
    >
    >
    > Traceback (most recent call last):
    >
    >   File "sendcpu.py", line 36, in <module>
    >
    >     msg = cpu_temperature
    >
    > NameError: name 'cpu_temperature' is not defined
    >
    >
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >
    >
    >
    > You should copy and paste the code here including the context around the error.  You say print cputemp works, but cpu_temperature is not defined.  They are spelled differently.  Start there
    >
    >
    >
    >
    > Thanx!
    >
    >
    >
    > //Alexander
    >
    >
    >
    > --
    >
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >
    >
    >
    > --
    > Joel Goldstick


    Hi!

    I made a typing error, and couldnt edit the post :( this is the code:


    #!/usr/bin/env python
    from __future__ import division
    from subprocess import PIPE, Popen
    import psutil
    import smtplib

    def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:eek:utput.rindex("'")])


    def main():
    cpu_temperature = get_cpu_temperature()
    cpu_usage = psutil.cpu_percent()

    ram = psutil.phymem_usage()
    ram_total = ram.total / 2**20 # MiB.
    ram_used = ram.used / 2**20
    ram_free = ram.free / 2**20
    ram_percent_used = ram.percent

    disk = psutil.disk_usage('/')
    disk_total = disk.total / 2**30 # GiB.
    disk_used = disk.used / 2**30
    disk_free = disk.free / 2**30
    disk_percent_used = disk.percent
    #
    # Print top five processes in terms of virtual memory usage.
    #
    print 'CPU temperature is: ', cpu_temperature

    fromaddr = 'myemailadress'
    toaddrs = 'myemailadress'
    #msg = 'There was a terrible error that occured and I wanted you to know!'
    msg = cpu_temperature

    # Credentials (if needed)
    username = 'myusername'
    password = 'mypassword'

    # The actual mail send
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, cpu_temperature)
    server.quit()




    if __name__ == '__main__':
    main()

    running it gives the following error:

    pi@raspberrypi /home/python $ python sendcpu.py
    Traceback (most recent call last):
    File "sendcpu.py", line 36, in <module>
    msg = cpu_temperature
    NameError: name 'cpu_temperature' is not defined
    pi@raspberrypi /home/python $


    isnt cpu_temperature defined?
     
    KarlE, Dec 22, 2012
    #5
  6. Alexander Ranstam

    KarlE Guest

    On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
    > On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <> wrote:
    >
    > Hi!
    >
    >
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending methe CPU temp via Email.
    >
    >
    >
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    >
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    >
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    >
    >
    > despite the command "print cputemp" working in the same program.
    >
    >
    >
    > When i run the program i get the error:
    >
    >
    >
    > Traceback (most recent call last):
    >
    >   File "sendcpu.py", line 36, in <module>
    >
    >     msg = cpu_temperature
    >
    > NameError: name 'cpu_temperature' is not defined
    >
    >
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >
    >
    >
    > You should copy and paste the code here including the context around the error.  You say print cputemp works, but cpu_temperature is not defined.  They are spelled differently.  Start there
    >
    >
    >
    >
    > Thanx!
    >
    >
    >
    > //Alexander
    >
    >
    >
    > --
    >
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >
    >
    >
    > --
    > Joel Goldstick


    Hi!

    I made a typing error, and couldnt edit the post :( this is the code:


    #!/usr/bin/env python
    from __future__ import division
    from subprocess import PIPE, Popen
    import psutil
    import smtplib

    def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:eek:utput.rindex("'")])


    def main():
    cpu_temperature = get_cpu_temperature()
    cpu_usage = psutil.cpu_percent()

    ram = psutil.phymem_usage()
    ram_total = ram.total / 2**20 # MiB.
    ram_used = ram.used / 2**20
    ram_free = ram.free / 2**20
    ram_percent_used = ram.percent

    disk = psutil.disk_usage('/')
    disk_total = disk.total / 2**30 # GiB.
    disk_used = disk.used / 2**30
    disk_free = disk.free / 2**30
    disk_percent_used = disk.percent
    #
    # Print top five processes in terms of virtual memory usage.
    #
    print 'CPU temperature is: ', cpu_temperature

    fromaddr = 'myemailadress'
    toaddrs = 'myemailadress'
    #msg = 'There was a terrible error that occured and I wanted you to know!'
    msg = cpu_temperature

    # Credentials (if needed)
    username = 'myusername'
    password = 'mypassword'

    # The actual mail send
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, cpu_temperature)
    server.quit()




    if __name__ == '__main__':
    main()

    running it gives the following error:

    pi@raspberrypi /home/python $ python sendcpu.py
    Traceback (most recent call last):
    File "sendcpu.py", line 36, in <module>
    msg = cpu_temperature
    NameError: name 'cpu_temperature' is not defined
    pi@raspberrypi /home/python $


    isnt cpu_temperature defined?
     
    KarlE, Dec 22, 2012
    #6
  7. Alexander Ranstam

    Gary Herron Guest

    On 12/22/2012 12:54 PM, KarlE wrote:
    > On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
    >> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <> wrote:
    >>
    >> Hi!
    >>
    >>
    >>
    >> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >>
    >>
    >>
    >>
    >> The two programs work very well on their own, but this doesnt work.
    >>
    >>
    >>
    >> this works: server.sendmail(fromaddr, toaddrs, msg)
    >>
    >> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >>
    >>
    >>
    >> despite the command "print cputemp" working in the same program.
    >>
    >>
    >>
    >> When i run the program i get the error:
    >>
    >>
    >>
    >> Traceback (most recent call last):
    >>
    >> File "sendcpu.py", line 36, in <module>
    >>
    >> msg = cpu_temperature
    >>
    >> NameError: name 'cpu_temperature' is not defined
    >>
    >>
    >>
    >> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >>
    >>
    >>
    >> You should copy and paste the code here including the context around the error. You say print cputemp works, but cpu_temperature is not defined. They are spelled differently. Start there
    >>
    >>
    >>
    >>
    >> Thanx!
    >>
    >>
    >>
    >> //Alexander
    >>
    >>
    >>
    >> --
    >>
    >> http://mail.python.org/mailman/listinfo/python-list
    >>
    >>
    >>
    >>
    >> --
    >> Joel Goldstick

    > Hi!
    >
    > I made a typing error, and couldnt edit the post :( this is the code:
    >
    >
    > #!/usr/bin/env python
    > from __future__ import division
    > from subprocess import PIPE, Popen
    > import psutil
    > import smtplib
    >
    > def get_cpu_temperature():
    > process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    > output, _error = process.communicate()
    > return float(output[output.index('=') + 1:eek:utput.rindex("'")])
    >
    >
    > def main():
    > cpu_temperature = get_cpu_temperature()
    > cpu_usage = psutil.cpu_percent()
    >
    > ram = psutil.phymem_usage()
    > ram_total = ram.total / 2**20 # MiB.
    > ram_used = ram.used / 2**20
    > ram_free = ram.free / 2**20
    > ram_percent_used = ram.percent
    >
    > disk = psutil.disk_usage('/')
    > disk_total = disk.total / 2**30 # GiB.
    > disk_used = disk.used / 2**30
    > disk_free = disk.free / 2**30
    > disk_percent_used = disk.percent
    > #
    > # Print top five processes in terms of virtual memory usage.
    > #
    > print 'CPU temperature is: ', cpu_temperature
    >
    > fromaddr = 'myemailadress'
    > toaddrs = 'myemailadress'
    > #msg = 'There was a terrible error that occured and I wanted you to know!'
    > msg = cpu_temperature
    >
    > # Credentials (if needed)
    > username = 'myusername'
    > password = 'mypassword'
    >
    > # The actual mail send
    > server = smtplib.SMTP('smtp.gmail.com:587')
    > server.starttls()
    > server.login(username,password)
    > server.sendmail(fromaddr, toaddrs, cpu_temperature)
    > server.quit()
    >
    >
    >
    >
    > if __name__ == '__main__':
    > main()
    >
    > running it gives the following error:
    >
    > pi@raspberrypi /home/python $ python sendcpu.py
    > Traceback (most recent call last):
    > File "sendcpu.py", line 36, in <module>
    > msg = cpu_temperature
    > NameError: name 'cpu_temperature' is not defined
    > pi@raspberrypi /home/python $
    >
    >
    > isnt cpu_temperature defined?
    >
    >


    First: Learn about Python SCOPES. You are defining variables inside
    (as local variables) the procedure main, but they are lost as soon as
    main returns. If you want values computed inside main but available
    outside main, you should return them.

    Second: Some confusion over what order things are executed in. The
    code in main is run when you call main -- ans that's at the very end of
    the file. The lines before the call to main expect to use the value
    cpu_temperature when you have not yet called main to compute the value
    (and which doesn't even return the value as noted above).

    The confusion is partly caused by having some of your code inside main
    and some of it outside main and expecting the two parts to
    communicate. I'd suggest putting everything up through the
    server.quit() into procedure main.
     
    Gary Herron, Dec 22, 2012
    #7
  8. Alexander Ranstam

    No One Guest

    On 2012-12-22, KarlE <> wrote:
    > On Saturday, December 22, 2012 9:44:39 PM UTC+1, Joel Goldstick wrote:
    >> On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <> wrote:
    >>
    >> Hi!
    >>
    >>
    >>
    >> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >>
    >>
    >>
    >>
    >> The two programs work very well on their own, but this doesnt work.
    >>
    >>
    >>
    >> this works: server.sendmail(fromaddr, toaddrs, msg)
    >>
    >> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >>
    >>
    >>
    >> despite the command "print cputemp" working in the same program.
    >>
    >>
    >>
    >> When i run the program i get the error:
    >>
    >>
    >>
    >> Traceback (most recent call last):
    >>
    >> ? File "sendcpu.py", line 36, in <module>
    >>
    >> ? ? msg = cpu_temperature
    >>
    >> NameError: name 'cpu_temperature' is not defined
    >>
    >>
    >>
    >> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >>
    >>
    >>
    >> You should copy and paste the code here including the context around the error.? You say print cputemp works, but cpu_temperature is not defined.? They are spelled differently.? Start there
    >>
    >>
    >>
    >>
    >> Thanx!
    >>
    >>
    >>
    >> //Alexander
    >>
    >>
    >>
    >> --
    >>
    >> http://mail.python.org/mailman/listinfo/python-list
    >>
    >>
    >>
    >>
    >> --
    >> Joel Goldstick

    >
    > Hi!
    >
    > I made a typing error, and couldnt edit the post :( this is the code:
    >
    >
    > #!/usr/bin/env python
    > from __future__ import division
    > from subprocess import PIPE, Popen
    > import psutil
    > import smtplib
    >
    > def get_cpu_temperature():
    > process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    > output, _error = process.communicate()
    > return float(output[output.index('=') + 1:eek:utput.rindex("'")])
    >
    >
    > def main():
    > cpu_temperature = get_cpu_temperature()
    > cpu_usage = psutil.cpu_percent()
    >
    > ram = psutil.phymem_usage()
    > ram_total = ram.total / 2**20 # MiB.
    > ram_used = ram.used / 2**20
    > ram_free = ram.free / 2**20
    > ram_percent_used = ram.percent
    >
    > disk = psutil.disk_usage('/')
    > disk_total = disk.total / 2**30 # GiB.
    > disk_used = disk.used / 2**30
    > disk_free = disk.free / 2**30
    > disk_percent_used = disk.percent
    > #
    > # Print top five processes in terms of virtual memory usage.
    > #
    > print 'CPU temperature is: ', cpu_temperature
    >
    > fromaddr = 'myemailadress'
    > toaddrs = 'myemailadress'
    > #msg = 'There was a terrible error that occured and I wanted you to know!'
    > msg = cpu_temperature
    >
    > # Credentials (if needed)
    > username = 'myusername'
    > password = 'mypassword'
    >
    > # The actual mail send
    > server = smtplib.SMTP('smtp.gmail.com:587')
    > server.starttls()
    > server.login(username,password)
    > server.sendmail(fromaddr, toaddrs, cpu_temperature)
    > server.quit()
    >
    >
    >
    >
    > if __name__ == '__main__':
    > main()
    >
    > running it gives the following error:
    >
    > pi@raspberrypi /home/python $ python sendcpu.py
    > Traceback (most recent call last):
    > File "sendcpu.py", line 36, in <module>
    > msg = cpu_temperature
    > NameError: name 'cpu_temperature' is not defined
    > pi@raspberrypi /home/python $
    >
    >
    > isnt cpu_temperature defined?
    >
    >


    You might have a look at the indentation, as well. At least on my reader, the lines from "fromaddr =" to "server.quit()" are in a left indented block from the main function. You probably want to get them all in the same indentation level to solve the scope issue. Also, on the code as it copies to my editor there are mixed tabs and spaces. If your code also mixes tabs and spaces, pick one (spaces preferred over tabs) and make sure to eliminate the other.

    Norm.
     
    No One, Dec 22, 2012
    #8
  9. Alexander Ranstam

    KarlE Guest

    On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
    > Hi!
    >
    >
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >
    >
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    >
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    >
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    >
    >
    > despite the command "print cputemp" working in the same program.
    >
    >
    >
    > When i run the program i get the error:
    >
    >
    >
    > Traceback (most recent call last):
    >
    > File "sendcpu.py", line 36, in <module>
    >
    > msg = cpu_temperature
    >
    > NameError: name 'cpu_temperature' is not defined
    >
    >
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >
    >
    >
    > Thanx!
    >
    >
    >
    > //Alexander


    Thanx for the help!

    After reading your comments i am starting to suspect that i lack basic knowledge of Python programming. I will try to do some reading and undertand what i got my self into!
     
    KarlE, Dec 22, 2012
    #9
  10. On Sun, Dec 23, 2012 at 9:50 AM, KarlE <> wrote:
    > Thanx for the help!
    >
    > After reading your comments i am starting to suspect that i lack basic knowledge of Python programming. I will try to do some reading and undertand what i got my self into!


    That happens :) Python has rules that are different from the ones many
    other languages follow (indentation defining blocks, etc); but it has
    an excellent tutorial that walks you through all that:

    http://docs.python.org/3/tutorial/index.html

    Work through that and you'll be Pythoning with the best in no time!

    (Disclaimer: "in no time" requires a Faster-Than-Light drive, not
    included. All guarantees are conditional on functional FTL drive.)

    ChrisA
     
    Chris Angelico, Dec 22, 2012
    #10
  11. Alexander Ranstam

    KarlE Guest

    On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
    > Hi!
    >
    >
    >
    > Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >
    >
    >
    > The two programs work very well on their own, but this doesnt work.
    >
    >
    >
    > this works: server.sendmail(fromaddr, toaddrs, msg)
    >
    > but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >
    >
    >
    > despite the command "print cputemp" working in the same program.
    >
    >
    >
    > When i run the program i get the error:
    >
    >
    >
    > Traceback (most recent call last):
    >
    > File "sendcpu.py", line 36, in <module>
    >
    > msg = cpu_temperature
    >
    > NameError: name 'cpu_temperature' is not defined
    >
    >
    >
    > Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >
    >
    >
    > Thanx!
    >
    >
    >
    > //Alexander


    Ok, im back with a little more understanding of python! I got the program working, every time my Raspberry Pi reboots i get an Email containing information about the boot and the CPU temperature.

    The issue now is that there seems to be a limitation to how long the message string can be, about 32 letters. The code below works well, but when i add more letters to the string "ord" and pass about 32 in size the email comes through emptpy...

    I cant find any information about limitations to strings in Python, or the email module. can anyone give me a pointer?

    (the code lines my appear with different tabbings due to beeing copied from my raspberry pi with Putty, but this is not an issue, all the lines are on the same tab)

    #!/usr/bin/env python
    from __future__ import division
    from subprocess import PIPE, Popen
    import psutil
    import smtplib

    def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:eek:utput.rindex("'")])


    def main():
    cpu_temperature = get_cpu_temperature()
    cpu_usage = psutil.cpu_percent()

    ram = psutil.phymem_usage()
    ram_percent_used = ram.percent

    disk = psutil.disk_usage('/')
    disk_percent_used = disk.percent

    print 'CPU temperature: ', cpu_temperature

    fromaddr = 'xxx'
    toaddrs = 'xxx'
    username = 'xxx'
    password = 'xxx'

    ord = "Subject: Pi Boot, CPU: " + str(cpu_temperature)

    print len(ord)
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, ord)
    server.quit()

    main()
     
    KarlE, Dec 23, 2012
    #11
  12. On 12/23/2012 08:46 AM, KarlE wrote:
    > On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
    >> Hi!
    >>
    >>
    >>
    >> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >>
    >>
    >>
    >> The two programs work very well on their own, but this doesnt work.
    >>
    >>
    >>
    >> this works: server.sendmail(fromaddr, toaddrs, msg)
    >>
    >> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >>
    >>
    >>
    >> despite the command "print cputemp" working in the same program.
    >>
    >>
    >>
    >> When i run the program i get the error:
    >>
    >>
    >>
    >> Traceback (most recent call last):
    >>
    >> File "sendcpu.py", line 36, in <module>
    >>
    >> msg = cpu_temperature
    >>
    >> NameError: name 'cpu_temperature' is not defined
    >>
    >>
    >>
    >> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >>
    >>
    >>
    >> Thanx!
    >>
    >>
    >>
    >> //Alexander

    > Ok, im back with a little more understanding of python! I got the program working, every time my Raspberry Pi reboots i get an Email containing information about the boot and the CPU temperature.
    >
    > The issue now is that there seems to be a limitation to how long the message string can be, about 32 letters. The code below works well, but when i add more letters to the string "ord" and pass about 32 in size the email comes through emptpy...
    >
    > I cant find any information about limitations to strings in Python, or the email module. can anyone give me a pointer?
    >
    > (the code lines my appear with different tabbings due to beeing copied from my raspberry pi with Putty, but this is not an issue, all the lines are on the same tab)
    >
    > #!/usr/bin/env python
    > from __future__ import division
    > from subprocess import PIPE, Popen
    > import psutil
    > import smtplib
    >
    > def get_cpu_temperature():
    > process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    > output, _error = process.communicate()
    > return float(output[output.index('=') + 1:eek:utput.rindex("'")])
    >
    >
    > def main():
    > cpu_temperature = get_cpu_temperature()
    > cpu_usage = psutil.cpu_percent()
    >
    > ram = psutil.phymem_usage()
    > ram_percent_used = ram.percent
    >
    > disk = psutil.disk_usage('/')
    > disk_percent_used = disk.percent
    >
    > print 'CPU temperature: ', cpu_temperature
    >
    > fromaddr = 'xxx'
    > toaddrs = 'xxx'
    > username = 'xxx'
    > password = 'xxx'
    >
    > ord = "Subject: Pi Boot, CPU: " + str(cpu_temperature)
    >
    > print len(ord)
    > server = smtplib.SMTP('smtp.gmail.com:587')
    > server.starttls()
    > server.login(username,password)
    > server.sendmail(fromaddr, toaddrs, ord)
    > server.quit()
    >
    > main()
    >


    I'm not sure if Raspberry Pi has it, but usually you want to
    use the email module, as in example on this page:

    http://docs.python.org/2/library/email-examples.html#email-examples

    I think what happens is that because your message starts
    with 'Subject:', it's interpreted as subject header instead of
    an email. You can try adding two newlines after Subject:,
    that might help... but using email module is best if possible.

    -m

    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/
     
    Mitya Sirenef, Dec 23, 2012
    #12
  13. Alexander Ranstam

    Dave Angel Guest

    On 12/23/2012 08:46 AM, KarlE wrote:
    > On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:
    >> Hi!
    >>
    >>
    >>
    >> Im totally new to Python, and im using it on my Raspberry pi. I found a program that sends an email, and one that checks the temperature of my CPU, but i cant seem to combine the to into the funktion that i want, sending me the CPU temp via Email.
    >>
    >>
    >>
    >> The two programs work very well on their own, but this doesnt work.
    >>
    >>
    >>
    >> this works: server.sendmail(fromaddr, toaddrs, msg)
    >>
    >> but this doesnt: server.sendmail(fromaddr, toaddrs, cpu_temperature)
    >>
    >>
    >>
    >> despite the command "print cputemp" working in the same program.
    >>
    >>
    >>
    >> When i run the program i get the error:
    >>
    >>
    >>
    >> Traceback (most recent call last):
    >>
    >> File "sendcpu.py", line 36, in <module>
    >>
    >> msg = cpu_temperature
    >>
    >> NameError: name 'cpu_temperature' is not defined
    >>
    >>
    >>
    >> Does anyone know why the program claims that cpu_temperature isnt defined, when it is?
    >>
    >>
    >>
    >> Thanx!
    >>
    >>
    >>
    >> //Alexander

    > Ok, im back with a little more understanding of python! I got the program working, every time my Raspberry Pi reboots i get an Email containing information about the boot and the CPU temperature.
    >
    > The issue now is that there seems to be a limitation to how long the message string can be, about 32 letters. The code below works well, but when i add more letters to the string "ord" and pass about 32 in size the email comes through emptpy...


    I don't know the email protocols that well, but I can tell you a Python
    string isn't limited in size to any small value. Maybe a few hundred
    million characters, but i haven't tried beyond that.

    i suspect the limit you're hitting is the limit of subject size in an
    email protocol. In particular, the 3rd argument to sendmail() needs to
    have newlines in a particular format before you get to the body of the
    email. The body can be quite large, but you probably are required to
    have the appropriate headers first.

    When you send a short message, does it all come out as a subject line?

    It would be good to look up the docs on smtplib, but if I had to just do
    some blind testing, I'd try first adding a newline pair, changing the
    line to something like:

    ord = "Subject: Pi Boot, CPU: \r\n" + str(cpu_temperature) + WhateverOtherStuffYouWantedToTry






    --

    DaveA
     
    Dave Angel, Dec 23, 2012
    #13
  14. Alexander Ranstam

    Terry Reedy Guest

    On 12/23/2012 12:23 PM, Mitya Sirenef wrote:
    > On 12/23/2012 08:46 AM, KarlE wrote:
    >> On Saturday, December 22, 2012 9:36:41 PM UTC+1, KarlE wrote:


    >> from __future__ import division


    Depending on the linux installed, you should be able to run 3.2 or 3.3
    instead of 2.7. Though there are still 2.x only modules, some things
    work better in 3.x (including default division).

    >> server.sendmail(fromaddr, toaddrs, ord)
    >> server.quit()
    >>
    >> main()
    >>


    > I'm not sure if Raspberry Pi has it, but usually you want to
    > use the email module, as in example on this page:
    >
    > http://docs.python.org/2/library/email-examples.html#email-examples


    3.2+ have a separate .send_message method for email message objects.

    > I think what happens is that because your message starts
    > with 'Subject:', it's interpreted as subject header instead of
    > an email. You can try adding two newlines after Subject:,
    > that might help... but using email module is best if possible.


    The 3.3 SMTP doc says that the fromaddr and toaddrs are only used by the
    transport layer and do not become part of the email message. The doc
    example has

    msg = ("From: %s\r\nTo: %s\r\n\r\n"
    % (fromaddr, ", ".join(toaddrs)))

    The body is appended after the double return. A subject line and any
    other standard headers would go before.

    OT note: the PSF (Python Software Foundation) has bought a Raspberry PI
    and another ARM board to test Python on. I am happy to read that it
    seems to be working so far.

    --
    Terry Jan Reedy
     
    Terry Reedy, Dec 24, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,660
    Joe Knapka
    Jul 24, 2006
  2. Flemming Hansen

    CPU temperature reading with C

    Flemming Hansen, Mar 16, 2005, in forum: C Programming
    Replies:
    8
    Views:
    1,296
    Thomas Matthews
    Mar 17, 2005
  3. deanfamily11

    Temperature conversion

    deanfamily11, Sep 10, 2005, in forum: C++
    Replies:
    1
    Views:
    464
    Shezan Baig
    Sep 10, 2005
  4. Newbie

    CPU temperature reading

    Newbie, Dec 9, 2005, in forum: C++
    Replies:
    1
    Views:
    375
    Victor Bazarov
    Dec 9, 2005
  5. arnuld
    Replies:
    4
    Views:
    497
    arnuld
    Mar 4, 2007
Loading...

Share This Page