Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python, email temperature

Reply
Thread Tools

Python, email temperature

 
 
Alexander Ranstam
Guest
Posts: n/a
 
      12-22-2012
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

 
Reply With Quote
 
 
 
 
KarlE
Guest
Posts: n/a
 
      12-22-2012
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"
 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      12-22-2012
On Sat, Dec 22, 2012 at 3:36 PM, Alexander Ranstam <(E-Mail Removed)>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

 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      12-22-2012
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


 
Reply With Quote
 
KarlE
Guest
Posts: n/a
 
      12-22-2012
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 <(E-Mail Removed)> 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('=') + 1utput.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?


 
Reply With Quote
 
KarlE
Guest
Posts: n/a
 
      12-22-2012
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 <(E-Mail Removed)> 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('=') + 1utput.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?


 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      12-22-2012
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 <(E-Mail Removed)> 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('=') + 1utput.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.
 
Reply With Quote
 
No One
Guest
Posts: n/a
 
      12-22-2012
On 2012-12-22, KarlE <(E-Mail Removed)> 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 <(E-Mail Removed)> 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('=') + 1utput.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.
 
Reply With Quote
 
KarlE
Guest
Posts: n/a
 
      12-22-2012
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!


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-22-2012
On Sun, Dec 23, 2012 at 9:50 AM, KarlE <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SNMP - Monitor Temperature of any Cisco Devices tyeung@mk Cisco 3 11-20-2005 01:57 AM
check temperature on Cisco 4500 Richard Gliebe Cisco 1 10-11-2005 07:51 AM
Thermaltake Water Level Indicator and Water Temperature ... Silverstrand Front Page News 2 07-03-2005 07:59 AM
Check temperature on Cisco noway Cisco 2 07-26-2004 07:02 AM
Cat 2980 - Query temperature? Academic Technology Services Cisco 0 11-24-2003 07:10 PM



Advertisments