def poquerytag(tagsn, logtype="prod"): fplog.l("=> in poquerytag",logtype) error = True while error: try: GPIO.output(GPIO_GREEN_LED, GPIO.HIGH) fplog.l('Query tag: ' + USEURL + '/flowerquery/' + str(tagsn),logtype) r = requests.get(USEURL + '/flowerquery/' + str(tagsn)) error = False except: fplog.l('Error: Site not accessible') blinkredled() error = True fplog.l('Got response from URL: ' + str(r),logtype) tagstatus = "not_existing" if r.status_code == 200: tagstatus = "existing" fplog.l("<= out fppost, return value: tagstatus = " + str(tagstatus),logtype) return tagstatus
def abortonerror(): blinkredled() GPIO.output(GPIO_RED_LED, GPIO.HIGH) GPIO.output(GPIO_GREEN_LED, GPIO.LOW) GPIO.output(GPIO_BLUE_LED, GPIO.LOW) GPIO.output(GPIO_YELLOW_LED, GPIO.LOW) fplog.l("ABORT ON PREVIOUS ERROR!") exit()
def fertilizercheck(logtype="prod"): fplog.l("=> in fertilizercheck",logtype) input = GPIO.input(GPIO_FERTILIZER_SENSOR) if input: GPIO.output(GPIO_ORANGE_LED, GPIO.HIGH) fertilizer = "yes" else: GPIO.output(GPIO_ORANGE_LED, GPIO.LOW) fertilizer = "no" fplog.l("<= out fertilizercheck, return value: fertilizer = " + str(fertilizer),logtype) return fertilizer
def poquerytag(tagsn, logtype="prod"): fplog.l("=> in fppost.py/poquerytag",logtype) fplog.l('Using URL: ' + str(USEURL),logtype) r = requests.get(USEURL + '/flowerquery/' + str(tagsn)) fplog.l('Got response from URL: ' + str(r),logtype) tagstatus = "not_existing" if r.status_code == 200: tagstatus = "existing" fplog.l("<= out fppost, return value: tagstatus = " + str(tagstatus),logtype) return tagstatus
def poquerysite(logtype="prod"): fplog.l("=> in fppost.py/poquerysite",logtype) fplog.l('Using URL: ' + str(USEURL),logtype) r = requests.get(USEURL) fplog.l('Got response from URL: ' + str(r),logtype) tagstatus = "False" if r.status_code == 200: tagstatus = "True" fplog.l("<= out fppost, return value: tagstatus = " + str(tagstatus),logtype) return tagstatus
def poquerysite(logtype="prod"): fplog.l("=> in poquerysite",logtype) error = True while error: try: fplog.l('Try to query site: ' + str(USEURL),logtype) r = requests.get(USEURL) error = False except: fplog.l('Error: Site not accessible') blinkredled() error = True fplog.l('Got response from URL: ' + str(r),logtype) tagstatus = "False" if r.status_code == 200: tagstatus = "True" fplog.l("<= out fppost, return value: tagstatus = " + str(tagstatus),logtype) return tagstatus
import RPi.GPIO as GPIO # Import this module to gain access to the RFID driver import rfid # Import this module for generic logging used in flowerpi project import fplog # Set to the GPIO required to activate the blue LED (BCM notation!) GPIO_YELLOW_LED = 11 # ...run automated tests if library is started as a script if __name__ == "__main__": fplog.l("+++ In: fprfid.py Test +++ ","test") # GPIO_YELLOW_LED = output => Ansteuerung der gelben LED = Warten auf TAG GPIO.setup(GPIO_YELLOW_LED, GPIO.OUT) while True: # wait for a card to be detected as present GPIO.output(GPIO_YELLOW_LED, GPIO.HIGH) fplog.l("Waiting for a card...","test") rfid.waitTag() fplog.l("Card is present","test") GPIO.output(GPIO_YELLOW_LED, GPIO.LOW)
def phase4storeinwebapp(): fplog.l("PHASE4: Store information in web application") tagsn = RFIDUID fplog.l('Query for RF-ID tag: '+str(RFIDUID)) # Query if tag is existing, if not, create one querytag = poquerytag(tagsn, "prod") if querytag == "existing": fplog.l('Tag matches to following flower: ') r = requests.get(USEURL + '/flowerquery/' + str(tagsn)) fplog.l(r.text) flower_hash = json.loads(r.text) flower_id = str(flower_hash["id"]) flower_id.encode('ascii','ignore') fplog.l('The ID of the new flower is: ' + flower_id) flower = str(flower_hash["flowertype"]) flower.encode('ascii','ignore') fplog.l('The flower is a: ' + flower) else: fplog.l("Sorry, tag does not exist!") fplog.l("But it will try to create one") payload = {'tagsn': tagsn, 'pisn': PISERIAL, 'flowertype': 'new dummy', 'litershould': '0'} r = requests.post(USEURL + '/flowers.json', json=payload) if r.status_code == 201: fplog.l("... success in creating new flower") blinkyellowblueled() flower_hash = json.loads(r.text) flower_id = str(flower_hash["id"]) flower_id.encode('ascii','ignore') fplog.l('The ID of the new flower is: ' + flower_id) else: fplog.l("Sorry, was not able to create a new tag") fplog.l('Response code is: ' + str(r.status_code)) fplog.l('Response text is: ' + str(r.text)) abortonerror() fplog.l("PHASE4: Tag is available (either existed or was newly created)") fertilizer = fertilizercheck() datetime = "--" payload = {'tagsn': tagsn, 'pisn': PISERIAL, 'liter': WATERCOUNT, 'fertilizer' : fertilizer, 'datetime' : datetime, 'flower_id' : flower_id} r = requests.post(USEURL + '/waters.json', json=payload) if r.status_code == 201: fplog.l("... success in creating new water record") water_hash = json.loads(r.text) fplog.l('The ID of the new water is: ' + str(water_hash["id"])) else: fplog.l("Sorry, was not able to create a new water record") fplog.l('Response code is: ' + str(r.status_code)) fplog.l('Response text is: ' + str(r.text)) abortonerror() blinkgreenled()
def phase3readwater(): # Inititalize condition to evaluate wheter water did flow within 10 seconds or not nostart = True while nostart: # RF-ID lesen global RFIDREAD; RFIDREAD = False while not(RFIDREAD): try: phase2readrfid() except Exception: fplog.l("Error: Exception during execution of phase2readrfid()") blinkredled() # Blaue LED einschalten GPIO.output(GPIO_BLUE_LED, GPIO.HIGH) fplog.l("PHASE3: Waiting for water to flow, timeout 10 seconds") counts = fpflow.wfstart(10) if counts==0: fplog.l("Timout kicked in, no water is flowing") GPIO.output(GPIO_BLUE_LED, GPIO.LOW) else: nostart = False # Water is flowing, start to count the water ... fplog.l("Water is flowing ...") # Initialize counter for loop condition count = 11 totalcount = counts while count > 10: count = fpflow.wfcount(1) totalcount += count if GPIO.input(GPIO_BLUE_LED) == 1: GPIO.output(GPIO_BLUE_LED, GPIO.LOW) else: GPIO.output(GPIO_BLUE_LED, GPIO.HIGH) fplog.l("Final pulses from waterflow sensor:" + str(totalcount)) # overriding GLOBAL variable with local content; Division of factor 1.76 mL/counts, turns counts into mL global WATERCOUNT; WATERCOUNT = int(totalcount * 1.76) fplog.l("PHASE3: done - Amount of water available = " + str(WATERCOUNT) + " mL") GPIO.output(GPIO_BLUE_LED, GPIO.LOW)
def phase2readrfid(): global RFIDREAD; RFIDREAD = False fplog.l("PHASE2: wait for RF-ID tag") fertilizercheck() # wait for a card to be detected as present GPIO.output(GPIO_YELLOW_LED, GPIO.HIGH) fplog.l("Waiting for a card...") rfid.waitTag() fplog.l("Card is present") # This program expects Mifare cards if not rfid.readMifare(): fplog.l("Error: The scanned RF-id tag is not a mifare card") blinkredled() rfid.waitNoTag() rfid.waitTag() # get unique ID if the card rfidid = rfid.getUniqueId() # overriding GLOBAL variable with local content global RFIDUID; RFIDUID = rfidid fplog.l("Card UID:" + RFIDUID) blinkyellowled() # wait for the card to be removed fplog.l("Waiting for card to be removed...") rfid.waitNoTag() fplog.l("Card removed") GPIO.output(GPIO_YELLOW_LED, GPIO.LOW) RFIDREAD = True fplog.l("PHASE2: done - RF-ID tag available") if RFIDUID == "A1CF06B0": fplog.l("Read Shutdown-Tag; shutdown will be initiated; goodby!") os.system("sudo shutdown -h now")
water_hash = json.loads(r.text) fplog.l('The ID of the new water is: ' + str(water_hash["id"])) else: fplog.l("Sorry, was not able to create a new water record") fplog.l('Response code is: ' + str(r.status_code)) fplog.l('Response text is: ' + str(r.text)) abortonerror() blinkgreenled() # ****************************************** # START if MAIN PROGRAM # ****************************************** fplog.l("+++ In: flowerpi-main.py +++") # ------------------------------------------ # Various intializations # ------------------------------------------ # URL of web application USEURL = 'https://gentle-taiga-6367.herokuapp.com' # BCM (GPIO Nummern) verwenden um mit fprfid compatibel zu sein! GPIO.setmode(GPIO.BCM) # Set to the GPIO required to monitor the waterflow sensor (BCM notation!) GPIO_WATERFLOW_SENSOR = 22 # GPIO_WATERFLOW_SENSOR = input => Read Pulses from Waterflow-Sensor
fplog.l('Got response from URL: ' + str(r),logtype) tagstatus = "not_existing" if r.status_code == 200: tagstatus = "existing" fplog.l("<= out fppost, return value: tagstatus = " + str(tagstatus),logtype) return tagstatus # ...run automated tests if library is started as a script if __name__ == "__main__": fplog.l("+++ In: fppost.py Test +++ ","test") poquerysite("test") tagsn = input('Please enter tagsn: ') querytag = poquerytag(tagsn, "test") if querytag == "existing": fplog.l('Tag matches to following flower: ',"test") r = requests.get(USEURL + '/flowerquery/' + str(tagsn)) fplog.l(r.text,"test") flower_hash = json.loads(r.text) fplog.l('The flower is a: ' + str(flower_hash["flowertype"]),"test") else: