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 smbus
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…
Follow exactly what have been told in the tutorial. (DONE)
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 ?
Save the python code as bh1750.py in the folder.
Correctly done ?
Open the link below to download the Cayenne MQTT Python Library: (DONE)
pi@raspberrypi:~/Luminosity $ ls cayenne/
client.py init.py pycache
Follow the steps from Requirement until Cayenne setup.
pip install cayenne-mqtt (Done)
All requirements is ok.
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.
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
Open Cayenne-MQTT-Python folder > example > Example-01-SendData.py. Copy all the codes in the example and combine together with bh1750.py.
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.
#!/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