Python, email temperature

A

Alexander Ranstam

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
 
K

KarlE

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"
 
J

Joel Goldstick

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
 
G

Gary Herron

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
 
K

KarlE

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

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?
 
K

KarlE

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

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?
 
G

Gary Herron

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
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.
 
N

No One

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

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.
 
K

KarlE

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!
 
C

Chris Angelico

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
 
K

KarlE

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()
 
M

Mitya Sirenef

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
 
D

Dave Angel

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
 
T

Terry Reedy

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).
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,520
Latest member
JudithMorf

Latest Threads

Top