def getTime(): #frudenlund stop 3010307 #helsfyr 3011442 #jbt 3010370 r = requests.get('http://reisapi.ruter.no/StopVisit/GetDepartures/3010012') result = json.loads(r.text) for obj in result: ruteTid = parse(obj['MonitoredVehicleJourney']['MonitoredCall'] ['ExpectedDepartureTime']).replace(tzinfo=None) tid = datetime.datetime.now().replace(tzinfo=None) diff = ruteTid - tid print(diff) lcdlib.lcd_string(obj['MonitoredVehicleJourney']['PublishedLineName'], LCD_LINE_1, 1) lcdlib.lcd_string(obj['MonitoredVehicleJourney']['DestinationName'], LCD_LINE_2, 1) lcdlib.lcd_string( str(round(diff.total_seconds() / 60, 1)) + " min", LCD_LINE_3, 1) lcdlib.lcd_string("--------------------", LCD_LINE_4, 2) time.sleep(5) # 20 second delay # Blank display lcdlib.lcd_byte(0x01, LCD_CMD)
def readADC(channel): if channel == 1: humidity, adc_value = Adafruit_DHT.read_retry(11, 4) elif channel == 2: adc_value, temperature = Adafruit_DHT.read_retry(11, 4) elif channel == 3: spi_mods = subprocess.Popen(['sudo lsmod |grep spi_b*'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True) output = spi_mods.communicate()[0] spi_mod_name = output.split()[0] #print("SPI MOD = ",spi_mod_name) result = subprocess.check_output(['sudo', 'rmmod', spi_mod_name]) result = subprocess.check_output(['sudo', 'modprobe', spi_mod_name]) SPI_PORT = 0 SPI_DEVICE = 0 mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) moisture_value = mcp.read_adc(0) adc_value = moisture_value elif channel == 4: lcdlib.lcd_init() GPIO.output(TRIG, False) #Set TRIG as LOW print("Waitng For Sensor To Settle") time.sleep(2) #Delay of 2 seconds GPIO.output(TRIG, True) #Set TRIG as HIGH time.sleep(0.00001) #Delay of 0.00001 seconds GPIO.output(TRIG, False) #Set TRIG as LOW while GPIO.input(ECHO) == 0: #Check whether the ECHO is LOW pulse_start = time.time() #Saves the last known time of LOW pulse while GPIO.input(ECHO) == 1: #Check whether the ECHO is HIGH pulse_end = time.time() #Saves the last known time of HIGH pulse pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance distance = round(distance) lcdlib.lcd_string("Distance : " + str(distance), 0x80) adc_value = distance else: state = False print "Specified input channel is out of range." print(adc_value) return float(adc_value)
#print(json.dumps(json.loads(r.text), indent=4, sort_keys=True)) def main(): # Main program block GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers GPIO.setup(LCD_E, GPIO.OUT) # E GPIO.setup(LCD_RS, GPIO.OUT) # RS GPIO.setup(LCD_D4, GPIO.OUT) # DB4 GPIO.setup(LCD_D5, GPIO.OUT) # DB5 GPIO.setup(LCD_D6, GPIO.OUT) # DB6 GPIO.setup(LCD_D7, GPIO.OUT) # DB7 GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable # Initialise display lcdlib.lcd_init() getTime() if __name__ == '__main__': try: main() except KeyboardInterrupt: pass finally: lcdlib.lcd_byte(0x01, LCD_CMD) lcdlib.lcd_string("Goodbye!", LCD_LINE_1, 2) GPIO.cleanup()
pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable distance = pulse_duration * 17150 #Multiply pulse duration by 17150 to get distance distance = round(distance, 2) #Round to two decimal points max = 162 if distance > 2 and distance < 400: #Check whether the distance is within range print("Distance:", distance - 0.5, "cm") garbage_level = 100 - (100 * float(distance - 0.5) / max) print("{0:.0f}% full".format(garbage_level)) if garbage_level > 65: print "Sending text to Pick up garbage bin 1" else: print("Out Of Range") # lcdlib.lcd_string("GEOFF",0x80) lcdlib.lcd_string("Garb Lvl:" + str(int(garbage_level)) + "%", 0x80) data = json.dumps([{ "sensor": "sc-waste-manage", "value": int(garbage_level), "timestamp": "", "context": "Ultrasonic sensor!" }]) client.publish(topic, data) print("") print("Published data to cloud") print("") print("") time.sleep(5)