Monday, September 5, 2011

Sending Text Messages with Python

One of the neat things I like to do with my Astro-Imaging scripts is to have the ability to send periodic status messages to my cell phone. With this capability, if I am away from my imaging computer, I can always know how imaging is progressing. Also, I can have my script send any error messages that crop up so that I may immediately return to my imaging setup if needed.

The key to sending text messages is contained in this List of SMS Gateways that contains e-mail addresses for each of the major cell phone carriers. Basically, all you need to do is send an e-mail message to the domain that applies to your cell phone carrier with your phone's ten-digit number as the address. As the Wikipedia page explains, you can only send simple text messages to the SMS gateway. The following Python code listing shows how to send the e-mail message programmatically. Obviously, you will need to know the outgoing e-mail domain, your user name and password for accessing your personal e-mail service provider to make the code work.

The listing shown below is coded for my particular service carrier (AT&T Wireless). To personalize the code for your particular setup, just modify the five constants defined prior to the class declaration. The source file corresponding to this listing can be downloaded from cTexting.zip.
from smtplib import SMTP
import datetime
import socket
import traceback

SMTP_DOMAIN = r'<OUTGOING EMAIL DOMAIN>'
SMTP_USER   = r'<USER NAME>'
SMTP_PASS   = r'<PASSWORD>'
FROM_ADDR   = r'AstroPhoto Update <anyone@gmail.com>'
TO_ADDR     = r'<TEN-DIGIT CELL NUMBER>@txt.att.net'

socket.setdefaulttimeout(10)

##--------------------------------------------------------------------------------
## Class: cTextMsg
##------------------------------------------------------------------------------
class cTextMsg:
    __objectName = ''

    def __init__(self):
        pass
        
    def set_objectName(self, value):
        self.__objectName = value
        return
    
    def get_objectName(self):
        return self.__objectName

    def sendSMSMessage(self,message):
        smtp = SMTP()
        smtp.set_debuglevel(0)
        
        subject = self.get_objectName()
        date = datetime.datetime.now().strftime('%d/%m/%Y %H:%M')

        msg = 'From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s' % \
                (FROM_ADDR,TO_ADDR,subject,date,message)
        
        try:
            smtp.connect(SMTP_DOMAIN,0)
            smtp.login(SMTP_USER,SMTP_PASS)
            smtp.sendmail(FROM_ADDR,TO_ADDR,msg)
            print "SMS Message Sent Successfully"
            smtp.quit()
        except socket.timeout:
            print "ERROR: Timeout sending SMS Message"
        except:
            print "ERROR: Unable to send SMS Message"
            print '!'*60
            print traceback.format_exc()
        
##
##    END OF 'cTextMsg' Class
##

if __name__ == '__main__':
    
    email_msg = cTextMsg()
    email_msg.set_objectName('M31')
    print 'Object Name: ' + email_msg.get_objectName()
    email_msg.sendSMSMessage('This is a sample message from an imaging session.')

Saturday, September 3, 2011

Vertical Antenna Tilt Mount

For the past 10+ years (which covers almost the entire span of my amateur radio career) my main antenna has been a GAP Titan DX 8-band vertical antenna. This versatile antenna weighs in at around 25 lbs. and has a height of about 25 feet. For most of this time the antenna has been mounted on one corner of a second floor wooden deck and was very difficult to raise and lower. In fact, the antenna was only lowered once, in September 2004, when our area was hit by two hurricanes in a three-week period. Consequently, I have not been able to easily check the condition of the antenna and perform some needed periodic maintenance.

Due to the obvious deterioration of the antenna from lack of maintenance and from this area's highly corrosive seaside location, I've decided to remove the antenna from its second floor mount and to re-mount it in a more convenient location in my back yard. A main requirement of the this new mount was to provide a means to easily raise and lower the antenna when needed for maintenance and when adverse weather conditions threaten. Below is a photo of the new tilt ground-mount base for the GAP antenna that I've come up with.





The base is made up of two 8-foot long treated 2"x4"s that are sunk 3 feet into the ground. A 42" deep by 18" diameter hole was dug out, filled with 6" of rock then poured with concrete around the 2"x4"s. Three sets of 6" long carriage bolts were installed between the 2"x4"s below ground level to give the concrete something to grab onto other than the treated wood.

Above the ground, the 2"x4" were joined with a 4-foot piece of treated 2"x6". A 6-foot piece of schedule-40 1" galvanized water pipe (1.32" OD) is mounted inside the channel formed by the 2"x4"s and 2"x6" (backside of photo on the left).





The pivot-point for the tilt mechanism is drilled through the galvanized pipe near the top end of the pipe. The pipe swings on a piece of 3/32" stainless steel threaded rod secured with two nuts of the outside of the 2"x4"s. Another set of nuts is installed on either side of the pipe to prevent the pipe from wobbling when it swings in and out of the channel between the 2"x4"s. Refer to the photo to the right.







In the normal, upright position, the galvanized pipe is secured to the base mount using 3 stainless steel 1/4" U-bolts pushed through holes in the 2"x6". Two of the three U-bolts are visible in the photo to the left.





For the antenna to be lowered, the three U-bolts are removed and the antenna will be either walked or winched down to a horizontal position. (I'm considering installation of a hand-winching mechanism.) The top of the 2"x6" will prevent the antenna from swinging lower than a nearly-horizontal position. Refer to the photo of the unloaded swing pipe in the photo to the right.




The antenna will be mounted onto the top portion of the 1" galvanized pipe using a stainless steel mounting plate and U-bolts provided with the GAP antenna. The 5'x5' wire hoop at the base of the antenna will be installed after the antenna is raised into its vertical position. Likewise, the hoop will have to be removed prior to lowering the antenna into its horizontal position. By elevating the base of the antenna about 6' off the ground, the hoop will be positioned approximately 8' off the the ground and well above the heads of anyone walking in the yard.