mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-10 07:10:42 +00:00
Merge branch 'dev' into dev
This commit is contained in:
commit
a8e8d41de6
@ -35,8 +35,8 @@
|
|||||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#endif
|
#endif
|
||||||
#include <fdrs_functions.h> //Use global functions file
|
//#include <fdrs_functions.h> //Use global functions file
|
||||||
//#include "fdrs_functions.h" //Use local functions file
|
#include "fdrs_functions.h" //Use local functions file
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
@ -98,6 +98,7 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
handleCommands();
|
||||||
#ifdef ESPNOWG_DELAY
|
#ifdef ESPNOWG_DELAY
|
||||||
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
|
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
|
||||||
timeESPNOWG = millis();
|
timeESPNOWG = millis();
|
||||||
|
@ -20,6 +20,13 @@ enum {
|
|||||||
event_lora2
|
event_lora2
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
cmd_clear,
|
||||||
|
cmd_ping,
|
||||||
|
cmd_add,
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef FDRS_DEBUG
|
#ifdef FDRS_DEBUG
|
||||||
#define DBG(a) (Serial.println(a))
|
#define DBG(a) (Serial.println(a))
|
||||||
#else
|
#else
|
||||||
@ -132,6 +139,11 @@ typedef struct __attribute__((packed)) DataReading {
|
|||||||
|
|
||||||
} DataReading;
|
} DataReading;
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)) SystemPacket {
|
||||||
|
uint8_t cmd;
|
||||||
|
uint32_t param;
|
||||||
|
} SystemPacket;
|
||||||
|
|
||||||
const uint8_t espnow_size = 250 / sizeof(DataReading);
|
const uint8_t espnow_size = 250 / sizeof(DataReading);
|
||||||
const uint8_t lora_size = 256 / sizeof(DataReading);
|
const uint8_t lora_size = 256 / sizeof(DataReading);
|
||||||
const uint8_t mac_prefix[] = {MAC_PREFIX};
|
const uint8_t mac_prefix[] = {MAC_PREFIX};
|
||||||
@ -166,10 +178,12 @@ char logBuffer[512];
|
|||||||
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
|
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
|
||||||
uint32_t timeLOGBUF = 0;
|
uint32_t timeLOGBUF = 0;
|
||||||
#endif
|
#endif
|
||||||
|
SystemPacket theCmd;
|
||||||
DataReading theData[256];
|
DataReading theData[256];
|
||||||
uint8_t ln;
|
uint8_t ln;
|
||||||
uint8_t newData = event_clear;
|
uint8_t newData = event_clear;
|
||||||
|
uint8_t newCmd = cmd_clear;
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_ESPNOW
|
#ifdef USE_ESPNOW
|
||||||
DataReading ESPNOW1buffer[256];
|
DataReading ESPNOW1buffer[256];
|
||||||
@ -235,6 +249,12 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
|||||||
}
|
}
|
||||||
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||||
#endif
|
#endif
|
||||||
|
if (len < sizeof(DataReading)) {
|
||||||
|
DBG("ESP-NOW System Packet");
|
||||||
|
memcpy(&theCmd, incomingData, sizeof(theCmd));
|
||||||
|
memcpy(&incMAC, mac, sizeof(incMAC));
|
||||||
|
return;
|
||||||
|
}
|
||||||
memcpy(&theData, incomingData, sizeof(theData));
|
memcpy(&theData, incomingData, sizeof(theData));
|
||||||
memcpy(&incMAC, mac, sizeof(incMAC));
|
memcpy(&incMAC, mac, sizeof(incMAC));
|
||||||
DBG("Incoming ESP-NOW.");
|
DBG("Incoming ESP-NOW.");
|
||||||
@ -277,40 +297,41 @@ void getSerial() {
|
|||||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||||
void releaseLogBuffer()
|
void releaseLogBuffer()
|
||||||
{
|
{
|
||||||
#ifdef USE_SD_LOG
|
#ifdef USE_SD_LOG
|
||||||
DBG("Releasing Log buffer to SD");
|
DBG("Releasing Log buffer to SD");
|
||||||
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
|
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
|
||||||
logfile.print(logBuffer);
|
logfile.print(logBuffer);
|
||||||
logfile.close();
|
logfile.close();
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_FS_LOG
|
#ifdef USE_FS_LOG
|
||||||
DBG("Releasing Log buffer to internal flash.");
|
DBG("Releasing Log buffer to internal flash.");
|
||||||
File logfile = LittleFS.open(FS_FILENAME, "a");
|
File logfile = LittleFS.open(FS_FILENAME, "a");
|
||||||
logfile.print(logBuffer);
|
logfile.print(logBuffer);
|
||||||
logfile.close();
|
logfile.close();
|
||||||
#endif
|
#endif
|
||||||
memset(&(logBuffer[0]), 0, sizeof(logBuffer)/sizeof(char));
|
memset(&(logBuffer[0]), 0, sizeof(logBuffer) / sizeof(char));
|
||||||
logBufferPos = 0;
|
logBufferPos = 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void sendLog()
|
void sendLog()
|
||||||
{
|
{
|
||||||
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
|
||||||
DBG("Logging to buffer");
|
DBG("Logging to buffer");
|
||||||
for (int i = 0; i < ln; i++)
|
for (int i = 0; i < ln; i++)
|
||||||
{
|
{
|
||||||
char linebuf[34]; // size depends on resulting length of the formatting string
|
char linebuf[34]; // size depends on resulting length of the formatting string
|
||||||
sprintf(linebuf, "%lld,%d,%d,%g\r\n", time(nullptr), theData[i].id, theData[i].t, theData[i].d);
|
sprintf(linebuf, "%lld,%d,%d,%g\r\n", time(nullptr), theData[i].id, theData[i].t, theData[i].d);
|
||||||
|
|
||||||
if (logBufferPos+strlen(linebuf) >= (sizeof(logBuffer)/sizeof(char))) // if buffer would overflow, release first
|
if (logBufferPos + strlen(linebuf) >= (sizeof(logBuffer) / sizeof(char))) // if buffer would overflow, release first
|
||||||
{
|
{
|
||||||
releaseLogBuffer();
|
releaseLogBuffer();
|
||||||
}
|
}
|
||||||
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
|
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
|
||||||
logBufferPos+=strlen(linebuf);
|
logBufferPos += strlen(linebuf);
|
||||||
}
|
}
|
||||||
#endif //USE_xx_LOG
|
#endif //USE_xx_LOG
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect(short int attempts, bool silent) {
|
void reconnect(short int attempts, bool silent) {
|
||||||
@ -410,13 +431,13 @@ void getLoRa() {
|
|||||||
void sendESPNOW(uint8_t address) {
|
void sendESPNOW(uint8_t address) {
|
||||||
#ifdef USE_ESPNOW
|
#ifdef USE_ESPNOW
|
||||||
DBG("Sending ESP-NOW.");
|
DBG("Sending ESP-NOW.");
|
||||||
uint8_t NEWPEER[] = {MAC_PREFIX, address};
|
uint8_t temp_peer[] = {MAC_PREFIX, address};
|
||||||
#if defined(ESP32)
|
#if defined(ESP32)
|
||||||
esp_now_peer_info_t peerInfo;
|
esp_now_peer_info_t peerInfo;
|
||||||
peerInfo.ifidx = WIFI_IF_STA;
|
peerInfo.ifidx = WIFI_IF_STA;
|
||||||
peerInfo.channel = 0;
|
peerInfo.channel = 0;
|
||||||
peerInfo.encrypt = false;
|
peerInfo.encrypt = false;
|
||||||
memcpy(peerInfo.peer_addr, NEWPEER, 6);
|
memcpy(peerInfo.peer_addr, temp_peer, 6);
|
||||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||||
DBG("Failed to add peer");
|
DBG("Failed to add peer");
|
||||||
return;
|
return;
|
||||||
@ -428,14 +449,16 @@ void sendESPNOW(uint8_t address) {
|
|||||||
for (int i = 0; i < ln; i++) {
|
for (int i = 0; i < ln; i++) {
|
||||||
if ( j > espnow_size) {
|
if ( j > espnow_size) {
|
||||||
j = 0;
|
j = 0;
|
||||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, sizeof(thePacket));
|
esp_now_send(temp_peer, (uint8_t *) &thePacket, sizeof(thePacket));
|
||||||
}
|
}
|
||||||
thePacket[j] = theData[i];
|
thePacket[j] = theData[i];
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
esp_now_send(NEWPEER, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
|
||||||
esp_now_del_peer(NEWPEER);
|
|
||||||
#endif //USE_ESPNOW
|
esp_now_send(temp_peer, (uint8_t *) &thePacket, j * sizeof(DataReading));
|
||||||
|
esp_now_del_peer(temp_peer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendSerial() {
|
void sendSerial() {
|
||||||
@ -720,12 +743,12 @@ void begin_espnow() {
|
|||||||
esp_now_register_send_cb(OnDataSent);
|
esp_now_register_send_cb(OnDataSent);
|
||||||
esp_now_register_recv_cb(OnDataRecv);
|
esp_now_register_recv_cb(OnDataRecv);
|
||||||
// Register peers
|
// Register peers
|
||||||
#ifdef ESPNOW1_PEER
|
//#ifdef ESPNOW1_PEER
|
||||||
esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
// esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||||
#endif
|
//#endif
|
||||||
#ifdef ESPNOW2_PEER
|
//#ifdef ESPNOW2_PEER
|
||||||
esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
// esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||||
#endif
|
//#endif
|
||||||
#elif defined(ESP32)
|
#elif defined(ESP32)
|
||||||
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
|
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
|
||||||
if (esp_now_init() != ESP_OK) {
|
if (esp_now_init() != ESP_OK) {
|
||||||
@ -744,20 +767,20 @@ void begin_espnow() {
|
|||||||
DBG("Failed to add peer bcast");
|
DBG("Failed to add peer bcast");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#ifdef ESPNOW1_PEER
|
//#ifdef ESPNOW1_PEER
|
||||||
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
|
// memcpy(peerInfo.peer_addr, ESPNOW1, 6);
|
||||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
// if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||||
DBG("Failed to add peer 1");
|
// DBG("Failed to add peer 1");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
#endif
|
//#endif
|
||||||
#ifdef ESPNOW2_PEER
|
//#ifdef ESPNOW2_PEER
|
||||||
memcpy(peerInfo.peer_addr, ESPNOW2, 6);
|
// memcpy(peerInfo.peer_addr, ESPNOW2, 6);
|
||||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
// if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||||
DBG("Failed to add peer 2");
|
// DBG("Failed to add peer 2");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
#endif
|
//#endif
|
||||||
#endif //ESP8266
|
#endif //ESP8266
|
||||||
DBG(" ESP-NOW Initialized.");
|
DBG(" ESP-NOW Initialized.");
|
||||||
#endif //USE_ESPNOW
|
#endif //USE_ESPNOW
|
||||||
@ -811,4 +834,35 @@ void begin_FS() {
|
|||||||
#endif // USE_FS_LOG
|
#endif // USE_FS_LOG
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleCommands() {
|
||||||
|
switch (theCmd.cmd) {
|
||||||
|
case cmd_ping:
|
||||||
|
DBG("Ping back to sender");
|
||||||
|
SystemPacket sys_packet;
|
||||||
|
sys_packet.cmd = cmd_ping;
|
||||||
|
#if defined(ESP32)
|
||||||
|
esp_now_peer_info_t peerInfo;
|
||||||
|
peerInfo.ifidx = WIFI_IF_STA;
|
||||||
|
peerInfo.channel = 0;
|
||||||
|
peerInfo.encrypt = false;
|
||||||
|
memcpy(peerInfo.peer_addr, incMAC, 6);
|
||||||
|
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||||
|
DBG("Failed to add peer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
esp_now_send(incMAC, (uint8_t *) &sys_packet, sizeof(SystemPacket));
|
||||||
|
esp_now_del_peer(incMAC);
|
||||||
|
break;
|
||||||
|
case cmd_add:
|
||||||
|
DBG("Add sender to peer list (not completed)");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
theCmd.cmd = cmd_clear;
|
||||||
|
theCmd.param = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif //__FDRS_FUNCTIONS_H__
|
#endif //__FDRS_FUNCTIONS_H__
|
||||||
|
@ -18,6 +18,7 @@ void setup() {
|
|||||||
beginFDRS();
|
beginFDRS();
|
||||||
}
|
}
|
||||||
void loop() {
|
void loop() {
|
||||||
|
pingFDRS(1000);
|
||||||
data1 = readHum();
|
data1 = readHum();
|
||||||
loadFDRS(data1, HUMIDITY_T);
|
loadFDRS(data1, HUMIDITY_T);
|
||||||
data2 = readTemp();
|
data2 = readTemp();
|
||||||
|
@ -61,6 +61,17 @@ typedef struct __attribute__((packed)) DataReading {
|
|||||||
|
|
||||||
} DataReading;
|
} DataReading;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
cmd_clear,
|
||||||
|
cmd_ping,
|
||||||
|
cmd_add,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)) SystemPacket {
|
||||||
|
uint8_t cmd;
|
||||||
|
uint32_t param;
|
||||||
|
} SystemPacket;
|
||||||
|
|
||||||
const uint16_t espnow_size = 250 / sizeof(DataReading);
|
const uint16_t espnow_size = 250 / sizeof(DataReading);
|
||||||
uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
|
uint8_t gatewayAddress[] = {MAC_PREFIX, GTWY_MAC};
|
||||||
uint8_t gtwyAddress[] = {gatewayAddress[3], gatewayAddress[4], GTWY_MAC};
|
uint8_t gtwyAddress[] = {gatewayAddress[3], gatewayAddress[4], GTWY_MAC};
|
||||||
@ -69,6 +80,27 @@ uint8_t LoRaAddress[] = {0x42, 0x00};
|
|||||||
uint32_t wait_time = 0;
|
uint32_t wait_time = 0;
|
||||||
DataReading fdrsData[espnow_size];
|
DataReading fdrsData[espnow_size];
|
||||||
uint8_t data_count = 0;
|
uint8_t data_count = 0;
|
||||||
|
bool is_ping = false;
|
||||||
|
|
||||||
|
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||||
|
#if defined(ESP8266)
|
||||||
|
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
|
||||||
|
}
|
||||||
|
void OnDataRecv(uint8_t* mac, uint8_t *incomingData, uint8_t len) {
|
||||||
|
#elif defined(ESP32)
|
||||||
|
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
||||||
|
}
|
||||||
|
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||||
|
#endif
|
||||||
|
if (len < sizeof(DataReading)) {
|
||||||
|
SystemPacket command;
|
||||||
|
memcpy(&command, incomingData, sizeof(command));
|
||||||
|
if (command.cmd == cmd_ping) {
|
||||||
|
is_ping = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void beginFDRS() {
|
void beginFDRS() {
|
||||||
@ -95,13 +127,18 @@ void beginFDRS() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
|
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
|
||||||
|
esp_now_register_recv_cb(OnDataRecv);
|
||||||
|
|
||||||
// Register peers
|
// Register peers
|
||||||
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
esp_now_add_peer(gatewayAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||||
#elif defined(ESP32)
|
#elif defined(ESP32)
|
||||||
|
|
||||||
if (esp_now_init() != ESP_OK) {
|
if (esp_now_init() != ESP_OK) {
|
||||||
DBG("Error initializing ESP-NOW");
|
DBG("Error initializing ESP-NOW");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
esp_now_register_recv_cb(OnDataRecv);
|
||||||
|
|
||||||
esp_now_peer_info_t peerInfo;
|
esp_now_peer_info_t peerInfo;
|
||||||
peerInfo.ifidx = WIFI_IF_STA;
|
peerInfo.ifidx = WIFI_IF_STA;
|
||||||
peerInfo.channel = 0;
|
peerInfo.channel = 0;
|
||||||
@ -191,3 +228,26 @@ void sleepFDRS(int sleep_time) {
|
|||||||
DBG(" Delaying.");
|
DBG(" Delaying.");
|
||||||
delay(sleep_time * 1000);
|
delay(sleep_time * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void pingFDRS(int timeout) {
|
||||||
|
SystemPacket sys_packet;
|
||||||
|
sys_packet.cmd = cmd_ping;
|
||||||
|
#ifdef USE_ESPNOW
|
||||||
|
esp_now_send(gatewayAddress, (uint8_t *) &sys_packet, sizeof(SystemPacket));
|
||||||
|
DBG(" ESP-NOW ping sent.");
|
||||||
|
uint32_t ping_start = millis();
|
||||||
|
is_ping = false;
|
||||||
|
while ((millis() - ping_start) <= timeout) {
|
||||||
|
yield(); //do I need to yield or does it automatically?
|
||||||
|
if (is_ping) {
|
||||||
|
DBG("Ping Returned:" + String(millis() - ping_start));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#ifdef USE_LORA
|
||||||
|
//transmitLoRa(gtwyAddress, sys_packet, data_count); // TODO: Make this congruent to esp_now_send()
|
||||||
|
DBG(" LoRa ping not sent because it isn't implemented.");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
//#define USE_LORA
|
//#define USE_LORA
|
||||||
#define DEEP_SLEEP
|
#define DEEP_SLEEP
|
||||||
//#define POWER_CTRL 14
|
//#define POWER_CTRL 14
|
||||||
|
#define DYNAMIC_GTWY
|
||||||
#define FDRS_DEBUG
|
#define FDRS_DEBUG
|
||||||
|
|
||||||
//SPI Configuration -- Needed only on chipsets with multiple SPI interfaces (ESP32)
|
//SPI Configuration -- Needed only on chipsets with multiple SPI interfaces (ESP32)
|
||||||
|
Loading…
Reference in New Issue
Block a user