1

I'd successfully got my Nano 33 BLE Sense to read accelerometer data and subject to there being a difference between current and last readings, to send this via the EasyTransfer library to an XBee module over hardware serial that communicated with another module which published the readings to the AWS Cloud via a Nano IoT. I then ambitiously decided I'd add more sensor data to the payload to include pressure, temperature and humidity. I added these floats to my data structure (see code below) as well as Boolean flags to indicate if there had been an event (i.e. a reading over a threshold) and if any one of the flags was true, to transmit the payload. The code compiles fine, uploads to complete but then the LED on the Nano flashes 3 times slowly and 5 times quickly in succession. I had this last time because I'd forgotten a .begin() statement but adding this fixed the issue. However, I've gone through my code and can't see what is causing the issue this time. I have run smaller code units to check the sensors are working which they are. Help!!

#include <EasyTransfer.h>
#include <Arduino_LSM9DS1.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_HTS221.h>

#define TX_PIN 6
#define RX_PIN 5

const float MOTION_TOLERANCE = 0.01;
const float PRESSURE_THRESHOLD = 1000.00;
const float TEMPERATURE_THRESHOLD = 20.00;
const float HUMIDITY_THRESHOLD = 50.00;
 
UART XBeeTx(digitalPinToPinName(TX_PIN), digitalPinToPinName(RX_PIN), NC, NC);

EasyTransfer ET;

struct SEND_DATA_STRUCTURE {

  String sensorLocation;
  bool motionEvent;
  float xAcc;
  float yAcc;
  float zAcc;
  bool pressureEvent;
  float pressure;
  bool temperatureEvent;
  float temperature;
  bool humidityEvent;
  float humidity;
    
};

SEND_DATA_STRUCTURE myData;

float xAccCurrent, yAccCurrent, zAccCurrent, xAccLast, yAccLast, zAccLast, pressureReading, temperatureReading, humidityReading;

// ================================================================
// ===                      INITIAL SETUP                       ===
// ================================================================

void setup() {

if(!IMU.begin()) {

  // Stop if IMU fails to initialise
  while(1);
  
}

if(!BARO.begin()) {

    // Stop if pressure sensor fails to initialise
  while(1);
  
}
if (!HTS.begin()) {

  // Stop if temp and humidity sensor fails to initialise
  while(1);
  
}

 // Accelerometer code
   IMU.setAccelFS(3);
   IMU.setAccelODR(5);
   IMU.setAccelOffset(0.003955, -0.010136, -0.013462);
   IMU.setAccelSlope (0.997163, 1.000438, 0.995678);


if (XBeeTx.available()) {
  
  XBeeTx.begin(9600);
  ET.begin(details(myData), &XBeeTx);
  
  }

   myData.sensorLocation = "Zone 1";

}


// ================================================================
// ===                    MAIN PROGRAM LOOP                     ===
// ================================================================

void loop() {

if (IMU.accelerationAvailable()) {
  
    IMU.readAcceleration(xAccCurrent, yAccCurrent, zAccCurrent);    

  }

 
if ((abs(xAccLast -xAccCurrent) > MOTION_TOLERANCE) || (abs(yAccLast -yAccCurrent) > MOTION_TOLERANCE) || (abs(zAccLast -zAccCurrent) > MOTION_TOLERANCE)) {
 
  myData.motionEvent = true;
  myData.xAcc = xAccCurrent;
  myData.yAcc = yAccCurrent;
  myData.zAcc = zAccCurrent;

  xAccLast = xAccCurrent;
  yAccLast = yAccCurrent;
  zAccLast = zAccCurrent;
    
}

pressureReading = BARO.readPressure(MILLIBAR);
if (pressureReading > PRESSURE_THRESHOLD) {

  myData.pressureEvent = true;
  myData.pressure = pressureReading;
  
}

temperatureReading = HTS.readTemperature();
if (temperatureReading > TEMPERATURE_THRESHOLD) {

  myData.temperatureEvent = true;
  myData.temperature = temperatureReading;
  
}

humidityReading = HTS.readHumidity();
if (humidityReading > HUMIDITY_THRESHOLD) {

  myData.humidityEvent = true;
  myData.humidity = humidityReading;
  
}

if (myData.motionEvent || myData.pressureEvent || myData.temperatureEvent || myData.humidityEvent) {


 ET.sendData();

 delay(1000);
 
 myData.motionEvent = false;
 myData.pressureEvent =false;
 myData.temperatureEvent = false;
 myData.humidityEvent =false;
  
}

}
7
  • Add serial prints to find out how long each part of the code takes – Sim Son Apr 26 at 13:49
  • Hi, I could do the board is locking up so the serial monitor doesn't load. Oddly if I comment out the if (XBeeTx.available()) and just begin() the XBeeTx and ET then it seems to load fine and I can see from the Xbee LEDs that data is being sent by one and received by the other. So not sure why the XBeeTx.available() isn't triggering the right outcome - it obviously is available otherwise I couldn't begin it? – RobM Apr 26 at 14:40
  • K, I have no experience with your hardware. Is 9600 the correct baudrate? – Sim Son Apr 26 at 16:39
  • 1
    Are you sure that calling .begin() after .available() is correct? Seems odd to me. – Sim Son Apr 26 at 16:41
  • I've got the code loading ok and the Xbees are obviously communcating but the nano board doesn't seem to be getting any data. Looking at the GitHub docs for the EasyTransfer library there is some mentioned of a limit of 255 bytes for the struct() so I may have hit that issue but I have the same problem if my struct is just a string and 3 floats. Worlds fine with 3 floats but adding the string seems to cause a problem. odd! – RobM Apr 27 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.