mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
add LoRa
This commit is contained in:
parent
33809f569d
commit
b16489b42e
@ -8,20 +8,23 @@
|
||||
|
||||
|
||||
#include "fdrs_sensor.h"
|
||||
#include "sensor_setup.h"
|
||||
|
||||
FDRSLoRa FDRS(GTWY_MAC,READING_ID,MISO,MOSI,SCK,SS,RST,DIO0,BAND,SF);
|
||||
|
||||
float data1;
|
||||
float data2;
|
||||
|
||||
void setup() {
|
||||
beginFDRS();
|
||||
FDRS.begin();
|
||||
}
|
||||
void loop() {
|
||||
data1 = readHum();
|
||||
loadFDRS(data1, HUMIDITY_T);
|
||||
FDRS.load(data1, HUMIDITY_T);
|
||||
data2 = readTemp();
|
||||
loadFDRS(data2, TEMP_T);
|
||||
sendFDRS();
|
||||
sleepFDRS(10); //Sleep time in seconds
|
||||
FDRS.load(data2, TEMP_T);
|
||||
FDRS.send();
|
||||
FDRS.sleep(10); //Sleep time in seconds
|
||||
}
|
||||
|
||||
float readTemp() {
|
||||
|
@ -9,8 +9,6 @@
|
||||
#define READING_ID 1 //Unique ID for this sensor
|
||||
#define GTWY_MAC 0x04 //Address of the nearest gateway
|
||||
|
||||
//#define USE_ESPNOW
|
||||
#define USE_LORA
|
||||
#define DEEP_SLEEP
|
||||
//#define POWER_CTRL 14
|
||||
#define DEBUG
|
||||
|
@ -32,44 +32,28 @@ FDRSBase::~FDRSBase(){
|
||||
|
||||
void FDRSBase::begin() {
|
||||
#ifdef DEBUG
|
||||
Serial.begin(115200);
|
||||
Serial.begin(115200);
|
||||
#endif
|
||||
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
|
||||
DBG(" Gateway: " + String (_gtwy_mac, HEX));
|
||||
DBG("FDRS Sensor ID " + String(READING_ID) + " initializing...");
|
||||
DBG(" Gateway: " + String (_gtwy_mac, HEX));
|
||||
#ifdef POWER_CTRL
|
||||
DBG("Powering up the sensor array!");
|
||||
pinMode(POWER_CTRL, OUTPUT);
|
||||
digitalWrite(POWER_CTRL, 1);
|
||||
DBG("Powering up the sensor array!");
|
||||
pinMode(POWER_CTRL, OUTPUT);
|
||||
digitalWrite(POWER_CTRL, 1);
|
||||
#endif
|
||||
|
||||
init();
|
||||
|
||||
|
||||
}
|
||||
// void transmitLoRa(uint8_t* mac, DataReading * packet, uint8_t len) {
|
||||
// #ifdef USE_LORA
|
||||
// uint8_t pkt[5 + (len * sizeof(DataReading))];
|
||||
// memcpy(&pkt, mac, 3); //
|
||||
// memcpy(&pkt[3], &LoRaAddress, 2);
|
||||
// memcpy(&pkt[5], packet, len * sizeof(DataReading));
|
||||
// LoRa.beginPacket();
|
||||
// LoRa.write((uint8_t*)&pkt, sizeof(pkt));
|
||||
// LoRa.endPacket();
|
||||
// #endif
|
||||
// }
|
||||
|
||||
void FDRSBase::send(void) {
|
||||
DBG("Sending FDRS Packet!");
|
||||
|
||||
if(_data_count == 0){
|
||||
return;
|
||||
}
|
||||
|
||||
transmit(fdrsData,_data_count);
|
||||
|
||||
#ifdef USE_LORA
|
||||
transmitLoRa(gtwyAddress, fdrsData, _data_count);
|
||||
DBG(" LoRa sent.");
|
||||
#endif
|
||||
_data_count = 0;
|
||||
}
|
||||
|
||||
@ -154,36 +138,50 @@ void FDRS_EspNow::transmit(DataReading *fdrsData, uint8_t _data_count){
|
||||
DBG(" ESP-NOW sent.");
|
||||
}
|
||||
|
||||
FDRSLoRa::FDRSLoRa(uint8_t gtwy_mac,uint8_t reading_id):
|
||||
FDRSBase(gtwy_mac,reading_id)
|
||||
FDRSLoRa::FDRSLoRa(uint8_t gtwy_mac, uint8_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf):
|
||||
FDRSBase(gtwy_mac,reading_id),
|
||||
_miso(miso),
|
||||
_mosi(mosi),
|
||||
_sck(sck),
|
||||
_ss(ss),
|
||||
_rst(rst),
|
||||
_dio0(dio0),
|
||||
_band(band),
|
||||
_sf(sf)
|
||||
{
|
||||
_gtwyAddress[0] = prefix[3];
|
||||
_gtwyAddress[1] = prefix[4];
|
||||
_gtwyAddress[2] = _gtwy_mac;
|
||||
_gatewayAddress[0] = prefix[3];
|
||||
_gatewayAddress[1] = prefix[4];
|
||||
_gatewayAddress[2] = gtwy_mac;
|
||||
}
|
||||
|
||||
|
||||
void FDRSLoRa::init(void){
|
||||
|
||||
DBG("Initializing LoRa!");
|
||||
DBG(BAND);
|
||||
DBG(SF);
|
||||
DBG(_band);
|
||||
DBG(_sf);
|
||||
#ifndef __AVR__
|
||||
SPI.begin(SCK, MISO, MOSI, SS);
|
||||
SPI.begin(_sck, _miso, _mosi, _ss);
|
||||
#endif
|
||||
LoRa.setPins(SS, RST, DIO0);
|
||||
if (!LoRa.begin(FDRS_BAND)) {
|
||||
LoRa.setPins(_ss, _rst, _dio0);
|
||||
if (!LoRa.begin(_band)) {
|
||||
DBG("LoRa Initialize Failed.");
|
||||
while (1);
|
||||
}
|
||||
LoRa.setSpreadingFactor(FDRS_SF);
|
||||
DBG("LoRa Initialized.");
|
||||
#endif
|
||||
LoRa.setSpreadingFactor(_sf);
|
||||
DBG("LoRa Initialized.");
|
||||
}
|
||||
|
||||
void FDRSLoRa::buildPacket(uint8_t* mac, DataReading * packet, uint8_t len) {
|
||||
uint8_t pkt[5 + (len * sizeof(DataReading))];
|
||||
memcpy(&pkt, mac, 3); //
|
||||
memcpy(&pkt[3], &LoRaAddress, 2);
|
||||
memcpy(&pkt[5], packet, len * sizeof(DataReading));
|
||||
LoRa.beginPacket();
|
||||
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
|
||||
LoRa.endPacket();
|
||||
}
|
||||
|
||||
void FDRSLoRa::transmit(DataReading *fdrsData, uint8_t _data_count){
|
||||
esp_now_send(_gatewayAddress, (uint8_t *) fdrsData, _data_count * sizeof(DataReading));
|
||||
delay(5);
|
||||
DBG(" ESP-NOW sent.");
|
||||
buildPacket(_gatewayAddress, fdrsData, _data_count);
|
||||
DBG(" LoRa sent.");
|
||||
}
|
@ -110,10 +110,22 @@ private:
|
||||
|
||||
class FDRSLoRa: public FDRSBase{
|
||||
public:
|
||||
FDRSLoRa(uint8_t gtwy_mac, uint8_t reading_id);
|
||||
FDRSLoRa(uint8_t gtwy_mac, uint8_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck, uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf);
|
||||
private:
|
||||
|
||||
uint8_t _gatewayAddress[LORA_GATEWAY_ADDRESS_SIZE];
|
||||
uint8_t _miso;
|
||||
uint8_t _mosi;
|
||||
uint8_t _sck;
|
||||
uint8_t _ss;
|
||||
uint8_t _rst;
|
||||
uint8_t _dio0;
|
||||
uint32_t _band;
|
||||
uint8_t _sf;
|
||||
|
||||
void buildPacket(uint8_t* mac, DataReading * packet, uint8_t len);
|
||||
|
||||
|
||||
void transmit(DataReading *fdrsData, uint8_t _data_count) override;
|
||||
void init(void) override;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user