except: logging.error("failed posting") exit(1) if __name__ == "__main__": import argparse import scaleConfig import logging configFile = './scaleConfig.yaml' parser = argparse.ArgumentParser() parser.add_argument("-c", "--config", action="store", dest="configFile", help="specify a configuration file") args = parser.parse_args() if args.configFile: configFile = args.configFile cfg = scaleConfig.readConfig(configFile) #tweetStatus(cfg['twitterConfiguration']['twitterCredsFile'], # 'y u no have beans? #testtweet #coffebeans') postStatus()
api = twitter.Api(access_token_key=twitterCredentials['accessToken'], access_token_secret=twitterCredentials['accessSecret'], consumer_key=twitterCredentials['consumerKey'], consumer_secret=twitterCredentials['consumerSec']) status = api.PostUpdate(tweet) if __name__ == "__main__": import argparse import scaleConfig import logging configFile = './scaleConfig.yaml' parser = argparse.ArgumentParser() parser.add_argument("-c","--config", action="store", dest="configFile", help="specify a configuration file") args = parser.parse_args() if args.configFile: configFile = args.configFile cfg = scaleConfig.readConfig(configFile) tweetStatus(cfg['twitterConfiguration']['twitterCredsFile'], 'y u no have beans? #testtweet #coffebeans')
def initialize(self): configFile = './scaleConfig.yaml' parser = argparse.ArgumentParser() parser.add_argument( "-d", "--debug", type=int, choices=[0, 1], nargs='?', const=1, help="turn debugging on (1) or off (0); this overrides" + "the value in the configuration file ") parser.add_argument("-c", "--config", action="store", dest="configFile", help="specify a configuration file") parser.add_argument("-r", "--requires-root", action="store_true", dest="configFile", help="for Matt Daemon") args = parser.parse_args() if args.configFile: configFile = args.configFile self.logger = logging.getLogger("beanscale") self.logger.setLevel(logging.INFO) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler = logging.FileHandler("/home/pi/scale/scaledaemon.log") handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.info("Starting") cfg = scaleConfig.readConfig(self, configFile) if args.debug == None: DEBUG = cfg['raspberryPiConfig']['debug'] else: DEBUG = args.debug # if DEBUG: # logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(message)s') # else: # logging.basicConfig(filename="scale.log",level=logging.DEBUG, # format='%(asctime)s %(message)s') logger.debug("Initializing.") try: f = open(cfg['raspberryPiConfig']['plotlyCredsFile']) args.plotlyConfig = yaml.safe_load(f) f.close() except: logger.error( "I couldn't open the file %s to read the plot.ly settings, so I can't make a plot and am giving up. I am %s/%s", cfg['raspberryPiConfig']['plotlyCredsFile'], os.path.abspath(os.path.dirname(sys.argv[0])), sys.argv[0]) exit(1) if not cfg['raspberryPiConfig']['alertChannels']: cfg['raspberryPiConfig']['alertChannels'] = '' if not cfg['raspberryPiConfig']['updateChannels']: cfg['raspberryPiConfig']['updateChannels'] = '' global GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings( False) # to stop the "This channel is already in use" warning # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler # ADCPort Cobbler # -------- ---------- args.SPICLK = cfg['raspberryPiConfig']['ADCtoCobbler']['SPICLK'] args.SPIMISO = cfg['raspberryPiConfig']['ADCtoCobbler']['SPIMISO'] args.SPIMOSI = cfg['raspberryPiConfig']['ADCtoCobbler']['SPIMOSI'] args.SPICS = cfg['raspberryPiConfig']['ADCtoCobbler']['SPICS'] # Diagram of ADC (MCP3008) is at: # https://learn.adafruit.com/ # reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/ # connecting-the-cobbler-to-a-mcp3008 # set up the SPI interface pins GPIO.setup(args.SPIMOSI, GPIO.OUT) GPIO.setup(args.SPIMISO, GPIO.IN) GPIO.setup(args.SPICLK, GPIO.OUT) GPIO.setup(args.SPICS, GPIO.OUT) # FSR connected to adc #0 args.fsr_adc = cfg['raspberryPiConfig']['adcPortWithFSR'] # to keep from being jittery we'll only change when the FSR has moved # more than this many 'counts' args.tolerance = cfg['raspberryPiConfig']['tolerance'] # not ideal, but at least it's just in one place now args.logString = '{0} {1}\tfsr: {2:4d}\tlast_value: {3:4d}\tchange: {4:4d}\tbeans: {5}' # If we will need Twitter credentials, read them now # if 'twitter' in cfg['raspberryPiConfig'][ 'alertChannels'] or 'twitter' in cfg['raspberryPiConfig'][ 'updateChannels']: logger.debug("Reading Twitter credentials from {}".format( cfg['twitterConfiguration']['twitterCredsFile'])) try: f = open(cfg['twitterConfiguration']['twitterCredsFile']) twitterCredentials = yaml.safe_load(f) f.close() except: logger.error( "I couldn't open the file %s to read the twitter credentials, so I can't tweet. I am: %s/%s", cfg['twitterConfiguration']['twitterCredsFile'], os.path.abspath(os.path.dirname(sys.argv[0])), sys.argv[0]) cfg['raspberryPiConfig']['alertChannels'].remove('twitter') cfg['raspberryPiConfig']['updateChannels'].remove('twitter') oTime = cfg['raspberryPiConfig']['updateTime'] while cfg['raspberryPiConfig']['updateTime'] % cfg[ 'raspberryPiConfig']['checkTime']: cfg['raspberryPiConfig']['updateTime'] += 1 if oTime != cfg['raspberryPiConfig']['updateTime']: logger.debug( "\"updateTime\" changed from %s to %s so it would divide evenly by \"checkTime\".", oTime, cfg['raspberryPiConfig']['updateTime']) # This totally isn't a clock, it's a counter. But we use it sort of # like a clock. args.scaleClock = 0 # Python shelves let us keep some state between runs; in this case, we # are keeping the state of the alerts we have generated, so we don't # re-send emails or tweets if one has already been sent for a given # state args.alertState = shelve.open("scaleState.db", writeback=True) for alert in cfg['raspberryPiConfig']['alertChannels']: if alert not in args.alertState: args.alertState[alert] = 0 logger.debug("Ready.") return args, cfg
def initialize(self): configFile = './scaleConfig.yaml' parser = argparse.ArgumentParser() parser.add_argument ("-d", "--debug", type=int, choices=[0,1], nargs='?', const=1, help="turn debugging on (1) or off (0); this overrides"+ "the value in the configuration file ") parser.add_argument("-c","--config", action="store", dest="configFile", help="specify a configuration file") parser.add_argument("-r","--requires-root", action="store_true", dest="configFile", help="for Matt Daemon") args = parser.parse_args() if args.configFile: configFile = args.configFile self.logger = logging.getLogger("beanscale") self.logger.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler = logging.FileHandler("/home/pi/scale/scaledaemon.log") handler.setFormatter(formatter) self.logger.addHandler(handler) self.logger.info("Starting") cfg = scaleConfig.readConfig(self,configFile) if args.debug == None: DEBUG = cfg['raspberryPiConfig']['debug'] else: DEBUG = args.debug # if DEBUG: # logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(message)s') # else: # logging.basicConfig(filename="scale.log",level=logging.DEBUG, # format='%(asctime)s %(message)s') logger.debug("Initializing.") try: f = open(cfg['raspberryPiConfig']['plotlyCredsFile']) args.plotlyConfig = yaml.safe_load(f) f.close() except: logger.error("I couldn't open the file %s to read the plot.ly settings, so I can't make a plot and am giving up. I am %s/%s", cfg['raspberryPiConfig']['plotlyCredsFile'], os.path.abspath(os.path.dirname(sys.argv[0])),sys.argv[0]) exit (1) if not cfg['raspberryPiConfig']['alertChannels']: cfg['raspberryPiConfig']['alertChannels'] = '' if not cfg['raspberryPiConfig']['updateChannels']: cfg['raspberryPiConfig']['updateChannels'] = '' global GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # to stop the "This channel is already in use" warning # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler # ADCPort Cobbler # -------- ---------- args.SPICLK = cfg['raspberryPiConfig']['ADCtoCobbler']['SPICLK'] args.SPIMISO = cfg['raspberryPiConfig']['ADCtoCobbler']['SPIMISO'] args.SPIMOSI = cfg['raspberryPiConfig']['ADCtoCobbler']['SPIMOSI'] args.SPICS = cfg['raspberryPiConfig']['ADCtoCobbler']['SPICS'] # Diagram of ADC (MCP3008) is at: # https://learn.adafruit.com/ # reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi/ # connecting-the-cobbler-to-a-mcp3008 # set up the SPI interface pins GPIO.setup(args.SPIMOSI, GPIO.OUT) GPIO.setup(args.SPIMISO, GPIO.IN) GPIO.setup(args.SPICLK, GPIO.OUT) GPIO.setup(args.SPICS, GPIO.OUT) # FSR connected to adc #0 args.fsr_adc = cfg['raspberryPiConfig']['adcPortWithFSR'] # to keep from being jittery we'll only change when the FSR has moved # more than this many 'counts' args.tolerance = cfg['raspberryPiConfig']['tolerance'] # not ideal, but at least it's just in one place now args.logString = '{0} {1}\tfsr: {2:4d}\tlast_value: {3:4d}\tchange: {4:4d}\tbeans: {5}' # If we will need Twitter credentials, read them now # if 'twitter' in cfg['raspberryPiConfig']['alertChannels'] or 'twitter' in cfg['raspberryPiConfig']['updateChannels']: logger.debug ("Reading Twitter credentials from {}".format(cfg['twitterConfiguration']['twitterCredsFile'])) try: f = open(cfg['twitterConfiguration']['twitterCredsFile']) twitterCredentials = yaml.safe_load(f) f.close() except: logger.error("I couldn't open the file %s to read the twitter credentials, so I can't tweet. I am: %s/%s", cfg['twitterConfiguration']['twitterCredsFile'], os.path.abspath(os.path.dirname(sys.argv[0])), sys.argv[0]) cfg['raspberryPiConfig']['alertChannels'].remove('twitter') cfg['raspberryPiConfig']['updateChannels'].remove('twitter') oTime = cfg['raspberryPiConfig']['updateTime'] while cfg['raspberryPiConfig']['updateTime'] % cfg['raspberryPiConfig']['checkTime']: cfg['raspberryPiConfig']['updateTime'] += 1 if oTime != cfg['raspberryPiConfig']['updateTime']: logger.debug ("\"updateTime\" changed from %s to %s so it would divide evenly by \"checkTime\".", oTime, cfg['raspberryPiConfig']['updateTime']) # This totally isn't a clock, it's a counter. But we use it sort of # like a clock. args.scaleClock = 0 # Python shelves let us keep some state between runs; in this case, we # are keeping the state of the alerts we have generated, so we don't # re-send emails or tweets if one has already been sent for a given # state args.alertState = shelve.open("scaleState.db", writeback=True) for alert in cfg['raspberryPiConfig']['alertChannels']: if alert not in args.alertState: args.alertState[alert] = 0 logger.debug ("Ready.") return args,cfg