Beispiel #1
0
def Main():
    try:
        #====================================================
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(2, GPIO.OUT)
        GPIO.setup(3, GPIO.OUT)
        GPIO.setup(4, GPIO.OUT)
        GPIO.setup(5, GPIO.OUT)
        GPIO.setup(21, GPIO.IN)
        while (True):
            a = GPIO.input(21)
            if a == GPIO.HIGH:
                GPIO.output(2, GPIO.HIGH)
                time.sleep(0.5)
                GPIO.output(2, GPIO.LOW)
                GPIO.output(3, GPIO.HIGH)
                time.sleep(0.5)
                GPIO.output(3, GPIO.LOW)
                GPIO.output(4, GPIO.HIGH)
                time.sleep(0.5)
                GPIO.output(4, GPIO.LOW)
                GPIO.output(5, GPIO.HIGH)
                time.sleep(0.5)
                GPIO.output(5, GPIO.LOW)


#=====================================================
    except Exception as ex:
        traceback.print_exc()
Beispiel #2
0
def main():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17, GPIO.OUT)
    GPIO.setup(18, GPIO.OUT)
    GPIO.setup(22, GPIO.OUT)
    GPIO.setup(23, GPIO.OUT)
    print('Red LED on')
    GPIO.output(17, True)
    time.sleep(3)
    GPIO.output(17, False)
    time.sleep(1)
    print('Yellow LED on')
    GPIO.output(18, True)
    time.sleep(3)
    GPIO.output(18, False)
    time.sleep(1)
    print('Green LED on')
    GPIO.output(22, True)
    time.sleep(3)
    GPIO.output(22, False)
    time.sleep(1)
    print('Blue LED on')
    GPIO.output(23, True)
    time.sleep(3)
    GPIO.output(23, False)
Beispiel #3
0
 def setGPIO(system):
     GPIO.setmode(GPIO.BCM) # Set GPIO mode for pi
     
     # Sets the GPIO Pins of the PI's sensors to send input to PI 
     for i in range(0,len(sensors)):
         GPIO.setup(self._sensors.getSensor(i).pin,GPIO.IN)
         
     # Sets the GPIO Pins of the PI's LEDs to receive output from the PI 
     for i in range(0,len(LEDS)):
         GPIO.setup(self._leds.getLed(i).pin,GPIO.OUT)
