YALP (yet another LoRa/LoRaWAN project)

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
chromo23
Normal user
Posts: 827
Joined: 10 Sep 2020, 16:02
Location: germany

YALP (yet another LoRa/LoRaWAN project)

#1 Post by chromo23 » 16 Apr 2024, 12:16

Dear community,

Today I want to present my latest project to you:

YALP (yet another lora project :))


Motivation:

I built a couple of beehive scales for friends of mine some years ago. I am no beekeeper myself but I do like honey and now I have more than I can eat. :)

All these scales were based on a Raspberry Pi which brought some issues along the way.
One is high energy consumption when using a RPi because these scales are bound to a power supply.
The other issue is the data transmission via WiFi, which limits the range and adds to the power consumption.
Later I stumbled upon ESPEasy which opened the option to reduce power consumption and therefore get rid of a power supply using a chargeable battery instead. But since the Wifi range issue remained I waited patiently until someone would integrate LoRa and/or LoRaWAN in form of a cheap module like the SX1276…. (… I am still waiting :) )
In the meantime, I took matters into my own hands and hacked together a solution.


Goals:

- affordable
- commonly available parts
- easy to assemble
- runs basically forever
- the possibility to also use LoRa (without the WAN) with my own Gateway
- easily adjustable software


What I did:

I started with creating a board in KiCad.
The first attempt had no integrated battery holder.
In the second attempt I added a battery holder and an option for charging circuitry including a port for a solar cell.
I also wanted to save costs and made the board multi-functional so that I can choose between node and gateway using the same board.
After assembly I realized, that I used the wrong LoRa template in KiCad (SX1278 instead of SX1276).
But in my third attempt, I got it almost right. I only had to reroute RX and TX to the Hardware UART of the ESP in the gateway option.
pcb.png
pcb.png (214.25 KiB) Viewed 194 times
After that I tried to bundle a software package where one can easily choose between LoRa (client or gateway), LoRaWan and a set of given sensors.
While the LoRa implementation worked almost out of the box, LoRaWAN made me almost giving up and starting from scratch.
Since one of my goals is affordability, I used the Arduino Pro Mini. Not necessarily because it costs a bit less than other MCUs but also because I still had a lot of them lying around. (Lesson learned: Don’t pick your hardware without knowing which obstacles this brings software wise because of hardware limitations)
The LoraWAN library is quite heavy memory wise so it took me some time to understand what to do (I am no programmer nor do I know a lot about hardware but I love to learn :) ).
I had to reduce the size of memory allocated for the payload and to disable the serial communication. (Which made debugging a bit harder)

With Tinkercad I then created the housing. Not the most sophisticated piece of cad software but I am very pragmatic and it is good for creating simple things quickly. The top part consists of 2 parts which adds a layer of protection against water. (I did extensive tests with a bowl of water :) )
housing.png
housing.png (338.74 KiB) Viewed 194 times

Issues:

1. Using the Atmega328P was not the smartest choice. Memory wise and also because using a watchdog timer only gives you 10 different time settings to choose from where 8 seconds is the maximum. Therefor I had to add a counter which puts the MCU to sleep immediately after waking up until the counter meets the actual wakeup time value which is of course always a multiple of 8s. (I never used the watchdog of this chip so I didn’t know about this limitation)

2. Initially I wanted to use a MOSFET for cutting power to all peripherals while asleep. Since I only had N-channel MOSFET lying around I used this one even though I had a hunch, that this might not work as expected but it didn’t add up the to the total cost so I gave it a try.
…and it didn’t work :)
Next iteration of the board will get a high side switch (P-channel MOSFET)


Power consumption and runtime:

I was a little bit afraid because of the aforementioned issues regarding power consumption and the resulting runtime.
But luckily the peripherals went to sleep nicely and the current draw while sleeping is less than 20uA. Waking the MCU up for a fraction of time every 8 seconds adds to power consumption too but I calculated an average current draw of 30uA. That’s not great but given the circumstances I am fine with it.
Awake (500ms), the system draws in average 40mA where 300ms is for gathering the data (10mA) and around 200ms for sending (100mA). (These are rough estimations more than exact measurements)

