beegee-tokyo / SX126x-Arduino

Arduino library to use Semtech SX126x LoRa chips and modules to communicate
MIT License
226 stars 64 forks source link

Timeout Waiting For BUSY Now #51

Closed Muuhoooo closed 2 years ago

Muuhoooo commented 2 years ago

Hi Bernd, I hope you're fine. I try send "Helllo" over LoRaWAN wşth ESP32. But I saw serial "timeout waiting for BUSY now". I defined txen and rxen. And I send picture and LoraWAN settings. What should i change, this is the first time i saw such an error. ADR : Disabled Datarate : DR_2 Duty cycles : Enabled Region : LORAMAC_REGION_EU868 LORAWAN_APP_INTERVAL 20000 LMH_UNCONFIRMED_MSG;

Ekran Alıntısı

beegee-tokyo commented 2 years ago

@Muuhoooo You closed it, so I guess you solved it.

Timeout waiting for busy is caused by the SPI communication not working or Busy pin of SX1262 not connected or defined incorrect.

Muuhoooo commented 2 years ago

Hi Bernd, I saw another issue and I tried but did not solved. I checked the spi and all the other pins connected to the sx1262 with a multimeter.All of them are all connected and defined in my code. Again, an interesting error could not make sense.

//#ifdef ESP32 // ESP32 - SX126x pin configuration int PIN_LORA_RESET = 32; // LORA RESET int PIN_LORA_NSS = 15; // LORA SPI CS int PIN_LORA_SCLK = 14; // LORA SPI CLK int PIN_LORA_MISO = 12; // LORA SPI MISO int PIN_LORA_DIO_1 = 4; // LORA DIO_1 int PIN_LORA_BUSY = 33; // LORA SPI BUSY int PIN_LORA_MOSI = 13; // LORA SPI MOSI int RADIO_TXEN = 2; int RADIO_RXEN = 25;

// Define the HW configuration between MCU and SX126x hwConfig.CHIP_TYPE = SX1262_CHIP; // Example uses an eByte E22 module with an SX1262 hwConfig.PIN_LORA_RESET = PIN_LORA_RESET; // LORA RESET hwConfig.PIN_LORA_NSS = PIN_LORA_NSS; // LORA SPI CS hwConfig.PIN_LORA_SCLK = PIN_LORA_SCLK; // LORA SPI CLK hwConfig.PIN_LORA_MISO = PIN_LORA_MISO; // LORA SPI MISO hwConfig.PIN_LORA_DIO_1 = PIN_LORA_DIO_1; // LORA DIO_1 hwConfig.PIN_LORA_BUSY = PIN_LORA_BUSY; // LORA SPI BUSY hwConfig.PIN_LORA_MOSI = PIN_LORA_MOSI; // LORA SPI MOSI hwConfig.RADIO_TXEN = RADIO_TXEN; // LORA ANTENNA TX ENABLE hwConfig.RADIO_RXEN = RADIO_RXEN; // LORA ANTENNA RX ENABLE

Muuhoooo commented 2 years ago

I tried new SX1262 and ESP32 and I checked all pins and same result. "Timeout Waiting For BUSY now"

beegee-tokyo commented 2 years ago

Can you retry your LoRa P2P test that you mentioned earlier.

I have an ESP32 working with an SX1262 (not eByte) without problems using LoRaWAN and sending packets. It's up since weeks. And another ESP32 connected to the same eByte module working on LoRa P2P working since months.

I just cannot take them out for tests, because they are part of my home control system.

So I am not sure how to solve your problem, but I am quite sure it is not the library.

Muuhoooo commented 2 years ago

Okey understand. Thank you very much for your help.

Muuhoooo commented 2 years ago

And LoRa P2P test is successful. I have seen serial screen "Hello World".

beegee-tokyo commented 2 years ago

Digged in my "old devices cabinet" and found my Adafruit ESP32 Feather and the Alora1262 Feather (which has the eByte module on it): image image

