示例#1
0
    def get_lux(self):
        # Read R, G, B, C color data from the sensor.
        r, g, b, c = self.tcs.get_raw_data()
        # Calulate color temp
        temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
        #Calculate lux and multiply it with gain
        lux = Adafruit_TCS34725.calculate_lux(r, g, b)
        real_lux = lux
        # Calculate lux out of RGB measurements.
        print("temp [k]= ", temp)
        print("r :", r)
        print("g :", g)
        print("b :", b)
        print("c :", c)
        print("lux = ", lux)
        print("real_lux: ", real_lux)
        # Publish to topic

        # TODO: add other things to header
        self.msg_light_sensor.header.stamp = rospy.Time.now()
        self.msg_light_sensor.header.frame_id = rospy.get_namespace()[
            1:-1]  # splicing to remove /

        self.msg_light_sensor.real_lux = real_lux
        self.msg_light_sensor.lux = lux
        self.msg_light_sensor.temp = temp
        self.sensor_pub.publish(self.msg_light_sensor)
        return lux
示例#2
0
    def _update(self):
        if self._sensor_temp is not None:
            self.temperature = self._sensor_temp.readTempC()

        if self._sensor_tcs is not None:
            # Read the R, G, B, C color data.
            self.r, self.g, self.b, self.c = self._sensor_tcs.get_raw_data()
            # Calculate color temperature using utility functions.
            self.color_temp = Adafruit_TCS34725.calculate_color_temperature(
                self.r, self.g, self.b)
            # Calculate lux with another utility function.
            self.lux = Adafruit_TCS34725.calculate_lux(self.r, self.g, self.b)

        if self._touch_sensor is None:
            return
        touch_data = self._touch_sensor.touched()
        touched = False
        for i in range(12):
            pin_bit = 1 << i
            # Only transitions from not touched to touched are valid.
            if touch_data & pin_bit and not self._last_touched_data & pin_bit:
                touched = True
                break
        self._last_touched_data = touch_data

        list_len = len(self.touched_list)
        # Start storing touch events on initial touch.
        if touched and list_len == 0:
            self.touched_list.append(touched)
            self._collect_touches = True
        # Fill events until list is full.
        elif self._collect_touches and list_len < 20:
            self.touched_list.append(touched)
示例#3
0
def readTCSAll():
    avgr = 0
    avgg = 0
    avgb = 0
    avgc = 0
    count = 0
    while (count < 130):
        tcs = Adafruit_TCS34725.TCS34725()
        r, g, b, c = tcs.get_raw_data()
        avgr = avgr + r
        avgg = avgg + g
        avgb = avgb + b
        avgc = avgc + c
        count = count + 1
        tcs.disable()
    avgr = avgr / 130
    avgg = avgg / 130
    avgb = avgb / 130
    avgc = avgc / 130
    r = float(avgr)
    g = float(avgg)
    b = float(avgb)
    c = float(avgc)
    lux = float(Adafruit_TCS34725.calculate_lux(r, g, b))
    color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
    if color_temp is None:
        color_temp = 0
    color_temp = float(color_temp)
    return r, g, b, c, lux, color_temp
示例#4
0
def Capture(integrationtime=2.4, gain=1, output='all'):  # Capture function for RGB sensor
    try:
        # Dictionary for possible integration time values:
        dict = {}  # creates a variable dictionary, for information about dictionaries: https://www.programiz.com/python-programming/dictionary
        # Add all integration time values to dictionary
        dict[2.4] = Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_2_4MS  # (2.4ms, default)
        dict[24] = Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_24MS
        dict[50] = Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_50MS
        dict[101] = Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_101MS
        dict[154] = Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_154MS
        dict[700] = Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_700MS
        # Add possible gain values to Dictionary:
        dict[1] = Adafruit_TCS34725.TCS34725_GAIN_1X
        dict[4] = Adafruit_TCS34725.TCS34725_GAIN_4X
        dict[16] = Adafruit_TCS34725.TCS34725_GAIN_16X
        dict[60] = Adafruit_TCS34725.TCS34725_GAIN_60X

        # Create a TCS34725 instance with default integration time (2.4ms) and gain (4x).
        #tcs = Adafruit_TCS34725.TCS34725()

        # You can also override the I2C device address and/or bus with parameters:
        #tcs = Adafruit_TCS34725.TCS34725(address=0x30, busnum=2)

        # Or you can change the integration time and/or gain:

        tcs = Adafruit_TCS34725.TCS34725(integration_time=dict[integrationtime],
                                         gain=dict[gain])

        # Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too).
        tcs.set_interrupt(False)

        # Read the R, G, B, C color data.
        r, g, b, c = tcs.get_raw_data()

        # Calculate color temperature using utility functions.  You might also want to
        # check out the colormath library for much more complete/accurate color functions.
        color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)

        # Calculate lux with another utility function.
        lux = Adafruit_TCS34725.calculate_lux(r, g, b)

        if color_temp is None:
            raise AttributeError('Too dark to determine color temperature!')

        tcs.set_interrupt(True)
        tcs.disable()
        # Determines outputs depending on function parameters:
        if output == 'all':
            return r, g, b, c, lux, color_temp
        elif output == 'rgbc':
            return r, g, b, c
        elif output == 'lux':
            return lux
        elif output == 'temp':
            return color_temp

    except KeyError:
        raise ValueError('No such integration time or gain, refer to coliform-project guide in github for possible options.')
