Пример #1
0
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.setupUi(self)
        self.setWindowTitle(
            'Tinkerforge CES Demo: Thermal Imaging Bricklet by FLIR')

        self.qtcb_ipcon_enumerate.connect(self.cb_ipcon_enumerate)
        self.qtcb_ipcon_connected.connect(self.cb_ipcon_connected)
        self.qtcb_high_contrast_image.connect(self.cb_high_contrast_image)

        self.image = QImage(QSize(80, 60), QImage.Format_RGB32)

        # Add Tinkerforge logo
        # Adapt this path
        self.label_logo.setPixmap(
            QPixmap('/home/pi/Desktop/demo/logo_klein.png'))

        # create and setup ipcon
        self.ipcon = IPConnection()
        self.ipcon.connect(HOST, PORT)
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.qtcb_ipcon_enumerate.emit)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.qtcb_ipcon_connected.emit)

        self.ti = BrickletThermalImaging(UID_TI, self.ipcon)
        self.ti.register_callback(self.ti.CALLBACK_HIGH_CONTRAST_IMAGE,
                                  self.qtcb_high_contrast_image.emit)
        self.ti.set_image_transfer_config(
            self.ti.IMAGE_TRANSFER_CALLBACK_HIGH_CONTRAST_IMAGE)

        self.ti.set_spotmeter_config([35, 25, 45, 35])
        #print(ti.get_spotmeter_config())

        al = BrickletAmbientLightV2(UID_AL, self.ipcon)
        al.register_callback(al.CALLBACK_ILLUMINANCE, self.cb_illuminance)
        al.set_illuminance_callback_period(500)

        hu = BrickletHumidityV2(UID_HU, self.ipcon)
        hu.register_callback(hu.CALLBACK_HUMIDITY, self.cb_humidity)
        hu.set_humidity_callback_configuration(500, False, "x", 0, 0)

        bm = BrickletBarometer(UID_BM, self.ipcon)
        bm.register_callback(bm.CALLBACK_AIR_PRESSURE, self.cb_air_pressure)
        bm.set_air_pressure_callback_period(500)

        self.rgb_lookup = []
        for x in range(256):
            x /= 255.0
            r = int(round(255 * math.sqrt(x)))
            g = int(round(255 * pow(x, 3)))
            if math.sin(2 * math.pi * x) >= 0:
                b = int(round(255 * math.sin(2 * math.pi * x)))
            else:
                b = 0
            self.rgb_lookup.append((r, g, b))

        def paintEvent(event):
            painter = QPainter(self.label_image)

            painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

            w = self.label_image.size().width()
            h = self.label_image.size().height()

            painter.scale(w / 80.0, h / 60.0)
            painter.drawImage(0, 0, self.image)

            if 35 != None and 45 != None:
                pen = QPen()
                pen.setColor(Qt.white)
                pen.setWidth(0.2)
                painter.setPen(pen)

                from_x, from_y, to_x, to_y = [35, 25, 45, 35]

                from_x = from_x * self.image_pixel_width + 1
                from_y = from_y * self.image_pixel_width + 1
                to_x = to_x * self.image_pixel_width + 1
                to_y = to_y * self.image_pixel_width + 1

                cross_x = from_x + (to_x - from_x) / 2.0
                cross_y = from_y + (to_y - from_y) / 2.0

                if to_x - from_x > 5 or to_y - from_y > 5:
                    lines = [
                        QLine(from_x, from_y, from_x + self.crosshair_width,
                              from_y),
                        QLine(from_x, from_y, from_x,
                              from_y + self.crosshair_width),
                        QLine(to_x, to_y, to_x, to_y - self.crosshair_width),
                        QLine(to_x, to_y, to_x - self.crosshair_width, to_y),
                        QLine(from_x, to_y, from_x,
                              to_y - self.crosshair_width),
                        QLine(from_x, to_y, from_x + self.crosshair_width,
                              to_y),
                        QLine(to_x, from_y, to_x,
                              from_y + self.crosshair_width),
                        QLine(to_x, from_y, to_x - self.crosshair_width,
                              from_y)
                    ]
                    painter.drawLines(lines)

                lines = [
                    QLine(cross_x - self.crosshair_width, cross_y,
                          cross_x + self.crosshair_width, cross_y),
                    QLine(cross_x, cross_y - self.crosshair_width, cross_x,
                          cross_y + self.crosshair_width)
                ]
                painter.drawLines(lines)

            self.update_spotmeter_roi_label()

        self.label_image.paintEvent = paintEvent
