B4R Question Lora library Inline C

Hi.
I used LoRa library inline C. https://github.com/sandeepmistry/arduino-LoRa .
I used reading method stream from https://www.b4x.com/android/forum/threads/hardware-serial-on-esp32.97318/post-613593.
But I do not get any data.


B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Dim LoraReader As Timer
    Dim bytcon As ByteConverter
    Dim Lora_Stream As Stream
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")
    Delay(1000)
    RunNative("Lora_Stream",Null)
    Delay(1000)
    LoraReader.Initialize("Lora_Reader_Tick",500)
    LoraReader.Enabled=True  
End Sub

Sub Lora_Reader_Tick
    Dim x As Int = Lora_Stream.BytesAvailable
    Log(x)
    If x> 0 Then
        Log("size ", x)
        Dim buffer(x) As Byte
        Lora_Stream.ReadBytes(buffer,0,x)
        Log(bytcon.StringFromBytes(buffer))
    End If
End Sub

#if C
#include <SPI.h>
#include <LoRa.h>

void Lora_Stream (B4R::Object* unused) {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Sender");
  LoRa.setPins(15, 16, 4);
//  if (!LoRa.begin(433E6)) {
//    Serial.println("Starting LoRa failed!");
//    while (1);
//  }
  ::LoRa.begin(433E6);
  b4r_main::_lora_stream->wrappedStream = &::LoRa;
  LoRa.setTxPower(20);
  LoRa.setSignalBandwidth(62.5E3);//41.7E3
  LoRa.setSpreadingFactor(12);
  LoRa.setCodingRate4(8);
  Serial.println("LoRa init succeeded.");
 
}

#End If
Please help me.
Thankful.
 

candide

Active Member
Licensed User
i can propose you this first draft of wrapper for LoRa library, to see if it can help you.
original library : https://github.com/sandeepmistry/arduino-LoRa (this one you want to use)

2 initialize available:
not working for ARDUINO_SAMD_MKRWAN1300
configuration with callbacks
void Initialize( Subvoid_int OnReceiveSub, Subvoid_void OnTxDoneSub);
configuration without callbacks
void Initialize1();

2 setSPI for esp32
one for HSPI or VSPI with standard configuration of pins
void setSPI(byte SPIbus);
one for HSPI or VSPI and with reconfiguration of pins. (pin=255 => pin not changed)
void setSPI1(byte SPIbus,byte sck, byte miso, byte mosi, byte ss);

SPIbus is a parameter available in library
 
Last edited:
Upvote 0
i can propose you this first draft of wrapper for LoRa library, to see if it can help you.
original library : https://github.com/sandeepmistry/arduino-LoRa (this one you want to use)

2 initialize available:
not working for ARDUINO_SAMD_MKRWAN1300
configuration with callbacks
void Initialize( Subvoid_int OnReceiveSub, Subvoid_void OnTxDoneSub);
configuration without callbacks
void Initialize1();

2 setSPI for esp32
one for HSPI or VSPI with standard configuration of pins
void setSPI(byte SPIbus);
one for HSPI or VSPI and with reconfiguration of pins. (pin=255 => pin not changed)
void setSPI1(byte SPIbus,byte sck, byte miso, byte mosi, byte ss);

SPIbus is a parameter available in library
HI Candide.
Many thanks for wrapping this library.
I tested this library but could not send or receive any data.
I used ESP8266 and the parameters of the transmitter and the receiver are the same.
Thank you for your help.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Dim repeater As Timer
    Dim LoraReader As Timer
'    Dim bytcon As ByteConverter
    Dim Lora As LoRa
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")
    repeater.Initialize("Repeater_Tick",5000)
    repeater.Enabled=True
    LoraReader.Initialize("Lora_Reader_Tick",500)
'    LoraReader.Enabled=True
    Lora.Initialize("Loa_OnReceive","Lora_OnTxDone")
'    Lora.Initialize1
    Lora.setPins(15, 16, 4)
    Log(Lora.begin(433E6))
    Lora.setTxPower(20,1)
    Lora.setSignalBandwidth(62.5E3)
    Lora.setSpreadingFactor(12)
    Lora.setCodingRate4(8)
End Sub

Sub Repeater_Tick
    Lora.write1("test",4)
End Sub

Sub Lora_Reader_Tick
'    Log(Lora.available)   
    
End Sub

Sub Loa_OnReceive(packetSize As Int)
    Log(packetSize)
