0

I'm not an expert in programming so i googled a lot to get this script to work. It listens on the serial interface ans is searching for 3 values (temperature, humidity and battery level). If it finds one of zhem it saves it to a text file and checks if the value is above or under a certain level. I f this is the case it sends an e-mail to warn.

My problem is that it uses constatntly about 99% of cpu power... Can you help me to limit the CPU usage to a minimum.

Thanks

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import serial
import time
import sys
import smtplib
from time import sleep

def mail(kind, how, value, unit):
    fromaddr = 'sender@domain.com'
    toaddrs  = 'recipient@domain.com'
    msg =  "\r\n".join([
    "From: sender",
    "To: recipient",
    "Subject: Warning",
    "",
    "The " + str(kind) + " is too " + str(how) + ". It is " + str(value) + str(unit)
    ])
    username = 'user'
    password = 'password'
    server = smtplib.SMTP('server:port')
    server.ehlo()
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()

def main():

        port = '/dev/ttyAMA0'
        baud = 9600

        ser = serial.Serial(port=port, baudrate=baud)

        sleep(0.2)    

        while True:
            while ser.inWaiting():
                # read a single character
                char = ser.read()
                # check we have the start of a LLAP message
                if char == 'a':
                # start building the full llap message by adding the 'a' we have
                llapMsg = 'a'
                # read in the next 11 characters form the serial buffer
                # into the llap message
                llapMsg += ser.read(11)


        if "TMPB" in llapMsg:
            TMPB = llapMsg[7:]
            with open("TMPB.txt", "w") as text_file:
                text_file.write(TMPB)
                if float(TMPB) >= 19:
                    mail("temperature", "high", TMPB, "°C")
                elif float(TMPB) <= 15:
                    mail("temperature", "low", TMPB, "°C")
                else:
                    pass

        elif "HUMB" in llapMsg:
            HUMB = llapMsg[7:]
            with open("HUMB.txt", "w") as text_file:
                text_file.write(HUMB)
                if float(HUMB) >= 80:
                    mail("humidity", "high", HUMB, "%")
                elif float(HUMB) <= 70:
                    mail("humidity", "low", HUMB, "%")
                else:
                    pass

        elif "BATT" in llapMsg:
            BATT = llapMsg[7:11]
            with open("BATT.txt", "w") as text_file:
                text_file.write(BATT)
                if float(BATT) < 1:
                    mail("battery level", "low", BATT, "V")
                else:
                    pass

        sleep(0.2)

if __name__ == "__main__":
        main()
CrazyAndy
  • 37
  • 1
  • 6
  • Is your indentation correctly copied into the question, otherwise this itself might be the error. And have you otherwise checked if `ser.inWaiting()` maybe always is true, and therefore staying in the while loop? – agold Nov 28 '15 at 16:55
  • @agold The indentation is exactly like this and it works.It should stay in the loop because the data on the serial port comes about every 5 minutes so it has to listen to the serial interface constantly. – CrazyAndy Nov 28 '15 at 19:31
  • I don't have time to grok the program but here are some things to look at: (1) make sure you use your minimum sampling rate, (2) always use a sleep API to wait between samplings, and (3) never busy loop. One of the most common reasons for really high CPU usage is over sampling. – Taylor Kidd Nov 30 '15 at 15:58
  • @TaylorKidd Thanks for your help. I'm trying to change the script accordingly. – CrazyAndy Dec 11 '15 at 21:41

1 Answers1

0

I solved the question myself.

The while ser.inWaiting(): loop was causing the heavy cpu load. I removed it and corrected the indentation and it works great with a few % cpu load.

Thanks for your hints, it helped me solving the problem.

CrazyAndy
  • 37
  • 1
  • 6