Farm-Data-Relay-System/examples/Universal_Sensor_beta/fdrs_sensor.cpp

182 lines
4.3 KiB
C++
Raw Normal View History

2022-06-20 19:50:02 +00:00
#include "fdrs_sensor.h"
const uint8_t prefix[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE}; // Should only be changed if implementing multiple FDRS systems.
uint8_t LoRaAddress[] = {0x42, 0x00};
uint32_t wait_time = 0;
2022-07-11 01:38:43 +00:00
FDRSBase::FDRSBase(uint8_t gtwy_mac,uint16_t reading_id):
2022-06-20 19:50:02 +00:00
_gtwy_mac(gtwy_mac),
2022-06-24 16:21:39 +00:00
_espnow_size(250 / sizeof(DataReading_t)),
2022-06-20 19:50:02 +00:00
_reading_id(reading_id),
_data_count(0)
{
2022-06-24 16:21:39 +00:00
fdrsData = new DataReading_t[_espnow_size];
2022-06-20 19:50:02 +00:00
}
FDRSBase::~FDRSBase(){
delete[] fdrsData;
}
void FDRSBase::begin() {
#ifdef FDRS_DEBUG
2022-06-20 20:24:45 +00:00
Serial.begin(115200);
2022-06-20 19:50:02 +00:00
#endif
2022-06-24 16:22:05 +00:00
DBG("FDRS Sensor ID " + String(_reading_id) + " initializing...");
2022-06-20 20:24:45 +00:00
DBG(" Gateway: " + String (_gtwy_mac, HEX));
2022-06-20 19:50:02 +00:00
#ifdef POWER_CTRL
2022-06-20 20:24:45 +00:00
DBG("Powering up the sensor array!");
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
2022-06-20 19:50:02 +00:00
#endif
init();
}
2022-06-20 20:24:45 +00:00
2022-06-20 19:50:02 +00:00
void FDRSBase::send(void) {
DBG("Sending FDRS Packet!");
if(_data_count == 0){
return;
}
transmit(fdrsData,_data_count);
_data_count = 0;
}
void FDRSBase::load(float data, uint8_t type) {
2022-06-24 16:22:05 +00:00
DBG("Data loaded. Type: " + String(type));
2022-06-20 19:50:02 +00:00
if (_data_count > _espnow_size){
send();
}
2022-06-24 16:21:39 +00:00
DataReading_t dr;
2022-06-20 19:50:02 +00:00
dr.id = _reading_id;
dr.type = type;
dr.data = data;
fdrsData[_data_count] = dr;
_data_count++;
}
void FDRSBase::sleep(int seconds){
DBG("Sleepytime!");
#ifdef DEEP_SLEEP
DBG(" Deep sleeping.");
#ifdef ESP32
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
#endif
#ifdef ESP8266
ESP.deepSleep(sleep_time * 1000000);
#endif
#endif
DBG(" Delaying.");
delay(seconds * 1000);
}
2022-07-11 01:38:43 +00:00
FDRS_EspNow::FDRS_EspNow(uint8_t gtwy_mac,uint16_t reading_id):
2022-06-20 19:50:02 +00:00
FDRSBase(gtwy_mac,reading_id)
{
memcpy(_gatewayAddress,MAC_PREFIX,5);
_gatewayAddress[5] = gtwy_mac;
}
void FDRS_EspNow::init(void){
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
DBG("Initializing ESP-NOW!");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
#if defined(ESP8266)
if (esp_now_init() != 0) {
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Register peers
esp_now_add_peer(_gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
#elif defined(ESP32)
if(esp_now_init() != ESP_OK){
DBG("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
peerInfo.ifidx = WIFI_IF_STA;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register first peer
memcpy(peerInfo.peer_addr, _gatewayAddress, 6);
if(esp_now_add_peer(&peerInfo) != ESP_OK) {
DBG("Failed to add peer");
return;
}
#endif
DBG(" ESP-NOW Initialized.");
}
2022-06-24 16:21:39 +00:00
void FDRS_EspNow::transmit(DataReading_t *fdrsData, uint8_t _data_count){
esp_now_send(_gatewayAddress, (uint8_t *) fdrsData, _data_count * sizeof(DataReading_t));
2022-06-20 19:50:02 +00:00
delay(5);
DBG(" ESP-NOW sent.");
}
2022-06-20 20:38:28 +00:00
#ifdef USE_LORA
2022-06-20 20:42:46 +00:00
FDRSLoRa::FDRSLoRa(uint8_t gtwy_mac,
2022-07-11 01:38:43 +00:00
uint16_t reading_id,uint8_t miso,uint8_t mosi,uint8_t sck,
2022-06-20 20:42:46 +00:00
uint8_t ss,uint8_t rst,uint8_t dio0,uint32_t band,uint8_t sf):
2022-06-20 20:24:45 +00:00
FDRSBase(gtwy_mac,reading_id),
_miso(miso),
_mosi(mosi),
_sck(sck),
_ss(ss),
_rst(rst),
_dio0(dio0),
_band(band),
_sf(sf)
2022-06-20 19:50:02 +00:00
{
2022-06-20 20:24:45 +00:00
_gatewayAddress[0] = prefix[3];
_gatewayAddress[1] = prefix[4];
_gatewayAddress[2] = gtwy_mac;
2022-06-20 19:50:02 +00:00
}
void FDRSLoRa::init(void){
DBG("Initializing LoRa!");
2022-06-20 20:24:45 +00:00
DBG(_band);
DBG(_sf);
2022-06-30 13:30:24 +00:00
#ifdef ESP32
2022-07-07 10:38:57 +00:00
SPI.begin(_sck, _miso, _mosi);
2022-06-20 19:50:02 +00:00
#endif
2022-06-20 20:24:45 +00:00
LoRa.setPins(_ss, _rst, _dio0);
if (!LoRa.begin(_band)) {
2022-06-20 19:50:02 +00:00
DBG("LoRa Initialize Failed.");
while (1);
}
2022-06-20 20:24:45 +00:00
LoRa.setSpreadingFactor(_sf);
DBG("LoRa Initialized.");
}
2022-06-20 19:50:02 +00:00
2022-06-24 16:21:39 +00:00
void FDRSLoRa::buildPacket(uint8_t* mac, DataReading_t * packet, uint8_t len) {
uint8_t pkt[5 + (len * sizeof(DataReading_t))];
2022-06-20 20:24:45 +00:00
memcpy(&pkt, mac, 3); //
memcpy(&pkt[3], &LoRaAddress, 2);
2022-06-24 16:21:39 +00:00
memcpy(&pkt[5], packet, len * sizeof(DataReading_t));
2022-06-20 20:24:45 +00:00
LoRa.beginPacket();
LoRa.write((uint8_t*)&pkt, sizeof(pkt));
LoRa.endPacket();
2022-06-20 19:50:02 +00:00
}
2022-06-24 16:21:39 +00:00
void FDRSLoRa::transmit(DataReading_t *fdrsData, uint8_t _data_count){
2022-06-20 20:24:45 +00:00
buildPacket(_gatewayAddress, fdrsData, _data_count);
DBG(" LoRa sent.");
2022-06-20 20:38:28 +00:00
}
#endif