BH1750/GY-30 Luminosity Sensor with Cayenne using Raspberry Pi 3 Model B

The connection is done via Raspberry Pi 3 Model B. This is done by me in my Final Year Project. The procedures are explained as below:

  1. Click the link below to open the Tutorial:
    Using the BH1750FVI I2C Digital Light Sensor - Raspberry Pi Spy

  2. Follow exactly what have been told in the tutorial.

  3. Copy down the python code in the tutorial and make a new folder named Luminosity.

  4. Save the python code as bh1750.py in the folder.

  5. Open the link below to download the Cayenne MQTT Python Library:
    GitHub - myDevicesIoT/Cayenne-MQTT-Python: Python Library for Cayenne MQTT API

  6. Follow the steps from Requirement until Cayenne setup.

  7. For the Cayenne Setup, after creating the Cayenne account you must add the new device by clicking “Add new…” tab at the top left. Then, click the “Device/Widget” tab and scroll down. You will find the Cayenne API tab named “Bring Your Own Thing”. By clicking the tab, you will get the MQTT information given:
    a) Username
    b) Password
    c) Client ID
    d) MQTT server and port
    e) Name your device (it can be changed)
    ** These information are used to combine together with bh1750 python code so that the luminosity value can be sent to Cayenne.

  8. Once step 5 until 7 are completed, go to the /home/pi and open the Cayenne-MQTT-Python folder, copy the “cayenne” folder and paste into the Luminosity folder.

  9. Open Cayenne-MQTT-Python folder > example > Example-01-SendData.py. Copy all the codes in the example and combine together with bh1750.py.

  10. The combination of codes is shown as below:

    #!/usr/bin/env python
    import cayenne.client
    import time
    import smbus

    MQTT_USERNAME = “MQTT username”
    MQTT_PASSWORD = “MQTT Password”
    MQTT_CLIENT_ID = “MQTT Client ID”

    client = cayenne.client.CayenneMQTTClient()

    client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

    timestamp = 0

    DEVICE = 0x23

    POWER_DOWN = 0x00
    POWER_ON = 0x01
    RESET = 0x07

    CONTINUOUS_LOW_RES_MODE = 0x13
    CONTINUOUS_HIGH_RES_MODE_1 = 0x10
    CONTINUOUS_HIGH_RES_MODE_2 = 0x11
    ONE_TIME_HIGH_RES_MODE_1 = 0x20
    ONE_TIME_HIGH_RES_MODE_2 = 0x21
    ONE_TIME_HIGH_RES_MODE_ = 0x23

    bus = smbus.SMBus(1)

    while True:
    client.loop()

    def convertToNumber(data):
    	return ((data[1] + (256*data[0]))/1.2)
    
    def luxWrite(addr=DEVICE):
    	data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
    	return convertToNumber(data)
    
    LuxWrite = luxWrite()
    
    if (time.time() > timestamp +10):
    	client.luxWrite(2, LuxWrite)
    	timestamp = time.time()
    
  11. Save the python code and run the python shell.

Gudluck!!! :blush::blush:

1 Like

Nice project!

1 Like

Thank you sir… without your suggestions I’m not able to make it real…:blush::blush::blush:

Hi All

