AM2320 temperature sensor reading NaN with WiFi connection

Hi guys

I have my sketch running fine in my Nano 33 via USB showing temperature readings in the Serial Monitor from an AM2320 I2C sensor connected to the board.

When I connect the board to the Arduino IoT Cloud from my WiFi connection (I power the board via USB connected to my computer) a NaN readings begins. I have these NaN readings in my Serial Monitor and in the IoT Cloud Dashboard. More specifically, I have one correct temperature reading (only the first one) follow by NaN readings. You can see an example of the output in the Serial in the jpg attached.

Follow code:

  Sketch generated by the Arduino IoT Cloud Thing "SensorT" 

  Arduino IoT Cloud Properties description

  The following variables are automatically generated and updated when changes are made to the Thing properties

  float temperature;

  Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.

#include "arduino_secrets.h"
#include "thingProperties.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_AM2320.h"

Adafruit_AM2320 am2320 = Adafruit_AM2320();

void setup() {
  // Initialize serial and wait for port to open:
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found

  // Defined in thingProperties.h

  // Connect to Arduino IoT Cloud
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4

void loop() {
  // Your code here 
  temperature = am2320.readTemperature();

Anyone with similar issue?

Thank you in advance.

A little bit of information from my attemps...
If I comment the sentence ArduinoCloud.update() everything works fine and readings begin to show in the Serial.
I have checked too that my dashboard is receiving data from my board but with NaN readings of course.
Any help?

Can you read the sensor into a separate variable and print that? Then copy the variable into the temperature variable. This will show whether there is an issue with the sensor library or the variable update.

I also recommend to remove the delay and control the timing with millis() timer. Have a look at the following example.

File -> Examples -> 02.Digital -> BlinkWithoutDelay

It is not a good idea to use delay in general but especially with communication stacks.

Hi Klaus_K and thank you for having a look to my code...

Asnwering your question, no, I do not have correct readings in a separate variable while connected to the Cloud. You can see in the picture attached two values per row printed, the first one is the reading in the separate variable and the second one is the IoT "temperature" variable once copied from the first one. Correct readings only comes without Arduinocloud.update() function.

I have checked the code with other AM2320 library and I have the same problem (wrong values in this case).

Note: I know problems with delay() function and I have already run code with and without this function, same NaN readings. This dont seems to be the problem.

void loop() {


  // Your code here  
  float tmp;
  tmp = am2320.readTemperature();
  temperature = tmp;

The NaN comes from the AM2320 library. It is created by the readTemperature() function when it receives a 0xFFFF from the readRegister16() function. This function returns the 0xFFFF value as an error code for multiple conditions.

Have a look into the Adafruit_AM2320.cpp file. There are some prints in the readRegister16() function which are commented out. You can enable them to see what is happening.