Wireless control over a long distance with the LoRa modem
A Practical Telemetry System
To demonstrate the utility of the LoRa modem module, I packaged two Raspberry Pis into waterproof plastic boxes. One Pi was set up as the transmitter running the switch application, with two control switches and an LED mounted externally (Figure 5). Toggling either switch causes a packet to be sent to the receiver. The LED flashes when the transmitter receives an ACK from the receiver.
The second Pi runs the relay application and has a small module with two relays [11] wired and mounted internally, and a single LED mounted externally that flashes on reception of a valid packet from the transmitter (Figure 6).
This is pretty much the simplest application of LoRa. You can configure a lot more, such as an example spreading factor, channel frequency, and bandwidth that allow you to tailor the radio link to a specific situation. I was able to outfit both modules to run on solar-charged battery packs (Figure 7).
Software
The software for this system builds on the test software described earlier. The complete code for both switch and relay applications, together with a makefile are available for download from my GitHub page [12]. Only the end-user parts of the code are listed here; for brevity, I have omitted error-reporting code.
Listing 1 shows the switch module. This module sets up the required GPIO pins and then enters a loop looking for pin changes from the external switches. In detecting a change, it sends a message to the receiver to turn on or off the associated relay.
Listing 1
The Switch
§§number 01 #include "SX1272.h" 02 #include <stdio.h> 03 #include <syslog.h> 04 05 int main () 06 { 07 // set up the LoRa modem 08 sx1272.ON(); 09 sx1272.setMode(4); 10 sx1272.setHeaderON(); 11 sx1272.setChannel(CH_17_868); 12 sx1272.setCRC_ON(); 13 sx1272.setPower('M'); 14 sx1272.setNodeAddress(3); 15 16 // set up GPIO: 2 inputs and one output 17 bcm2835_gpio_fsel(RPI_V2_GPIO_P1_18,BCM2835_GPIO_FSEL_INPT); 18 bcm2835_gpio_set_pud(RPI_V2_GPIO_P1_18,BCM2835_GPIO_PUD_UP); 19 bcm2835_gpio_fsel(RPI_V2_GPIO_P1_08, BCM2835_GPIO_FSEL_INPT); 20 bcm2835_gpio_set_pud(RPI_V2_GPIO_P1_08, BCM2835_GPIO_PUD_UP); 21 bcm2835_gpio_fsel(RPI_V2_GPIO_P1_16,BCM2835_GPIO_FSEL_OUTP); 22 23 // get the current switch states 24 uint8_t oldLev_08=bcm2835_gpio_lev(RPI_V2_GPIO_P1_08); 25 uint8_t oldLev_18=bcm2835_gpio_lev(RPI_V2_GPIO_P1_18); 26 27 while(1) 28 { 29 // get the new states 30 uint8_t lev_08=bcm2835_gpio_lev(RPI_V2_GPIO_P1_08); 31 uint8_t lev_18=bcm2835_gpio_lev(RPI_V2_GPIO_P1_18); 32 33 //if state is different, send message and update 34 if(lev_08 != oldLev_08) 35 { 36 char *msg=(char*)(!lev_08?"ON0":"OFF0"); 37 syslog(LOG_USER,msg); 38 uint8_t e = sx1272.sendPacketTimeoutACKRetries(8, msg); 39 40 if(e==0) 41 { 42 // flash the LED 43 bcm2835_gpio_set(RPI_V2_GPIO_P1_16); 44 bcm2835_delay(500); 45 bcm2835_gpio_clr(RPI_V2_GPIO_P1_16); 46 // update rthe state 47 oldLev_08=lev_08; 48 } 49 } 50 51 //if state is different, send message and update 52 if(lev_18 != oldLev_18) 53 { 54 char *msg = (char*)(!lev_18?"ON1":"OFF1"); 55 syslog(LOG_USER,msg); 56 uint8_t e=sx1272.sendPacketTimeoutACKRetries(8, msg); 57 58 if(e==0) 59 { 60 // flash the LED 61 bcm2835_gpio_set(RPI_V2_GPIO_P1_16); 62 bcm2835_delay(500); 63 bcm2835_gpio_clr(RPI_V2_GPIO_P1_16); 64 // update rthe state 65 oldLev_18=lev_18; 66 } 67 } 68 69 // wait a bit & repeat 70 bcm2835_delay(500); 71 } 72 }
Listing 2 shows the relay module, which sets up the required GPIO pins and then enters a loop listening for messages from the switch module. In detecting a message, it turns on or off the associated relay.
Listing 2
The Relay
§§number 01 #include "SX1272.h" 02 #include <stdio.h> 03 #include <string.h> 04 05 int main () 06 { 07 // set up the LoRa modem 08 sx1272.ON(); 09 sx1272.setMode(4); 10 sx1272.setHeaderON(); 11 sx1272.setChannel(CH_17_868); 12 sx1272.setCRC_ON(); 13 sx1272.setPower('M'); 14 sx1272.setNodeAddress(8); 15 16 // set up GPIO: 3 outputs 17 bcm2835_gpio_fsel(RPI_V2_GPIO_P1_26,BCM2835_GPIO_FSEL_OUTP); 18 bcm2835_gpio_fsel(RPI_V2_GPIO_P1_18,BCM2835_GPIO_FSEL_OUTP); 19 bcm2835_gpio_fsel(RPI_V2_GPIO_P1_16,BCM2835_GPIO_FSEL_OUTP); 20 21 // turn both relays off 22 bcm2835_gpio_clr(RPI_V2_GPIO_P1_18); 23 bcm2835_gpio_clr(RPI_V2_GPIO_P1_26); 24 25 while(1) 26 { 27 // wait for an incoming message and sens an ACK 28 if (sx1272.receivePacketTimeoutACK(10000) == 0) 29 { 30 // get the packet data and act on it 31 char *msg = (char *)sx1272.packet_received.data; 32 printf("%s\n", msg); 33 sx1272.getRSSI(); 34 35 if(strcmp("ON0", msg)==0) 36 { 37 bcm2835_gpio_set(RPI_V2_GPIO_P1_18); 38 } 39 40 if(strcmp("OFF0", msg)==0) 41 { 42 bcm2835_gpio_clr( RPI_V2_GPIO_P1_18); 43 } 44 45 if(strcmp("ON1", msg)==0) 46 { 47 bcm2835_gpio_set(RPI_V2_GPIO_P1_26); 48 } 49 50 if(strcmp("OFF1", msg)==0) 51 { 52 bcm2835_gpio_clr(RPI_V2_GPIO_P1_26); 53 } 54 55 // flash the LED 56 bcm2835_gpio_set(RPI_V2_GPIO_P1_16); 57 bcm2835_delay(500); 58 bcm2835_gpio_clr(RPI_V2_GPIO_P1_16); 59 } 60 } 61 }
Next Steps
For further development, I should point out that LoRa data flow is truly bidirectional. There's nothing to stop the remote node, in this case the relay node, from sending unsolicited packets to the switch node. So any node can be both a sender and a receiver. It is also possible to send broadcast packets to any node listening on the same channel, as well as to send unacknowledged packets, similar to TCP/IP's UDP packets. A complete, freely available WAN implementation called LoRaWAN [13] even turns a bunch of LoRa nodes and a gateway node into a true network, with semantics very similar to TCP/IP.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome Fans Everywhere Rejoice for the Latest Release
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.