Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 212b9ce

Browse files
committedDec 9, 2024·
enabled recieved messages from the lorawan that control the state of the on-board LED
1 parent 99b10d2 commit 212b9ce

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed
 

‎sfeIoTNodeLoRaWAN/flxLoRaWANDigi.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ const uint32_t kProcessMessagesTime = 2000;
4242
static void OnReceiveCallback(void *data)
4343
{
4444
XBeeLRPacket_t *packet = (XBeeLRPacket_t *)data;
45+
46+
// If the packet is on channel two and size of 4, let's post an event
47+
if (packet->port == 2 && packet->payloadSize <= 4)
48+
{
49+
uint32_t message = 0;
50+
memcpy(&message, packet->payload, packet->payloadSize);
51+
52+
// Post an event
53+
flxSendEvent(flxEvent::kLoRaWANReceivedMessage, message);
54+
return;
55+
}
4556
Serial.print("Received Packet: ");
4657
for (int i = 0; i < packet->payloadSize; i++)
4758
{
@@ -303,7 +314,7 @@ bool flxLoRaWANDigi::connect(void)
303314
}
304315
// We are connected -- make sure the class is set.
305316
if (!_pXBeeLR->setLoRaWANClass('C'))
306-
flxLog_I(F("%s: Failed to set the LoRaWAN Class"), name());
317+
flxLog_D(F("%s: Failed to set the LoRaWAN Class"), name());
307318

308319
flxSerial.textToGreen();
309320
flxLog_N(F("Connected!"));

‎sfeIoTNodeLoRaWAN/flxLoRaWANDigi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
// Setup an event
2121
flxDefineEventID(kLoRaWANSendStatus);
22+
// Event for received messages
23+
flxDefineEventID(kLoRaWANReceivedMessage);
2224

2325
class flxLoRaWANDigi : public flxActionType<flxLoRaWANDigi>
2426
{

‎sfeIoTNodeLoRaWAN/sfeIoTNodeLoRaWAN.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ static const char *kProductName = "SparkFun IoT Node LoRaWAN";
2525
// delay used in loop during startup
2626
const uint32_t kStartupLoopDelayMS = 70;
2727

28+
//---------------------------------------------------------------------------
29+
// LoRaWAN Receive message ids
30+
//
31+
// Set the on-board LED to the RGB value in the message
32+
const uint8_t kLoRaWANMsgLEDRGB = 0x01;
33+
34+
// Turn off the on-board LED
35+
const uint8_t kLoRaWANMsgLEDOff = 0x02;
36+
37+
// Set the on-board LED to blink with the given RGB color
38+
const uint8_t kLoRaWANMsgLEDBlink = 0x03;
39+
40+
// Set the on-board LED to fast blink with the given RGB color
41+
const uint8_t kLoRaWANMsgLEDFastBlink = 0x04;
42+
43+
// Set the on-board LED to flash with the given RGB color
44+
const uint8_t kLoRaWANMsgLEDFlash = 0x05;
45+
46+
//---------------------------------------------------------------------------
47+
2848
// Application keys - used to encrypt runtime secrets for the app.
2949
//
3050
// NOTE: Gen a base 64 key % openssl rand -base64 32
@@ -265,6 +285,9 @@ bool sfeIoTNodeLoRaWAN::onSetup()
265285
// Event Callback for lorawan send status
266286
flxRegisterEventCB(flxEvent::kLoRaWANSendStatus, this, &sfeIoTNodeLoRaWAN::onLoRaWANSendEvent);
267287

288+
// Event Callback for lorawan receive status
289+
flxRegisterEventCB(flxEvent::kLoRaWANReceivedMessage, this, &sfeIoTNodeLoRaWAN::onLoRaWANReceiveEvent);
290+
268291
// Set the default timer interval, before restore of settings
269292
_timer.interval = kDefaultLogInterval;
270293

@@ -542,7 +565,41 @@ void sfeIoTNodeLoRaWAN::onLoRaWANSendEvent(bool bOkay)
542565
else
543566
sfeLED.flash(sfeLED.Red);
544567
}
568+
//---------------------------------------------------------------------------
569+
// Callback for LoRaWAN receive events
570+
void sfeIoTNodeLoRaWAN::onLoRaWANReceiveEvent(uint32_t data)
571+
{
572+
573+
// flxLog_I("LoRaWAN Received Event: 0x%0.8X", data);
574+
575+
uint8_t *pData = (uint8_t *)&data;
576+
577+
// We basically update/change the state of the on-board LED
578+
sfeLEDColor_t color;
579+
switch (pData[0])
580+
{
581+
case kLoRaWANMsgLEDRGB:
582+
color = pData[1] << 16 | pData[2] << 8 | pData[3];
583+
sfeLED.on(color);
584+
break;
545585

586+
case kLoRaWANMsgLEDOff:
587+
sfeLED.off();
588+
break;
589+
case kLoRaWANMsgLEDBlink:
590+
color = pData[1] << 16 | pData[2] << 8 | pData[3];
591+
sfeLED.blink(color, 1000);
592+
break;
593+
case kLoRaWANMsgLEDFastBlink:
594+
color = pData[1] << 16 | pData[2] << 8 | pData[3];
595+
sfeLED.blink(color, 500);
596+
break;
597+
case kLoRaWANMsgLEDFlash:
598+
color = pData[1] << 16 | pData[2] << 8 | pData[3];
599+
sfeLED.flash(color);
600+
break;
601+
}
602+
}
546603
//---------------------------------------------------------------------------
547604
// checkBatteryLevels()
548605
//

‎sfeIoTNodeLoRaWAN/sfeIoTNodeLoRaWAN.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class sfeIoTNodeLoRaWAN : public flxApplication
113113
void onLogEvent(void);
114114
void onQwiicButtonEvent(bool);
115115
void onLoRaWANSendEvent(bool);
116+
void onLoRaWANReceiveEvent(uint32_t);
116117

117118
// support for onInit
118119
void onInitStartupCommands(uint32_t);

0 commit comments

Comments
 (0)