示例#5
0
 def color(self):
     self.r, self.g, self.b, self.c = self.sensor_tcs.get_raw_data(
     )  # Read the R, G, B, C color data.
     self.color_temp = Adafruit_TCS34725.calculate_color_temperature(
         self.r, self.g,
         self.b)  # Calculate color temperature using utility functions.
     self.lux = Adafruit_TCS34725.calculate_lux(
         self.r, self.g,
         self.b)  # Calculate lux with another utility function.
示例#6
0
    def get_lux(self):
        # Read R, G, B, C color data from the sensor.
        r, g, b, c = self.tcs.get_raw_data()
        # Calulate color temp
        temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
        # Calculate lux and multiply it with gain
        self.lux = Adafruit_TCS34725.calculate_lux(r, g, b)

        # Calculate lux out of RGB measurements.
        print("temp [k]= ", temp)
        print("r :", r)
        print("g :", g)
        print("b :", b)
        print("c :", c)
        print("lux = ", self.lux)
    def get_data(self):
        # tcs = Adafruit_TCS34725.TCS34725()
        tcs = Adafruit_TCS34725.TCS34725(
            address=0x29,
            integration_time=Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_2_4MS)

        tcs.set_interrupt(False)
        r, g, b, c = tcs.get_raw_data()
        color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
        lux = Adafruit_TCS34725.calculate_lux(r, g, b)
        tcs.set_interrupt(True)

        tcs.disable()

        return lux
    def read_sensor(self):

        # Read the R, G, B, C color data.
        r, g, b, c = self._tcs.get_raw_data()

        # Calculate color temperature using utility functions.  You might also want to
        # check out the colormath library for much more complete/accurate color functions.
        color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)

        # Calculate lux with another utility function.
        lux = Adafruit_TCS34725.calculate_lux(r, g, b)

        # Print out the values.
        print(
            'Color: red={0} green={1} blue={2} clear={3}, Temperature: {4} K, Luminosity: {0} lux'
            .format(r, g, b, c, color_temp, lux))
示例#9
0
def CheckColor():
    tcs = Adafruit_TCS34725.TCS34725()
    tcs.set_interrupt(False)

    r, g, b, c = tcs.get_raw_data()

    color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)

    lux = Adafruit_TCS34725.calculate_lux(r, g, b)

    if color_temp is None:
        color_temp = 0
    
    tcs.set_interrupt(True)
    tcs.disable()
    return r, g, b, c, lux, color_temp