Log output on the USB:

mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
=====================================
SX126x LoRaWan test
=====================================
Start Join request
OVER_THE_AIR_ACTIVATION != 0
Sending frame
Data: {"i":1,"n":000}
Size: 15
Port: 2
lmh_send result 0
Uncomfirmed TX finished
Sending frame
Data: {"i":1,"n":001}
Size: 15
Port: 2
lmh_send result 0
Uncomfirmed TX finished
Sending frame
Data: {"i":1,"n":002}
Size: 15
Port: 2
lmh_send result 0

Log from Chirpstack LNS: image

My code:

#include <Arduino.h>

#include <LoRaWan-Arduino.h>
#include <SPI.h>
#include <Ticker.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
WiFiMulti wifiMulti;

// #define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */
// #define SCHED_QUEUE_SIZE 60                                        /**< Maximum number of events in the scheduler queue. */

#define LORAWAN_APP_DATA_BUFF_SIZE 256  /**< Size of the data to be transmitted. */
#define LORAWAN_APP_TX_DUTYCYCLE 30000 /**< Defines the application data transmission duty cycle. 30s, value in [ms]. */
#define APP_TX_DUTYCYCLE_RND 1000     /**< Defines a random delay for application data transmission duty cycle. 1s, value in [ms]. */
#define JOINREQ_NBTRIALS 3             /**< Number of trials for the join request. */

hw_config hwConfig;

// ESP32 Feather - SX126x pin configuration
int PIN_LORA_RESET = 32; //16; // LORA RESET
int PIN_LORA_DIO_1 = 14; //14; // LORA DIO_1
int PIN_LORA_BUSY = 27;  //33; // LORA SPI BUSY
int PIN_LORA_NSS = 33;   //27; // LORA SPI CS
int PIN_LORA_SCLK = 5;   // LORA SPI CLK
int PIN_LORA_MISO = 19;  // LORA SPI MISO
int PIN_LORA_MOSI = 18;  // LORA SPI MOSI

uint8_t nodeDeviceEUI[8] = {0x00, 0x0d, 0x75, 0xe6, 0x56, 0x4d, 0xc1, 0xf2};
uint8_t nodeAppEUI[8] = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0x01, 0xE1};
// Only needed for OOTA registration
uint8_t nodeAppKey[16] = {0x2b, 0x84, 0xe0, 0xb0, 0x9b, 0x68, 0xe5, 0xcb, 0x42, 0x17, 0x6f, 0xe7, 0x53, 0xdc, 0xee, 0x79};

#define LED_ON HIGH
#define LED_OFF LOW

// Foward declaration
/** LoRaWAN callback when join network finished */
static void lorawan_has_joined_handler(void);
/** LoRaWAN callback when join network failed */
static void lorawan_join_fail_handler(void);
/** LoRaWAN callback when data arrived */
static void lorawan_rx_handler(lmh_app_data_t *app_data);
/** LoRaWAN callback after class change request finished */
static void lorawan_confirm_class_handler(DeviceClass_t Class);
/** LoRaWAN callback after class change request finished */
static void lorawan_unconfirm_tx_finished(void);
/** LoRaWAN callback after class change request finished */
static void lorawan_confirm_tx_finished(bool result);
/** LoRaWAN Function to send a package */
static void send_lora_frame(void);
static uint32_t timers_init(void);

// APP_TIMER_DEF(lora_tx_timer_id);                                              ///< LoRa tranfer timer instance.
TimerEvent_t appTimer;                                                        ///< LoRa tranfer timer instance.
static uint8_t m_lora_app_data_buffer[LORAWAN_APP_DATA_BUFF_SIZE];            ///< Lora user application data buffer.
static lmh_app_data_t m_lora_app_data = {m_lora_app_data_buffer, 0, 0, 0, 0}; ///< Lora user application data structure.

