Advice needed on best way to deal with intermittent user input

Hi,

I’m currently undertaking a project which uses infrared to find out a persons general location within a building for example in the kitchen or in the living room, and can relay that information to a purpose built application on request.

The person to be tracked will be wearing a wrist band which has an IR LED which emits a modulated signal every 15 seconds. An IR receiver, which is fixed in some position within the room, will then receive and decode the signal to determine which room the person is located in. I am also going to build an app which can be used by someone who is not in the house to check the most up to date location of the person in the house. I am still at an early stage in this project and as such I have created a basic app using MIT app inventor which uses Bluetooth to send data between the fixed position IR receiver and the app.

The issue I’m currently having is that I’m not sure on the best way to deal with the intermittent requests that come from the app while still polling for new IR signals to decode. Ideally, a request from the app to send the most recent location should be dealt with as soon as it comes in whereas at the moment the I need to wait until the next time the IR receiver has processed an incoming signal which could be up to 15 seconds from when the request was made.

I have had a look at some of the forum posts about doing several things at once, interrupts and FreeRTOS but with little experience I’m finding it difficult to choose the most suitable option and/or I am unaware of what other options might be open to me. I would greatly appreciate your input on this!

My current setup and code as as follow:

IR Transmitter
I have an IR LED hooked up to Adafruits Huzzah32 (ESP32) which emits a modulated signal via using IR every 15 seconds. This uses the following code:

//===================================
//---------Libraries-----------------
#include <IRremote.h>

//===================================
//---------Constants-----------------
const long interval = 15000; //Send modulated signal every 15s

//===================================
//---------Variables-----------------
unsigned long previousMillis = 0;

//===================================

IRsend irsend;

//===================================

void setup() {
}

void loop() {

  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    //Modulated signal for Living Room
    irsend.sendNEC(0x005a6b23,32);
  }
}

IR Receiver
I have an IR receiver hooked up to a different Adafruit Huzzah32 (ESP32) which polls for infrared signals to decode and then sets a location flag based on the IR signal received.

Additionally, I have some code which polls for requests from the basic app to send the most recent data location. When a button is pressed on the app it sends a ‘1’ to the ESP32 via Bluetooth and the ESP32 then serial prints the last known location to the app via Bluetooth.

The code for the receiver is:

//===================================
//---------Libraries-----------------
#include <IRremote.h>
#include "BluetoothSerial.h"

//===================================
//---------Check Bluetooth-----------
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


//===================================
//---------Constants-----------------
const int RECV_PIN = 4;


//===================================
//---------Variables-----------------
int LocationData;
char LocationDataRequest;

//===================================

IRsend irsend;
IRrecv irrecv(RECV_PIN);
decode_results results;
BluetoothSerial SerialBT;

//==================================

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!"); //Print to serial monitor (for my benefit)
  irrecv.enableIRIn(); // Start the receiver
}

//==================================

void loop() {

  //Receive & decode IR signals
  if (irrecv.decode(&results)) {
    if (results.value == 0x005a6b23) {
      LocationData = 1;
      irrecv.resume();
    }
    else {  //This is a proxy for another IR signal to be added in the future.
      LocationData = 2;
      irrecv.resume();
    }
  }

  //Deal with Location Data request from app
  if (SerialBT.available() > 0) {
    LocationDataRequest = SerialBT.read();
    Serial.println(LocationDataRequest);  //Print to serial monitor (for my benefit)
    if (LocationDataRequest == '1' && LocationData == 1) {
      SerialBT.println("Last known location: Living Room"); //Send last know location to app
      LocationData = 0; //Reset location data
      LocationDataRequest = 0; //Reset Location Data Request
      irrecv.resume(); //Resume 
    }
    else if (LocationDataRequest == '1' && LocationData == 2) {
      SerialBT.println("Out of sight \r\n"); //Send last know location to app
      LocationData = 0; //Reset location data
      LocationDataRequest = 0; //Reset Location Data Request
      irrecv.resume();
    }
    else {
      irrecv.resume();
    }
  }
  else {
    irrecv.resume();
  }
  
}

In general, the system is working ok. An IR signal is sent every 15 seconds from 1 ESP32 and is received and decoded on the other and the location based on the IR signal is being assigned as it should. When a button is pressed on the app a ‘1’ is sent to the IR receiver and then the data location is sent to the app (eventually).
The only issue I currently have is not knowing the best way to deal with a request from the app as detailed above.

Thanks to anyone who has made it this far, I hope I provided enough detail and I appreciate the help of anyone willing to give it!

Upon a request for position the last known position ought to be the only answer.

Wow ye, that's much simpler. It seems to be printing to the app as soon as I send the request now as well! Thanks for your input.

My pleasure.

You may also find my tutorial on Multi-tasking in Arduino useful

Thank you!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.