I have a ble280 sensor that I connected to my Raspberry Pi Zero W which read the data, and sent it to AIO, ran for about a month with few problems. This was indoors, while on my desk.
I decided that I wanted the sensor outside, and that I wanted it to be battery powered and the Pi and wifi consumes more power than I wanted to deal with, so I rewrote the program to run on my itsybitsy nRF52840. Well, after a few mis-steps, I got the dode to run and send the temperature, humidity, and pressure data to my raspberry Pi zero W over BLE. The issue then was that sending that data forward to AIO was giving me fits. Thsi is the error I get:
"Adafruit_IO.errors.RequestError: Adafruit IO request failed: 422 Unprocessable Entity - request failed - Record invalid. Failed to save data to MYUSERNAME/feeds/temperature, data missing required value.
Now here is the send to AIO portion of the code that worked:
- Code: Select all | TOGGLE FULL SIZE
while True:
humidity = bme280.relative_humidity
temperature = bme280.temperature
pressure = bme280.pressure
if humidity is not None and temperature is not None and pressure is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}% Pressure={2:0.1f}hPa'.format(temperature, humidity, pressure))
# Send humidity and temperature feeds to Adafruit IO
temperature = '%.2f'%(temperature)
humidity = '%.2f'%(humidity)
pressure = '%.2f'%(pressure)
aio.send(temperature_feed.key, str(temperature))
aio.send(humidity_feed.key, str(humidity))
aio.send(pressure_feed.key, str(pressure))
time.sleep(300)
for whatever reason, the formating lines " temperature = '%.2f'%(temperature)" are required, and obtained from example code and I have an idea it is a formatting statement.
Now, when I send the data over BLE to the PI Zero W, I use:
- Code: Select all | TOGGLE FULL SIZE
while True:
ble.start_advertising(advertisement)
print("Waiting to connect")
while not ble.connected:
pass
print("Connected")
while ble.connected:
time.sleep(5)
temperature = bme280.temperature
uart.write("%0.2f " % temperature)
time.sleep(5)
humidity = bme280.relative_humidity
uart.write("%0.2f " % humidity)
time.sleep(5)
pressure = bme280.pressure
uart.write("%0.1f " % pressure)
time.sleep(300)
This is recieved by the Pi zero W using the following:
- Code: Select all | TOGGLE FULL SIZE
while True:
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:
temperature = uart_service.readline().decode("utf-8")
#temperature = '%.2f'%(temperature)
print(temperature)
#aio.send(temperature_feed.key, str(temperature))
humidity = uart_service.readline().decode("utf-8")
#humidity = '%.2f'%(humidity)
print(humidity)
aio.send(humidity_feed.key, str(humidity))
pressure = uart_service.readline().decode("utf-8")
#pressure = '%.2f'%(pressure)
print(pressure)
aio.send(pressure_feed.key, str(pressure))
Now, If I comment out the aio.send commands, the data is recieved by the Pi and prints. However, if I uncomment the aio.send commands, I get the errors noted above. Now, the AIO portions of the two programs are identical, as I copied from one to the other. I have tried to include or exclude the formatting code: #temperature = '%.2f'%(temperature) without success. I have tried a number of other things without avail, one error is that the value has to be a real number not a string, things like that.
Any ideas that can help would be appreciated. I would include the entire code, buyt we would have three fairly long pieces of code here and might be too cumbersome, but anything anyone needs to help, I will include.
Thanks,
Roger Ayotte