SapFlow Probe
A low-cost HRM probe for measuring a tree's water consumption
lora.cpp
Go to the documentation of this file.
1 #include "lora.h"
2 
4 
5 #include <RHReliableDatagram.h>
6 #include <SPI.h>
7 #include <RH_RF95.h>
8 #include <ArduinoJson.h>
9 #include <ftoa.h> //< for float to string conversion
10 
11 //< Singleton instance of the radio driver
12 RH_RF95 rf95(RFM95_CS, RFM95_INT);
13 
14 // Class to manage message delivery and receipt, using the driver declared above
15 static RHReliableDatagram manager(rf95, CLIENT_ADDRESS);
16 
17 void lora_init(void)
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 }
52 
53 static int16_t packetnum = 0; //< packet counter, we increment per xmission
54 
55 static char radiopacket[RH_RF95_MAX_MESSAGE_LEN];
56 static uint8_t packet_len; //< Max message length is 251, so we won't overflow
57 
58 void build_msg(float flow, char * weight, float temp, float maxtemp)
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 }
81 
82 
83 void send_msg( void )
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 }
send_msg
void send_msg(void)
Sends a LoRa packet to the base station.
Definition: lora.cpp:83
rtc_ds
static RTC_DS3231 rtc_ds
Instance of our real-time clock.
Definition: schedule.h:11
lora_init
void lora_init(void)
Initialize the LoRa radio.
Definition: lora.cpp:17
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
RFM95_INT
@ RFM95_INT
Interrupt pin used for LoRa. Input. Active-low?
Definition: pinout.h:30
CLIENT_ADDRESS
#define CLIENT_ADDRESS
Our LoRa address.
Definition: lora.h:6
lora.h
build_msg
void build_msg(float flow, char *weight, float temp, float maxtemp)
Builds a JSON string to send over LoRa.
Definition: lora.cpp:58