#!/usr/bin/env python
import cayenne.client
import time
import logging
import bme680
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"
print("""read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
""")
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
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
# These calibration data can safely be commented
# out, if desired.
print('Calibration data:')
for name in dir(sensor.calibration_data):
if not name.startswith('_'):
value = getattr(sensor.calibration_data, name)
if isinstance(value, int):
print('{}: {}'.format(name, value))
# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the data.
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)
print('\n\nInitial reading:')
for name in dir(sensor.data):
value = getattr(sensor.data, name)
if not name.startswith('_'):
print('{}: {}'.format(name, value))
sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
# Up to 10 heater profiles can be configured, each
# with their own temperature and duration.
# sensor.set_gas_heater_profile(200, 150, nb_profile=1)
# sensor.select_gas_heater_profile(1)
print('\n\nPolling:')
try:
while True:
client.loop()
if (time.time() > timestamp + 10):
if sensor.get_sensor_data():
output = '{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH'.format(
sensor.data.temperature,
sensor.data.pressure,
sensor.data.humidity)
client.celsiusWrite(1, bme_sensor.data.temperature)
client.hectoPascalWrite(2, bme_sensor.data.pressure)
client.virtualWrite(3, bme_sensor.data.humidity,"rel_hum","p")
if sensor.data.heat_stable:
print('{0},{1} Ohms'.format(
output,
sensor.data.gas_resistance))
client.virtualWrite(4, sensor.data.gas_resistance, "co","ohms")
else:
print(output)
except KeyboardInterrupt:
pass
I try Your code but I have error: $python bme680.py
read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
Traceback (most recent call last):
File "bme680.py", line 6, in <module>
import bme680
File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 18, in <module>
client.on_message = on_message
NameError: name 'on_message' is not defined
I don’t know what to type there: on_message
Now I try Example-01-SendData.py and also I don’t see anything in Dashboard (I add new device>Bring Your own thing) in web browser [waiting for live data…] or android app [No active widgets or sensors].
Edit. I add port=8883 and I see temperature etc in example device dashboard.
lets start fresh, i guess you have installed the cayenne python library. if not install it using:
git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
python3 setup.py install
Next, on your cayenne dashboard, add a new device by navigating to add new ---> device/widgets --> arduino You will get the MQTT credentials.
On your pi, navigate to cd Cayenne-MQTT-Python/examples and open sudo nano Example-03-CayenneClient.py . add the MQTT credentials into this code, save and exit. Now, run using sudo python3 Example-03-CayenneClient.py. you should see the device shown online on the cayenne dashboard.
Next, open the code again and just add the BME680 code from the code i gave above.
Hi, as I post above Example-01-SendData.py works fine and show data on dashboard.
Where I have to paste that code You gave me? Do I have to delete some code from example.py file?
I try to combine those files and always have some errors I post You above. I’m not a programmer and I don’t know where to paste that code. Can You help me and show me how it should look?
import cayenne.client
import time
import logging
import bme680
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"
print("""read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
""")
client = cayenne.client.CayenneMQTTClient()
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
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
# These calibration data can safely be commented
# out, if desired.
print('Calibration data:')
for name in dir(sensor.calibration_data):
if not name.startswith('_'):
value = getattr(sensor.calibration_data, name)
if isinstance(value, int):
print('{}: {}'.format(name, value))
# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the data.
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)
print('\n\nInitial reading:')
for name in dir(sensor.data):
value = getattr(sensor.data, name)
if not name.startswith('_'):
print('{}: {}'.format(name, value))
sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
# Up to 10 heater profiles can be configured, each
# with their own temperature and duration.
# sensor.set_gas_heater_profile(200, 150, nb_profile=1)
# sensor.select_gas_heater_profile(1)
print('\n\nPolling:')
try:
while True:
client.loop()
if (time.time() > timestamp + 10):
if sensor.get_sensor_data():
output = '{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH'.format(
sensor.data.temperature,
sensor.data.pressure,
sensor.data.humidity)
client.celsiusWrite(1, bme_sensor.data.temperature)
client.hectoPascalWrite(2, bme_sensor.data.pressure)
client.virtualWrite(3, bme_sensor.data.humidity,"rel_hum","p")
if sensor.data.heat_stable:
print('{0},{1} Ohms'.format(
output,
sensor.data.gas_resistance))
client.virtualWrite(4, sensor.data.gas_resistance, "co","ohms")
else:
print(output)
except KeyboardInterrupt:
pass
you have two files with same name bme680.py now when the python code looks for the main library to import bme680 it lands to your new file where there is nothing.
Now rename the file using mv bme680.py test_bme,py
python test_bme680.py
read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
Connecting to mqtt.mydevices.com:8883
Traceback (most recent call last):
File "test_bme680.py", line 7, in <module>
import bme680
File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'BME680'
python test_bme680.py
read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
Connecting to mqtt.mydevices.com:8883
Traceback (most recent call last):
File "test_bme680.py", line 7, in <module>
import bme680
File "/home/pi/Cayenne-MQTT-Python/examples/bme680.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'BME680'