SapFlow Probe
A low-cost HRM probe for measuring a tree's water consumption
lora.cpp File Reference
#include "lora.h"
#include <RHReliableDatagram.h>
#include <SPI.h>
#include <RH_RF95.h>
#include <ArduinoJson.h>
#include <ftoa.h>

Go to the source code of this file.

Functions

RH_RF95 rf95 (RFM95_CS, RFM95_INT)
 
static RHReliableDatagram manager (rf95, CLIENT_ADDRESS)
 
void lora_init (void)
 Initialize the LoRa radio. More...
 
void build_msg (float flow, char *weight, float temp, float maxtemp)
 Builds a JSON string to send over LoRa. More...
 
void send_msg (void)
 Sends a LoRa packet to the base station. More...
 

Variables

static int16_t packetnum = 0
 
static char radiopacket [RH_RF95_MAX_MESSAGE_LEN]
 
static uint8_t packet_len
 

Function Documentation

◆ build_msg()

void build_msg ( float  flow,
char *  weight,
float  temp,
float  maxtemp 
)

Builds a JSON string to send over LoRa.

Builds a JSON string containing sapflow, weight, temperature, time, and tree ID. The string is stored in a global variable to be read by send_msg()

Parameters
flowThe calculated sapflow
weightThe text received from the scale. Use "0" if scale is not connected.
tempThe baseline temperature of the tree
timeThe date and time from the Real-Time Clock

Definition at line 58 of file lora.cpp.

59 {
60  Serial.print("Building message...");
61  const int capacity=JSON_OBJECT_SIZE(10);
62  StaticJsonDocument<capacity>doc;
63  char str1[15];
64  char str2[15];
65  char str3[15];
66  char * tstring = rtc_ds.now().text();
67  tstring += 11; //< skip the date, just use the time
68  ftoa(flow, str1);
69  ftoa(temp, str3);
70  ftoa(maxtemp, str2);
71  doc["flow"].set(str1);
72  doc["weight"].set("0");
73  doc["temp"].set(str3);
74  doc["maxtemp"].set(str2);
75  doc["id"].set("0");
76  doc["time"].set(tstring);
77  packet_len = serializeJson(doc,radiopacket);
78  Serial.println(radiopacket);
79  radiopacket[packet_len] = 0;
80 }

◆ lora_init()

void lora_init ( void  )

Initialize the LoRa radio.

This function turns on the radio, sets the frequency, and prepares it for use. It does not take any parameters.

Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

The default transmitter power is 13dBm, using PA_BOOST. If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then you can set transmitter powers from 5 to 23 dBm:

Definition at line 17 of file lora.cpp.

18 {
19  digitalWrite(RFM95_CS, LOW); //< enable LoRa
20  pinMode(RFM95_RST, OUTPUT);
21  digitalWrite(RFM95_RST, HIGH);
22 
23 
24  // manual reset
25  digitalWrite(RFM95_RST, LOW);
26  delay(10);
27  digitalWrite(RFM95_RST, HIGH);
28  delay(10);
29 
30  while (!manager.init()) {
31  Serial.println("LoRa radio init failed");
32  Serial.println("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info");
33  while (1);
34  }
35  Serial.println("LoRa radio init OK!");
36 
37  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
38  if (!rf95.setFrequency(RF95_FREQ)) {
39  Serial.println("setFrequency failed");
40  while (1);
41  }
42  Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
43 
49  rf95.setTxPower(23, false);
50  digitalWrite(RFM95_CS, HIGH); //< disable LoRa
51 }

◆ send_msg()

void send_msg ( void  )

Sends a LoRa packet to the base station.

Sends the string made by build_json() to the base station over LoRa. Calling this function without first calling build_msg() produces undefined behavior.

Definition at line 83 of file lora.cpp.

84 {
85  digitalWrite(RFM95_CS, LOW); //< enable LoRa
86  Serial.println("Transmitting..."); //< Send a message to rf95_server
87  // Send a message to manager_server
88  if (manager.sendtoWait((uint8_t *)radiopacket, packet_len, SERVER_ADDRESS))
89  {
90  // Now wait for a reply from the server
91  uint8_t from;
92  if (manager.recvfromAckTimeout((uint8_t *)radiopacket, &packet_len, 2000, &from))
93  {
94  Serial.print("got reply from : 0x");
95  Serial.print(from, HEX);
96  Serial.print(": ");
97  Serial.println(radiopacket);
98  Serial.print("RSSI: ");
99  Serial.println(rf95.lastRssi(), DEC);
100  }
101  else
102  {
103  Serial.println("No reply, is server running?");
104  }
105  }
106  else
107  Serial.println("sendtoWait failed");
108 
109  digitalWrite(RFM95_CS, HIGH); //< disable LoRa
110 }
rtc_ds
static RTC_DS3231 rtc_ds
Instance of our real-time clock.
Definition: schedule.h:11
RFM95_RST
@ RFM95_RST
Reset pin used for LoRa. Output. Active-low?
Definition: pinout.h:29
RF95_FREQ
#define RF95_FREQ
Radio frequency (in MHz).
Definition: lora.h:14
RFM95_CS
@ RFM95_CS
SPI chip select used for LoRa. Output, Active-low.
Definition: pinout.h:28
SERVER_ADDRESS
#define SERVER_ADDRESS
The LoRa address of the base station.
Definition: lora.h:7