Farm-Data-Relay-System/FDRS_Sensor2000/fdrs_sensor.h

101 lines
2.4 KiB
C
Raw Normal View History

2021-12-18 20:46:02 +00:00
// FARM DATA RELAY SYSTEM
//
// "fdrs_sensor.h"
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
//
2022-02-19 01:48:12 +00:00
#include "fdrs_config.h"
#include "DataReading.h"
2021-12-18 20:46:02 +00:00
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <espnow.h>
#elif defined(ESP32)
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#endif
#include <LoRa.h>
const uint16_t espnow_size = 250 / sizeof(DataReading);
2022-02-19 01:48:12 +00:00
uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
uint8_t gtwyAddress[] = {gatewayAddress[3], gatewayAddress[4], GTWY_MAC};
uint8_t LoRaAddress[] = {0x42, 0x00};
2021-12-18 20:46:02 +00:00
uint32_t wait_time = 0;
2022-02-19 01:48:12 +00:00
DataReading fdrsData[espnow_size];
uint8_t data_count = 0;
2021-12-18 20:46:02 +00:00
void beginFDRS() {
2022-02-19 01:48:12 +00:00
#ifdef USE_ESPNOW
2021-12-18 20:46:02 +00:00
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW for either ESP8266 or ESP32 and set MAC address
#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) {
Serial.println("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) {
Serial.println("Failed to add peer");
return;
}
#endif
2022-02-19 01:48:12 +00:00
#endif
#ifdef USE_LORA
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
while (1);
}
#endif
}
#ifdef USE_LORA
void transmitLoRa(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();
2021-12-18 20:46:02 +00:00
}
2022-02-19 01:48:12 +00:00
#endif
2021-12-18 20:46:02 +00:00
void sendFDRS() {
2022-02-19 01:48:12 +00:00
#ifdef USE_ESPNOW
2021-12-18 20:46:02 +00:00
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
2022-02-19 01:48:12 +00:00
#endif
#ifdef USE_LORA
transmitLoRa(gtwyAddress, fdrsData, data_count);
#endif
2021-12-18 20:46:02 +00:00
data_count = 0;
}
void loadFDRS(float d, uint8_t t) {
if (data_count > espnow_size) sendFDRS();
DataReading dr;
dr.id = READING_ID;
dr.t = t;
dr.d = d;
fdrsData[data_count] = dr;
data_count++;
}