/**@brief Structure containing LoRaWan parameters, needed for lmh_init()
 */
static lmh_param_t lora_param_init = {LORAWAN_ADR_OFF, DR_3, LORAWAN_PUBLIC_NETWORK, JOINREQ_NBTRIALS, LORAWAN_DEFAULT_TX_POWER};
// DR_8
// LORAWAN_DEFAULT_DATARATE
/**@brief Structure containing LoRaWan callback functions, needed for lmh_init()
*/
static lmh_callback_t lora_callbacks = {BoardGetBatteryLevel, BoardGetUniqueId, BoardGetRandomSeed,
                                        lorawan_rx_handler, lorawan_has_joined_handler, lorawan_confirm_class_handler, lorawan_join_fail_handler,
                                        lorawan_unconfirm_tx_finished, lorawan_confirm_tx_finished};

// Check if the board has an LED port defined
#ifndef LED_BUILTIN
#ifdef ESP32
#ifdef RED_BOARD
#define LED_BUILTIN 16
#else
#define LED_BUILTIN 13
#endif
#endif
#ifdef ESP8266
#define LED_BUILTIN 0
#endif
#endif

int32_t chipTemp = 0;

Ticker ledTicker;

bool wifiFirstConnect = true;

/**@brief Setup function
 */
void ledOff(void)
{
    digitalWrite(LED_BUILTIN, LED_OFF);
}

/**@brief Setup function
 */
void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LED_OFF);

    // Define the HW configuration between MCU and SX126x
    hwConfig.CHIP_TYPE = SX1262_CHIP;         // Example uses an eByte E22 module with an SX1262
    hwConfig.PIN_LORA_RESET = PIN_LORA_RESET; // LORA RESET
    hwConfig.PIN_LORA_NSS = PIN_LORA_NSS;    // LORA SPI CS
    hwConfig.PIN_LORA_SCLK = PIN_LORA_SCLK;   // LORA SPI CLK
    hwConfig.PIN_LORA_MISO = PIN_LORA_MISO;   // LORA SPI MISO
    hwConfig.PIN_LORA_DIO_1 = PIN_LORA_DIO_1; // LORA DIO_1
    hwConfig.PIN_LORA_BUSY = PIN_LORA_BUSY;   // LORA SPI BUSY
    hwConfig.PIN_LORA_MOSI = PIN_LORA_MOSI;   // LORA SPI MOSI
    hwConfig.RADIO_TXEN = -1;                 // LORA ANTENNA TX ENABLE
    hwConfig.RADIO_RXEN = -1;                 // LORA ANTENNA RX ENABLE
    hwConfig.USE_DIO2_ANT_SWITCH = true;      // Example uses an CircuitRocks Alora RFM1262 which uses DIO2 pins as antenna control
    hwConfig.USE_DIO3_TCXO = true;            // Example uses an CircuitRocks Alora RFM1262 which uses DIO3 to control oscillator voltage
    hwConfig.USE_DIO3_ANT_SWITCH = false;    // Only Insight ISP4520 module uses DIO3 as antenna control

    // Initialize Serial for debug output
    Serial.begin(115200);

    Serial.println("=====================================");
    Serial.println("SX126x LoRaWan test");
    Serial.println("=====================================");

    // Initialize Scheduler and timer
    uint32_t err_code = timers_init();
    if (err_code != 0)
    {
        Serial.printf("timers_init failed - %d\n", err_code);
    }

    // Initialize LoRa chip.
    err_code = lora_hardware_init(hwConfig);
    if (err_code != 0)
    {
        Serial.printf("lora_hardware_init failed - %d\n", err_code);
    }

    // Setup the EUIs and Keys
    lmh_setDevEui(nodeDeviceEUI);
    lmh_setAppEui(nodeAppEUI);
    lmh_setAppKey(nodeAppKey); // Only needed for OOTA registration

    // #ifndef RED_BOARD
    //  // Setup connection to a single channel gateway
    //  lmh_setSingleChannelGateway(0, DR_3);
    // #endif

    // Initialize LoRaWan
    err_code = lmh_init(&lora_callbacks, lora_param_init, true, CLASS_A, LORAMAC_REGION_AS923_3);
    if (err_code != 0)
    {
        Serial.printf("lmh_init failed - %d\n", err_code);
    }

    // For some regions we might need to define the sub band the gateway is listening to
    /// \todo This is for Dragino LPS8 gateway. How about other gateways???
    if (!lmh_setSubBandChannels(1))
    {
        Serial.println("lmh_setSubBandChannels failed. Wrong sub band requested?");
    }

    Serial.println("Start Join request");
    // Start Join procedure
    lmh_join();
}

