Example #1
0
def work(scl_pin, sda_pin, tca_address, intervall, sensor_mappings,
         factor_sunny, mqtt_broker, mqtt_client_id, mqtt_port, mqtt_user,
         mqtt_password, mqtt_topic):
    tca = tca9548a.TCA9548A(scl_pin, sda_pin, tca_address)
    mqtt_client = MQTTClient(mqtt_client_id, mqtt_broker, mqtt_port, mqtt_user,
                             mqtt_password)

    print("Starting infinite loop")
    while True:
        try:
            mqtt_client.connect()
            sensor_readings = []
            for row in sensor_mappings:
                tca.switch_channel(row[0])
                if row[1] == "BH1750":
                    sensor = bh1750.BH1750(tca.bus)
                    sensor.reset()
                    reading = sensor.luminance(
                        bh1750.BH1750.ONCE_HIRES_1) * row[4]
                    if row[3]:
                        sensor_readings.append([row[2], reading])
                    else:
                        mqtt_client.publish(mqtt_topic + row[2], str(reading))
                    sensor.off()
                elif row[1] == "VEML7700":
                    sensor = veml7700.VEML7700(address=0x10,
                                               i2c=tca.bus,
                                               it=25,
                                               gain=1 / 8)
                    reading = sensor.read_lux() * row[4]
                    if row[3]:
                        sensor_readings.append([row[2], reading])
                    else:
                        mqtt_client.publish(mqtt_topic + row[2], str(reading))
                else:
                    print("Unknown sensor or bad config!")
            sensor_readings.sort(key=sort_readings)
            sunny_sides = []
            for row in sensor_readings:
                if row[1] >= sensor_readings[0][1] * factor_sunny:
                    sunny_sides.append(row[0])
            if len(sunny_sides) >= 1:
                mqtt_client.publish(mqtt_topic + "weather/sun_direction",
                                    str(sunny_sides))
            else:
                mqtt_client.publish(mqtt_topic + "weather/sun_direction",
                                    "none")
            mqtt_client.disconnect()
        except:
            print("Error in loop!")
        print("These values were recorded: " + str(sensor_readings) +
              " and these are the sunny sides: " + str(sunny_sides))
        sleep(intervall)
Example #2
0
# UV sensing using library
import bh1750
import machine

i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
'''
confirm your device is present on the bus
it should return [35] for this particular sensor
if this returns nothing, check your wiring
then check the Pin GPIO numbers are correct 
'''

print(i2c.scan())

s = bh1750.BH1750(i2c)

while True:
    s.luminance(BH1750.ONCE_HIRES_1)
'''
there are lots of options to sense luminance
read bh1750.py for details
this is a high resolution single reading
it returns a float of luminance in LUX
'''
Example #3
0
#!/usr/bin/python
import bh1750
import time

light = bh1750.BH1750()
print("Default parameters:" + str(light.get_light_mode()))
print()

light = bh1750.BH1750(mode=5)
print("Low resolution, but faster:" + str(light.get_light_mode()))
print()

light = bh1750.BH1750(0, 1, 0)  # Address pin low, I2Cbus 1, continuous mode
light.set_mode()
for i in range(20):
    print("Continuous readings, " + str(i) + ":" + str(light.get_light()))
    time.sleep(1)
# simple script to get lux values from BH1750 sensor

import time

import bh1750

try:
    import smbus2 as smbus
except ImportError:
    import smbus

# create i2c bus object. usually bus 1.
bus = smbus.SMBus(1)

# create sensor object. default address is 0x23. secondary address is 0x5C
sensor = bh1750.BH1750(bus, addr=0x23)

try:
    print("Reading lux values from sensor. Press Ctrl-C to exit")
    print("")
    print("Sensitivity: {:d}".format(sensor.mtreg))

    while True:
        print("Light level: {:3.2f} lx  ".format(sensor.measure_high_res()),
              end='\r')
        time.sleep(1)

except KeyboardInterrupt:
    print('script stopped by user')
finally:
    bus.close()
Example #5
0
 def __init__(self, _sleep):
     self.sleep = _sleep
     self.data = data.Sensors_data()
     self.bus = smbus.SMBus(1)
     self.light = bh1750.BH1750(self.bus)
     self.dht = dht11.DHT()
Example #6
0
# Pot qui pense - CREPP 2019
# GPL V3.0
# www.crepp.org - [email protected]

import machine
import bh1750

i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
lum = bh1750.BH1750(i2c)


def test():
    print(lum.lecture_lumiere(bh1750.MODE_CONTINU_HAUTE_RESOLUTION))
#
# Utilisation capteur de luminosite BH1750
# Tester sur Wemos D1 mini
#
# Auteur : iTechnoFrance
#

import machine, time
import bh1750  # librairie capteur BH1750
# Declaration I2C
# // D1 -> GPIO05 --> SCL
# // D2 -> GPIO04 --> SDA
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4), freq=400000)
capteur_lumiere = bh1750.BH1750(i2c)  # declaration BH1750

if (capteur_lumiere.detect()):
    # Resolutions et modes possibles pour les mesures
    # MODE_CONTINU_HAUTE_RESOLUTION = Mesure continue, résolution 1 Lux, temps de mesure 120ms
    # MODE_2_CONTINU_HAUTE_RESOLUTION = Mesure continue, résolution 0.5 Lux, temps de mesure 120ms
    # MODE_CONTINU_BASSE_RESOLUTION = Mesure continue, résolution 4 Lux, temps de mesure 16ms
    # MODE_UNE_MESURE_HAUTE_RESOLUTION = 1 mesure puis passe en veille, résolution 1 Lux, temps de mesure 120ms
    # MODE_2_UNE_MESURE_HAUTE_RESOLUTION = 1 mesure puis passe en veille, résolution 0.5 Lux, temps de mesure 120ms
    # MODE_UNE_MESURE_BASSE_RESOLUTION = 1 mesure puis passe en veille, résolution 4 Lux, temps de mesure 16ms
    while True:
        mesure_lux = capteur_lumiere.lecture_lumiere(
            bh1750.MODE_CONTINU_HAUTE_RESOLUTION)
        print(mesure_lux)
        time.sleep(1)
else:
    print("Capteur BH1750 non detecte")