Example #1
0
 def __init__(self):
     self.sensor = BH1750(SoftI2C(scl=Pin(22), sda=Pin(21)))
     self.max_level = 500
     self.last_day_list = None
     self.list = []
     self.last_measurement_time = 0
     self.get_level()
Example #2
0
def main():
    c = simple.MQTTClient(clientID,
                          broker,
                          user=user,
                          password=authToken,
                          ssl=True)
    c.set_callback(sub_cb)
    c.connect()
    c.subscribe(colorCommandTopic)
    c.subscribe(ledCommandTopic)
    # init input button
    ugfx.input_attach(ugfx.JOY_UP, lambda pressed: btn_cb(c, 'U', pressed))
    ugfx.input_attach(ugfx.JOY_DOWN, lambda pressed: btn_cb(c, 'D', pressed))
    ugfx.input_attach(ugfx.JOY_LEFT, lambda pressed: btn_cb(c, 'L', pressed))
    ugfx.input_attach(ugfx.JOY_RIGHT, lambda pressed: btn_cb(c, 'R', pressed))
    ugfx.input_attach(ugfx.BTN_MID, lambda pressed: btn_cb(c, 'M', pressed))
    ugfx.input_attach(ugfx.BTN_A, lambda pressed: btn_cb(c, 'A', pressed))
    ugfx.input_attach(ugfx.BTN_B, lambda pressed: btn_cb(c, 'B', pressed))
    # init BH1750
    bh1750 = BH1750(i2c)
    print("Connected, waiting for event")

    lumi = {'d': {'luminance': {}}}
    try:
        while True:
            lumi['d']['luminance'] = bh1750.luminance(BH1750.ONCE_HIRES_1)
            c.publish(lumiTopic, json.dumps(lumi))

            time.sleep_ms(300)
            #c.wait_msg()
            c.check_msg()
    finally:
        c.disconnect()
        ugfx.input_init()
        print("Disonnected")
Example #3
0
File: main.py Project: NiekB4/aqs
def final(sleeping):
    """
    This is the final function, used for the outdoor measurements.
    The function uses one argument sleeping, which represents the interval between measurement cycles in seconds. 
    """
    x = 1
    while True:
        print("7d58. Round ", x)
        time.sleep(2)
        aq1 = aq(20, 1)
        aq2 = aq(20, 2)
        time.sleep(2)
        th1 = am2302.th1.read()
        th2 = am2302.th2.read()
        i2c_light = machine.I2C(0, pins=("P21", "P22"))
        lightsensor = BH1750(i2c_light)
        luminance = lightsensor.luminance(BH1750.ONCE_HIRES_1)
        time.sleep(2)
        print("Results: ")
        print("PMS5003 1:           ", aq1[0:3])
        print("PMS5003 2:           ", aq2[0:3])
        print("Temperature 1:       ", th1.temperature)
        print("Temperature 2:       ", th2.temperature)
        print("Humdity 1:           ", th1.humidity)
        print("Humdity 2:           ", th2.humidity)
        print("Light intensity:     ", luminance)
        gc.collect()
        time.sleep(sleeping)
        gc.collect()
        x += 1
    pms5003.fan_off()
Example #4
0
    def data_collect(self, header_length):
        #column 0, 0-90 by 5s
        column_degrees = np.arange(0, 95, 5, dtype='int')
        column_degrees = np.array([column_degrees])
        column_degrees = column_degrees.T
        #create empty array
        #data_array = np.zeros((column_degrees.size,header_length))
        data_array = np.zeros(column_degrees.size * header_length)
        # collect/store data
        bus = smbus2.SMBus(1)
        sensor = BH1750(bus)

        i = 0
        while data_array[i] < data_array.size:
            print(sensor.measure_high_res2())
            data_array[i] = float(sensor.measure_high_res2())
            #data_array[i]=self.read_light()
            i = i + 1
            time.sleep(self.delay)
            if (i == data_array.size):
                break
        #go from 1d array to 2d array
        data_array = np.resize(data_array,
                               (column_degrees.size, header_length))
        #combine column array and data array
        data_array = np.concatenate((column_degrees, data_array), axis=1)
        #turn numpy array to data_frame
        data_array = pd.DataFrame(data=data_array)
        return (data_array)