this works perfectly for local reading, I`m trying to send the results by Cayenne, and haven’t had any luck doing so… can you give me some ideas on where the problem might be? I’m currently connecting to cayenne and the device is seen by the platform but no data is transmitted…

add #define CAYENNE_DEBUG in your code and check the issue in your serial monitor.
Also, can you share the code you are using.

Hello guys,

i´m really new here trying to set up BH1750 with Raspberry Pi 3.

Will resume what i did, maybe you can find the issue and help.

  1. Click the link below to open the Tutorial: (DONE)

pi@raspberrypi:~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – – – – – – – – – – – – –
10: – – – – – – – – – – – – – – – –
20: – – – 23 – – – – – – – – – – – –
30: – – – – – – – – – – – – – – – –
40: – – – – – – – – – – – – – – – –
50: – – – – – – – – – – – – – – – –
60: – – – – – – – – – – – – – – – –
70: – – – – – – – –

Sensor is working with “Python bh1750.py command”

Light Level : 691.67 lx
Light Level : 694.17 lx
Light Level : 690.83 lx
Light Level : 687.50 lx
Light Level : 699.17 lx
Light Level : 698.33 lx
Light Level : 696.67 lx
Light Level : 697.50 lx

  1. Follow exactly what have been told in the tutorial. (DONE)

  2. Copy down the python code in the tutorial and make a new folder named Luminosity.
    Do not understand, in this tutorial there is code with name, bh1750.py i saved it into the folder Luminosity

Correctlly done ?

  1. Save the python code as bh1750.py in the folder.

Correctly done ?

  1. Open the link below to download the Cayenne MQTT Python Library: (DONE)
    pi@raspberrypi:~/Luminosity $ ls cayenne/
    client.py init.py pycache

  2. Follow the steps from Requirement until Cayenne setup.

pip install cayenne-mqtt (Done)
All requirements is ok.

  1. For the Cayenne Setup, after creating the Cayenne account you must add the new device by clicking “Add new…” tab at the top left. Then, click the “Device/Widget” tab and scroll down. You will find the Cayenne API tab named “Bring Your Own Thing”. By clicking the tab, you will get the MQTT information given:
    a) Username
    b) Password
    c) Client ID
    d) MQTT server and port
    e) Name your device (it can be changed)
    ** These information are used to combine together with bh1750 python code so that the luminosity value can be sent to Cayenne.

— This also done, i added this information, when i run the bh1750.py Sensor appears in Cayenne dashboard.

  1. Once step 5 until 7 are completed, go to the /home/pi and open the Cayenne-MQTT-Python folder, copy the “cayenne” folder and paste into the Luminosity folder.

pi@raspberrypi:~/Luminosity $ ls
bh1750.py cayenne

  1. Open Cayenne-MQTT-Python folder > example > Example-01-SendData.py. Copy all the codes in the example and combine together with bh1750.py.

pi@raspberrypi:~/Luminosity $cat bh1750.py

#!/usr/bin/env python
import cayenne.client
import time
import smbus

MQTT_USERNAME = "MY USER"
MQTT_PASSWORD = "MY PASS"
MQTT_CLIENT_ID = "MY CLIENT ID"

client = cayenne.client.CayenneMQTTClient()

client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

timestamp = 0

DEVICE = 0x23

POWER_DOWN = 0x00
POWER_ON = 0x01
RESET = 0x07

CONTINUOUS_LOW_RES_MODE = 0x13
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
ONE_TIME_HIGH_RES_MODE_1 = 0x20
ONE_TIME_HIGH_RES_MODE_2 = 0x21
ONE_TIME_HIGH_RES_MODE_ = 0x23

bus = smbus.SMBus(1)

while True:
        client.loop()

	def convertToNumber(data):
		return ((data[1] + (256*data[0]))/1.2)

	def luxWrite(addr=DEVICE):
		data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
		return convertToNumber(data)

LuxWrite = luxWrite()

	if (time.time() > timestamp +10):
		client.luxWrite(2, LuxWrite)
		timestamp = time.time()
  1. I´m creating Custom widget → Value > with device just detected, data:Luminocity > Unit: Lux > Channel: (not clear but i put 2)
    And after adding and run the bh1750.py no info is displayed.

Can you please help me to find the issue and fix ?
Really need to fix asap.

Thank you very much !

do not add the widget like this. Delete it.

this will create a green temporary widget automatically when the code is runned. add it by clicking on the + sign on the top rigth of the widget.

First of all see if this giving you the sensor reading,

Hello Sharmik,

Thank you for quick answer,

when i Run the code no widget added automaticlly,

How can i see LUXWrite sensor reading ?

Thanks

how are you getting this, similarly do in the code.

is your device connected?

Do you have posibility to make quick 5 min call with Zoom and clarify.
Really apreciating your.

every time i run the code, device is connected once I cancel script device appears Offline so i think it ok.

this is the final script i ejecute,

pi@raspberrypi:~/Luminosity ls bh1750_original.py bh1750.py cayenne pi@raspberrypi:~/Luminosity
pi@raspberrypi:~/Luminosity pi@raspberrypi:~/Luminosity
pi@raspberrypi:~/Luminosity $ cat bh1750.py
#!/usr/bin/env python
import cayenne.client
import time
import smbus

MQTT_USERNAME = " removed"
MQTT_PASSWORD = " removed"
MQTT_CLIENT_ID = " removed"

client = cayenne.client.CayenneMQTTClient()

client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

timestamp = 0

DEVICE = 0x23

POWER_DOWN = 0x00
POWER_ON = 0x01
RESET = 0x07

CONTINUOUS_LOW_RES_MODE = 0x13
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
ONE_TIME_HIGH_RES_MODE_1 = 0x20
ONE_TIME_HIGH_RES_MODE_2 = 0x21
ONE_TIME_HIGH_RES_MODE_ = 0x23

bus = smbus.SMBus(1)

while True:
client.loop()

def convertToNumber(data):
return ((data[1] + (256*data[0]))/1.2)

def luxWrite(addr=DEVICE):
data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
return convertToNumber(data)

LuxWrite = luxWrite()

if (time.time() > timestamp +10):
client.luxWrite(2, LuxWrite)
timestamp = time.time()
print LuxWrite

so what does this print

also try the basic code and see if the device connects and create widgets https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-03-CayenneClient.py

Hello,

Yes this basic script is working and its creates 3 widgetes.

now, try this code:

#!/usr/bin/env python
import cayenne.client
import time
import logging

import smbus
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"

# Define some constants from the datasheet

DEVICE     = 0x23 # Default device I2C address

POWER_DOWN = 0x00 # No active state
POWER_ON   = 0x01 # Power on
RESET      = 0x07 # Reset data register value

# Start measurement at 4lx resolution. Time typically 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13
# Start measurement at 1lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
# Start measurement at 0.5lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_1 = 0x20
# Start measurement at 0.5lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_LOW_RES_MODE = 0x23

#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1)  # Rev 2 Pi uses 1

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

i=0
timestamp = 0

def convertToNumber(data):
  # Simple function to convert 2 bytes of data
  # into a decimal number. Optional parameter 'decimals'
  # will round to specified number of decimal places.
  result=(data[1] + (256 * data[0])) / 1.2
  return (result)

def readLight(addr=DEVICE):
  # Read data from I2C interface
  data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
  return convertToNumber(data)



while True:
  client.loop()
  if (time.time() > timestamp + 10):
    lightLevel=readLight()
    print("Light Level : " + format(lightLevel,'.2f') + " lx")
    client.celsiusWrite(1, i)  
    client.virtualWrite(2, lightLevel)      
    timestamp = time.time()
    i = i+1

pi@raspberrypi:~/Luminosity $ python final.py
Traceback (most recent call last):
File “final.py”, line 40, in
client.on_message = on_message
NameError: name ‘on_message’ is not defined

remove client.on_message = on_message

1 Like

Seems workin,
Thank you very much for Support !
Will try to set up another one with 5c address.

1 Like

I have added second one, both of them are working and sending info to Dashboard, will check if Triggers are working.

Again thenak you very much for help !

for triggers have a look at this topic Sending MQTT messages within notification limit and modify the code accordingly.