LoRa LoRa LoRa

...and that is not THIS type of Lorra Lorra laughs : Cyla Black

although, my laughs were more tears of woe and then tears of joy-ish

What is LoRa and why is it spelt that way? well, you could DuckDuckGo and search for it, however, simple description: "Lo-ng Ra-nge comms" .

[UPDATE: okay, I'll answer the question, "why bother? why not just use WiFi?" - that is such a non-Gen X question.  Right. WiFi sucks a LOT of power, I mean a LOT.  It also requires an AP (access point), one device will have to be a gateway router for one device to connect to the other... and also, well, the "LoRa" name says it all... can you move your laptop outside your house, walk down the street, keep walking, keep walking some more and still get a signal to that WiFi AP? no. no you cannot. Ah, ha, then you'll attempt to be smart and say, "4G" or "5G", well, yes, but that needs another device, such as your phone to be required, then you have to connect your device to the phone (burning through 2 lots of battery power sources), what if you just want to have a tracking device connected to, let's say your trailer that sits on your driveway for 9 months of the year.  you don't want it to be on all the time do you? no. so upon movement detection you want a mercury switch to turn on the device, then transmit to your home device to let you know that the trailer is moving.  This device can sit in minimal usage for years and years, without requiring charging or if you're smart you can have a solar charger somewhere in the equation so it never needs maintenance.  However, the usage is to be notified - it's not about streaming Tik-Tok or YouTube videos, that's what your phone is for, this is for sending small but very important pieces of data from one device to another over quite long range distances that are just not possible via WiFi or BLE.]


Find out more at THIS WEBSITE:



As is usual, there is a lot of information on the internet and then there is a lack of information on the internet - huh? what do I mean?  Well, I purchase 1 of the OLED screen devices and I purchased the same thing without the OLED screen - thinking that I would save a few quid.

My thinking was: I would put this first board onto the RPi4 that has just arrived as that OS will be nice and clean and it should just work straight away.  In fact, that is indeed what happened. Pretty awesome really.





This second board, I thought to myself, I know what I'll connect that up to the Arduino UNO R4 that I have just received and I'll just have that sending some raw data and I'll do 2-way comms with the device above and I'll just use the Serial monitor as a debug output.

Oh, how silly of me to think this would all be over and done with within an hour.

First woes were the fact that (and I am ashamed to admit it took a lot longer than it should have done) one of the Arduino UNO R4 devices was kerput, DOA, dead on arrival.  Well, if it were dead dead, that would have been easy - however as they put the default blink app on the device, you're lulled into a false sense of "everything is fine".  I actually ordered 2 Arduino UNO R4 devices - as yes, you guessed it, I've had similar experience in the past - however, I naturally assumed I was the problem and not the hardware.

I plugged in the arduino device to my personal honor laptop (Ubuntu 22.04) and what I should have done is run "sudo dmesg" at this point as I would have seen that the laptop was having problems with the device hardware, ie. recognising it as a USB or even a serial device.  I foolishly assumed that it was obviously the library files within the Arduino IDE, so I blitzed the Arduino IDE 1.5 / 1.8 versions that I had, I installed the new Arduino v2 IDE - went through the teething problems with getting that all up & running and using the Radiohead lora library - only to end up wasting about 6hours and being no further ahead.

I then thought, I have another device, let me try that... ah, that worked straight away. Picked it up in the IDE, let me access the port and even allowed me to upload a new sketch.

Unplugged, plugged in the first device, attempted the same. no dice. hmmmm...

Swapped back over. I can upload a new sketch and I can interface with the RFM95W device (that lacks any form of indication that it is working, maybe a small LED would have been nice to at least tell you it is ON and maybe a Tx/Rx LED to show that traffic is going in/out - appreciate that would eat some battery power, but make it turn on/off-able? anyway)

I used the PIN layout as suggested and the Arduino code as suggested... and it does nothing.

Hmmm.... that's when the good old internet got in my way.  I searched high and low for a simple Arduino LoRa library that I could use for 915MHz and I got a variety of flavours, except nothing seemed to work with transmit / receive from the RPi LoRa device.

I went and watched Guardians of the Galaxy 3 movie.

The next day, I thought, hmmm, how about if I use an RPi Pico instead?  Well, kiss good bye to another couple of hours.  Similar scenario - I built some code just to do the basics, load the LoRa library and device and test being a transmit or receive.  No dice.

(yes, it took a double-look for me to realise that the pins were laid at inversely on that vga-board that I was using - I'll come back to trying this again later)


The Pico code was actually nice and simple C code, it was getting the internal Pico board temperature value and then attempting to just broadcast / transmit that data out.  However, it looked like the internal library code was "assuming" that it was going to connect to a LoRa gateway (LoRaWan) device. sigh, not what I wanted or need - I actually DO NOT WANT to send this data to the internet, I want to send it to a device sitting over there, 3ft away. Directly.  Why does everyone "ass-u-me" that you want or even need to send data to their servers on the internet (yes, of course I know the answer to this = money, I was being factitious)..... arrgghhhhh!


I ended up doing what I really did not want to do - I dug out an RPi 3+ board to use that instead.

However, in my frustration, I pinged thepihut to ask about a refund for the Arduino UNO R4 - normally, I wouldn't care, I'd just bin it, but these are £28-ish each, so not cheap and cheerful anymore.  Whilst I was there, I ordered another Adafruit LoRa Radio Bonnet with OLED - RFM95W for plugging into the RPi 3+ (when it arrives).


I ended up going around in circles with web articles, but settled on this page

https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi/raspberry-pi-wiring

which has a reliance to install this library:

https://github.com/adafruit/Adafruit_CircuitPython_RFM9x

Which is all good and all - for the RPi, but will not assist me with the Arduino or Pico that I really want to run this on - I'll get back to them later.




As you can see, I used the default code from the first RPi board, just commented out the Button pressing code - basically, I just wanted to check that I could initialise the LoRa board correctly - I could put it into listen mode (receive) and then if I received a specific message, could I then send (transmit) a message back out?

Okay, yes, I had to do a double-check on the PINs and due to probably my lack of concentration I did indeed mix up the first time the SPI0 and SPI1 pins, remember to keep all the SPI0 pins for the board; especially in ref to the CE1 pin - don't use that top left one use the bottom right one.


Okay, so we have both devices running.  I have the RPi 3+ with the little adapter board attached basically sitting there LISTENING:

On the first board, I press the "B" button this triggers a "B: Send STATUS" message to be sent out:


Oh looksie, I sent out the "B" button message and got a response:

On the second board, RPi 3+, I received the "B: Send Status" message and as per the code shown earlier, we check to see if that value was received and if it was we send a message back out as it shown here:

Phew! okay, so we now have 2 LoRa devices, NOT connected to a LoRa gateway, NOT connected to the internet, being able to transmit / receive messages between themselves (and potentially any other device within a 1-10Km range as I've not secured the message content!)

I will take that small win.  I have spent an entire weekend getting, what I believed to be a 1-hour job, done.  Where to next?
Well, that's the thing, small packets of data can now be transmitted with very low power consumption between several devices - making up a mesh-network of small but important data.
No, it cannot send images/video/voice data - don't believe those spoof YouTube videos or websites that say they will sell you a device that can do that - it's fake, the protocol cannot do that, it just cannot.... you could abuse it to do so, but it'd take a month of Sundays to break the data down into 252byte packets to send and well, you can do the mathematics to work out how long it'd take to send a 13Mb photo / image.  More important to send a "I detected a thing, let me inform you of it" and then trigger booting up the bigger WiFi enabled RPi camera or something like that.  Am yet to work out what I'll use it for - potentially opening and closing greenhouse window based upon heat build up and then informing me that it has done so... and then passing that data to an old Z80 CPU machine to draw some 1980s cheesy graphics to show me the state of the greenhouse window.




Next steps?  Well... to return back to getting this to work with the Pico and then the Arduino.  I would really prefer the Pico due to the minimal footprint, but will take the Arduino (both have ability to connect other devices for connecting sensors / relays, so who knows which will win)


Well... also, that other board I ordered will be arriving in a few days, I think I'll end up just connecting that up to the RPi 3+ board as-is and then move the small blue board over to the Pico or Arduino - that way I can experiment with 3-way comms.

I also want to see if I can NOT use CircuitPython? Can I do this natively? Can I do this and have it working from Pico C-code or from Arduino C-code.... there in lies the "simple" challenge.  The fact I've got this far along the journey, I'm pretty sure the rest will be smooth sailing..... LOL...


UPDATE:

Well....I got bored watching WOLF .... so I decided to get the Arduino UNO R4 and the second board setup after I found this website:

https://www.electronics-lab.com/project/introduction-lora-send-data-two-arduino-using-lora/

I setup the wiring as per the original connections and as shown below:


I had to download the [LoRa] library v0.8.0 and then used the code from the above website to test the Receiver code.  As you can see the Serial Monitor outputs that all looking good.

I shall now test to see if I can receive a message from the first board........

HA!


Well, would you take a look at that! Okay, I need to clean up the code or the debug output - but it does look like we have a winner!  I have the Arduino UNO R4 connected up and doing some receiving!

Maybe later in the week, I'll hook it up to a breadboard, some buttons and can then do some back and forth.  Awesome, looks like I got the right LoRa library and can use Arduino C-code without having to use CircuitPython etc... sweet.

(Hmmm....I wonder if I could do something using Node-Red too.... hmmmm....)

Here's the receiver code from above:

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

//define the pins used by the transceiver module
#define ss D4 //D8 D4 //CS
#define rst D2 //D1 D2 //RST
#define dio0 D3 //D2 D3 //INT

//no need to tell anything about MISO/MOSI as its all default

void setup() {
//initialize Serial Monitor
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");

//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0);
//replace the LoRa.begin(---E-) argument with your location's frequency
//433E6 for Asia
//866E6 for Europe/Africa
//915E6 for North America
if(!LoRa.begin(915E6)) { //915Mhz
// if(!LoRa.begin(868E6)) {
Serial.println(".");
while (1);
}
// Change sync word (0xF3) to match the receiver
// The sync word assures you don't get LoRa messages from other LoRa transceivers
// ranges from 0-0xFF
// LoRa.setSyncWord(0xF3);
//we shall add this back in so we get "secure" messages
Serial.println("LoRa Initializing OK!");
}

void loop()
{

String LoRaData;
int packetSize = LoRa.parsePacket();
if (packetSize)
{
// received a packet
Serial.print("Received packet '");

// read packet
while (LoRa.available())
{
LoRaData = LoRa.readString();
//LoRa Receiver
//Received packet '��A: COMMS Connect
//seems to have got 2 dodgy chars at the start - it's actually 4, need to investigate further
Serial.print(LoRaData);
}

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

UPDATE:

I did modify the code above to listen for a specific message and then return a transmission message back - except in my haste I seemed to have not saved the code.  simple enough to redo.

I received the "other" LoRa board with the OLED and plugged it into the RPi3+ device, got it setup in less than 5mins with same code and had it sending/receiving with the LoRa node module - just amusing that as the device already has an OLED screen for outputting networking info, the same info is output onto both screens - will have to change that, but for now, it's good to see it works and the button pressing does the job:



Comments