End Sub

Sub Lora_OnTxDone()
    Log("data sent")   
End Sub
 
Upvote 0

candide

Active Member
Licensed User
thanks for your feedback.

i will check again wrapper and i will come back


can you check what is return of "Lora.begin" ? 0= init NOK / 1 = init OK

can you add at end of AppStart a " Lora.dumpRegisters "
it will send in log a dump of registers: it can be a way to check if SPI is OK and if access to hard is OK

to have callback "Loa_onreceive" working, you need to run a "Lora.receive" to start reception by library, and after each callback "Loa_onreceive"


also, can you check your hard configuration under arduino with an example provided with original library ? (just to confirm issue in wrapper)
 
Last edited:
Upvote 0

candide

Active Member
Licensed User
hi
can you make tests with this file ? it is an arduino example provided with original library and adapted to B4R
 

Attachments

  • testLoRa.zip
    2.3 KB · Views: 246
Upvote 0
thanks for your feedback.

i will check again wrapper and i will come back


can you check what is return of "Lora.begin" ? 0= init NOK / 1 = init OK

can you add at end of AppStart a " Lora.dumpRegisters "
it will send in log a dump of registers: it can be a way to check if SPI is OK and if access to hard is OK

to have callback "Loa_onreceive" working, you need to run a "Lora.receive" to start reception by library, and after each callback "Loa_onreceive"


also, can you check your hard configuration under arduino with an example provided with original library ? (just to confirm issue in wrapper)
Hi.
Thanks for your reply.
I checked return "Lora.begin" and the result was 1 . I even disconnected pin SCK to make sure SPI works properly and returns 0.
I ran a project with the Original library on the Arduino. Even in the example above, the sender is programmed with Arduino code.
I'll check the "Lora.dumpRegisters " and "Lora_onreceive".
 
Upvote 0
hi
can you make tests with this file ? it is an arduino example provided with original library and adapted to B4R
Hi.
I'm sorry it's a little late.
I was testing your example code. It still did not work in "Callback" mode , but it did work with these changes.


B4X:
Sub Process_Globals
    Public Serial1 As Serial
'    Private wifi As ESP8266WiFi
    Private LoRa As LoRa
    Private bc As ByteConverter
    Private csPin As Int = 15                 '// LoRa radio chip select
    Private resetPin As Int = 16              '// LoRa radio reset
    Private irqPin As Int = 4                 '// change for your board; must be a hardware interrupt pin

    Private msgCount As Byte = 0              '// count of outgoing messages
    Private localAddress As Byte = 0xBB       '// address of this device
    Private destination As Byte = 0xFF        '// destination to send to
    Private lastSendTime As Long = 0          '// last send time
    Private interval As Int = 3000            '// interval between sends
End Sub


Sub AppStart
    Serial1.Initialize(9600)

    Log("AppStart")
    'example of connecting to a local network
'    If Not(wifi.Connect2("SSID", "PASSWORD")) Then
'        Log("Failed to connect to network")
'    Else
'        Log("test LoRa Duplex with callback")
'    End If
'        LoRa.Initialize("Lora_OnReceive","Lora_OnTxDone")
        LoRa.Initialize1
        Log("Lora init done")
        '// override the default CS, reset, and IRQ pins (optional)
        LoRa.setPins(csPin, resetPin, irqPin)      '// set CS, reset, IRQ pin
        Log("Lora setpins done")
          Dim init As Int = LoRa.begin(433E6)     '// initialize ratio at 433 MHz
        Log("init result: ",init)            
          If init = 0 Then
            Log("LoRa init failed. Check your connections.")   '// if failed, do nothing
        Else
    '          LoRa.receive(0)                    '// 0 = explicitHeaderMode
              Log("LoRa init succeeded.")
              AddLooper("Lora_loop")             '// to send message every 3sec
        End If

End Sub

Sub Lora_loop
 
    If LoRa.parsePacket(0) > 0 Then
        Dim incoming() As Byte                     '// payload of packet
        Dim received() As Byte
        Do While (LoRa.available > 0)                '// can't use readString() in callback, so
            received = Array As Byte(LoRa.read)
            incoming = JoinBytes(Array(incoming, received))         '// add bytes one by one
        Loop
        Log(incoming)
        Log("RSSI: " ,LoRa.packetRssi)
        Log("Snr: " ,LoRa.packetSnr)
    End If
 
    If (Millis - lastSendTime > interval)  Then
        Dim message As String = "Hello LoRa World!"     '// send a message
        sendMessage(message.getbytes)
        Log("Sending ", message)
        lastSendTime = Millis                   '// timestamp the message
