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


#1

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:
    https://www.raspberrypi-spy.co.uk/2015/03/bh1750fvi-i2c-digital-light-intensity-sensor/

  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:
    https://github.com/myDevicesIoT/Cayenne-MQTT-Python

  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:


#2

Nice project!


#3

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