示例#10
0
 def get_color(self, mode="RGB"):
     try:
         self.r, self.g, self.b, self.c = self.sensor_tcs.get_raw_data(
         )  # Read the R, G, B, C color data.
         self.color_temp = Adafruit_TCS34725.calculate_color_temperature(
             self.r, self.g,
             self.b)  # Calculate color temperature using utility functions.
         self.lux = Adafruit_TCS34725.calculate_lux(
             self.r, self.g,
             self.b)  # Calculate lux with another utility function.
     except:
         self.r, self.g, self.b, self.c = [-1, -1, -1, -1]
         self.color_temp = -1
         self.lux = -1
     finally:
         if mode == "RGB": return (self.r, self.g, self.b)
         if mode == "hex": return '#%02x%02x%02x' % (self.r, self.g, self.b)
         if mode == "temp": return self.color_temp
         if mode == "lux": return self.lux
    def get_lux(self):
        # Read R, G, B, C color data from the sensor.
        r, g, b, c = self.tcs.get_raw_data()
        # Calulate color temp
        temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
        # Calculate lux and multiply it with gain
        lux = Adafruit_TCS34725.calculate_lux(r, g, b)
        real_lux = self.mult * lux + self.offset

        # Publish to topic
        #header
        self.msg_light_sensor.header.stamp = rospy.Time.now()
        self.msg_light_sensor.header.frame_id = rospy.get_namespace()[1:-1] 

        self.msg_light_sensor.r = r
        self.msg_light_sensor.g = g
        self.msg_light_sensor.b = b
        self.msg_light_sensor.c = c
        self.msg_light_sensor.real_lux = real_lux
        self.msg_light_sensor.lux = lux
        self.msg_light_sensor.temp = temp
        self.sensor_pub.publish(self.msg_light_sensor)
示例#12
0
def vrpnDataObserver(vrpnData):

    xpos = vrpnData.pose.position.x
    ypos = vrpnData.pose.position.y

    # Read the R, G, B, C color data.
    r, g, b, c = tcs.get_raw_data()
    #r = 0
    #g = 0
    #b = 0
    #c = 0
    #color_temp = 0
    #lux = 0

    # Calculate color temperature using utility functions.  You might also want to
    # check out the colormath library for much more complete/accurate color functions.
    color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)

    # Calculate lux with another utility function.
    lux = Adafruit_TCS34725.calculate_lux(r, g, b)

    # Print out the values.
    print('Color: red={0} green={1} blue={2} clear={3}'.format(r, g, b, c))

    # Print out color temperature.
    if color_temp is None:
        print('Too dark to determine color temperature!')
        color_temp = -1
    else:
        print('Color Temperature: {0} K'.format(color_temp))

    # Print out the lux.
    print('Luminosity: {0} lux'.format(lux))

    f.write("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\r\n" %
            (r, g, b, c, color_temp, lux, xpos, ypos))
示例#13
0
import Adafruit_TCS34725 as af
import os

ip = "10.46.82.2"
nt.initialize(server=ip)

#Create an instance of the Color Sensor
sensor = af.TCS34725()

sensor.set_interrupt(False)

while (1):

    r, g, b, c = sensor.get_raw_data()
    time.sleep(0.5)
    print("rgb=(" + str(r) + ", " + str(g) + ", " + str(b) + ")")
    #print(r)
    #print(g)
    #print(b)
    cls()
    colorTemp = af.calculate_color_temperature(r, g, b)
    lux = af.calculate_lux(r, g, b)
    print(str(colorTemp) + "k")
    print(str(lux) + " lux")

print(colorTemp)
print(lux)

sensor.set_interrupt(True)
sensor.disable()
示例#14
0
import MySQLdb

db = MySQLdb.connect(
    host='danube.stevens.edu',
    user='******',  #This changes for every sensor suite
    passwd='vesonder',  #This also changes for every sensor suite
    db='smartcampus',
    port=3306)

curs = db.cursor()

try:
    while True:
        #RGB SENSOR
        r, g, b, c = tcs.get_raw_data()
        color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
        lux = Adafruit_TCS34725.calculate_lux(r, g, b)
        print('Color: red={0}, green={1}, blue={2}, clear={3}'.format(
            r, g, b, c))

        if color_temp is not None:
            print('Color Temperature: {0} K'.format(color_temp))

        print('Luminosity: {0} lux'.format(lux))
        print(
            '----------------------------------------------------------------')

        #add_rgb = ("INSERT INTO rgb VALUES(%s,%s,%s,%s,%s,%s,%s,%s)")
        #curs.execute(add_rgb, (time.strftime("%Y/%m/%d"), time.strftime("%H:%M:%S"), r, g, b, c, color_temp, lux))
        #db.commit()
