RadioLib/examples/SX127x/SX127x_Transmit_Interrupt/SX127x_Transmit_Interrupt.ino

132 wiersze
3.6 KiB
Arduino
Czysty Zwykły widok Historia

2018-08-02 13:11:38 +00:00
/*
RadioLib SX127x Transmit with Interrupts Example
2018-08-02 13:11:38 +00:00
This example transmits LoRa packets with one second delays
between them. Each packet contains up to 255 bytes
of data, in the form of:
- Arduino String
- null-terminated char array (C-string)
- arbitrary binary data (byte array)
2018-08-02 13:11:38 +00:00
Other modules from SX127x/RFM9x family can also be used.
2019-06-02 13:03:33 +00:00
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
2018-08-02 13:11:38 +00:00
*/
// include the library
2019-02-08 14:58:29 +00:00
#include <RadioLib.h>
2018-08-02 13:11:38 +00:00
2019-06-02 13:03:33 +00:00
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
2019-06-02 13:03:33 +00:00
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
2019-06-02 13:03:33 +00:00
// or using RadioShield
// https://github.com/jgromes/RadioShield
//SX1278 radio = RadioShield.ModuleA;
2018-08-02 13:11:38 +00:00
2019-09-17 06:38:31 +00:00
// save transmission state between loops
2021-11-14 10:42:12 +00:00
int transmissionState = RADIOLIB_ERR_NONE;
2019-09-17 06:38:31 +00:00
2018-08-02 13:11:38 +00:00
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
2019-02-10 10:19:50 +00:00
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.begin();
2021-11-14 10:42:12 +00:00
if (state == RADIOLIB_ERR_NONE) {
2018-08-02 13:11:38 +00:00
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
2019-05-24 12:32:07 +00:00
// set the function that will be called
2018-08-02 13:11:38 +00:00
// when packet transmission is finished
2023-06-21 20:03:07 +00:00
radio.setPacketSentAction(setFlag);
2018-08-02 13:11:38 +00:00
// start transmitting the first packet
2019-02-10 10:19:50 +00:00
Serial.print(F("[SX1278] Sending first packet ... "));
2018-08-02 13:11:38 +00:00
// you can transmit C-string or Arduino string up to
// 255 characters long
transmissionState = radio.startTransmit("Hello World!");
2018-08-02 13:11:38 +00:00
// you can also transmit byte array up to 255 bytes long
2018-08-02 13:11:38 +00:00
/*
2020-03-22 07:13:27 +00:00
byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
transmissionState = radio.startTransmit(byteArr, 8);
2018-08-02 13:11:38 +00:00
*/
}
2019-09-17 06:38:31 +00:00
// flag to indicate that a packet was sent
2018-08-02 13:11:38 +00:00
volatile bool transmittedFlag = false;
2019-09-17 06:38:31 +00:00
// this function is called when a complete packet
// is transmitted by the module
// IMPORTANT: this function MUST be 'void' type
// and MUST NOT have any arguments!
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
2018-08-02 13:11:38 +00:00
void setFlag(void) {
2019-09-17 06:38:31 +00:00
// we sent a packet, set the flag
2018-08-02 13:11:38 +00:00
transmittedFlag = true;
}
2023-06-24 20:24:49 +00:00
// counter to keep track of transmitted packets
int count = 0;
2018-08-02 13:11:38 +00:00
void loop() {
// check if the previous transmission finished
if(transmittedFlag) {
2019-09-17 06:38:31 +00:00
// reset flag
transmittedFlag = false;
2021-11-14 10:42:12 +00:00
if (transmissionState == RADIOLIB_ERR_NONE) {
2019-09-17 06:38:31 +00:00
// packet was successfully sent
Serial.println(F("transmission finished!"));
2018-08-02 13:11:38 +00:00
2019-09-17 06:38:31 +00:00
// NOTE: when using interrupt-driven transmit method,
// it is not possible to automatically measure
// transmission data rate using getDataRate()
} else {
Serial.print(F("failed, code "));
Serial.println(transmissionState);
}
2022-09-18 14:13:39 +00:00
// clean up after transmission is finished
// this will ensure transmitter is disabled,
// RF switch is powered down etc.
radio.finishTransmit();
2019-09-17 06:38:31 +00:00
// wait a second before transmitting again
2018-08-02 13:11:38 +00:00
delay(1000);
2019-09-17 06:38:31 +00:00
// send another one
2019-02-10 10:19:50 +00:00
Serial.print(F("[SX1278] Sending another packet ... "));
2018-08-02 13:11:38 +00:00
// you can transmit C-string or Arduino string up to
// 255 characters long
String str = "Hello World! #" + String(count++);
transmissionState = radio.startTransmit(str);
2019-05-24 12:32:07 +00:00
// you can also transmit byte array up to 255 bytes long
2018-08-02 13:11:38 +00:00
/*
2020-03-22 07:13:27 +00:00
byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF};
transmissionState = radio.startTransmit(byteArr, 8);
2018-08-02 13:11:38 +00:00
*/
}
}