/**@brief Main loop
 */
void loop()
{
    // We are on FreeRTOS, give other tasks a chance to run
    // delay(100);
}

static void lorawan_join_fail_handler(void)
{
    Serial.println("OTAA joined failed");
    Serial.println("Check LPWAN credentials and if a gateway is in range");
    // Restart Join procedure
    Serial.println("Restart network join request");
}

/**@brief LoRa function for handling HasJoined event.
 */
static void lorawan_has_joined_handler(void)
{
#if (OVER_THE_AIR_ACTIVATION != 0)
    Serial.println("Network Joined");
#else
    Serial.println("OVER_THE_AIR_ACTIVATION != 0");

#endif
    lmh_class_request(CLASS_A);

    TimerSetValue(&appTimer, LORAWAN_APP_TX_DUTYCYCLE);
    TimerStart(&appTimer);
    // app_timer_start(lora_tx_timer_id, APP_TIMER_TICKS(LORAWAN_APP_TX_DUTYCYCLE), NULL);
    Serial.println("Sending frame");
    send_lora_frame();
}

/**@brief Function for handling LoRaWan received data from Gateway
 *
 * @param[in] app_data  Pointer to rx data
 */
static void lorawan_rx_handler(lmh_app_data_t *app_data)
{
    Serial.printf("LoRa Packet received on port %d, size:%d, rssi:%d, snr:%d\n",
                  app_data->port, app_data->buffsize, app_data->rssi, app_data->snr);

    for (int i = 0; i < app_data->buffsize; i++)
    {
        Serial.printf("%0X ", app_data->buffer[i]);
    }
    Serial.println("");

    switch (app_data->port)
    {
    case 3:
        // Port 3 switches the class
        if (app_data->buffsize == 1)
        {
            switch (app_data->buffer[0])
            {
            case 0:
                lmh_class_request(CLASS_A);
                break;

            case 1:
                lmh_class_request(CLASS_B);
                break;

            case 2:
                lmh_class_request(CLASS_C);
                break;

            default:
                break;
            }
        }
        break;

    case LORAWAN_APP_PORT:
        // YOUR_JOB: Take action on received data
        break;

    default:
        break;
    }
}

/**@brief Function to confirm LORaWan class switch.
 *
 * @param[in] Class  New device class
 */
static void lorawan_confirm_class_handler(DeviceClass_t Class)
{
    Serial.printf("switch to class %c done\n", "ABC"[Class]);

    // Informs the server that switch has occurred ASAP
    m_lora_app_data.buffsize = 0;
    m_lora_app_data.port = LORAWAN_APP_PORT;
    lmh_send(&m_lora_app_data, LMH_UNCONFIRMED_MSG);
}

/**
 * @brief Called after unconfirmed packet was sent
 * 
 */
static void lorawan_unconfirm_tx_finished(void)
{
    Serial.println("Uncomfirmed TX finished");
}

/**
 * @brief Called after confirmed packet was sent
 * 
 * @param result Result of sending true = ACK received false = No ACK
 */
static void lorawan_confirm_tx_finished(bool result)
{
    Serial.printf("Comfirmed TX finished with result %s", result ? "ACK" : "NAK");
}

