Battery Temp monitor for Android

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Battery Temp monitor for Android

Postby Guitarman2010 » Wed Aug 06, 2014 3:39 am

Here is my crude battery temperature monitor for Python running on an Android device. You can have the option of having the current temp displayed periodically in the notifications. Please give me suggestions and comments!

Code: Select all
import android
from time import sleep

#Initiates android module
droid = android.Android()


#Dialog box for choosing whether to have temp displayed in notifications
title = 'Real Time Monitoring?'
droid.dialogCreateAlert(title)
droid.dialogSetPositiveButtonText('Yes')
droid.dialogSetNegativeButtonText('No')
droid.dialogShow()
response = droid.dialogGetResponse().result
yeah = response['which'] == 'positive'

if yeah:
    monitor = 1
else:
    monitor = 0

#counter variable for while loop
temp = 1


while temp < 100000:

  #Initiates battery monitoring
    droid.batteryStartMonitoring()

  #program halt to allow time for battery information
    sleep(.5)

  #Gets temp from system and sets 'o' as temp in celcius( * 100)
    (id, result, error) = droid.batteryGetTemperature()
    o = result

  #divides so temp will come out correct in C
    o1 = o / 10

  #converts celcius temp to fahrenheit
    c_to_f = (o1 * 1.8) + 32
    notif = str(c_to_f)

  #stops battery monitoring(who would've thunk that!) 
    droid.batteryStopMonitoring()
 
  #sleepy time again
    sleep(.5)
 
    temp +=1 
 
  #crude real time monitoring initiation (counts first time and every 50th)
    if monitor == 1: 
        if temp == 2:
            droid.notify('Temperature', notif + ' F') 
     if temp %50 == 0:
         droid.notify('Temperature', notif + ' F')

         
#Alert dialog box for high temperature 
    if c_to_f >= 95:
        title = 'Temp Alert!'
        message = 'The battery temperature is high!  Current temp: ' + str(c_to_f)
        droid.dialogCreateAlert(title,message)
        droid.dialogSetPositiveButtonText('Ok')
        droid.dialogShow()
        response = droid.dialogGetResponse().result
        yep = response['which'] == 'positive'
     
     #to be continued with options for lowering temp (decrease backlight brightness, decrease maximum CPU frequency)
Last edited by Guitarman2010 on Fri Sep 05, 2014 12:14 pm, edited 2 times in total.
Guitarman2010
 
Posts: 22
Joined: Thu Mar 07, 2013 4:39 pm

Re: Battery Temp monitor for Android

Postby freddyhard » Wed Aug 06, 2014 10:12 am

these are two very small things, but
if yeah == True:
can be written
if yeah:
there is no need to test a boolean

also with
c_to_f = (o1 * 9) / 5 + 32
i would write
c_to_f = o1 * 1.8 + 32
since you know 9/5 = 1.8. it would save the ALU doing one extra division operation

only since you asked, otherwise i wouldn't be so pedantic
freddyhard
 
Posts: 36
Joined: Wed Jul 16, 2014 9:32 pm

Re: Battery Temp monitor for Android

Postby Guitarman2010 » Fri Aug 08, 2014 2:08 am

thanks for the input :)
Guitarman2010
 
Posts: 22
Joined: Thu Mar 07, 2013 4:39 pm


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 4 guests