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)
|
||||
#include <time.h>
|
||||
#endif
|
||||
#include <fdrs_functions.h> //Use global functions file
|
||||
//#include "fdrs_functions.h" //Use local functions file
|
||||
//#include <fdrs_functions.h> //Use global functions file
|
||||
#include "fdrs_functions.h" //Use local functions file
|
||||
|
||||
void setup() {
|
||||
#if defined(ESP8266)
|
||||
@ -98,6 +98,7 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
handleCommands();
|
||||
#ifdef ESPNOWG_DELAY
|
||||
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
|
||||
timeESPNOWG = millis();
|
||||
|
@ -20,6 +20,13 @@ enum {
|
||||
event_lora2
|
||||
};
|
||||
|
||||
|
||||
enum {
|
||||
cmd_clear,
|
||||
cmd_ping,
|
||||
cmd_add,
|
||||
};
|
||||
|
||||
#ifdef FDRS_DEBUG
|
||||
#define DBG(a) (Serial.println(a))
|
||||
#else
|
||||
@ -132,6 +139,11 @@ typedef struct __attribute__((packed)) 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 lora_size = 256 / sizeof(DataReading);
|
||||
const uint8_t mac_prefix[] = {MAC_PREFIX};
|
||||
@ -166,10 +178,12 @@ char logBuffer[512];
|
||||
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
|
||||
uint32_t timeLOGBUF = 0;
|
||||
#endif
|
||||
|
||||
SystemPacket theCmd;
|
||||
DataReading theData[256];
|
||||
uint8_t ln;
|
||||
uint8_t newData = event_clear;
|
||||
uint8_t newCmd = cmd_clear;
|
||||
|
||||
|
||||
#ifdef USE_ESPNOW
|
||||
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) {
|
||||
#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(&incMAC, mac, sizeof(incMAC));
|
||||
DBG("Incoming ESP-NOW.");
|
||||
@ -311,6 +331,7 @@ void sendLog()
|
||||
logBufferPos += strlen(linebuf);
|
||||
}
|
||||
#endif //USE_xx_LOG
|
||||
|
||||
}
|
||||
|
||||
void reconnect(short int attempts, bool silent) {
|
||||
@ -410,13 +431,13 @@ void getLoRa() {
|
||||
void sendESPNOW(uint8_t address) {
|
||||
#ifdef USE_ESPNOW
|
||||
DBG("Sending ESP-NOW.");
|
||||
uint8_t NEWPEER[] = {MAC_PREFIX, address};
|
||||
uint8_t temp_peer[] = {MAC_PREFIX, address};
|
||||
#if defined(ESP32)
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.ifidx = WIFI_IF_STA;
|
||||
peerInfo.channel = 0;
|
||||
peerInfo.encrypt = false;
|
||||
memcpy(peerInfo.peer_addr, NEWPEER, 6);
|
||||
memcpy(peerInfo.peer_addr, temp_peer, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer");
|
||||
return;
|
||||
@ -428,14 +449,16 @@ void sendESPNOW(uint8_t address) {
|
||||
for (int i = 0; i < ln; i++) {
|
||||
if ( j > espnow_size) {
|
||||
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];
|
||||
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() {
|
||||
@ -720,12 +743,12 @@ void begin_espnow() {
|
||||
esp_now_register_send_cb(OnDataSent);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
// Register peers
|
||||
#ifdef ESPNOW1_PEER
|
||||
esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
#endif
|
||||
#ifdef ESPNOW2_PEER
|
||||
esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
#endif
|
||||
//#ifdef ESPNOW1_PEER
|
||||
// esp_now_add_peer(ESPNOW1, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
//#endif
|
||||
//#ifdef ESPNOW2_PEER
|
||||
// esp_now_add_peer(ESPNOW2, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
|
||||
//#endif
|
||||
#elif defined(ESP32)
|
||||
esp_wifi_set_mac(WIFI_IF_STA, &selfAddress[0]);
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
@ -744,20 +767,20 @@ void begin_espnow() {
|
||||
DBG("Failed to add peer bcast");
|
||||
return;
|
||||
}
|
||||
#ifdef ESPNOW1_PEER
|
||||
memcpy(peerInfo.peer_addr, ESPNOW1, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer 1");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef ESPNOW2_PEER
|
||||
memcpy(peerInfo.peer_addr, ESPNOW2, 6);
|
||||
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
DBG("Failed to add peer 2");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
//#ifdef ESPNOW1_PEER
|
||||
// memcpy(peerInfo.peer_addr, ESPNOW1, 6);
|
||||
// if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
// DBG("Failed to add peer 1");
|
||||
// return;
|
||||
// }
|
||||
//#endif
|
||||
//#ifdef ESPNOW2_PEER
|
||||
// memcpy(peerInfo.peer_addr, ESPNOW2, 6);
|
||||
// if (esp_now_add_peer(&peerInfo) != ESP_OK) {
|
||||
// DBG("Failed to add peer 2");
|
||||
// return;
|
||||
// }
|
||||
//#endif
|
||||
#endif //ESP8266
|
||||
DBG(" ESP-NOW Initialized.");
|
||||
#endif //USE_ESPNOW
|
||||
@ -811,4 +834,35 @@ void begin_FS() {
|
||||
#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__
|
||||
|
@ -18,6 +18,7 @@ void setup() {
|
||||
beginFDRS();
|
||||
}
|
||||
void loop() {
|
||||
pingFDRS(1000);
|
||||
data1 = readHum();
|
||||
loadFDRS(data1, HUMIDITY_T);
|
||||
data2 = readTemp();
|
||||
|
@ -61,6 +61,17 @@ typedef struct __attribute__((packed)) 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);
|
||||
uint8_t gatewayAddress[] = {MAC_PREFIX, 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;
|
||||
DataReading fdrsData[espnow_size];
|
||||
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() {
|
||||
@ -95,13 +127,18 @@ void beginFDRS() {
|
||||
return;
|
||||
}
|
||||
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
|
||||
esp_now_register_recv_cb(OnDataRecv);
|
||||
|
||||
// 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_register_recv_cb(OnDataRecv);
|
||||
|
||||
esp_now_peer_info_t peerInfo;
|
||||
peerInfo.ifidx = WIFI_IF_STA;
|
||||
peerInfo.channel = 0;
|
||||
@ -191,3 +228,26 @@ void sleepFDRS(int sleep_time) {
|
||||
DBG(" Delaying.");
|
||||
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 DEEP_SLEEP
|
||||
//#define POWER_CTRL 14
|
||||
#define DYNAMIC_GTWY
|
||||
#define FDRS_DEBUG
|
||||
|
||||
//SPI Configuration -- Needed only on chipsets with multiple SPI interfaces (ESP32)
|
||||
|
Loading…
Reference in New Issue
Block a user