/**@brief Function for sending a LoRa package.
 */
static void send_lora_frame(void)
{
    if (lmh_join_status_get() != LMH_SET)
    {
        //Not joined, try again later
        Serial.println("Did not join network, skip sending frame");
        return;
    }

    uint32_t i = 0;
    char t100 = (char)(chipTemp / 100);
    char t10 = (char)((chipTemp - (t100 * 100)) / 10);
    char t1 = (char)((chipTemp - (t100 * 100) - (t10 * 10)) / 1);
    m_lora_app_data.port = LORAWAN_APP_PORT;

    m_lora_app_data.buffer[i++] = '{';
    m_lora_app_data.buffer[i++] = '"';
    m_lora_app_data.buffer[i++] = 'i';
    m_lora_app_data.buffer[i++] = '"';
    m_lora_app_data.buffer[i++] = ':';
    // m_lora_app_data.buffer[i++] = '\"';
    // m_lora_app_data.buffer[i++] = 'E';
    // m_lora_app_data.buffer[i++] = 'S';
    // m_lora_app_data.buffer[i++] = 'P';
#ifdef ESP32
#ifdef RED_BOARD
    m_lora_app_data.buffer[i++] = '0';
    // m_lora_app_data.buffer[i++] = '3';
    // m_lora_app_data.buffer[i++] = '2';
    // m_lora_app_data.buffer[i++] = 'R';
#else
    m_lora_app_data.buffer[i++] = '1';
    // m_lora_app_data.buffer[i++] = '3';
    // m_lora_app_data.buffer[i++] = '2';
#endif
#else
    m_lora_app_data.buffer[i++] = '2';
    // m_lora_app_data.buffer[i++] = '8';
    // m_lora_app_data.buffer[i++] = '6';
#endif
    m_lora_app_data.buffer[i++] = ',';
    m_lora_app_data.buffer[i++] = '"';
    m_lora_app_data.buffer[i++] = 'n';
    m_lora_app_data.buffer[i++] = '"';
    m_lora_app_data.buffer[i++] = ':';
    // m_lora_app_data.buffer[i++] = ' ';
    m_lora_app_data.buffer[i++] = t100 + 0x30;
    m_lora_app_data.buffer[i++] = t10 + 0x30;
    m_lora_app_data.buffer[i++] = t1 + 0x30;
    m_lora_app_data.buffer[i++] = '}';
    m_lora_app_data.buffsize = i;

    Serial.print("Data: ");
    Serial.println((char *)m_lora_app_data.buffer);
    Serial.print("Size: ");
    Serial.println(m_lora_app_data.buffsize);
    Serial.print("Port: ");
    Serial.println(m_lora_app_data.port);

    chipTemp += 1;
    if (chipTemp >= 999)
        chipTemp = 0;

    lmh_error_status error = lmh_send(&m_lora_app_data, LMH_UNCONFIRMED_MSG);
    if (error == LMH_SUCCESS)
    {
    }
    Serial.printf("lmh_send result %d\n", error);
    digitalWrite(LED_BUILTIN, LED_ON);
    ledTicker.once(1, ledOff);
}

/**@brief Function for handling a LoRa tx timer timeout event.
 */
static void tx_lora_periodic_handler(void)
{
    TimerSetValue(&appTimer, LORAWAN_APP_TX_DUTYCYCLE);
    TimerStart(&appTimer);
    Serial.println("Sending frame");
    send_lora_frame();
}

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module. This creates and starts application timers.
 */
static uint32_t timers_init(void)
{
    appTimer.timerNum = 3;
    TimerInit(&appTimer, tx_lora_periodic_handler);
    return 0;
}
Muuhoooo commented 2 years ago

Hi Bernd, I tried updating your code. I finally made it.ESP32 Wrover B module +EBYTE 22 +LoRaWAN OTAA with Helium Console.Thank you for all your help. Ekran Alıntısı