示例#15
0
def iothub_client_run():

    try:

        client = iothub_client_init()

        if client.protocol == IoTHubTransportProvider.MQTT:
            print ( "IoTHubClient is reporting state" )
            reported_state = "{\"newState\":\"standBy\"}"
            client.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)

        while True:
            print ( "IoTHubClient sending telemetry data" )

	    (airtemp,airpressure,airhumidity) = readBME280All(addr=DEVICE)
	    avgr = 0
	    avgg = 0
	    avgb = 0
	    avgc = 0
            count = 0
	    while (count < 130):
        		tcs = Adafruit_TCS34725.TCS34725()
        		r, g, b, c = tcs.get_raw_data()
        		avgr = avgr + r
        		avgg = avgg + g
        		avgb = avgb + b
        		avgc = avgc + c
        		count = count + 1
			tcs.disable()
     	    avgr = avgr/130
	    avgg = avgg/130
	    avgb = avgb/130
	    avgc = avgc/130
	    
            print ( avgr )

	    r = float(avgr)
	    g = float(avgg)
	    b = float(avgb)
	    c = float(avgc)

	    lux = float(Adafruit_TCS34725.calculate_lux(r, g, b))

 	    color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
	    if color_temp is None:
        	color_temp = 0
	    color_temp = float(color_temp)

            msg_txt_formatted = MSG_TXT % (airtemp, airpressure, airhumidity, r, g, b, lux, color_temp)
            # messages can be encoded as string or bytearray
            #if (message_counter & 1) == 1:
            #    message = IoTHubMessage(bytearray(msg_txt_formatted, 'utf8'))
            #else:
            message = IoTHubMessage(msg_txt_formatted)
            # optional: assign properties
            # prop_map = message.properties()
            # prop_map.add("temperatureAlert", 'true' if temperature > 28 else 'false')

            client.send_event_async(message, send_confirmation_callback, 1)
            print ( "IoTHubClient.send_event_async accepted message 1 for transmission to IoT Hub." )
	    print ( msg_txt_formatted )
	    i=1
	    while i<=60:
		status = client.get_send_status()
                print ( "Send status: %s" % status )	
		i = i + 1
		time.sleep (10)

    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        return
    except KeyboardInterrupt:
        print ( "IoTHubClient sample stopped" )

    print_last_message_time(client)
示例#16
0
    #       GPIO.output(greenLed, False)
    #   if B < 50:
    #       GPIO.output(blueLed, False)
    #   if avgC < 100:
    #       GPIO.output(whiteLed, False)
    #   print('Color: red={0} green={1} blue={2} clear={3}'.format(R, G, B, C))
    #   print('Average C:', avgC)
    #   idx = idx + 1
    #   time.sleep(0.25)
    #############################end part1########################################

    ##############################part2############################################
    time.sleep(0.001)
    while press:
        R, G, B, C = tcs.get_raw_data()
        color_temp = Adafruit_TCS34725.calculate_color_temperature(R, G, B)
        slidingWindow2[idx % 6] = C
        bright = 0
        for x in range(0, 6):
            bright = bright + slidingWindow2[x]
        avgC = bright / 6
        if R >= 50:
            GPIO.output(redLed, True)
        if G >= 50:
            GPIO.output(greenLed, True)
        if B >= 50:
            GPIO.output(blueLed, True)
        if avgC >= 100:
            GPIO.output(whiteLed, True)
        if R < 50:
            GPIO.output(redLed, False)
示例#17
0
#  - TCS34725_INTEGRATIONTIME_700MS
# Possible gain values:
#  - TCS34725_GAIN_1X
#  - TCS34725_GAIN_4X
#  - TCS34725_GAIN_16X
#  - TCS34725_GAIN_60X

# Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too).
tcs.set_interrupt(False)

# Read the R, G, B, C color data.
r, g, b, c = tcs.get_raw_data()

# Calculate color temperature using utility functions.  You might also want to
# check out the colormath library for much more complete/accurate color functions.
color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)

# Calculate lux with another utility function.
lux = Adafruit_TCS34725.calculate_lux(r, g, b)

# Print out the values.
print('Color: red={0} green={1} blue={2} clear={3}'.format(r, g, b, c))

# Print out color temperature.
if color_temp is None:
    print('Too dark to determine color temperature!')
else:
    print('Color Temperature: {0} K'.format(color_temp))

# Print out the lux.
print('Luminosity: {0} lux'.format(lux))
示例#18
0
 def printColorInfo(self):
     r, g, b, c = self.tcs.get_raw_data()
     temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
     lux = Adafruit_TCS34725.calculate_lux(r, g, b)
     print('Color: r={0} g={1} b={2} temp={3} lux={4}'.format(
         r, g, b, temp, lux))