#!/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 = "06487d60-1537-11ea-84bb-8f71124cfdfb"
MQTT_PASSWORD = "23309cdee31964b2ae4caed9417b689c9bebece2"
MQTT_CLIENT_ID = "79503770-153d-11ea-a38a-d57172a4b4d4"
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, port=8883)
# 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
pi@raspberrypi:~/Cayenne-MQTT-Python/examples $ python3 test_bme680.py
Connecting to mqtt.mydevices.com:1883
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bus_device/i2c_device.py", line 68, in __init__
i2c.writeto(device_address, b'')
File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 82, in writeto
return self._i2c.writeto(address, buffer, stop=stop)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 38, in writeto
self._i2c_bus.write_bytes(address, buffer[start:end])
File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_PureIO/smbus.py", line 244, in write_bytes
self._device.write(buf)
OSError: [Errno 121] Remote I/O error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bus_device/i2c_device.py", line 74, in __init__
i2c.readfrom_into(device_address, result)
File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 72, in readfrom_into
return self._i2c.readfrom_into(address, buffer, stop=stop)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 44, in readfrom_into
readin = self._i2c_bus.read_bytes(address, end-start)
File "/home/pi/.local/lib/python3.7/site-packages/Adafruit_PureIO/smbus.py", line 155, in read_bytes
return self._device.read(number)
OSError: [Errno 121] Remote I/O error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test_bme680.py", line 28, in <module>
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bme680.py", line 348, in __init__
self._i2c = i2c_device.I2CDevice(i2c, address)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_bus_device/i2c_device.py", line 76, in __init__
raise ValueError("No I2C device at address: %x" % device_address)
ValueError: No I2C device at address: 77
That is file read-all.py from folder /bme680/examples/
Installed from github
#!/usr/bin/env python
import bme680
import time
print("""read-all.py - Displays temperature, pressure, humidity, and gas.
Press Ctrl+C to exit!
""")
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:
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)
if sensor.data.heat_stable:
print('{0},{1} Ohms'.format(
output,
sensor.data.gas_resistance))
else:
print(output)
time.sleep(1)
except KeyboardInterrupt:
pass