Пример #2
0
class WeatherStation:
    HOST = "localhost"
    PORT = 4223

    ipcon = None
    lcd = None
    al = None
    al_v2 = None
    hum = None
    hum_v2 = None
    baro = None

    def __init__(self):
        self.xively = Xively()
        self.ipcon = IPConnection()
        while True:
            try:
                self.ipcon.connect(WeatherStation.HOST, WeatherStation.PORT)
                break
            except Error as e:
                log.error('Connection Error: ' + str(e.description))
                time.sleep(1)
            except socket.error as e:
                log.error('Socket error: ' + str(e))
                time.sleep(1)

        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.cb_connected)

        while True:
            try:
                self.ipcon.enumerate()
                break
            except Error as e:
                log.error('Enumerate Error: ' + str(e.description))
                time.sleep(1)

    def cb_illuminance(self, illuminance):
        if self.lcd is not None:
            text = 'Illuminanc %6.2f lx' % (illuminance / 10.0)
            self.lcd.write_line(0, 0, text)
            self.xively.put('AmbientLight', illuminance / 10.0)
            log.info('Write to line 0: ' + text)

    def cb_illuminance_v2(self, illuminance):
        if self.lcd is not None:
            text = 'Illumina %8.2f lx' % (illuminance / 100.0)
            self.lcd.write_line(0, 0, text)
            self.xively.put('AmbientLight', illuminance / 100.0)
            log.info('Write to line 0: ' + text)

    def cb_humidity(self, humidity):
        if self.lcd is not None:
            text = 'Humidity   %6.2f %%' % (humidity / 10.0)
            self.lcd.write_line(1, 0, text)
            self.xively.put('Humidity', humidity / 10.0)
            log.info('Write to line 1: ' + text)

    def cb_air_pressure(self, air_pressure):
        if self.lcd is not None:
            text = 'Air Press %7.2f mb' % (air_pressure / 1000.0)
            self.lcd.write_line(2, 0, text)
            self.xively.put('AirPressure', air_pressure / 1000.0)
            log.info('Write to line 2: ' + text)

            temperature = self.baro.get_chip_temperature() / 100.0
            # \xDF == ° on LCD 20x4 charset
            text = 'Temperature %5.2f \xDFC' % temperature
            self.lcd.write_line(3, 0, text)
            self.xively.put('Temperature', temperature)
            log.info('Write to line 3: ' + text.replace('\xDF', '°'))

    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):
        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            if device_identifier == LCD20x4.DEVICE_IDENTIFIER:
                try:
                    self.lcd = LCD20x4(uid, self.ipcon)
                    self.lcd.clear_display()
                    self.lcd.backlight_on()
                    log.info('LCD 20x4 initialized')
                except Error as e:
                    log.error('LCD 20x4 init failed: ' + str(e.description))
                    self.lcd = None
            elif device_identifier == BrickletAmbientLight.DEVICE_IDENTIFIER:
                try:
                    self.al = BrickletAmbientLight(uid, self.ipcon)
                    self.al.set_illuminance_callback_period(1000)
                    self.al.register_callback(self.al.CALLBACK_ILLUMINANCE,
                                              self.cb_illuminance)
                    log.info('Ambient Light initialized')
                except Error as e:
                    log.error('Ambient Light init failed: ' +
                              str(e.description))
                    self.al = None
            elif device_identifier == BrickletAmbientLightV2.DEVICE_IDENTIFIER:
                try:
                    self.al_v2 = BrickletAmbientLightV2(uid, self.ipcon)
                    self.al_v2.set_configuration(
                        self.al_v2.ILLUMINANCE_RANGE_64000LUX,
                        self.al_v2.INTEGRATION_TIME_200MS)
                    self.al_v2.set_illuminance_callback_period(1000)
                    self.al_v2.register_callback(
                        self.al_v2.CALLBACK_ILLUMINANCE,
                        self.cb_illuminance_v2)
                    log.info('Ambient Light 2.0 initialized')
                except Error as e:
                    log.error('Ambient Light 2.0 init failed: ' +
                              str(e.description))
                    self.al_v2 = None
            elif device_identifier == BrickletHumidity.DEVICE_IDENTIFIER:
                try:
                    self.hum = BrickletHumidity(uid, self.ipcon)
                    self.hum.set_humidity_callback_period(1000)
                    self.hum.register_callback(self.hum.CALLBACK_HUMIDITY,
                                               self.cb_humidity)
                    log.info('Humidity initialized')
                except Error as e:
                    log.error('Humidity init failed: ' + str(e.description))
                    self.hum = None
            elif device_identifier == BrickletHumidityV2.DEVICE_IDENTIFIER:
                try:
                    self.hum_v2 = BrickletHumidityV2(uid, self.ipcon)
                    self.hum_v2.set_humidity_callback_configuration(
                        1000, True, 'x', 0, 0)
                    self.hum_v2.register_callback(
                        self.hum_v2.CALLBACK_HUMIDITY, self.cb_humidity_v2)
                    log.info('Humidity 2.0 initialized')
                except Error as e:
                    log.error('Humidity 2.0 init failed: ' +
                              str(e.description))
                    self.hum_v2 = None
            elif device_identifier == BrickletBarometer.DEVICE_IDENTIFIER:
                try:
                    self.baro = BrickletBarometer(uid, self.ipcon)
                    self.baro.set_air_pressure_callback_period(1000)
                    self.baro.register_callback(
                        self.baro.CALLBACK_AIR_PRESSURE, self.cb_air_pressure)
                    log.info('Barometer initialized')
                except Error as e:
                    log.error('Barometer init failed: ' + str(e.description))
                    self.baro = None

    def cb_connected(self, connected_reason):
        if connected_reason == IPConnection.CONNECT_REASON_AUTO_RECONNECT:
            log.info('Auto Reconnect')

            while True:
                try:
                    self.ipcon.enumerate()
                    break
                except Error as e:
                    log.error('Enumerate Error: ' + str(e.description))
                    time.sleep(1)