'        LoRa.receive(0)                            '// go back into receive mode
    End If
End Sub
                                 
Sub sendMessage(outgoing() As Byte)
    LoRa.beginPacket(0)                           '// start packet
    LoRa.write(destination)                    '// add destination address
    LoRa.write(localAddress)                   '// add sender address
    LoRa.write(msgCount)                       '// add message ID
    LoRa.write(outgoing.length)                '// add payload length
    LoRa.write1(outgoing,outgoing.length)      '// add payload
    LoRa.endPacket(False)                             '// finish packet and send it
    msgCount = msgCount+1                      '// increment message ID
End Sub

Also, routine "Lora_onTXdone" is still not called even though "TX" is done.
To make sure the wiring was correct, I tested the hardware under Arduino with the original library and "callback" sample code.
Another thing is that when I compile on "Pro or Pro Mini", gets this error
D:\EEAA3~1.FIL\Harpag\ALLPRO~1\LORATE~1\promini\lora\Objects\src\rLoRa.cpp: In member function 'void B4R::B4RLoRa::dumpRegisters()':
rLoRa.cpp:241:27: error: 'Serial1' was not declared in this scope
rcs->dumpRegisters(Serial1);
^~~~~~~
D:\EEAA3~1.FIL\Harpag\ALLPRO~1\LORATE~1\promini\lora\Objects\src\rLoRa.cpp:241:27: note: suggested alternative: 'Serial'
rcs->dumpRegisters(Serial1);
^~~~~~~
Serial
 
Upvote 0

candide

Active Member
Licensed User
i fond the problem : "setpins" was done after init of pins for interruption done.

Now init of pins for interruption is done at "begin", and after optional "setpins"
DumpRegister is now providing 128 bytes in an arraybyte.

now callback should be OK.

Please to test again. (please to install arduino library provided and B4R library library in rLoRa directory)
 

Attachments

  • rLoRa1.2.1.zip
    12 KB · Views: 242
Upvote 0
i fond the problem : "setpins" was done after init of pins for interruption done.

Now init of pins for interruption is done at "begin", and after optional "setpins"
DumpRegister is now providing 128 bytes in an arraybyte.

now callback should be OK.

Please to test again. (please to install arduino library provided and B4R library library in rLoRa directory)
Hi.
Sorry for late reply.
I tried exactly the sample code you provided, but it still "Callback" did not work.
Sampler code of the sender under Arduino (board pro mini , Setpins : defult):

C:
#include <SPI.h>
#include <LoRa.h>

int counter = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Sender");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  // send packet
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;

  delay(5000);
}

Sampler code of the receiver with callback under Arduino:
C:
#include <SPI.h>
#include <LoRa.h>

#ifdef ARDUINO_SAMD_MKRWAN1300
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
#endif

void setup() {
  LoRa.setPins(15, 16, 4);
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver Callback");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }

  // Uncomment the next line to disable the default AGC and set LNA gain, values between 1 - 6 are supported
  // LoRa.setGain(6);
 
  // register the receive callback
  LoRa.onReceive(onReceive);

  // put the radio into receive mode
  LoRa.receive();
}

void loop() {
  // do nothing
}

void onReceive(int packetSize) {
  // received a packet
  Serial.print("Received packet '");

  // read packet
  for (int i = 0; i < packetSize; i++) {
    Serial.print((char)LoRa.read());
  }

  // print RSSI of packet
  Serial.print("' with RSSI ");
  Serial.println(LoRa.packetRssi());
}

The data is received by the receiver board.
Note: The receiver board is the same board on which I uploaded the above sample code.