def read_bh1750():
    """
     Function to read externally connected BH1750 Light Sensor

    Returns:
        float: Light intensity in lumens
    """

    bus = I2C(scl=Pin(22), sda=Pin(21), freq=100000)

    s = BH1750(bus)

    return s.luminance(BH1750.ONCE_HIRES_1)
Example #6
0
from machine import UART, Pin, I2C
import time, binascii
from bh1750 import BH1750
from bmp180 import BMP180

reset = Pin(23, Pin.OUT)
led_onboard = Pin(2, Pin.OUT)

#define pins 4 and 5 for UART because UART0 are used for USB and
# UART1  are "used by Flash chip and cannot be used for other purposes."*
#* https://forum.micropython.org/viewtopic.php?p=27259&sid=5ffaee5b5c0819cefc11f73e91b804aa#p27259
scl1 = Pin(5, pull=Pin.PULL_UP)
sda1 = Pin(4, pull=Pin.PULL_UP)
i2c = I2C(0)
i2c = I2C(1, scl=scl1, sda=sda1)
s = BH1750(i2c)
bmp180 = BMP180(i2c)
bmp180.oversample_sett = 2


def pisca(tiempo):
    led_onboard.on()
    time.sleep(tiempo)
    led_onboard.off()
    time.sleep(tiempo)
    led_onboard.on()
    time.sleep(tiempo)
    led_onboard.off()
    time.sleep(tiempo)

Example #7
0
from network import WLAN, STA_IF
from bh1750 import BH1750
from bme280 import BME280, BME280_OSAMPLE_16
from ssd1306 import SSD1306_I2C
from dht import DHT11
from math import floor
import urequests
import utime

# set i2c bus
i2c = I2C(sda=Pin(4), scl=Pin(5))
devices = i2c.scan()

# setup all devices
display = None if 0x3c not in devices else SSD1306_I2C(128, 64, i2c)
light_sensor = None if 0x23 not in devices else BH1750(i2c)
bme = None if 0x76 not in devices else BME280(i2c=i2c, mode=BME280_OSAMPLE_16)
dht = DHT11(Pin(14))
# adc = ADC(0)

adafruit_url = 'https://io.adafruit.com/api/v2/plugowski/groups/micrometeo/data'


def show(method: str, *args):
    if display is not None:
        getattr(display, method)(*args)
    elif method == 'text':
        print(args[0])


def justify_right(value, y):
Example #8
0
                print("Lost connection to host")
                self.state_machine.current_state = state_network
                break


utime.sleep(5)
wifi = WiFi(secrets.network_ssid, secrets.network_password)
led = NetworkLed(33)
led.off()

state_machine = StateMachine()
state_no_network = NoNetworkState(state_machine, wifi)
state_network = NetworkState(state_machine, wifi)
state_connected = ConnectedState(state_machine, wifi)
state_machine.current_state = state_no_network

TEMP_IO = const(39)
adc = ADC(Pin(TEMP_IO, Pin.IN))
adc.atten(ADC.ATTN_6DB)

I2C0_SCL = const(26)
I2C0_SDA = const(25)

scl = Pin(I2C0_SCL, Pin.IN)
sda = Pin(I2C0_SDA, Pin.OUT)
i2c = I2C(-1, scl=scl, sda=sda)

light_sensor = BH1750(i2c)

state_machine.run()
Example #9
0
from machine import I2C, Pin
from bh1750 import BH1750
import time

bus = I2C(scl=Pin(17), sda=Pin(16))
sensor = BH1750(bus)

while True:
    for mode, name in BH1750.res_mod:
        #print(mode, name)
        lux = sensor.do_measurement(mode)
        print("mode : %-30s, lux: %d" % (name, lux))
    print("\n")
    time.sleep(1)
