Re: Python, email temperature
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 > > 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" -- http://mail.python.org/mailman/listinfo/python-list
Re: Python, email temperature
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
>
> 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:output.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
msg = cpu_temperature
NameError: name 'cpu_temperature' is not defined
pi@raspberrypi /home/python $
isnt cpu_temperature defined?
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python, email temperature
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 > > 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! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python, email temperature
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
>
> 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:output.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()
--
http://mail.python.org/mailman/listinfo/python-list