Пример #3
0
        logger = logging.getLogger('root')  # or pass string to give it a name
        logger.addHandler(handler)
        logger.setLevel(logging.INFO)
    except KeyboardInterrupt:
        # handle Ctrl-C
        logging.warn("Cancelled by user")
    except Exception as ex:
        # handle unexpected script errors
        logging.exception("Unhandled error\n{}".format(ex))
        raise
    finally:
        # perform an orderly shutdown by flushing and closing all handlers.
        logging.shutdown()

    # Register air pressure callback to function cb_air_pressure
    b.register_callback(b.CALLBACK_AIR_PRESSURE, cb_air_pressure)

    # Note: The air pressure callback is only called every 500 ms
    # if the air pressure has changed since the last call!
    b.set_air_pressure_callback_period(1000)

    # Send file to cloud
    sched = BackgroundScheduler()
    sched.add_job(send_log_file_to_cloud,
                  'interval',
                  seconds=5,
                  max_instances=1000)
    sched.start()

    input("Press key to exit\n")  # Use input() in Python 3
    ipcon.disconnect()
class WeatherStation:
    HOST = "localhost"
    PORT = 4223

    ipcon = None
    lcd = None
    al_v2 = None
    hum_v2 = None
    baro = None
    master = None

    def __init__(self):
        self.ipcon = IPConnection()
        while True:
            try:
                self.ipcon.connect(WeatherStation.HOST, WeatherStation.PORT)
                break
            except Error as e:
                log.error('Connection Error: ' + str(e.description))
                time.sleep(1)
            except socket.error as e:
                log.error('Socket error: ' + str(e))
                time.sleep(1)

        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED,
                                     self.cb_connected)

        while True:
            try:
                self.ipcon.enumerate()
                break
            except Error as e:
                log.error('Enumerate Error: ' + str(e.description))
                time.sleep(1)
                
    def cb_lcd_button_pressed(self, button):
        if self.lcd is not None:
            text = str(self.lcd.is_button_pressed(button))
            log.debug('Button pressed ' + text)
            log.debug('It was button ' + str(button))
            if button == 0:
                if self.lcd.is_backlight_on():
                    self.lcd.backlight_off()
                else:
                    self.lcd.backlight_on()

    def cb_illuminance_v2(self, illuminance):
        if self.lcd is not None:
            text = 'Illuminanz %6.2f lx' % (illuminance/100.0)
            self.lcd.write_line(0, 0, text)
            log.debug('Write to line 0: ' + text)

    def cb_humidity_v2(self, humidity):
        if self.lcd is not None:
            text = 'Luftfeuchte %5.2f %%' % (humidity/100.0)
            self.lcd.write_line(1, 0, text)
            log.debug('Write to line 1: ' + text)

            try:
                temperature = self.hum_v2.get_temperature()
            except Error as e:
                log.error('Could not get temperature: ' + str(e.description))
                return

            # \xDF == ° on LCD 20x4 charset
            text = 'Temperatur %6.2f \xDFC' % (temperature/100.0)
            self.lcd.write_line(3, 0, text)
            log.debug('Write to line 3: ' + text.replace('\xDF', '°'))


    def cb_air_pressure(self, air_pressure):
        if self.lcd is not None:
            text = 'Luftdruck %7.2f mb' % (air_pressure/1000.0)
            self.lcd.write_line(2, 0, text)
            log.debug('Write to line 2: ' + text)

    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):
        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            if device_identifier == BrickletLCD20x4.DEVICE_IDENTIFIER:
                try:
                    self.lcd = BrickletLCD20x4(uid, self.ipcon)
                    self.lcd.clear_display()
                    self.lcd.backlight_on()
                    self.lcd.register_callback(self.lcd.CALLBACK_BUTTON_PRESSED,
                                               self.cb_lcd_button_pressed)
                    log.info('LCD 20x4 initialized')
                except Error as e:
                    log.error('LCD 20x4 init failed: ' + str(e.description))
                    self.lcd = None
            elif device_identifier == BrickletAmbientLightV2.DEVICE_IDENTIFIER:
                try:
                    self.al_v2 = BrickletAmbientLightV2(uid, self.ipcon)
                    self.al_v2.set_configuration(self.al_v2.ILLUMINANCE_RANGE_64000LUX,
                                                 self.al_v2.INTEGRATION_TIME_200MS)
                    self.al_v2.set_illuminance_callback_period(1000)
                    self.al_v2.register_callback(self.al_v2.CALLBACK_ILLUMINANCE,
                                                 self.cb_illuminance_v2)
                    log.info('Ambient Light 2.0 initialized')
                except Error as e:
                    log.error('Ambient Light 2.0 init failed: ' + str(e.description))
                    self.al_v2 = None
            elif device_identifier == BrickletHumidityV2.DEVICE_IDENTIFIER:
                try:
                    self.hum_v2 = BrickletHumidityV2(uid, self.ipcon)
                    self.hum_v2.set_humidity_callback_configuration(1000, True, 'x', 0, 0)
                    self.hum_v2.register_callback(self.hum_v2.CALLBACK_HUMIDITY,
                                                  self.cb_humidity_v2)
                    log.info('Humidity 2.0 initialized')
                except Error as e:
                    log.error('Humidity 2.0 init failed: ' + str(e.description))
                    self.hum_v2 = None
            elif device_identifier == BrickletBarometer.DEVICE_IDENTIFIER:
                try:
                    self.baro = BrickletBarometer(uid, self.ipcon)
                    self.baro.set_air_pressure_callback_period(1000)
                    self.baro.register_callback(self.baro.CALLBACK_AIR_PRESSURE,
                                                self.cb_air_pressure)
                    log.info('Barometer initialized')
                except Error as e:
                    log.error('Barometer init failed: ' + str(e.description))
                    self.baro = None
            elif device_identifier == BrickMaster.DEVICE_IDENTIFIER:
                try:
                    self.master = BrickMaster(uid, self.ipcon)
                    self.master.disable_status_led()
                    log.info('MasterBrick initialized')
                except Error as e:
                    log.error('MasterBrick init failed: ' + str(e.description))
                    self.baro = None

    def cb_connected(self, connected_reason):
        if connected_reason == IPConnection.CONNECT_REASON_AUTO_RECONNECT:
            log.info('Auto Reconnect')

            while True:
                try:
                    self.ipcon.enumerate()
                    break
                except Error as e:
                    log.error('Enumerate Error: ' + str(e.description))
                    time.sleep(1)
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Barometer Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_barometer import BrickletBarometer

# Callback function for air pressure callback
def cb_air_pressure(air_pressure):
    print("Air Pressure: " + str(air_pressure/1000.0) + " mbar")

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    b = BrickletBarometer(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Register air pressure callback to function cb_air_pressure
    b.register_callback(b.CALLBACK_AIR_PRESSURE, cb_air_pressure)

    # Set period for air pressure callback to 1s (1000ms)
    # Note: The air pressure callback is only called every second
    #       if the air pressure has changed since the last call!
    b.set_air_pressure_callback_period(1000)

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()