Ejemplo n.º 1
0
def ler_sensor_ultrassonico():


    MQTT_PORT = 1883
    TOPICO = '/softway/iot'
    MQTT_TIMEOUT = 60

    GPIO.setmode(GPIO.BCM)  # Set GPIO pin numbering

    TRIG = 23  # Associate pin 23 to TRIG
    ECHO = 24  # Associate pin 24 to ECHO

    ID_LINE = 1
    MQTT_ADDRESS = '10.17.0.2'

    GPIO.setup(TRIG, GPIO.OUT)  # Set pin as GPIO out
    GPIO.setup(ECHO, GPIO.IN)  # Set pin as GPIO in

    def send_message(msg, MQTT_ADDRESS):  # Send MQTT Message
        client = mqtt.Client()
        client.connect(MQTT_ADDRESS, MQTT_PORT, MQTT_TIMEOUT)
        result, mid = client.publish(TOPICO, msg)
        print('Mensagem enviada ao canal: %d, [MQTT_ADDRESS: %s]' % (mid, MQTT_ADDRESS))

    for numero in range(4):

        timeout = 0.8 * 60 * 60  # Horas em segundos (48 min)
        timeout_start = time.time()

        while time.time() < timeout_start + timeout:
            GPIO.output(TRIG, False)  # Set TRIG as LOW
            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, 2)  # Round to two decimal points

            if distance > 5:
                mensagem = "{\"id\":" + str(ID_LINE) + "}"
                print("++ MSG-SEND:", mensagem, "\n")

                send_message(mensagem, MQTT_ADDRESS)  # Send message via MQTT protocol
        time.sleep(0.3)
        print("Resetando sensores...")
        time.sleep(0.3)
        print("Resetando sensores......")
        time.sleep(0.4)
        print("Resetando sensores.........")
Ejemplo n.º 2
0
 def __init__(self):
     ''' initialization of pins '''
     GPIO.setmode(GPIO.BOARD)
     GPIO.setup(self.servoPin, GPIO.OUT)
     GPIO.setup(self.ENB, GPIO.OUT)
     GPIO.setup(self.IN3, GPIO.OUT)
     GPIO.setup(self.IN4, GPIO.OUT)
     # 50 and 40 are frequency
     self.servo = GPIO(self.servoPin, 50)
     self.motor = GPIO(self.ENB, 40)
Ejemplo n.º 3
0
    def __init__(self, baudrate=9600, port='/dev/ttyS1', timeout=1, tr=6):
        self.tr = tr
        # initialize pyserial
        self.serial = pyserial.Serial(port=port,
                                      timeout=timeout,
                                      baudrate=baudrate)
        # if serial is not open, open it.
        if not self.serial.isOpen():
            self.serial.Open()

        # initialize GPIO
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.tr, 'out')
Ejemplo n.º 4
0
def main():
    array = ["?", "?", "?", "?"]
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(status_led, GPIO.OUT)
    GPIO.output(status_led, GPIO.HIGH)
    while True:
        humidity, temperature = DHT.read_retry(DHT.DHT22, 10)
        array[0] = str(temperature)
        array[1] = str(humidity)
        array[2], array[3] = ds1820()
        print(array)
        senddata(array)
        for i in range(12):
            time.sleep(5)
            GPIO.output(status_led, GPIO.LOW)
            time.sleep(0.5)
            GPIO.output(status_led, GPIO.HIGH)
Ejemplo n.º 5
0
class MotionSensor(threading.Thread):
    POLLING_TIME = 0.01
    #Una vegada sa configuracio
    gpio.setmode(gpio.BCM)

    def __init__(self, pin):
        threading.Thread.__init__(self)
        self.__pin = pin
        self.__lock = threading.Lock()
        self.setup()
        self.__seguirThread = False
        self.__motionListener = None

    def setup(self):
        gpio.setup(self.__pin, gpio.IN)

    def cleanUp(self):
        gpio.cleanup(self.__pin)

    def motion(self):
        return gpio.input(self.__pin)

    def __str__(self):
        return "Motion sensor pin " + str(self.__pin)

    def setMotionListener(self, listener):
        if isinstance(listener, MotionListener):
            self.__motionListener = listener

    def threadIsRunning(self):
        with self.__lock:
            return self.__seguirThread

    def stopThread(self):
        with self.__lock:
            self.__seguirThread = False

    def run(self):
        with self.__lock:
            self.__seguirThread = True
            seguir = self.__seguirThread
        flag = False
        while seguir:
            #Instead of synchronize the whole block
            with self.__lock:
                seguir = self.__seguirThread

            if self.motion():
                if not flag and self.__motionListener is not None:
                    self.__motionListener.motion()
                    flag = True
            else:
                flag = False

            time.sleep(self.POLLING_TIME)

        #print("MOTION SENSOR ACABAT")
        self.cleanUp()
Ejemplo n.º 6
0
    def get_image(self, angle_idx):
        if self.is_test:
            self.angles_imgs_counter[angle_idx] += 1
            img = self.angles_imgs_lst[angle_idx][
                self.angles_imgs_counter[angle_idx]]
            #        img = cv2.resize(img,(RESIZE_SIZE,RESIZE_SIZE))
            if (IS_PI):
                self.socket.send_image(img)  # asynchronus send, w/ timeout
            gui_img_manager.add_img(img)
            return img
        else:
            if (IS_PI):
                gpio.setmode(gpio.BOARD)
                gpio.setup(btn, gpio.IN, pull_up_down=gpio.PUD_DOWN)
                should_enter = True
                try:
                    print("plz press me honz")
                    while True:
                        if (gpio.input(btn) == 1):
                            should_enter = True

                        elif (should_enter):
                            img = self.one_still()

                            #img = cv2.resize(img,(RESIZE_SIZE,RESIZE_SIZE))
                            self.socket.send_image(img)
                            should_enter = False
                            gpio.cleanup()
                            return img

                except KeyboardInterrupt:
                    print('cam err!')

            else:  # gui
                img = self.socket.get_image()
                gui_img_manager.add_img(img)
                return img
