Merge branch 'main' into dev

This commit is contained in:
Timm Bogner 2022-07-24 20:28:54 -05:00
commit 7e208cbc9b
5 changed files with 152 additions and 38 deletions

View File

@ -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)
@ -86,6 +86,7 @@ void setup() {
}
void loop() {
handleCommands();
#ifdef ESPNOWG_DELAY
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
timeESPNOWG = millis();

View File

@ -19,6 +19,13 @@ enum {
event_lora1,
event_lora2
};
enum {
cmd_clear,
cmd_ping,
cmd_add,
};
#ifdef FDRS_DEBUG
#define DBG(a) (Serial.println(a))
#else
@ -64,6 +71,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};
@ -98,10 +110,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;
DataReading ESPNOW1buffer[256];
uint8_t lenESPNOW1 = 0;
@ -159,6 +173,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.");
@ -198,39 +218,39 @@ void getSerial() {
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
void releaseLogBuffer()
{
#ifdef USE_SD_LOG
#ifdef USE_SD_LOG
DBG("Releasing Log buffer to SD");
File logfile = SD.open(SD_FILENAME, FILE_WRITE);
logfile.print(logBuffer);
logfile.close();
#endif
#ifdef USE_FS_LOG
#endif
#ifdef USE_FS_LOG
DBG("Releasing Log buffer to internal flash.");
File logfile = LittleFS.open(FS_FILENAME, "a");
logfile.print(logBuffer);
logfile.close();
#endif
memset(&(logBuffer[0]), 0, sizeof(logBuffer)/sizeof(char));
#endif
memset(&(logBuffer[0]), 0, sizeof(logBuffer) / sizeof(char));
logBufferPos = 0;
}
#endif
void sendLog()
{
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
DBG("Logging to buffer");
for (int i = 0; i < ln; i++)
{
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);
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();
}
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
logBufferPos+=strlen(linebuf);
logBufferPos += strlen(linebuf);
}
#endif
#endif
}
void reconnect(short int attempts, bool silent) {
#ifdef USE_WIFI
@ -325,13 +345,13 @@ void getLoRa() {
void sendESPNOW(uint8_t address) {
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;
@ -343,13 +363,13 @@ 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);
esp_now_send(temp_peer, (uint8_t *) &thePacket, j * sizeof(DataReading));
esp_now_del_peer(temp_peer);
}
void sendSerial() {
@ -619,12 +639,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) {
@ -643,20 +663,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.");
}
@ -710,4 +730,35 @@ void begin_FS() {
#endif
}
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__

View File

@ -17,6 +17,7 @@ void setup() {
beginFDRS();
}
void loop() {
pingFDRS(1000);
data1 = readHum();
loadFDRS(data1, HUMIDITY_T);
data2 = readTemp();

View File

@ -57,6 +57,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};
@ -65,6 +76,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 debugConfig() {
@ -118,13 +150,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;
@ -211,3 +248,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
}

View File

@ -5,12 +5,13 @@
#include <fdrs_globals.h>
#define READING_ID 1 //Unique ID for this sensor
#define GTWY_MAC 0x01 //Address of the nearest gateway
#define GTWY_MAC 0x04 //Address of the nearest gateway
#define USE_ESPNOW
//#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)