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:
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:
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:
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:
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)
def convertToNumber(data): return ((data + (256*data))/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.