B4X:
test LoRa Duplex with callback
Lora init done
Lora setpins done
init result: 1
LoRa init succeeded.
Sending Hello LoRa World!
reg:0  Val:44
reg:1  Val:133
reg:2  Val:26
reg:3  Val:11
reg:4  Val:0
reg:5  Val:82
reg:6  Val:108
reg:7  Val:64
reg:8  Val:0
reg:9  Val:143
reg:10  Val:9
reg:11  Val:43
reg:12  Val:35
reg:13  Val:22
reg:14  Val:0
reg:15  Val:0
reg:16  Val:0
reg:17  Val:0
reg:18  Val:80
reg:19  Val:10
reg:20  Val:0
reg:21  Val:1
reg:22  Val:0
reg:23  Val:0
reg:24  Val:36
reg:25  Val:37
reg:26  Val:151
reg:27  Val:65
reg:28  Val:0
reg:29  Val:114
reg:30  Val:112
reg:31  Val:100
reg:32  Val:0
reg:33  Val:8
reg:34  Val:21
reg:35  Val:255
reg:36  Val:0
reg:37  Val:0
reg:38  Val:4
reg:39  Val:0
reg:40  Val:0
reg:41  Val:8
reg:42  Val:64
reg:43  Val:0
reg:44  Val:16
reg:45  Val:80
reg:46  Val:20
reg:47  Val:69
reg:48  Val:85
reg:49  Val:195
reg:50  Val:5
reg:51  Val:39
reg:52  Val:28
reg:53  Val:10
reg:54  Val:3
reg:55  Val:10
reg:56  Val:66
reg:57  Val:18
reg:58  Val:82
reg:59  Val:29
reg:60  Val:0
reg:61  Val:175
reg:62  Val:0
reg:63  Val:0
reg:64  Val:0
reg:65  Val:0
reg:66  Val:18
reg:67  Val:36
reg:68  Val:45
reg:69  Val:0
reg:70  Val:3
reg:71  Val:0
reg:72  Val:4
reg:73  Val:35
reg:74  Val:0
reg:75  Val:9
reg:76  Val:5
reg:77  Val:132
reg:78  Val:50
reg:79  Val:43
reg:80  Val:20
reg:81  Val:0
reg:82  Val:0
reg:83  Val:14
reg:84  Val:0
reg:85  Val:0
reg:86  Val:0
reg:87  Val:15
reg:88  Val:224
reg:89  Val:0
reg:90  Val:12
reg:91  Val:242
reg:92  Val:6
reg:93  Val:0
reg:94  Val:92
reg:95  Val:120
reg:96  Val:0
reg:97  Val:28
reg:98  Val:14
reg:99  Val:91
reg:100  Val:204
reg:101  Val:0
reg:102  Val:1
reg:103  Val:80
reg:104  Val:0
reg:105  Val:0
reg:106  Val:0
reg:107  Val:0
reg:108  Val:0
reg:109  Val:0
reg:110  Val:0
reg:111  Val:11
reg:112  Val:208
reg:113  Val:0
reg:114  Val:19
reg:115  Val:0
reg:116  Val:0
reg:117  Val:0
reg:118  Val:0
reg:119  Val:0
reg:120  Val:0
reg:121  Val:0
reg:122  Val:0
reg:123  Val:0
reg:124  Val:0
reg:125  Val:0
reg:126  Val:0
reg:127  Val:0
dumpregister done
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!

This is what I get from the sample code above in the log window.
Thanks for your help.
 
Upvote 0
Hi.
Sorry for late reply.
I tried exactly the sample code you provided, but it still "Callback" did not work.
Sampler code of the sender under Arduino (board pro mini , Setpins : defult):

C:
#include <SPI.h>
#include <LoRa.h>

int counter = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Sender");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  // send packet
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;

  delay(5000);
}

Sampler code of the receiver with callback under Arduino:
C:
#include <SPI.h>
#include <LoRa.h>

#ifdef ARDUINO_SAMD_MKRWAN1300
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
#endif

void setup() {
  LoRa.setPins(15, 16, 4);
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver Callback");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }

  // Uncomment the next line to disable the default AGC and set LNA gain, values between 1 - 6 are supported
  // LoRa.setGain(6);
 
  // register the receive callback
  LoRa.onReceive(onReceive);

  // put the radio into receive mode
  LoRa.receive();
}

void loop() {
  // do nothing
}

void onReceive(int packetSize) {
  // received a packet
  Serial.print("Received packet '");

  // read packet
  for (int i = 0; i < packetSize; i++) {
    Serial.print((char)LoRa.read());
  }

  // print RSSI of packet
  Serial.print("' with RSSI ");
  Serial.println(LoRa.packetRssi());
}

The data is received by the receiver board.
Note: The receiver board is the same board on which I uploaded the above sample code.