Example #10
0
File: main.py Project: NiekB4/aqs
BIGSLEEP = 800  # 800 seconds for 15 minutes intervals.
ROUND = 1
TICKER = 1

while True:
    f = open("data/data.csv", "a")
    print("862c. Round ", ROUND)
    time.sleep(2)
    insert_one_1 = aq(5, 1)
    insert_two_1 = aq(5, 2)
    time.sleep(2)
    temphum1 = am2302.th1.read()
    temphum2 = am2302.th2.read()
    i2c_light = machine.I2C(0,
                            pins=("P21", "P22"))  # (P21 is SDA, P22 is clock)
    lightsensor = BH1750(i2c_light)
    luminance = lightsensor.luminance(BH1750.ONCE_HIRES_1)
    time.sleep(2)
    timesec = time.time()
    f.write(
        str(ROUND) + '; ' + str("862c") + '; ' + str(insert_one_1[0]) + '; ' +
        str(insert_one_1[1]) + '; ' + str(insert_one_1[2]) + '; ' +
        str(insert_two_1[0]) + '; ' + str(insert_two_1[1]) + '; ' +
        str(insert_two_1[2]) + '; ' + str(temphum1.temperature) + '; ' +
        str(temphum2.temperature) + '; ' + str(temphum1.humidity) + '; ' +
        str(temphum2.humidity) + '; ' + str(luminance) + '; ' + str(timesec) +
        '\n')
    f.close()
    pms5003.fan_off()
    ROUND += 1
    TICKER += 1
Example #11
0
# micropython script to get lux values from BH1750 sensor

import time
import machine
from bh1750 import BH1750

scl = machine.Pin(5)
sda = machine.Pin(4)
# for esp32 use machine.I2C(-1, scl=scl, sda=sda, freq=100000)
i2c = machine.SoftI2C(scl=scl, sda=sda, freq=100000)

sensor = BH1750(i2c, 0x23)  # secondary address is 0x5c

try:
    while True:
        # get sensor light intensity (lux) and store value in variable 'lux'
        lux = sensor.luminance(BH1750.ONCE_HIRES_1)

        # print value to console.
        # {} is used in conjunction with format() for substitution.
        # .2f       - format to 2 decimal places.
        # end='\r'  - curser will go to the start of the current line instead of making a new line.
        print("Light intensity is {:.2f} lx".format(lux), end='\r')
        time.sleep(1)

except KeyboardInterrupt:
    print('script stopped by user')
finally:
    print('Goodbye!')
Example #12
0
# Refer to README at head of repository for HOW to upload the script to the board

import machine
import time
# Micropython BH1750 Library
# https://github.com/PinkInk/upylib/tree/master/bh1750
from bh1750 import BH1750

# Declare the pins that we are going to use for the I2C
# Then set up I2C
scl = machine.Pin(1)  # defines D1 as I2C SCL
sda = machine.Pin(2)  # defines D2 as I2C SDA
i2c = machine.I2C(scl, sda)

s = BH1750(i2c)  # addr defaults to 0x23, or
#s = BH1750(i2c, 0x23)  # specify addr explicitly, or
#s = BH1750(i2c, 0x5c)  # pull ADDR pin high and use alternate i2c address

while True:
    lux = s.luminance(BH1750.CONT_HIRES_1)  # see notes below for reading type
    print(f"Light: {lux} lx")

    time.sleep(1)  # delay for 1s before repeating loop

# Other modes for luminance sensor reading (CONT_HIRES_1 is default)
# CONT_LOWRES	    Continuous, low resolution (4lx), sampling takes ~24ms, sensor remains on after reading.
# CONT_HIRES_1	    Continuous, high resolution (1lx), sampling takes ~180ms, sensor remains on after reading.
# CONT_HIRES_2	    Continuous, very high resolution (.5lx), sampling takes ~180ms, sensor remains on after reading.
# ONCE_HIRES_1      One-shot, low resolution (4lx), sampling takes ~24ms, sensor powered down after reading
# ONCE_HIRES_2      One-shot, high resolution (1lx), sampling takes ~180ms, sensor powered down after reading