mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
77 lines
1.9 KiB
C
77 lines
1.9 KiB
C
// FARM DATA RELAY SYSTEM
|
|
//
|
|
// "fdrs_sensor.h"
|
|
//
|
|
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
|
|
//
|
|
#define READING_ID 57 //Unique ID for controller
|
|
#define GTWY_MAC 0x00 //Gateway MAC
|
|
|
|
#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 <ArduinoJson.h>
|
|
#include <PubSubClient.h>
|
|
#include <LoRa.h>
|
|
|
|
typedef struct DataReading {
|
|
float d;
|
|
uint16_t id;
|
|
uint8_t t;
|
|
|
|
} DataReading;
|
|
const uint16_t espnow_size = 250 / sizeof(DataReading);
|
|
uint8_t gatewayAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
|
|
|
|
uint32_t wait_time = 0;
|
|
DataReading fdrsData[espnow_size];
|
|
uint8_t data_count = 0;
|
|
|
|
void beginFDRS() {
|
|
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
|
|
}
|
|
void sendFDRS() {
|
|
esp_now_send(gatewayAddress, (uint8_t *) &fdrsData, data_count * sizeof(DataReading));
|
|
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++;
|
|
|
|
}
|