Muuhoooo commented 2 years ago

include

include

include

include

define LORAWAN_APP_DATA_BUFF_SIZE 256 /*< Size of the data to be transmitted. /

define LORAWAN_APP_TX_DUTYCYCLE 30000 /*< Defines the application data transmission duty cycle. 30s, value in [ms]. /

define APP_TX_DUTYCYCLE_RND 1000 /*< Defines a random delay for application data transmission duty cycle. 1s, value in [ms]. /

define JOINREQ_NBTRIALS 3 /*< Number of trials for the join request. /

hw_config hwConfig;

// ESP32 - SX126x pin configuration int PIN_LORA_RESET = 32; // LORA RESET int PIN_LORA_NSS = 15; // LORA SPI CS int PIN_LORA_SCLK = 14; // LORA SPI CLK int PIN_LORA_MISO = 12; // LORA SPI MISO int PIN_LORA_DIO_1 = 4; // LORA DIO_1 int PIN_LORA_BUSY = 33; // LORA SPI BUSY int PIN_LORA_MOSI = 13; // LORA SPI MOSI int RADIO_TXEN = 2; //2 LORA ANTENNA TX ENABLE ,issues kısmında bu txen ve rxen kesinlikle tanımlı olması gerektiğini söylemişler yoksa asla başarılı olamayacağını bildirmiişler. int RADIO_RXEN = 25; //25 LORA ANTENNA RX ENABLE ,Anten anahtarını kontrol etmeden gönderebilirsiniz,Join Accept paketini asla alamazsınız.

uint8_t nodeDeviceEUI[8] = {0x60, 0x81, 0xF9, 0x3B, 0x7A, 0x46, 0xEA, 0xFF}; // msb

uint8_t nodeAppEUI[8] = {0x60, 0x81, 0xF9, 0x86, 0x04, 0x39, 0x17, 0x32}; // msb

uint8_t nodeAppKey[16] = {0x30, 0x97, 0xD4, 0xAC, 0x4C, 0x8A, 0x8B, 0xE9, 0x6B, 0x69, 0x56, 0x29, 0x85, 0x3F, 0xFE, 0xBE}; //msb

define LED_ON HIGH

define LED_OFF LOW

// Foward declaration / LoRaWAN callback when join network finished */ static void lorawan_has_joined_handler(void); /* LoRaWAN callback when join network failed / static void lorawan_join_fail_handler(void); / LoRaWAN callback when data arrived / static void lorawan_rx_handler(lmh_app_data_t app_data); / LoRaWAN callback after class change request finished */ static void lorawan_confirm_class_handler(DeviceClass_t Class); /* LoRaWAN callback after class change request finished / static void lorawan_unconfirm_tx_finished(void); / LoRaWAN callback after class change request finished */ static void lorawan_confirm_tx_finished(bool result); /* LoRaWAN Function to send a package / static void send_lora_frame(void); static uint32_t timers_init(void);

// APP_TIMER_DEF(lora_tx_timer_id); ///< LoRa tranfer timer instance. TimerEvent_t appTimer; ///< LoRa tranfer timer instance. static uint8_t m_lora_app_data_buffer[LORAWAN_APP_DATA_BUFF_SIZE]; ///< Lora user application data buffer. static lmh_app_data_t m_lora_app_data = {m_lora_app_data_buffer, 0, 0, 0, 0}; ///< Lora user application data structure.

/*@brief Structure containing LoRaWan parameters, needed for lmh_init() / static lmh_param_t lora_param_init = {LORAWAN_ADR_OFF, DR_3, LORAWAN_PUBLIC_NETWORK, JOINREQ_NBTRIALS, LORAWAN_DEFAULT_TX_POWER}; // DR_8 // LORAWAN_DEFAULT_DATARATE /*@brief Structure containing LoRaWan callback functions, needed for lmh_init() / static lmh_callback_t lora_callbacks = {BoardGetBatteryLevel, BoardGetUniqueId, BoardGetRandomSeed, lorawan_rx_handler, lorawan_has_joined_handler, lorawan_confirm_class_handler, lorawan_join_fail_handler, lorawan_unconfirm_tx_finished, lorawan_confirm_tx_finished};

define LED_BUILTIN 0

int32_t chipTemp = 0;

/*@brief Setup function / void ledOff(void) { digitalWrite(LED_BUILTIN, LED_OFF); }

/*@brief Setup function / void setup() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LED_OFF);

// Define the HW configuration between MCU and SX126x hwConfig.CHIP_TYPE = SX1262_CHIP; // Example uses an eByte E22 module with an SX1262 hwConfig.PIN_LORA_RESET = PIN_LORA_RESET; // LORA RESET hwConfig.PIN_LORA_NSS = PIN_LORA_NSS; // LORA SPI CS hwConfig.PIN_LORA_SCLK = PIN_LORA_SCLK; // LORA SPI CLK hwConfig.PIN_LORA_MISO = PIN_LORA_MISO; // LORA SPI MISO hwConfig.PIN_LORA_DIO_1 = PIN_LORA_DIO_1; // LORA DIO_1 hwConfig.PIN_LORA_BUSY = PIN_LORA_BUSY; // LORA SPI BUSY hwConfig.PIN_LORA_MOSI = PIN_LORA_MOSI; // LORA SPI MOSI hwConfig.RADIO_TXEN = -1; // LORA ANTENNA TX ENABLE hwConfig.RADIO_RXEN = -1; // LORA ANTENNA RX ENABLE hwConfig.USE_DIO2_ANT_SWITCH = true; // Example uses an CircuitRocks Alora RFM1262 which uses DIO2 pins as antenna control hwConfig.USE_DIO3_TCXO = true; // Example uses an CircuitRocks Alora RFM1262 which uses DIO3 to control oscillator voltage hwConfig.USE_DIO3_ANT_SWITCH = false; // Only Insight ISP4520 module uses DIO3 as antenna control

// Initialize Serial for debug output Serial.begin(115200);

Serial.println("====================================="); Serial.println("SX126x LoRaWan test"); Serial.println("=====================================");

// Initialize Scheduler and timer uint32_t err_code = timers_init(); if (err_code != 0) { Serial.printf("timers_init failed - %d\n", err_code); }

// Initialize LoRa chip. err_code = lora_hardware_init(hwConfig); if (err_code != 0) { Serial.printf("lora_hardware_init failed - %d\n", err_code); }

// Setup the EUIs and Keys lmh_setDevEui(nodeDeviceEUI); lmh_setAppEui(nodeAppEUI); lmh_setAppKey(nodeAppKey); // Only needed for OOTA registration

// Initialize LoRaWan err_code = lmh_init(&lora_callbacks, lora_param_init, true, CLASS_A, LORAMAC_REGION_EU868); if (err_code != 0) { Serial.printf("lmh_init failed - %d\n", err_code); }

// For some regions we might need to define the sub band the gateway is listening to /// \todo This is for Dragino LPS8 gateway. How about other gateways??? if (!lmh_setSubBandChannels(1)) { Serial.println("lmh_setSubBandChannels failed. Wrong sub band requested?"); }

Serial.println("Start Join request"); // Start Join procedure lmh_join(); }

/*@brief Main loop / void loop() { // We are on FreeRTOS, give other tasks a chance to run // delay(100); }

static void lorawan_join_fail_handler(void) { Serial.println("OTAA joined failed"); Serial.println("Check LPWAN credentials and if a gateway is in range"); // Restart Join procedure Serial.println("Restart network join request"); }

/*@brief LoRa function for handling HasJoined event. / static void lorawan_has_joined_handler(void) {

if (OVER_THE_AIR_ACTIVATION != 0)

Serial.println("Network Joined");

else

Serial.println("OVER_THE_AIR_ACTIVATION != 0");

endif

lmh_class_request(CLASS_A);

TimerSetValue(&appTimer, LORAWAN_APP_TX_DUTYCYCLE); TimerStart(&appTimer); // app_timer_start(lora_tx_timer_id, APP_TIMER_TICKS(LORAWAN_APP_TX_DUTYCYCLE), NULL); Serial.println("Sending frame"); send_lora_frame(); }

/*@brief Function for handling LoRaWan received data from Gateway

/*@brief Function to confirm LORaWan class switch.

/**

/**

/*@brief Function for sending a LoRa package. / static void send_lora_frame(void) { if (lmh_join_status_get() != LMH_SET) { //Not joined, try again later Serial.println("Did not join network, skip sending frame"); return; }

uint32_t i = 0; char t100 = (char)(chipTemp / 100); char t10 = (char)((chipTemp - (t100 100)) / 10); char t1 = (char)((chipTemp - (t100 100) - (t10 * 10)) / 1); m_lora_app_data.port = LORAWAN_APP_PORT;

m_lora_app_data.buffer[i++] = '{'; m_lora_app_data.buffer[i++] = '"'; m_lora_app_data.buffer[i++] = 'i'; m_lora_app_data.buffer[i++] = '"'; m_lora_app_data.buffer[i++] = ':'; // m_lora_app_data.buffer[i++] = '\"'; // m_lora_app_data.buffer[i++] = 'E'; // m_lora_app_data.buffer[i++] = 'S'; // m_lora_app_data.buffer[i++] = 'P';

ifdef ESP32

ifdef RED_BOARD

m_lora_app_data.buffer[i++] = '0'; // m_lora_app_data.buffer[i++] = '3'; // m_lora_app_data.buffer[i++] = '2'; // m_lora_app_data.buffer[i++] = 'R';

else

m_lora_app_data.buffer[i++] = '1'; // m_lora_app_data.buffer[i++] = '3'; // m_lora_app_data.buffer[i++] = '2';

endif

else

m_lora_app_data.buffer[i++] = '2'; // m_lora_app_data.buffer[i++] = '8'; // m_lora_app_data.buffer[i++] = '6';

endif

m_lora_app_data.buffer[i++] = ','; m_lora_app_data.buffer[i++] = '"'; m_lora_app_data.buffer[i++] = 'n'; m_lora_app_data.buffer[i++] = '"'; m_lora_app_data.buffer[i++] = ':'; // m_lora_app_data.buffer[i++] = ' '; m_lora_app_data.buffer[i++] = t100 + 0x30; m_lora_app_data.buffer[i++] = t10 + 0x30; m_lora_app_data.buffer[i++] = t1 + 0x30; m_lora_app_data.buffer[i++] = '}'; m_lora_app_data.buffsize = i;

Serial.print("Data: "); Serial.println((char *)m_lora_app_data.buffer); Serial.print("Size: "); Serial.println(m_lora_app_data.buffsize); Serial.print("Port: "); Serial.println(m_lora_app_data.port);

chipTemp += 1; if (chipTemp >= 999) chipTemp = 0;

lmh_error_status error = lmh_send(&m_lora_app_data, LMH_UNCONFIRMED_MSG); if (error == LMH_SUCCESS) { } Serial.printf("lmh_send result %d\n", error); digitalWrite(LED_BUILTIN, LED_ON);

}

/*@brief Function for handling a LoRa tx timer timeout event. / static void tx_lora_periodic_handler(void) { TimerSetValue(&appTimer, LORAWAN_APP_TX_DUTYCYCLE); TimerStart(&appTimer); Serial.println("Sending frame"); send_lora_frame(); }

/*@brief Function for the Timer initialization.

beegee-tokyo commented 2 years ago

@Muuhoooo I am happy you could solve it. I will close this now.