def getTime(source, delay): print("getTime source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the GD (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "RT") print("response=", response); datecommand = "date -s '" + response + "'" print datecommand output = subprocess.check_output (datecommand,shell=True, stderr=subprocess.STDOUT ) # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); pclogging.log(pclogging.INFO, __name__, "RT - Time Set on Pi to Arduino Time ") ser.close()
def readTime(source, delay): print("readTime source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the GD (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(3.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "RT") print("Arduino readtime response=", response) myTime = response # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) pclogging.log(pclogging.INFO, __name__, "RT - Arduino Time Read " + myTime) ser.close()
def sendWatchDogTimer(source, delay, ser): print("sendWatchDogTimer source:%s" % source) time.sleep(delay) response = util.sendCommandAndRecieve(ser, "WD") print("response=", response) if (len(response) > 0): print("WD success") # pclogging.log(pclogging.INFO, __name__, "Watchdog Timer Reset on Arduino" ) else: # system setup pclogging.log(pclogging.ERROR, __name__, "WD failed from Pi to BatteryWatchDog")
def sendWatchDogTimer(source, delay,ser): print("sendWatchDogTimer source:%s" % source) time.sleep(delay) response = util.sendCommandAndRecieve(ser, "WD") print("response=", response); if (len(response) > 0): print("WD success") # pclogging.log(pclogging.INFO, __name__, "Watchdog Timer Reset on Arduino" ) else: # system setup pclogging.log(pclogging.ERROR, __name__, "WD failed from Pi to BatteryWatchDog")
def selectWind(source, delay): print("recieveInterruptFromBW source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the WA (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) # ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return globalvars.FAILED # Read the value response = util.sendCommandAndRecieve(ser, "SW") print("response=", response) if (len(response) > 0): pclogging.log(pclogging.INFO, __name__, "sent selectWind command to Battery Watchdog ") else: # system setup pclogging.log(pclogging.ERROR, __name__, "SW failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() return globalvars.FAILED response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() # now we have the data, dostuff with it return True
def setThresholds(source, delay): print ("setThresholds source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the STH (Set Thresholds Data) Command ser = serial.Serial("/dev/ttyAMA0", 9600, timeout=1) ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print ("response=", response) if response == "OK\n": print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "STH") print ("response=", response) if response == "OK\n": print "Good STH Response" # set up our time string # Dec 26 2009", time = "12:34:56 # myDateString = time.strftime("%b %d %Y", time.gmtime()) # myTimeString = time.strftime("%H:%M:%S", time.gmtime()) PI_BATTERY_SHUTDOWN_THRESHOLD = 3.706 # 20% PI_BATTERY_STARTUP_THRESHOLD = 3.839 # 30% INSIDE_TEMPERATURE_PI_SHUTDOWN_THRESHOLD = 49.33 # 110 INSIDE_TEMPERATURE_PI_STARTUP_THRESHOLD = 37.78 # 100 INSIDE_HUMIDITY_PI_SHUTDOWN_THRESHOLD = 98.0 INSIDE_HUMIDITY_PI_STARTUP_THRESHOLD = 93.0 PI_START_TIME = "10:00:00" # UTC PI_SHUTDOWN_TIME = "22:30:00" # UTC PI_MIDNIGHT_WAKEUP_SECONDS_LENGTH = 3600.0 # one hour PI_MIDNIGHT_WAKEUP_THRESHOLD = 3.971 # 60% mySendString = "%4.3f, %4.3f, %4.3f, %4.3f, %4.3f, %4.3f,%s,%s, %4.3f, %4.3f, %i\n" % ( conf.PI_BATTERY_SHUTDOWN_THRESHOLD, conf.PI_BATTERY_STARTUP_THRESHOLD, conf.INSIDE_TEMPERATURE_PI_SHUTDOWN_THRESHOLD, conf.INSIDE_TEMPERATURE_PI_STARTUP_THRESHOLD, conf.INSIDE_HUMIDITY_PI_SHUTDOWN_THRESHOLD, conf.INSIDE_HUMIDITY_PI_STARTUP_THRESHOLD, conf.PI_START_TIME, conf.PI_SHUTDOWN_TIME, conf.PI_MIDNIGHT_WAKEUP_SECONDS_LENGTH, conf.PI_MIDNIGHT_WAKEUP_THRESHOLD, conf.enableShutdowns, ) response = util.sendCommandAndRecieve(ser, mySendString) if response == "OK\n": print "Good STH Response" else: print "bad response from STH second line" pclogging.log(pclogging.ERROR, __name__, "STH second line failed from Pi to BatteryWatchDog") ser.close() return else: print "bad response from STH" pclogging.log(pclogging.ERROR, __name__, "STH failed from Pi to BatteryWatchDog") ser.close() return # say goodbye response = util.sendCommandAndRecieve(ser, "GB") print ("response=", response) pclogging.log(pclogging.INFO, __name__, "STH - Thresholds set on Arduino") ser.close()
def setTime(source, delay): print("setTime source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the GD (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "ST") print("response=", response) if (response == "OK\n"): print "Good ST Response" # set up our time string #Dec 26 2009", time = "12:34:56 myDateString = time.strftime("%b %d %Y", time.gmtime()) myTimeString = time.strftime("%H:%M:%S", time.gmtime()) #myTimeString = time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime()) response = util.sendCommandAndRecieve(ser, myDateString) response = util.sendCommandAndRecieve(ser, myTimeString) if (response == "OK\n"): print "Good ST-2 Response" else: print "bad response from ST-2 third line" pclogging.log( pclogging.ERROR, __name__, "ST-2 second line failed from Pi to BatteryWatchDog") ser.close() return else: print "bad response from ST" pclogging.log(pclogging.ERROR, __name__, "ST failed from Pi to BatteryWatchDog") ser.close() return # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) pclogging.log(pclogging.INFO, __name__, "ST - Time Set on Arduino to Pi Time ") ser.close()
def selectWind(source, delay): print("recieveInterruptFromBW source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the WA (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return globalvars.FAILED # Read the value response = util.sendCommandAndRecieve(ser, "SW") print("response=", response); if (len(response) > 0): pclogging.log(pclogging.INFO, __name__, "sent selectWind command to Battery Watchdog " ) else: # system setup pclogging.log(pclogging.ERROR, __name__, "SW failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() return globalvars.FAILED response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() # now we have the data, dostuff with it return True;
finally: cur.close() con.close() del cur del con else: print "bad response from SL" # system setup pclogging.log(pclogging.ERROR, __name__, "SL failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() return print("ArInputCurrent =", ArInputCurrent) # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() # now we have the data, stuff it in the database
def watchdogdatacollect(source, delay): print("watchdogdatacollect source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() #time.sleep(7.0) time.sleep(9.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value # send the watchdog timer first sendWatchDogTimer.sendWatchDogTimer("watchdogdatacollect", 0, ser) # Send the GD (Get Data) Command response = util.sendCommandAndRecieve(ser, "GD") print("response=", response) # stuff the values into variables splitList = response.split(',') print(splitList) for item in range(len(splitList)): splitList[item] = splitList[item].replace('NAN', "0.0") print(splitList) if (len(splitList) == 15): ArInputVoltage = float(splitList[0]) ArInputCurrent = float(splitList[1]) SolarCellVoltage = float(splitList[2]) SolarCellCurrent = float(splitList[3]) BatteryVoltage = float(splitList[4]) BatteryCurrent = float(splitList[5]) OutsideHumidity = float(splitList[6]) OutsideTemperature = float(splitList[7]) LastReboot = splitList[8] BatteryTemperature = float(splitList[9]) FreeMemory = float(splitList[10]) UnregulatedWindVoltage = float(splitList[11]) RegulatedWindVoltage = float(splitList[12]) SolarWind = int(splitList[13]) ArduinoPiBatteryVoltage = float(splitList[14]) else: print "bad response from GD" # system setup pclogging.log(pclogging.ERROR, __name__, "GD failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() return print("ArInputCurrent =", ArInputCurrent) # power efficiency if ((SolarCellCurrent * SolarCellVoltage + BatteryCurrent * BatteryVoltage) == 0): powerEfficiency = 10000.0 else: powerEfficiency = (ArInputCurrent * ArInputVoltage / (SolarCellCurrent * SolarCellVoltage + BatteryCurrent * BatteryVoltage)) * 100 # if power Efficiency < 0, then must be plugged in so add 500ma @ 5V if (powerEfficiency < 0.0): if ((SolarCellCurrent * SolarCellVoltage + BatteryCurrent * BatteryVoltage + 5.0 * 500.0) == 0.0): powerEfficiency = 10000.0 else: powerEfficiency = ( ArInputCurrent * ArInputVoltage / (SolarCellCurrent * SolarCellVoltage + BatteryCurrent * BatteryVoltage + 5.0 * 500.0)) * 100 # get the unread log from the Arduino response = util.sendCommandAndRecieve(ser, "SL") print("response=", response) try: countEntry = int(response) except ValueError: countEntry = 0 if (countEntry > 0): # read all unread entries for i in range(countEntry): logEntry = util.recieveLine(ser) print("recievedLogEntry =", logEntry) # parse and then stuff in log database # stuff the values into variables splitList = logEntry.split(',') print(splitList) if (len(splitList) == 4): ArduinoTime = splitList[0] ArduinoLevel = int(splitList[1]) ArduinoData0 = int(splitList[2]) ArduinoData1 = splitList[3] # now we have the data, stuff it in the database entryValue = util.convertArduinoEntry01ToText( ArduinoData0, ArduinoData1) if (ArduinoData0 == 9): # LOGAlarmTriggered entryValue = util.convertAlarmToText(int(ArduinoData1)) try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao') cur = con.cursor() print "before query" query = "INSERT INTO systemlog(TimeStamp, Level, Source, Message) VALUES('%s', '%s', '%s', '%s')" % ( ArduinoTime, ArduinoLevel, 'Ardinuo BatteryWatchDog', entryValue) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) con.rollback() #sys.exit(1) finally: cur.close() con.close() del cur del con
def environdatacollect(source, delay): print("environdatacollect source:%s" % source) # delay to not "everything happen at once" time.sleep(delay) # blink GPIO LED when it's run # double blink GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # now read in all the required data # Inside Temperature # Barometric Pressure # Initialise the BMP085 and use STANDARD mode (default value) # bmp = BMP085(0x77, debug=True) # bmp = BMP085(0x77) # To specify a different operating mode, uncomment one of the following: # bmp = BMP085(0x77, 0) # ULTRALOWPOWER Mode # bmp = BMP085(0x77, 1) # STANDARD Mode # bmp = BMP085(0x77, 2) # HIRES Mode bmp = BMP085(0x77, 3) # ULTRAHIRES Mode pressure = -1000.0 # bad data insidetemperature = -1000.0 try: pressure = bmp.readPressure() / 100.0 insidetemperature = bmp.readTemperature() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print "Unexpected error:", sys.exc_info()[0] raise # Inside Humidity Oldinsidehumidity = -1000.0 # bad data insidehumidity = -1000.0 # bad data try: maxCount = 20 count = 0 while (count < maxCount): output = subprocess.check_output([ "/home/pi/ProjectCuracao/main/hardware/Adafruit_DHT_MOD", "22", "23" ]) print "count=", count print output # search for humidity printout matches = re.search("Hum =\s+([0-9.]+)", output) if (not matches): count = count + 1 time.sleep(3.0) continue Oldinsidehumidity = float(matches.group(1)) count = maxCount # now do it again. Throw out the higher value (get rid of high spikes) time.sleep(1.0) maxCount = 20 count = 0 while (count < maxCount): output = subprocess.check_output([ "/home/pi/ProjectCuracao/main/hardware/Adafruit_DHT_MOD", "22", "23" ]) print "count=", count print output # search for humidity printout matches = re.search("Hum =\s+([0-9.]+)", output) if (not matches): count = count + 1 time.sleep(3.0) continue insidehumidity = float(matches.group(1)) count = maxCount if (Oldinsidehumidity < insidehumidity): insidehumidity = Oldinsidehumidity except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print "Unexpected error:", sys.exc_info()[0] raise # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Outside Temperature # read the latest value from the Arduino # Send the GD (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "GTH") print("response=", response) # stuff the values into variables splitList = response.split(',') print(splitList) if (len(splitList) == 2): outsidetemperature = float(splitList[0]) outsidehumidity = float(splitList[1]) else: print "bad response from GTH" # system setup pclogging.log(pclogging.ERROR, __name__, "GTH failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() return print("response=", response) # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() # Luminosity luminosity = -1000.0 # bad data try: oLuxmeter = Luxmeter() luminosity = oLuxmeter.getLux() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print "Unexpected error:", sys.exc_info()[0] raise # Fan State # read from fan state file try: f = open("./state/fanstate.txt", "r") tempString = f.read() f.close() fanstate = int(tempString) except IOError as e: fanstate = 0 print "fanstate=", fanstate # now find our colors bm017 = BM017(True) red_color = 0 blue_color = 0 green_color = 0 clear_color = 0 Gain = 0x00 IntegrationTime = 0xC0 bm017.disableDevice() bm017.setIntegrationTimeAndGain(IntegrationTime, Gain) if (bm017.isBM017There()): bm017.getColors() red_color = bm017.red_color blue_color = bm017.blue_color green_color = bm017.green_color clear_color = bm017.clear_color # now we have the data, stuff it in the database try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao') cur = con.cursor() print "before query" query = 'INSERT INTO environmentaldata(TimeStamp, InsideTemperature, InsideHumidity, OutsideTemperature, OutsideHumidity, BarometricPressure, Luminosity, FanState, Red_Color, Blue_Color, Green_Color, Clear_Color, Gain, IntegrationTime) VALUES(UTC_TIMESTAMP(), %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %i, %i, %i, %i, %i, %i, %i)' % ( insidetemperature, insidehumidity, outsidetemperature, outsidehumidity, pressure, luminosity, fanstate, red_color, blue_color, green_color, clear_color, Gain, IntegrationTime) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) con.rollback()
def environdatacollect(source, delay): print("environdatacollect source:%s" % source) # delay to not "everything happen at once" time.sleep(delay) # blink GPIO LED when it's run # double blink GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # now read in all the required data # Inside Temperature # Barometric Pressure # Initialise the BMP085 and use STANDARD mode (default value) # bmp = BMP085(0x77, debug=True) # bmp = BMP085(0x77) # To specify a different operating mode, uncomment one of the following: # bmp = BMP085(0x77, 0) # ULTRALOWPOWER Mode # bmp = BMP085(0x77, 1) # STANDARD Mode # bmp = BMP085(0x77, 2) # HIRES Mode bmp = BMP085(0x77, 3) # ULTRAHIRES Mode pressure = -1000.0 # bad data insidetemperature =-1000.0 try: pressure = bmp.readPressure()/100.0 insidetemperature = bmp.readTemperature() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print "Unexpected error:", sys.exc_info()[0] raise # Inside Humidity Oldinsidehumidity = -1000.0 # bad data insidehumidity = -1000.0 # bad data try: maxCount = 20 count = 0 while (count < maxCount): output = subprocess.check_output(["/home/pi/ProjectCuracao/main/hardware/Adafruit_DHT_MOD", "22", "23"]); print "count=", count print output # search for humidity printout matches = re.search("Hum =\s+([0-9.]+)", output) if (not matches): count = count + 1 time.sleep(3.0) continue Oldinsidehumidity = float(matches.group(1)) count = maxCount # now do it again. Throw out the higher value (get rid of high spikes) time.sleep(1.0) maxCount = 20 count = 0 while (count < maxCount): output = subprocess.check_output(["/home/pi/ProjectCuracao/main/hardware/Adafruit_DHT_MOD", "22", "23"]); print "count=", count print output # search for humidity printout matches = re.search("Hum =\s+([0-9.]+)", output) if (not matches): count = count + 1 time.sleep(3.0) continue insidehumidity = float(matches.group(1)) count = maxCount if (Oldinsidehumidity < insidehumidity): insidehumidity = Oldinsidehumidity except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print "Unexpected error:", sys.exc_info()[0] raise # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Outside Temperature # read the latest value from the Arduino # Send the GD (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "GTH") print("response=", response); # stuff the values into variables splitList = response.split(',') print(splitList) if (len(splitList) == 2): outsidetemperature = float(splitList[0]) outsidehumidity = float(splitList[1]) else: print "bad response from GTH" # system setup pclogging.log(pclogging.ERROR, __name__, "GTH failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() return print("response=", response); # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() # Luminosity luminosity = -1000.0 # bad data try: oLuxmeter=Luxmeter() luminosity = oLuxmeter.getLux() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except: print "Unexpected error:", sys.exc_info()[0] raise # Fan State # read from fan state file try: f = open("./state/fanstate.txt", "r") tempString = f.read() f.close() fanstate = int(tempString) except IOError as e: fanstate = 0 print "fanstate=", fanstate # now find our colors bm017 = BM017(True) red_color = 0 blue_color = 0 green_color = 0 clear_color = 0 Gain = 0x00 IntegrationTime = 0xC0 bm017.disableDevice() bm017.setIntegrationTimeAndGain(IntegrationTime, Gain) if (bm017.isBM017There()): bm017.getColors() red_color = bm017.red_color blue_color = bm017.blue_color green_color = bm017.green_color clear_color = bm017.clear_color # now we have the data, stuff it in the database try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao'); cur = con.cursor() print "before query" query = 'INSERT INTO environmentaldata(TimeStamp, InsideTemperature, InsideHumidity, OutsideTemperature, OutsideHumidity, BarometricPressure, Luminosity, FanState, Red_Color, Blue_Color, Green_Color, Clear_Color, Gain, IntegrationTime) VALUES(UTC_TIMESTAMP(), %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %i, %i, %i, %i, %i, %i, %i)' % (insidetemperature, insidehumidity, outsidetemperature, outsidehumidity, pressure, luminosity, fanstate, red_color, blue_color, green_color, clear_color, Gain, IntegrationTime) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) con.rollback()
def recieveInterruptFromBW(source, delay): print("recieveInterruptFromBW source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the WA (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return globalvars.FAILED # Read the value response = util.sendCommandAndRecieve(ser, "WA") print("response=", response); AWAState = 100 #UNKNOWNINTERRUPT AWAState = 101 #BAD INTERRUPT if (len(response) > 0): try: AWAState = int(response) except: AWAState = 101 pclogging.log(pclogging.INFO, __name__, "Recieved Interrupt %s from BatteryWatchDog to Pi" % returnNameFromInterrupt(AWAState)) else: # system setup pclogging.log(pclogging.ERROR, __name__, "WA failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() return globalvars.FAILED # say acknowledge WA response = util.sendCommandAndRecieve(ser, "AWA") print("response=", response); if (response == "OK\n"): print "Good AWA Response" pclogging.log(pclogging.INFO, __name__, "Acknowledged Interrupt %s from BatteryWatchDog to Pi" % returnNameFromInterrupt(AWAState)) else: print "bad response from AWA" pclogging.log(pclogging.ERROR, __name__, "AWA failed from Pi to BatteryWatchDog") ser.close() return globalvars.FAILED response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() # now we have the data, dostuff with it return AWAState
con.rollback() #sys.exit(1) finally: cur.close() con.close() del cur del con else: print "bad response from SLF" # system setup pclogging.log(pclogging.ERROR, __name__, "SLF failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() return 0 # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() return countEntry
def getFramArduinoLog(source, delay): print("Fram Log datacollect source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() ser.flushInput() ser.flushOutput() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return 0 # Read the value # get the unread log from the Arduino response = util.sendCommandAndRecieve(ser, "SLF") print("response=", response) try: countEntry = int(response) except ValueError: countEntry = 0 print("countEntry=", countEntry) pclogging.log(pclogging.INFO, __name__, "FRAM records read: %i " % countEntry) if (countEntry > 0): # read all unread entries for i in range(countEntry): logEntry = util.recieveLine(ser) print("recievedLogEntry =", logEntry) # parse and then stuff in log database # stuff the values into variables splitList = logEntry.split(',') print(splitList) if (len(splitList) == 11): WeatherLogTime = splitList[0] WeatherLogCWS = float(splitList[1]) WeatherLogCWG = float(splitList[2]) WeatherLogCWD = float(splitList[3]) WeatherLogCWDV = float(splitList[4]) WeatherLogCR = float(splitList[5]) WeatherLogURWV = float(splitList[6]) WeatherLogRWV = float(splitList[7]) WeatherLogPIS = float(splitList[8]) WeatherLogWSCUR = float(splitList[9]) WeatherLogSolarOrWind = int(splitList[10]) # now we have the data, stuff it in the database try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao'); cur = con.cursor() print "before query" query = "INSERT INTO weatherdata(TimeStamp, CWS, CWG, CWD, CWDV, CR, URWV, RWV, PIS,WSCUR, SolarOrWind) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %i)" % (WeatherLogTime, WeatherLogCWS, WeatherLogCWG, WeatherLogCWD, WeatherLogCWDV, WeatherLogCR,WeatherLogURWV, WeatherLogRWV, WeatherLogPIS, WeatherLogWSCUR, WeatherLogSolarOrWind) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) con.rollback() #sys.exit(1) finally: cur.close() con.close() del cur del con
def getFramArduinoLog(source, delay): print("Fram Log datacollect source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() ser.flushInput() ser.flushOutput() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return 0 # Read the value # get the unread log from the Arduino response = util.sendCommandAndRecieve(ser, "SLF") print("response=", response) try: countEntry = int(response) except ValueError: countEntry = 0 print("countEntry=", countEntry) pclogging.log(pclogging.INFO, __name__, "FRAM records read: %i " % countEntry) if (countEntry > 0): # read all unread entries for i in range(countEntry): logEntry = util.recieveLine(ser) print("recievedLogEntry =", logEntry) # parse and then stuff in log database # stuff the values into variables splitList = logEntry.split(',') print(splitList) if (len(splitList) == 11): WeatherLogTime = splitList[0] WeatherLogCWS = float(splitList[1]) WeatherLogCWG = float(splitList[2]) WeatherLogCWD = float(splitList[3]) WeatherLogCWDV = float(splitList[4]) WeatherLogCR = float(splitList[5]) WeatherLogURWV = float(splitList[6]) WeatherLogRWV = float(splitList[7]) WeatherLogPIS = float(splitList[8]) WeatherLogWSCUR = float(splitList[9]) WeatherLogSolarOrWind = int(splitList[10]) # now we have the data, stuff it in the database try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao') cur = con.cursor() print "before query" query = "INSERT INTO weatherdata(TimeStamp, CWS, CWG, CWD, CWDV, CR, URWV, RWV, PIS,WSCUR, SolarOrWind) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %i)" % ( WeatherLogTime, WeatherLogCWS, WeatherLogCWG, WeatherLogCWD, WeatherLogCWDV, WeatherLogCR, WeatherLogURWV, WeatherLogRWV, WeatherLogPIS, WeatherLogWSCUR, WeatherLogSolarOrWind) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) con.rollback() #sys.exit(1) finally: cur.close() con.close() del cur del con
def getArduinoLog(source, delay): print("watchdogdatacollect source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() ser.flushInput() ser.flushOutput() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value # send the watchdog timer first sendWatchDogTimer.sendWatchDogTimer("watchdogdatacollect", 0,ser) # get the unread log from the Arduino response = util.sendCommandAndRecieve(ser, "SL") print("response=", response) try: countEntry = int(response) except ValueError: countEntry = 0 if (countEntry > 0): # read all unread entries for i in range(countEntry): logEntry = util.recieveLine(ser) print("recievedLogEntry =", logEntry) # parse and then stuff in log database # stuff the values into variables splitList = logEntry.split(',') print(splitList) if (len(splitList) == 4): ArduinoTime = splitList[0] ArduinoLevel = int(splitList[1]) ArduinoData0 = int(splitList[2]) ArduinoData1 = splitList[3] # now we have the data, stuff it in the database entryValue = util.convertArduinoEntry01ToText(ArduinoData0, ArduinoData1) if (ArduinoData0 == 9): # LOGAlarmTriggered entryValue = util.convertAlarmToText(int(ArduinoData1)) try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao'); cur = con.cursor() print "before query" query = "INSERT INTO systemlog(TimeStamp, Level, Source, Message) VALUES('%s', '%s', '%s', '%s')" % (ArduinoTime, ArduinoLevel, 'Ardinuo BatteryWatchDog', entryValue) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) con.rollback() #sys.exit(1) finally: cur.close() con.close() del cur del con
def watchdogdatacollect(source, delay): print("watchdogdatacollect source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() #time.sleep(7.0) time.sleep(9.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value # send the watchdog timer first sendWatchDogTimer.sendWatchDogTimer("watchdogdatacollect", 0,ser) # Send the GD (Get Data) Command response = util.sendCommandAndRecieve(ser, "GD") print("response=", response); # stuff the values into variables splitList = response.split(',') print(splitList) for item in range(len(splitList)): splitList[item] = splitList[item].replace('NAN', "0.0") print(splitList) if (len(splitList) == 15): ArInputVoltage = float(splitList[0]) ArInputCurrent = float(splitList[1]) SolarCellVoltage = float(splitList[2]) SolarCellCurrent = float(splitList[3]) BatteryVoltage = float(splitList[4]) BatteryCurrent = float(splitList[5]) OutsideHumidity = float(splitList[6]) OutsideTemperature = float(splitList[7]) LastReboot = splitList[8] BatteryTemperature = float(splitList[9]) FreeMemory = float(splitList[10]) UnregulatedWindVoltage = float(splitList[11]) RegulatedWindVoltage = float(splitList[12]) SolarWind = int(splitList[13]) ArduinoPiBatteryVoltage = float(splitList[14]) else: print "bad response from GD" # system setup pclogging.log(pclogging.ERROR, __name__, "GD failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); ser.close() return print("ArInputCurrent =", ArInputCurrent) # power efficiency if ( (SolarCellCurrent*SolarCellVoltage+BatteryCurrent*BatteryVoltage) == 0): powerEfficiency = 10000.0; else: powerEfficiency = (ArInputCurrent*ArInputVoltage/(SolarCellCurrent*SolarCellVoltage+BatteryCurrent*BatteryVoltage))*100 # if power Efficiency < 0, then must be plugged in so add 500ma @ 5V if (powerEfficiency < 0.0): if ((SolarCellCurrent*SolarCellVoltage+BatteryCurrent*BatteryVoltage+5.0*500.0) == 0.0): powerEfficiency = 10000.0; else: powerEfficiency = (ArInputCurrent*ArInputVoltage/(SolarCellCurrent*SolarCellVoltage+BatteryCurrent*BatteryVoltage+5.0*500.0))*100 # get the unread log from the Arduino response = util.sendCommandAndRecieve(ser, "SL") print("response=", response) try: countEntry = int(response) except ValueError: countEntry = 0 if (countEntry > 0): # read all unread entries for i in range(countEntry): logEntry = util.recieveLine(ser) print("recievedLogEntry =", logEntry) # parse and then stuff in log database # stuff the values into variables splitList = logEntry.split(',') print(splitList) if (len(splitList) == 4): ArduinoTime = splitList[0] ArduinoLevel = int(splitList[1]) ArduinoData0 = int(splitList[2]) ArduinoData1 = splitList[3] # now we have the data, stuff it in the database entryValue = util.convertArduinoEntry01ToText(ArduinoData0, ArduinoData1) if (ArduinoData0 == 9): # LOGAlarmTriggered entryValue = util.convertAlarmToText(int(ArduinoData1)) try: print("trying database") con = mdb.connect('localhost', 'root', conf.databasePassword, 'ProjectCuracao'); cur = con.cursor() print "before query" query = "INSERT INTO systemlog(TimeStamp, Level, Source, Message) VALUES('%s', '%s', '%s', '%s')" % (ArduinoTime, ArduinoLevel, 'Ardinuo BatteryWatchDog', entryValue) print("query=%s" % query) cur.execute(query) con.commit() except mdb.Error, e: print "Error %d: %s" % (e.args[0],e.args[1]) con.rollback() #sys.exit(1) finally: cur.close() con.close() del cur del con
def setThresholds(source, delay): print("setThresholds source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the STH (Set Thresholds Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) # ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "STH") print("response=", response) if (response == "OK\n"): print "Good STH Response" # set up our time string #Dec 26 2009", time = "12:34:56 #myDateString = time.strftime("%b %d %Y", time.gmtime()) #myTimeString = time.strftime("%H:%M:%S", time.gmtime()) mySendString = "%4.3f, %4.3f, %4.3f, %4.3f, %4.3f, %4.3f,%s,%s, %4.3f, %4.3f, %i\n" % ( conf.PI_BATTERY_SHUTDOWN_THRESHOLD, conf.PI_BATTERY_STARTUP_THRESHOLD, conf.INSIDE_TEMPERATURE_PI_SHUTDOWN_THRESHOLD, conf.INSIDE_TEMPERATURE_PI_STARTUP_THRESHOLD, conf.INSIDE_HUMIDITY_PI_SHUTDOWN_THRESHOLD, conf.INSIDE_HUMIDITY_PI_STARTUP_THRESHOLD, conf.PI_START_TIME, conf.PI_SHUTDOWN_TIME, conf.PI_MIDNIGHT_WAKEUP_SECONDS_LENGTH, conf.PI_MIDNIGHT_WAKEUP_THRESHOLD, conf.enableShutdowns) response = util.sendCommandAndRecieve(ser, mySendString) if (response == "OK\n"): print "Good STH Response" else: print "bad response from STH second line" pclogging.log( pclogging.ERROR, __name__, "STH second line failed from Pi to BatteryWatchDog") ser.close() return else: print "bad response from STH" pclogging.log(pclogging.ERROR, __name__, "STH failed from Pi to BatteryWatchDog") ser.close() return # say goodbye response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) pclogging.log(pclogging.INFO, __name__, "STH - Thresholds set on Arduino") ser.close()
def setTime(source, delay): print("setTime source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the GD (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response); if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return # Read the value response = util.sendCommandAndRecieve(ser, "ST") print("response=", response); if (response == "OK\n"): print "Good ST Response" # set up our time string #Dec 26 2009", time = "12:34:56 myDateString = time.strftime("%b %d %Y", time.gmtime()) myTimeString = time.strftime("%H:%M:%S", time.gmtime()) #myTimeString = time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime()) response = util.sendCommandAndRecieve(ser, myDateString) response = util.sendCommandAndRecieve(ser, myTimeString) if (response == "OK\n"): print "Good ST-2 Response" else: print "bad response from ST-2 third line" pclogging.log(pclogging.ERROR, __name__, "ST-2 second line failed from Pi to BatteryWatchDog") ser.close() return else: print "bad response from ST" pclogging.log(pclogging.ERROR, __name__, "ST failed from Pi to BatteryWatchDog") ser.close() return # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response); pclogging.log(pclogging.INFO, __name__, "ST - Time Set on Arduino to Pi Time ") ser.close()
def recieveInterruptFromBW(source, delay): print("recieveInterruptFromBW source:%s" % source) time.sleep(delay) # blink GPIO LED when it's run GPIO.setmode(GPIO.BOARD) GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) time.sleep(0.1) GPIO.output(22, False) time.sleep(0.1) GPIO.output(22, True) # setup serial port to Arduino # interrupt Arduino to start listening GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) GPIO.output(7, False) GPIO.output(7, True) GPIO.output(7, False) # Send the WA (Get Data) Command ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) #ser.open() time.sleep(7.0) # send the first "are you there? command - RD - return from Arduino OK" response = util.sendCommandAndRecieve(ser, "RD") print("response=", response) if (response == "OK\n"): print "Good RD Response" else: print "bad response from RD" pclogging.log(pclogging.ERROR, __name__, "RD failed from Pi to BatteryWatchDog") ser.close() return globalvars.FAILED # Read the value response = util.sendCommandAndRecieve(ser, "WA") print("response=", response) AWAState = 100 #UNKNOWNINTERRUPT AWAState = 101 #BAD INTERRUPT if (len(response) > 0): try: AWAState = int(response) except: AWAState = 101 pclogging.log( pclogging.INFO, __name__, "Recieved Interrupt %s from BatteryWatchDog to Pi" % returnNameFromInterrupt(AWAState)) else: # system setup pclogging.log(pclogging.ERROR, __name__, "WA failed from Pi to BatteryWatchDog") # say goodby response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() return globalvars.FAILED # say acknowledge WA response = util.sendCommandAndRecieve(ser, "AWA") print("response=", response) if (response == "OK\n"): print "Good AWA Response" pclogging.log( pclogging.INFO, __name__, "Acknowledged Interrupt %s from BatteryWatchDog to Pi" % returnNameFromInterrupt(AWAState)) else: print "bad response from AWA" pclogging.log(pclogging.ERROR, __name__, "AWA failed from Pi to BatteryWatchDog") ser.close() return globalvars.FAILED response = util.sendCommandAndRecieve(ser, "GB") print("response=", response) ser.close() # now we have the data, dostuff with it return AWAState