To setup the MCU 4631 to receive and interrupt from the Blues.IO modem, on ATTNP (WB_IO5), I have configured the Blues module with {"req": "card.attn", "mode": "arm,signal"}
, and have setup the MCU’s interrupt handling on WB_IO5 for rising signals. I can’t get a reliable trigger. The interrupt handler appears to always be triggered.
What is wrong with this interrupt setup?
#include <Arduino.h>
#include <SPI.h>
#ifdef NRF52_SERIES
#include <Adafruit_TinyUSB.h>
#endif
#include <blues-minimal-i2c.h>
#define ATTN_INPUT_PIN WB_IO5
// Set to true whenever ATTN interrupt occurs
static bool attnInterruptOccurred;
//#include <SX126x-Arduino.h>
// Forward declarations
bool blues_hub_connected(void);
void blues_hub_status(void);
void startattnArm(void);
void resetattnArm(void);
void attnISR(void);
void attnArm();
// I2C functions for Blues NoteCard
RAK_BLUES rak_blues;
bool blues_connected = false;
char blues_response[2048];
long t;
/**
* @brief Arduino setup function
*
*/
void setup(void)
{
Serial.begin(115200);
time_t serial_timeout = millis();
// On nRF52840 the USB serial is not available immediately
while (!Serial)
{
if ((millis() - serial_timeout) < 5000)
{
delay(100);
digitalWrite(LED_GREEN, !digitalRead(LED_GREEN));
}
else
{
break;
}
}
// Initialize LoRa transceiver and send to sleep
#ifdef NRF52_SERIES
//lora_rak4630_init();
#endif
#ifdef ESP32
//lora_rak13300_init();
#endif
//Radio.Sleep();
// Initialize Blues Notecard interface
Wire.begin();
Wire.setClock(100000);
// Attach an interrupt pin
attnInterruptOccurred = false;
pinMode(ATTN_INPUT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(ATTN_INPUT_PIN), attnISR, RISING);
startattnArm();
t = millis();
}
/**
* @brief Arduino loop function
*
*/
void loop()
{
Serial.println("=============================");
// Check if Blues Notecard has cellular connection
blues_connected = blues_hub_connected();
if (blues_connected)
{
Serial.println("NoteCard reports it has cellular connection");
}
else
{
Serial.println("NoteCard reports it has no cellular connection");
}
if (millis() - t > 60000)
{
Serial.println("==========");
// Prepare a simple data packet
if (rak_blues.start_req((char *)"note.add"))
{
// Create data file
rak_blues.add_string_entry((char *)"file", (char *)"data.qo");
// Force sync
rak_blues.add_bool_entry((char *)"sync", true);
// Add some float values
rak_blues.add_nested_float_entry((char *)"body", (char *)"temp", 36.76);
rak_blues.add_nested_float_entry((char *)"body", (char *)"humid", 67);
rak_blues.add_nested_float_entry((char *)"body", (char *)"baro", 1024);
// Add connction status
if (blues_connected)
{
rak_blues.add_nested_string_entry((char *)"body", (char *)"status", (char *)"connected");
}
else
{
rak_blues.add_nested_string_entry((char *)"body", (char *)"status", (char *)"not connected");
}
Serial.println("Payload created");
if (!rak_blues.send_req())
{
Serial.println("Send request failed : 'note.add'");
}
else
{
Serial.println("Request sent to NoteCard");
}
}
else
{
Serial.println("Error creating the 'note.add' request");
}
t = millis();
}
// check interrupt flag
if (attnInterruptOccurred)
{
/**
J *req = NoteNewRequest("hub.signal");
NoteRequest(req);
if (req != NULL) {
J *body = JGetObject(req, "body");
if (body != NULL) {
J *control = JGetObject(body, "control");
if (control != NULL) {
Serial.println(JGetString(control, "value"));
}
}
}
**/
attnInterruptOccurred = false;
if (rak_blues.start_req((char *)"hub.signal"))
{
Serial.println("Payload created");
if (rak_blues.send_req(blues_response, 2048))
{
Serial.println("Got signal response");
Serial.printf("%s\n", blues_response);
}
else
{
Serial.println("Error signal request from NoteCard");
}
}
pinMode(ATTN_INPUT_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(ATTN_INPUT_PIN), attnISR, RISING);
//resetattnArm();
startattnArm();
}
Serial.println("======================================================");
// Get NoteCard status
blues_hub_status();
//delay(1000);
//Serial.println("\n");
}
/**
* @brief Check connection to cellular network
*
* @verbose
*
* @return true if connection is/was established
* @return false if no connection
*/
bool blues_hub_connected(void)
{
bool request_success = false;
bool cellular_connected = false;
for (int try_send = 0; try_send < 5; try_send++)
{
// Prepare "card.wireless" request
if (rak_blues.start_req((char *)"card.wireless"))
{
// Send request
if (rak_blues.send_req())
{
// Check if the response has an entry "net"
if (rak_blues.has_entry((char *)"net"))
{
// Check if the response has an entry "band"
if (rak_blues.has_nested_entry((char *)"net", (char *)"band"))
{
// If "band" is available, the NoteCard should have cellular connection
cellular_connected = true;
}
request_success = true;
break;
}
}
}
}
if (!request_success)
{
Serial.println("'card.wireless' request failed");
return false;
}
return cellular_connected;
}
/**
* @brief Request NoteHub status, only for debug purposes
*
*/
void blues_hub_status(void)
{
bool request_success = false;
for (int try_send = 0; try_send < 3; try_send++)
{
rak_blues.start_req((char *)"hub.status");
if (rak_blues.send_req(blues_response, 2048))
{
Serial.println("Notecard Hub status:");
Serial.printf("%s\n", blues_response);
request_success = true;
break;
}
}
if (!request_success)
{
Serial.println("'hub.status' request failed");
}
Serial.println("======================================================");
request_success = false;
for (int try_send = 0; try_send < 3; try_send++)
{
rak_blues.start_req((char *)"hub.get");
if (rak_blues.send_req(blues_response, 2048))
{
Serial.println("Notecard Hub setup:");
Serial.printf("%s\n", blues_response);
request_success = true;
break;
}
}
if (!request_success)
{
Serial.println("'hub.get' request failed");
}
Serial.println("======================================================");
request_success = false;
for (int try_send = 0; try_send < 3; try_send++)
{
rak_blues.start_req((char *)"card.wireless");
if (rak_blues.send_req(blues_response, 2048))
{
Serial.println("Notecard wireless status:");
Serial.printf("%s\n", blues_response);
request_success = true;
break;
}
}
if (!request_success)
{
Serial.println("'card.wireless' request failed");
}
Serial.println("======================================================");
}
// Interrupt Service Routine for ATTN_INPUT_PIN transitions rising from LOW
// to HIGH
void attnISR()
{
attnInterruptOccurred = true;
}
// Re-arm the interrupt
void attnArm()
{
// Make sure that we pick up the next RISING edge of the interrupt
attnInterruptOccurred = false;
// Set the ATTN pin low, and wait for the earlier of file modification or
// a timeout
//J *req = notecard.newRequest("card.attn");
//JAddStringToObject(req, "mode", "reset");
//JAddNumberToObject(req, "seconds", 120);
//notecard.sendRequest(req);
}
void resetattnArm()
{
// Setup signal interrupts
if (rak_blues.start_req((char *)"card.attn"))
{
// Create data file
rak_blues.add_string_entry((char *)"mode", (char *)"reset");
Serial.println("Payload created");
if (!rak_blues.send_req())
{
Serial.println("Send request failed : 'card.attn'");
}
else
{
Serial.println("Request card attn sent to NoteCard");
}
}
else
{
Serial.println("Error creating the 'card.attn' request");
}
}
void startattnArm()
{
// Setup signal interrupts
if (rak_blues.start_req((char *)"card.attn"))
{
// Create data file
rak_blues.add_string_entry((char *)"mode", (char *)"arm,signal");
Serial.println("Payload created");
if (!rak_blues.send_req())
{
Serial.println("Send request failed : 'card.attn'");
}
else
{
Serial.println("Request card attn sent to NoteCard");
}
}
else
{
Serial.println("Error creating the 'card.attn' request");
}
}