Beispiel #4
0
def Main():
    try:
        GPIO.setmode(GPIO.BCM)

        GPIO.setwarnings(False)

        GPIO.setup("P8_7", GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup("P8_8", GPIO.IN, initial=GPIO.LOW)

        # GPIO.setup(4, GPIO.OUT)
        # GPIO.setup(17, GPIO.OUT, initial = GPIO.LOW)
        # GPIO.setup(18, GPIO.OUT, initial = GPIO.LOW)
        # GPIO.setup(21, GPIO.OUT, initial = GPIO.LOW)
        # GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
        # GPIO.setup(15, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
        # GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
        # GPIO.setup(26, GPIO.IN)

        while (True):
            pass
            # if (GPIO.input(23) == False):
            #     GPIO.output(4,GPIO.HIGH)
            #     GPIO.output(17,GPIO.HIGH)
            #     time.sleep(1)

            # if (GPIO.input(15) == True):
            #     GPIO.output(18,GPIO.HIGH)
            #     GPIO.output(21,GPIO.HIGH)
            #     time.sleep(1)

            # if (GPIO.input(24) == True):
            #     GPIO.output(18,GPIO.LOW)
            #     GPIO.output(21,GPIO.LOW)
            #     time.sleep(1)

            # if (GPIO.input(26) == True):
            #     GPIO.output(4,GPIO.LOW)
            #     GPIO.output(17,GPIO.LOW)
            #     time.sleep(1)

    except Exception as ex:
        traceback.print_exc()
    finally:
        GPIO.cleanup()  #this ensures a clean exit
Beispiel #5
0
def setupGPIO():
	try:
		#GPIO.setmode(GPIO.BOARD) 		# sets input to unified board pin numbers
		GPIO.setmode(GPIO.BCM)			# sets input to chip numbers, can differ per poard type

		GPIO.setwarnings(False)

		GPIO.setup(4, GPIO.OUT)
		GPIO.setup(17, GPIO.OUT, initial = GPIO.LOW)
		GPIO.setup(18, GPIO.OUT, initial = GPIO.LOW)
		GPIO.setup(21, GPIO.OUT, initial = GPIO.LOW)
		GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
		GPIO.setup(15, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
		GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
		GPIO.setup(26, GPIO.IN)

		while(True):
			if (GPIO.input(23) == False):
				GPIO.output(4,GPIO.HIGH)
				GPIO.output(17,GPIO.HIGH)
				time.sleep(1)

			if (GPIO.input(15) == True):
				GPIO.output(18,GPIO.HIGH)
				GPIO.output(21,GPIO.HIGH)
				time.sleep(1)

			if (GPIO.input(24) == True):
				GPIO.output(18,GPIO.LOW)
				GPIO.output(21,GPIO.LOW)
				time.sleep(1)

			if (GPIO.input(26) == True):
				GPIO.output(4,GPIO.LOW)
				GPIO.output(17,GPIO.LOW)
				time.sleep(1)

	except Exception as ex:
		traceback.print_exc()
	finally:
		GPIO.cleanup() #this ensures a clean exit
Beispiel #6
0
    def init_gpio(relay_list, quiet=True):
        Logger.write.info("Initializing relays, please wait")

        GPIO.setmode(GPIO.BCM)
        GPIO.setup(SENSOR_GPIO_PIN, GPIO.IN, pull_up_down=SENSOR_PUD)

        for pin in relay_list:
            GPIO.setup(pin, GPIO.OUT)
            GPIO.output(pin, RELAY_OFF)

        if not quiet:
            for pin in relay_list:  # cycle individual
                GPIO.output(pin, RELAY_ON)
                time.sleep(0.25)
                GPIO.output(pin, RELAY_OFF)
                time.sleep(0)

            for pin in relay_list:  # all on
                GPIO.output(pin, RELAY_ON)
            time.sleep(1.0)

            for pin in relay_list:  # all off
                GPIO.output(pin, RELAY_OFF)
Beispiel #7
0
import os
import pickle
import time
import threading
# import serverEnd
# import serverStart
# import clientEnd
# import clientStart
import datetime
import time
import calendar

# import RPi.GPIO as GPIO
from EmulatorGUI import GPIO

GPIO.setmode(GPIO.BCM)
# Firebase =firebase.FirebaseApplication('https://prog-c99a8.firebaseio.com/')
Firebase = firebase.FirebaseApplication('https://slight-91c01.firebaseio.com/')


def pushData(dataModel):
    ts = time.time()
    hour = datetime.datetime.fromtimestamp(ts).strftime("%H")
    minute = datetime.datetime.fromtimestamp(ts).strftime("%M")
    day = datetime.datetime.fromtimestamp(ts).strftime("%d")
    year = datetime.datetime.fromtimestamp(ts).strftime("%Y")
    month = datetime.datetime.fromtimestamp(ts).strftime("%m")
    parent = "Pi_" + str(
        dataModel["id"]) + "/" + year + "/" + calendar.month_name[int(
            month)] + "/" + day + "/"
    Firebase.put(parent + "/" + hour + "/" + str(minute), "No Of Cars",
Beispiel #8
0
def Main():

    try:
        GPIO.setmode(GPIO.BCM)

        GPIO.setwarnings(False)

        GPIO.setup(14, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  # Kasa Açıldı
        GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  # Ev Kapısı Açıldı
        GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  # Araba Açık

        GPIO.setup(25, GPIO.IN,
                   pull_up_down=GPIO.PUD_DOWN)  # Araba Kapısı Kapandı
        GPIO.setup(16, GPIO.IN,
                   pull_up_down=GPIO.PUD_DOWN)  # Ev Kapısı Kapandı
        GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  # Kasa Kapandı

        GPIO.setup(4, GPIO.OUT,
                   initial=GPIO.HIGH)  # Kasa Açık 1, Kasa Kapalı 0
        GPIO.setup(9, GPIO.OUT, initial=GPIO.LOW)  # Ev Açık 1, Ev Kapalı 0
        GPIO.setup(17, GPIO.OUT,
                   initial=GPIO.HIGH)  # Araba Açık 1, Araba Kapalı 0

        GPIO.setup(19, GPIO.OUT, initial=GPIO.LOW)  # Nem kontrol

        GPIO.output(4, GPIO.LOW)
        GPIO.output(9, GPIO.LOW)
        GPIO.output(17, GPIO.LOW)
        Kasakapaliguncelle()
        Evkapaliguncelle()
        Arabakapaliguncelle()

        while (True):

            if (GPIO.input(14) == True):
                GPIO.output(4, GPIO.HIGH)
                Kasaacikguncelle()

            if (GPIO.input(8) == True):
                GPIO.output(4, GPIO.LOW)
                Kasakapaliguncelle()

            if (GPIO.input(15) == True):
                GPIO.output(9, GPIO.HIGH)
                Evacikguncelle()

            if (GPIO.input(16) == True):
                GPIO.output(9, GPIO.LOW)
                Evkapaliguncelle()

            if (GPIO.input(18) == True):
                GPIO.output(17, GPIO.HIGH)
                Arabaacikguncelle()

            if (GPIO.input(25) == True):
                GPIO.output(17, GPIO.LOW)
                Arabakapaliguncelle()

        con.close()
    except Exception as ex:
        traceback.print_exc()
    finally:
        GPIO.cleanup()
Beispiel #9
0
def main():
    try:
        # variables
        #This is where LEDS and SENSORs as it relates to the pi is hardcoded
        leds = Leds([
            15, 18, 23, 24, 8, 7, 12, 16, 21, 26, 19, 13, 5, 11, 9, 10, 27, 17,
            4, 3
        ])
        sensors = Sensors([14, 25, 20, 6, 22])

        firebaseSys = FirebaseSys(
            1
        )  #firebase id=1, this id is how the data will be identified on firebase
        passToLeadingPi = LeadingData()
        receiveFromTrailingPi = LeadingData()
        passToTrailingPi = TrailingData()
        receiveFromLeadingPi = TrailingData()
        vehicles = Vehicles()
        LEDsMult = int(
            leds.numLeds() /
            sensors.numSensors())  # How many LEDs that is between two sensors
        LEDsModel = genLEDsFirebaseModel(leds)
        dataModelInit = {
            'id': id,
            'avgSpeed': 0,
            'numOfNewCars': 0,
            'LEDsTurnedOn': {}
        }
        dataModel = dataModelInit

        GPIO.setmode(GPIO.BCM)

        sensors.setAllsensorsDistance(
            5)  # value used in this demo does not matter since it is constant
        sensors.setGPIOIn()
        sensors.initSensorState()
        leds.setAllLEDsDistance(sensors, LEDsMult)
        leds.setGPIOOut()
        leds.initLEDsState()

        lastPiSensorTime = -1

        while True:
            leds.initLEDsStateNew()

            # Pi to Pi communication
            receiveFromLeadingPi = ReceiveFromLeadingPiStore(
                receiveFromLeadingPi, leds)
            receiveFromTrailingPi = ReceiveFromTrailingPiStore(
                receiveFromTrailingPi, leds, vehicles, lastPiSensorTime,
                sensors, passToLeadingPi)

            # Keeps scanning for motion
            for i in range(0, sensors.numSensors()):
                state = sensors.getSensor(i).scan()

                if state and sensors.getSensor(
                        i).state == 0:  # Vehicle has now passed the sensor
                    sensors.getSensor(i).time = time.time(
                    )  #the time which the sensor was triggered is stored

                    # passToLeadingPi-LastSensorTime
                    #if state==sensorsNum-1:
                    #    passToLeadingPi.lastSensorTime(sensors.getSensor(i).time)

                    sensors.getSensor(i).state = 1

                    #*(to delete fnction isNewVehicle) Create new vehicle if 1st sensor is passed
                    if i == 0 and sensors.getSensor(
                            0).expectedSensorPass == -1:
                        # AddVehicle
                        vehicles.addNewVehicle(0,
                                               sensors.getSensor(i).time, -1,
                                               -1)
                        leds.addVehicleStartingLeds(LEDsMult)
                        sensors.updateExpectedSensorPass(
                            passToLeadingPi, i + 1,
                            vehicles.numVehicles() - 1
                        )  # Update expectedSensorPass of the next index to this vehicle that was just appended. This is to ensure that the correct vehicle speed is updated

                        #increment car counter to pass to firebase
                        dataModel[
                            "numOfNewCars"] = dataModel["numOfNewCars"] + 1
                    else:
                        if sensors.getSensor(i).expectedSensorPass >= 0:
                            if vehicles.getVehicle(
                                    sensors.getSensor(i).expectedSensorPass
                            ).speed == -1:  # If the last vehicle that entered the track speed=-1, then the starting lights must be removed
                                leds.removeVehicleStartingLeds(LEDsMult)

                        #update expectedsensor since vehicle is progressing through the track
                        if sensors.getSensor(i).expectedSensorPass != -2:
                            #*(for now treat it as though there is no previous pi, this line will be removed)
                            vehicles.getVehicle(
                                sensors.getSensor(
                                    i).expectedSensorPass).speed = calcSpeed(
                                        sensors.getSensor(i).distance,
                                        sensors.getSensor(i).time -
                                        sensors.getSensor(i - 1).time)

                            veh = sensors.getSensor(i).expectedSensorPass
                            sensors.updateExpectedSensorPass(
                                passToLeadingPi, i + 1, veh)
                            sensors.updateExpectedSensorPass(
                                passToLeadingPi, i, -1)
                        else:  # if it was -2 then the sensor was passed before the vehicle entered the track therefore a wrong
                            sensors.updateExpectedSensorPass(
                                passToLeadingPi, i + 1,
                                len(vehicles) - 1)
                            sensors.updateExpectedSensorPass(
                                sensorsNum, passToLeadingPi, i, -1)

                        #Update vision range because speed changed
                        vehicles.getVehicle(
                            sensors.getSensor(i).expectedSensorPass
                        ).visionRange = calcVisionRange(
                            vehicles.getVehicle(
                                sensors.getSensor(i).expectedSensorPass).speed)

                        #add onto maxSpeed with current speed value to calulate avg
                        vehicles.getVehicle(
                            sensors.getSensor(i).expectedSensorPass
                        ).maxSpeed = vehicles.getVehicle(
                            sensors.getSensor(i).expectedSensorPass
                        ).maxSpeed + vehicles.getVehicle(
                            sensors.getSensor(i).expectedSensorPass).speed
                        vehicles.getVehicle(
                            sensors.getSensor(i).expectedSensorPass
                        ).sensorsPassed = vehicles.getVehicle(
                            sensors.getSensor(
                                i).expectedSensorPass).sensorsPassed + 1

                elif state and sensors.getSensor(
                        i).state == 1:  # Vehicle is still passing the sensor
                    sensors.getSensor(i).state = 1
                elif state == 0 and sensors.getSensor(
                        i
                ).state == 1:  # Vehicle has finished passing the sensor
                    sensors.getSensor(i).state = 0

            #pass and receive data

            #creates cycle if multiple PIs does not exist
            #passToLeadingPi=PassToLeadingPiSend(passToLeadingPi,receiveFromTrailingPi)
            #passToTrailingPi=PassToTrailingPiSend(receiveFromLeadingPi,passToTrailingPi)

            # PI to PI communication
            #insertHere

            #reset pi to transfer data
            passToLeadingPi = LeadingData()
            passToTrailingPi = TrailingData()

            stepThrough(vehicles, sensors, leds, passToLeadingPi,
                        passToTrailingPi)
            leds.updateStateFromNew()

            timeAtLEDUpdate = time.time()
            LEDsModel = updateLEDsModel(
                LEDsModel, timeAtLEDUpdate - firebaseSys.timeBeforeLEDUpdate,
                leds)
            firebaseSys.timeBeforeLEDUpdate = timeAtLEDUpdate

            leds.turnOnLeds()

            # Push data to firebase if the minute changes
            now = datetime.datetime.now()
            if firebaseSys.sec == 60:
                firebaseSys.sec = 59

            if now.minute == firebaseSys.min + 1 and (
                    now.second == firebaseSys.sec
                    or now.second == firebaseSys.sec + 1):
                # resetdata
                firebaseSys.sec = now.second
                firebaseSys.min = now.minute

                #calculate avgspeed
                avgSpeed = 0
                maxSpeed = 0
                for i in range(0, vehicles.numVehicles()):
                    print(vehicles.getVehicle(i).maxSpeed)
                    print(vehicles.getVehicle(i).sensorsPassed)
                    vehicles.getVehicle(i).avgSpeed = vehicles.getVehicle(
                        i).maxSpeed / vehicles.getVehicle(
                            i).sensorsPassed  #* # avg speed of that vehicle
                    maxSpeed = maxSpeed + vehicles.getVehicle(
                        i).avgSpeed  #add average speed of all vehicles
                avgSpeed = maxSpeed / dataModel[
                    "numOfNewCars"]  #calculate avg speed of all new vehicles
                #reset vehicles data for next minute
                for i in range(0, vehicles.numVehicles()):
                    vehicles.getVehicle(i).avgSpeed = 0
                    vehicles.getVehicle(i).maxSpeed = 0
                    vehicles.getVehicle(i).sensorsPassed = 0

                #append avgspeed
                dataModel["avgSpeed"] = avgSpeed
                dataModel["LEDsTurnedOn"] = LEDsModel

                firebase.Sys.pushData(dataModel)

                #reset data
                dataModel = dataModelInit
                LEDsModel = genLEDsFirebaseModel(LEDsNum)
                dataModel["LEDsTurnedOn"] = LEDsModel

    except KeyboardInterrupt:
        run = False
    finally:
        GPIO.cleanup()