0

Success! BME280 to AIO via BLE and wifi
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Success! BME280 to AIO via BLE and wifi

by Rcayot on Sat Mar 06, 2021 12:16 pm

well, I have found a path to success. The project is to have an itsybitsy nRF52840 attached to a bme280 sensor, send the data to a Pi Zero W and then on to AIO via wifi.

The code is based loosly on examples found in the aio client libraries. The bme280 sensor library works really well and is easily implemented in Circuit Python.

The Pi Zero W code works well with Circuit Python via Blinka libraries.

Ok, so what I did was use the uarteval program example on the nRF562840 to 'evaluate' the incoming string which was a request for senso data. The 'evaluart' excample on the Pi Zero uses a send a string which contains the request for sensor data. the response is then sent on to aio.

Here is the Pi Zero W code:

Code: Select all | TOGGLE FULL SIZE
# pibleuart_ble280
# Connect to an "eval()" service over BLE UART.
# have nRF52840 evaluate temperature, humidity and pressure and return them to RPI bia BLE.
# Rpi will send data to aio via wifi
# Roger Ayotte March 5, 2021
# based on the eval example

# Connect to an "eval()" service over BLE UART.
import time
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService

ble = BLERadio()
uart_connection = None

from Adafruit_IO import Client, Feed, Data, RequestError
import datetime

# Set to your Adafruit IO key.
# Remember, your key is a secret,
# so make sure not to publish it when you publish this code!
ADAFRUIT_IO_KEY = 'MY KEYj'

# Set to your Adafruit IO username.
# (go to https://accounts.adafruit.com to find your username).
ADAFRUIT_IO_USERNAME = 'MY USERNAME'

# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Set up Adafruit IO Feeds.
temperature_feed = aio.feeds('temperature')
humidity_feed = aio.feeds('humidity')
pressure_feed = aio.feeds('pressure')

def get_and_send_temperature():
    s = "bme280.temperature"
    uart_service.write(s.encode("utf-8"))
    uart_service.write(b'\n')
    temperature = uart_service.readline().decode("utf-8")
    print(temperature)
    aio.send(temperature_feed.key, str(temperature))
   
def get_and_send_humidity():
    s = "bme280.humidity"
    uart_service.write(s.encode("utf-8"))
    uart_service.write(b'\n')
    humidity = uart_service.readline().decode("utf-8")
    print(humidity)
    aio.send(humidity_feed.key, str(humidity))

def get_and_send_pressure():
    s = "bme280.pressure"
    uart_service.write(s.encode("utf-8"))
    uart_service.write(b'\n')
    pressure = uart_service.readline().decode("utf-8")
    print(pressure)
    aio.send(pressure_feed.key, str(pressure))

while True:
    now = time.monotonic()
    if not uart_connection:
        print("Trying to connect...")
        for adv in ble.start_scan(ProvideServicesAdvertisement):
            if UARTService in adv.services:
                uart_connection = ble.connect(adv)
                print("Connected")
                break
        ble.stop_scan()

    if uart_connection and uart_connection.connected:
        uart_service = uart_connection[UARTService]
        while uart_connection.connected:
            get_and_send_temperature()
            time.sleep(2)
            get_and_send_humidity()
            time.sleep(2)
            get_and_send_pressure()
            time.sleep(300)
           
           


here is the itsybitsy nRF52840 code:

Code: Select all | TOGGLE FULL SIZE
# Provide an "eval()" service over BLE UART.

from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
import time
import board
import busio
import adafruit_bme280

ble = BLERadio()
uart = UARTService()
advertisement = ProvideServicesAdvertisement(uart)

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)


while True:
    ble.start_advertising(advertisement)
    print("Waiting to connect")
    while not ble.connected:
        pass
    print("Connected")
    while ble.connected:
        s = uart.readline()
        if s:
            try:
                result = (eval(s))
            except Exception as e:
                result = repr(e)
            uart.write(str(result))

Rcayot
 
Posts: 148
Joined: Sat Feb 08, 2020 6:48 pm

Re: Success! BME280 to AIO via BLE and wifi

by jwcooper on Sun Mar 07, 2021 11:22 am

Congrats! Also, thank you for posting your example code once you found the solution. Your solution will likely also help others in the future.

jwcooper
 
Posts: 785
Joined: Tue May 01, 2012 9:08 pm

Please be positive and constructive with your questions and comments.