Ejemplo n.º 7
0
    for i in reversed(LEDS):
        IO.output(i, 1)
        time.sleep(CLK)
    for i in reversed(LEDS):
        IO.output(i, 0)
        time.sleep(CLK)


def RunEnd():
    for i in LEDS:
        IO.output(i, 0)
        time.sleep(0.001)
        IO.cleanup(i)


IO.setmode(IO.BCM)

RED = 26
YELLOW = 21
GREEN = 20
R1 = 13
R2 = 6
R3 = 12
Y1 = 7
Y2 = 8
Y3 = 11
Trigger = 14

LEDS = [26, 21, 20, 13, 6, 12, 7, 8, 11]

for i in LEDS:
Ejemplo n.º 8
0
 def init_vib(self):
     gpio.setmode(gpio.BCM)
     gpio.setup(ur, gpio.OUT)
     gpio.setup(ul, gpio.OUT)
     gpio.setup(dr, gpio.OUT)
     gpio.setup(dl, gpio.OUT)
Ejemplo n.º 9
0
# this script turns on the LED when button is pressed and turns it off when button is released

import time as t

# in VS
import gpio as GPIO
# on Rasp
# import RPi.GPIO as GPIO

print("Script started")

_pin_output_led_red = 14
_pin_input_button = 15

GPIO.setmode(GPIO.BCM)

GPIO.setup(_pin_output_led_red, GPIO.OUT)
GPIO.setup(_pin_input_button, GPIO.IN, GPIO.PUD_UP)

while True:
    if GPIO.input(_pin_input_button) == False:
        print("Button pressed")
        GPIO.output(_pin_output_led_red, GPIO.HIGH)
    else:
        print("Button released")
        GPIO.output(_pin_output_led_red, GPIO.LOW)
    t.sleep(0.01)
Ejemplo n.º 10
0
import time

import gpio


def flashLed(bin, t):
    gpio.output(bin, gpio.HIGH)
    time.sleep(t)
    gpio.output(bin, gpio.LOW)
    time.sleep(t)


gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)

for i in range(1, 4):
    flashLed(18, 1)

for i in range(1, 10):
    flashLed(18, 0.5)

for i in range(1, 20):
    flashLed(18, 0.05)
Ejemplo n.º 11
0
# this script turns on the LED when button is pressed and turns it off when button is released

import time as t

# in VS
import gpio as GPIO
# on Rasp
# import RPi.GPIO as GPIO


print("Script started")

_pin_output_led_red = 14
_pin_input_button = 15

GPIO.setmode(GPIO.BCM)

GPIO.setup(_pin_output_led_red, GPIO.OUT)
GPIO.setup(_pin_input_button, GPIO.IN, GPIO.PUD_UP)

while True:
    if GPIO.input(_pin_input_button) == False:
        print("Button pressed")
        GPIO.output(_pin_output_led_red, GPIO.HIGH)
    else:
        print("Button released")
        GPIO.output(_pin_output_led_red, GPIO.LOW)
    t.sleep(0.01)
Ejemplo n.º 12
0
def threadVideoGet(source=0):
    """
    Dedicated thread for grabbing video frames with VideoGet object.
    Main thread shows video frames.
    """

    legcut = 100

    video_getter = VideoGet(source).start()
    cps = CountsPerSec().start()
    st = time.time()
    period_goal = 0.01
    period = period_goal
    counter = 0

    #we need to start the robot moving.

    GPIO.setmode(GPIO.BOARD)

    GPIO.setup(32, GPIO.OUT)
    GPIO.setup(33, GPIO.OUT)
    GPIO.setup(35, GPIO.OUT)
    GPIO.setup(36, GPIO.OUT)
    GPIO.setup(37, GPIO.OUT)
    GPIO.setup(38, GPIO.OUT)

    pwm0 = GPIO.PWM(32, 20000)
    pwm1 = GPIO.PWM(33, 20000)

    pwm0.start(50)
    pwm1.start(50)

    GPIO.output(36, GPIO.HIGH)
    GPIO.output(35, GPIO.HIGH)
    GPIO.output(38, GPIO.LOW)
    GPIO.output(37, GPIO.LOW)

    #robot is now moving in a stright line.
    while True:  # Again the commented part is for displaying the video and calibration
        #this is the main while loop which will run until the video is finished.

        #the counter will go from 0 to 3 to save and average 4 seperate wing beat angles to an array later on
        counter = counter + 1
        if counter == 4:
            #will need to reset the counter every 4 itterations.
            counter = 0
        starttime = time.time()
        #now go to the next function:
        putIterationsPerSec(video_getter.thr, cps.countsPerSec(), counter,
                            pwm0, pwm1)
        cps.increment()
        if cps._num_occurrences > 2500 or video_getter.stopped:
            print(time.time() - st)
            video_getter.stop()
            break

        ddt = time.time() - starttime

        if period - ddt < 0:
            period = ddt
        else:
            time.sleep(period - ddt)
            period = period - (time.time() - starttime - period_goal)