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:
-
Click the link below to open the Tutorial:
https://www.raspberrypi-spy.co.uk/2015/03/bh1750fvi-i2c-digital-light-intensity-sensor/ -
Follow exactly what have been told in the tutorial.
-
Copy down the python code in the tutorial and make a new folder named Luminosity.
-
Save the python code as bh1750.py in the folder.
-
Open the link below to download the Cayenne MQTT Python Library:
https://github.com/myDevicesIoT/Cayenne-MQTT-Python -
Follow the steps from Requirement until Cayenne setup.
-
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. -
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.
-
Open Cayenne-MQTT-Python folder > example > Example-01-SendData.py. Copy all the codes in the example and combine together with bh1750.py.
-
The combination of codes is shown as below:
#!/usr/bin/env python
import cayenne.client
import time
import smbusMQTT_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 = 0x07CONTINUOUS_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_ = 0x23bus = 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()
-
Save the python code and run the python shell.
Gudluck!!!