This all becomes negligible when we add the solar panel to the equation since even in the shade it charges the battery more than the circuit consumes.
Battery1000mah.png
Battery1000mah.png (26.52 KiB) Viewed 194 times
(https://www.of-things.de/battery-life-calculator.php)


What does all this have to do with ESPEasy?

Well, not so much but it is the only forum where I am „active“ and I use the power of ESPEasy for my own LoRa- Gateway.
The PCB is designed to act as a client as well as a gateway. Both setups use the Atmega328P for sending and receiving.
The gateway also uses either a Wemos D1 mini (esp8266)…. „Use the ESP32 instead“ I hear the voice of TD-er in my head but I have a lot of them lying around and there are definitely up for the task. There is also the pin compatible version of the ESP32 that could be used instead.

The Atmega sends the received values to ESPEasy as a CSV and there the Serial Proxy Plugin (P087) receives it and creates an event for further rules processing.

For testing I used these rules combined with easyfetch for nicer visualization:
lora_easyfetch.png
lora_easyfetch.png (31.98 KiB) Viewed 194 times

Code: Select all

On SerialProxyXX* Do
  AsyncEvent,ValueParser=%eventvalue1%
  Pulse,2,0,50 //pulse the internal led when a message is received
Endon

On ValueParser Do
 // Expected values from serial input, 1 = Counter, 2 = Node, 3 = Voltage, 4 = Temp1
 //5 = Temp2, 6 = Weight, 7 = RSSI, 8 = SNR

  If %eventvalue2% = 1 //Node #1
  
  //check if there was a packet lost
    Let,1,[Node1#Count]+1 
    If %eventvalue1% > [var#1] and %eventvalue1% > 0
      TaskValueSet,Lost_packets,1,[Lost_packets#Node1]+1
      PostToHTTP,ntfy.envs.net,80,/FailedLoRa,'title:LoRa_1','We missed one on Node1 !!!!!!!'
    Endif

      TaskValueSet,Node1,Count,%eventvalue1%
      TaskValueSet,Node1,Voltage,%eventvalue3%
      TaskValueSet,Node1,RSSI,%eventvalue7%
      TaskValueSet,Node1,SNR,%eventvalue8%
      
      TaskValueSet,last_seen,1,%syshour%.%sysmin_0%%syssec_0%

  Elseif %eventvalue2% = 2
  
   //check if there was a packet lost
    Let,2,[Node2#Count]+1 
    If %eventvalue1% > [var#2] and %eventvalue1% > 0
      TaskValueSet,Lost_packets,2,[Lost_packets#Node2]+1
      PostToHTTP,ntfy.envs.net,80,/FailedLoRa,'title:LoRa_1','We missed one on Node2 !!!!!!!'
    endif

    TaskValueSet,Node2,Count,%eventvalue1%
    TaskValueSet,Node2,Voltage,%eventvalue3%
    TaskValueSet,Node2,RSSI,%eventvalue7%
    TaskValueSet,Node2,SNR,%eventvalue8%

    TaskValueSet,last_seen,2,%syshour%.%sysmin_0%%syssec_0%  

  Endif
Endon

Software preparations and configuration:

1. Download the lora_2v2 folder from https://github.com/chromoxdor/LoRa-and-LoRaWAN
2. Put the libraries (/libs) into your Arduino library folder
3. Open the lora_2v2.ino sketch

The options to choose from in the „options“ section are mostly self-explanatory.
For simple LoRa communication you can also choose to use simple encryption (AES128) and a common phrase.
Make sure the password and the common phrase are the same for gateway and client!
The common phrase can be used to separate multiple gateway/node setups since the gateway only reacts if it has the same phrase than its clients.


Hardware:

…coming soon... KiCad files will be provided at the repository when I am back home


Future:

I am not sure if we have a future….
But next time I´ll probably us a Esp32 module as a replacement for the Atmega328P


Did I forget something?

Probably a lot! So, if interested please don’t hesitate to ask. :)
parts.png
parts.png (306.46 KiB) Viewed 187 times
Last edited by chromo23 on 16 Apr 2024, 13:49, edited 2 times in total.

User avatar
chromo23
Normal user
Posts: 827
Joined: 10 Sep 2020, 16:02
Location: germany

Re: YALP (yet another LoRa/LoRaWAN project)

#2 Post by chromo23 » 16 Apr 2024, 12:18

Pics:

3d sketch.png
3d sketch.png (194.37 KiB) Viewed 193 times
pcb_node.png
pcb_node.png (196.71 KiB) Viewed 193 times
pcb_gate.png
pcb_gate.png (174.8 KiB) Viewed 193 times
nodes1.JPG
nodes1.JPG (199.43 KiB) Viewed 193 times
Gateway.JPG
Gateway.JPG (167.1 KiB) Viewed 193 times
node_solar.JPG
node_solar.JPG (127.26 KiB) Viewed 193 times
node_conn.JPG
node_conn.JPG (147.5 KiB) Viewed 193 times

TD-er
Core team member
Posts: 8762
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: YALP (yet another LoRa/LoRaWAN project)

#3 Post by TD-er » 16 Apr 2024, 13:46

Looks really nice.
Just wondering, do you just use LoRa or LoRaWAN?
The SX12xx chip just does LoRa and LoRaWAN is adding some extra layers of authentication, encryption, etc.

I do have support for the RN2483 module, which is essentially some ATmega + SX12xx chip in a module and a certified LoRaWAN stack running on the ATmega.
However this is mainly focussed on TTN and needs some gateway to send uplinks to.
Also it is running on the ESP, which isn't really power efficient.

So if you don't have a LoRaWAN gateway, I guess using some p2p protocol you thought of yourself and thus using just LoRa is perfectly fine for a setup like this.


Just some extra hints for LoRa communications:

Line-of-sight is not just a "line". What needs to be free from obstacles becomes more wide inbetween the sender and receiver.
This "line-of-sight" is more shaped like a muscle.
This also implies that you can greatly extend the range by putting at least one end at a higher altitude.

Each step up from SF7 to SF12 will double the time needed to send the same amount of data.
However it can improve the actual range if both ends are stationary.

Sending at 868 MHz is quite sensitive to "polarisation". So try to orient the antennas in the same orientation (e.g. pointing "up")

You're only allowed to send 1% of the time (on some channels more, but just keep the 1% rule in mind).
So if you use a higher spread factor (SF) you need to lower the number of messages you will send.

User avatar
chromo23
Normal user
Posts: 827
Joined: 10 Sep 2020, 16:02
Location: germany

Re: YALP (yet another LoRa/LoRaWAN project)

#4 Post by chromo23 » 16 Apr 2024, 14:03

TD-er wrote: 16 Apr 2024, 13:46 Looks really nice.
Thanks :)
TD-er wrote: 16 Apr 2024, 13:46 Just wondering, do you just use LoRa or LoRaWAN?
I also use LoRaWAN with this Library: https://github.com/ElectronicCats/Beelan-LoRaWAN
I stripped it a bit and got rid of the receiving part for the node since the reply windows are quite long (1s) for class a and i wanted the atmega to sleep as much as it can.

You can easily choose how you want to use LoRa.
This is what the options of my sketch look like:

Code: Select all

//**********************************************************************************************
//************* OPTIONS ************************************************************************

// Common Setting Section ---------------------------------------------------------------
#define LORA_FREQUENCY 868E6  // Frequency in Hz (433E6, 868E6, 915E6)
#define NODE_NR "2"           // Give your node a number or a name

// LoRa Section ------------------------------------------------------------------------
#define LORA 0          // Is LoRa enabled? (0 or 1)
#define TX_RETRIES 2    // How often should the LoRa node try to send when no ACK is received until \
                        // the system goes to sleep again (interval for retry is 8 seconds). \
                        // It will then return to its given "sleep-time" interval.
#define LORA_GATEWAY 0  // Is it a Gateway? (0 or 1)

#define ENCRYPTION 1                   // Add simple AES128 encryption to LoRa
#define PASSWORD "XXXXXXXXXXXXXXXXXX"  // Enter a password and make sure it matches the one of \
                                       // the gateway/node
#define COMMON_PHRASE "CXD"            // Gateway checks if received message contains this phrase \
                                       // at the end and only reacts to that
#define MAX_PAYLOAD_SIZE 50            // Define maximum payload size

// LoRaWAN Section ----------------------------------------------------------------------
// NOTE: Define your maximum payload size for LoRaWAN (MAX_UPLINK_PAYLOAD_SIZE)
// in the config.h of the LoRaWAN library
#define LORAWAN 1
#define DEVEUI "0000000000000000"
#define APPEUI "0000000000000000"
#define APPKEY "00000000000000000000000000000000"
#define JOIN_RETRY 100  // How often should the node try to join a gateway until it will go \
                        // to sleep indefinitely

// Sensor Section ------------------------------------------------------------------------
#define SCALE 1
#define CALIBWEIGHT 1000  // Weight used for calibrating the scale in grams

#define SHT 0  // Use an SHT2X I2C device

#define DS18B20_TEMP 0   // Set to 1 if one or multiple sensors are connected to one pin (8)
#define DS18B20_TEMP2 1  // Set to 1 if one or two sensors (on different pins) are connected \
                         // sensor1 (8) sensor2 (A3)

// Extras Section ------------------------------------------------------------------------
#define SLEEP_TIME 0         // Sleep time in minutes (setting the sleep time to 0 will cause an 8-second interval)
#define ACK_BEEP 0           // If an acknowledge beep is needed when data was sent
#define SERIAL 1             // Save some memory if not needed since it is only for debugging
#define LONGPRESS_S 5        // Seconds to long press event
#define SERIAL_SPEED 115200  // Baud rate (e.g., 9600, 19200, 38400, 57600, 115200)
#define PARITY SERIAL_8N1    // Set serial mode (e.g., SERIAL_8N1, SERIAL_8E1, SERIAL_8O1)
//**********************************************************************************************
//**********************************************************************************************
and this is the TTN formater for thingspeak i use:

Code: Select all

function Decoder(bytes, port) {
  var payload = "";
  for (var i = 0; i < bytes.length; i++) {
    payload += String.fromCharCode(bytes[i]);
  }

  // Split the payload by ","
  var values = payload.split(",");

  // Define field names
  var fieldNames = [
    "field1",
    "field2",
    "field3",
    "field4",
    "field5",
    "field6",
    "field7",
    "field8"
  ];

  // Create an object to hold the decoded values
  var decodedData = {};

  // Assign values to corresponding fields
  for (var j = 0; j < Math.min(values.length, fieldNames.length); j++) {
    decodedData[fieldNames[j]] = values[j];
  }

  return decodedData;
}
TD-er wrote: 16 Apr 2024, 13:46 I do have support for the RN2483 module, which is essentially some ATmega + SX12xx chip in a module and a certified LoRaWAN stack running on the ATmega.
I know but i wanted a more versatile option for my needs and i also wanted to learn a bit...
And also the RN2483 didn´t meet my goals :)
TD-er wrote: 16 Apr 2024, 13:46 Just some extra hints for LoRa communications:
Thanks for adding these infos!
I use SF9 as it is a good compromise...
TD-er wrote: 16 Apr 2024, 13:46 You're only allowed to send 1% of the time (on some channels more, but just keep the 1% rule in mind).
So if you use a higher spread factor (SF) you need to lower the number of messages you will send
This is a good website for this:
https://avbentem.github.io/airtime-calculator/ttn/eu868

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: YALP (yet another LoRa/LoRaWAN project)

#5 Post by ThomasB » 17 Apr 2024, 18:43

Cool project. Thanks for sharing the details.

- Thomas

User avatar
Ath
Normal user
Posts: 3531
Joined: 10 Jun 2018, 12:06
Location: NL

Re: YALP (yet another LoRa/LoRaWAN project)

#6 Post by Ath » 17 Apr 2024, 20:09

That's a great project, and all the implementation details, and decisions you made, make it a very inspiring read. Thanks!
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 18 guests