B4X:
test LoRa Duplex with callback
Lora init done
Lora setpins done
init result: 1
LoRa init succeeded.
Sending Hello LoRa World!
reg:0  Val:44
reg:1  Val:133
reg:2  Val:26
reg:3  Val:11
reg:4  Val:0
reg:5  Val:82
reg:6  Val:108
reg:7  Val:64
reg:8  Val:0
reg:9  Val:143
reg:10  Val:9
reg:11  Val:43
reg:12  Val:35
reg:13  Val:22
reg:14  Val:0
reg:15  Val:0
reg:16  Val:0
reg:17  Val:0
reg:18  Val:80
reg:19  Val:10
reg:20  Val:0
reg:21  Val:1
reg:22  Val:0
reg:23  Val:0
reg:24  Val:36
reg:25  Val:37
reg:26  Val:151
reg:27  Val:65
reg:28  Val:0
reg:29  Val:114
reg:30  Val:112
reg:31  Val:100
reg:32  Val:0
reg:33  Val:8
reg:34  Val:21
reg:35  Val:255
reg:36  Val:0
reg:37  Val:0
reg:38  Val:4
reg:39  Val:0
reg:40  Val:0
reg:41  Val:8
reg:42  Val:64
reg:43  Val:0
reg:44  Val:16
reg:45  Val:80
reg:46  Val:20
reg:47  Val:69
reg:48  Val:85
reg:49  Val:195
reg:50  Val:5
reg:51  Val:39
reg:52  Val:28
reg:53  Val:10
reg:54  Val:3
reg:55  Val:10
reg:56  Val:66
reg:57  Val:18
reg:58  Val:82
reg:59  Val:29
reg:60  Val:0
reg:61  Val:175
reg:62  Val:0
reg:63  Val:0
reg:64  Val:0
reg:65  Val:0
reg:66  Val:18
reg:67  Val:36
reg:68  Val:45
reg:69  Val:0
reg:70  Val:3
reg:71  Val:0
reg:72  Val:4
reg:73  Val:35
reg:74  Val:0
reg:75  Val:9
reg:76  Val:5
reg:77  Val:132
reg:78  Val:50
reg:79  Val:43
reg:80  Val:20
reg:81  Val:0
reg:82  Val:0
reg:83  Val:14
reg:84  Val:0
reg:85  Val:0
reg:86  Val:0
reg:87  Val:15
reg:88  Val:224
reg:89  Val:0
reg:90  Val:12
reg:91  Val:242
reg:92  Val:6
reg:93  Val:0
reg:94  Val:92
reg:95  Val:120
reg:96  Val:0
reg:97  Val:28
reg:98  Val:14
reg:99  Val:91
reg:100  Val:204
reg:101  Val:0
reg:102  Val:1
reg:103  Val:80
reg:104  Val:0
reg:105  Val:0
reg:106  Val:0
reg:107  Val:0
reg:108  Val:0
reg:109  Val:0
reg:110  Val:0
reg:111  Val:11
reg:112  Val:208
reg:113  Val:0
reg:114  Val:19
reg:115  Val:0
reg:116  Val:0
reg:117  Val:0
reg:118  Val:0
reg:119  Val:0
reg:120  Val:0
reg:121  Val:0
reg:122  Val:0
reg:123  Val:0
reg:124  Val:0
reg:125  Val:0
reg:126  Val:0
reg:127  Val:0
dumpregister done
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!

This is what I get from the sample code above in the log window.
Thanks for your help.
This feedback was for Library 1.2. Let me try Library 1.2.1.
 
Upvote 0
i fond the problem : "setpins" was done after init of pins for interruption done.

Now init of pins for interruption is done at "begin", and after optional "setpins"
DumpRegister is now providing 128 bytes in an arraybyte.

now callback should be OK.

Please to test again. (please to install arduino library provided and B4R library library in rLoRa directory)
Hi.
I also tried the library and sample code 1.2.1, but unfortunately "Callback" still does not work.

AppStart
test LoRa Duplex with callback
Lora init done
Lora setpins done
init result: 1
LoRa init succeeded.
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!
print 0 20851A0B00526C40008F092B23160000
print 16 00005009000200002427903F00727064
print 32 000815FF000004000001C0001E501445
print 48 55C305271C0A030A4212521D00AF0000
print 64 000012242D000300042300090584322B
print 80 1400000E0000000FE0000CEE06005C78
print 96 001C0E5BCC000150000000000000000B
print 112 D0001300000000000000000000000000
stack: 0 dumpregister done
Sending Hello LoRa World!
Sending Hello LoRa World!
Sending Hello LoRa World!

Thanks.
 
Upvote 0
Top