mirror of
https://github.com/timmbogner/Farm-Data-Relay-System
synced 2024-11-08 13:10:29 +00:00
subscription list and unified callback
the functionality is almost complete
This commit is contained in:
parent
3654502afd
commit
1501817975
@ -76,6 +76,8 @@
|
||||
//#define LORA1_DELAY 1000
|
||||
//#define LORA2_DELAY 1000
|
||||
|
||||
#define PEER_TIMEOUT 300000
|
||||
|
||||
// FastLED -- Not yet fully implemented
|
||||
//#define USE_LED
|
||||
#define LED_PIN 32
|
||||
@ -94,4 +96,4 @@
|
||||
// MQTT Topics
|
||||
#define TOPIC_DATA "fdrs/data"
|
||||
#define TOPIC_STATUS "fdrs/status"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
||||
#define TOPIC_COMMAND "fdrs/command"
|
@ -97,8 +97,8 @@ void setup() {
|
||||
client.publish(TOPIC_STATUS, "FDRS initialized");
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
handleCommands();
|
||||
#ifdef ESPNOWG_DELAY
|
||||
if ((millis() - timeESPNOWG) >= ESPNOWG_DELAY) {
|
||||
timeESPNOWG = millis();
|
||||
|
@ -11,8 +11,8 @@
|
||||
// Current function options are: sendESPNOW(MAC), sendSerial(), sendMQTT(),
|
||||
// bufferLoRa(interface), bufferESPNOW(interface), bufferSerial(), and bufferMQTT().
|
||||
|
||||
#define ESPNOWG_ACT sendSerial(); sendESPNOWpeers();
|
||||
#define SERIAL_ACT sendESPNOW(0x02); bufferLoRa(1);
|
||||
#define ESPNOWG_ACT sendSerial();
|
||||
#define SERIAL_ACT sendESPNOW(0x02); bufferLoRa(1); sendESPNOWpeers();
|
||||
#define MQTT_ACT
|
||||
#define LORAG_ACT sendSerial();
|
||||
|
||||
@ -73,7 +73,7 @@
|
||||
//#define SERIAL_DELAY 0
|
||||
//#define MQTT_DELAY 0
|
||||
//#define LORAG_DELAY 1000
|
||||
#define LORA1_DELAY 500
|
||||
//#define LORA1_DELAY 500
|
||||
//#define LORA2_DELAY 1000
|
||||
|
||||
#define PEER_TIMEOUT 300000
|
||||
|
@ -9,42 +9,23 @@
|
||||
#include "fdrs_sensor_config.h"
|
||||
#include <fdrs_node.h>
|
||||
|
||||
float data1;
|
||||
float data2;
|
||||
|
||||
|
||||
|
||||
void ctrl_1_cb(DataReading* theData) {
|
||||
DBG("Controller1");
|
||||
}
|
||||
void ctrl_2_cb(DataReading* theData) {
|
||||
DBG("Controller2");
|
||||
}
|
||||
void ctrl_3_cb(DataReading* theData) {
|
||||
DBG("Controller3");
|
||||
}
|
||||
void ctrl_4_cb(DataReading* theData) {
|
||||
DBG("Controller4");
|
||||
void fdrs_recv_cb(DataReading theData) {
|
||||
DBG("ID: " + String(theData.id));
|
||||
DBG("Type: " + String(theData.t));
|
||||
DBG("Data: " + String(theData.d));
|
||||
}
|
||||
|
||||
void setup() {
|
||||
beginFDRS();
|
||||
addFDRS(1000);
|
||||
pingFDRS(1000);
|
||||
//pingFDRS(1000);
|
||||
addFDRS(1000, fdrs_recv_cb);
|
||||
subscribeFDRS(READING_ID);
|
||||
}
|
||||
void loop() {
|
||||
// data1 = readHum();
|
||||
// loadFDRS(data1, HUMIDITY_T);
|
||||
// data2 = readTemp();
|
||||
// loadFDRS(data2, TEMP_T);
|
||||
// sendFDRS();
|
||||
// sleepFDRS(10); //Sleep time in seconds
|
||||
}
|
||||
|
||||
float readTemp() {
|
||||
return 22.069;
|
||||
}
|
||||
|
||||
float readHum() {
|
||||
return random(0, 100);
|
||||
// data1 = readHum();
|
||||
// loadFDRS(data1, HUMIDITY_T);
|
||||
// data2 = readTemp();
|
||||
// loadFDRS(data2, TEMP_T);
|
||||
// sendFDRS();
|
||||
// sleepFDRS(10); //Sleep time in seconds
|
||||
}
|
||||
|
@ -7,12 +7,6 @@
|
||||
#define READING_ID 1 //Unique ID for this sensor
|
||||
#define GTWY_MAC 0x01 //Address of the nearest gateway
|
||||
|
||||
#define CONTROL_1 133 //Address for controller 1
|
||||
#define CONTROL_2 134 //Address for controller 2
|
||||
#define CONTROL_3 135 //Address for controller 3
|
||||
#define CONTROL_4 136 //Address for controller 4
|
||||
|
||||
|
||||
#define USE_ESPNOW
|
||||
//#define USE_LORA
|
||||
//#define DEEP_SLEEP
|
||||
|
@ -645,7 +645,7 @@ int findOpenPeer() { // Returns an expired entry in peer_list, -1 if full.
|
||||
DBG("No open peers");
|
||||
return -1;
|
||||
}
|
||||
int checkPeerExpired() { // Checks whether any entries in the peer_list have expired
|
||||
int checkPeerExpired() { // Checks whether any entries in the peer_list have expired. Not currently used.
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if ((millis() - peer_list[i].last_seen) >= PEER_TIMEOUT) {
|
||||
esp_now_del_peer(incMAC);
|
||||
|
@ -100,10 +100,9 @@ uint8_t data_count = 0;
|
||||
bool is_ping = false;
|
||||
bool is_added = false;
|
||||
uint32_t last_refresh;
|
||||
void ctrl_1_cb(DataReading* reading);
|
||||
void ctrl_2_cb(DataReading* reading);
|
||||
void ctrl_3_cb(DataReading* reading);
|
||||
void ctrl_4_cb(DataReading* reading);
|
||||
void (*callback_ptr)(DataReading);
|
||||
uint16_t subscription_list[256] = {};
|
||||
bool active_subs[256] = {};
|
||||
|
||||
// Set ESP-NOW send and receive callbacks for either ESP8266 or ESP32
|
||||
#if defined(ESP8266)
|
||||
@ -133,22 +132,15 @@ void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
} else{
|
||||
memcpy(&incData, incomingData, len);
|
||||
int pkt_readings = len / sizeof(DataReading);
|
||||
for (int i; i <= pkt_readings; i++) { //Cycle through array of incoming DataReadings for any addressed to this device
|
||||
switch (incData[i].id) {
|
||||
case CONTROL_1:
|
||||
ctrl_1_cb(&incData[i]);
|
||||
break;
|
||||
case CONTROL_2:
|
||||
ctrl_2_cb(&incData[i]);
|
||||
break;
|
||||
case CONTROL_3:
|
||||
ctrl_3_cb(&incData[i]);
|
||||
break;
|
||||
case CONTROL_4:
|
||||
ctrl_4_cb(&incData[i]);
|
||||
break;
|
||||
for (int i = 0; i <= pkt_readings; i++) { //Cycle through array of incoming DataReadings for any addressed to this device
|
||||
for (int j = 0; j < 255; j++){ //Cycle through subscriptions for active entries
|
||||
if (active_subs[j]){
|
||||
if (incData[i].id == subscription_list[j]){
|
||||
(*callback_ptr)(incData[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -468,17 +460,20 @@ bool seekFDRS(int timeout) {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool addFDRS(int timeout) {
|
||||
bool addFDRS(int timeout, void (*new_cb_ptr)(DataReading)) {
|
||||
|
||||
callback_ptr = new_cb_ptr;
|
||||
|
||||
SystemPacket sys_packet = { .cmd = cmd_add, .param = 0 };
|
||||
#ifdef USE_ESPNOW
|
||||
esp_now_send(gatewayAddress, (uint8_t *) &sys_packet, sizeof(SystemPacket));
|
||||
DBG("ESP-NOW peer subscription submitted to " + String(gatewayAddress[5]));
|
||||
DBG("ESP-NOW peer registration request submitted to " + String(gatewayAddress[5]));
|
||||
uint32_t add_start = millis();
|
||||
is_added = false;
|
||||
while ((millis() - add_start) <= timeout) {
|
||||
yield();
|
||||
if (is_added) {
|
||||
DBG("Subscription accepted. Timeout: " + String(gtwy_timeout));
|
||||
DBG("Registration accepted. Timeout: " + String(gtwy_timeout));
|
||||
last_refresh = millis();
|
||||
return true;
|
||||
}
|
||||
@ -508,3 +503,36 @@ uint32_t pingFDRS(int timeout) {
|
||||
DBG(" LoRa ping not sent because it isn't implemented.");
|
||||
#endif
|
||||
}
|
||||
bool subscribeFDRS(uint16_t sub_id){
|
||||
for(int i = 0; i < 255; i++){
|
||||
if ((subscription_list[i] == sub_id) && (active_subs[i])){
|
||||
DBG("You're already subscribed to that!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < 255; i++){
|
||||
if (!active_subs[i]){
|
||||
DBG("Adding subscription at position " + String(i));
|
||||
subscription_list[i] = sub_id;
|
||||
active_subs[i] = true;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
DBG("No subscription could be established!");
|
||||
return false;
|
||||
|
||||
}
|
||||
bool unsubscribeFDRS(uint16_t sub_id){
|
||||
for(int i = 0; i < 255; i++){
|
||||
if ((subscription_list[i] == sub_id) && (active_subs[i])){
|
||||
DBG("Removing subscription.");
|
||||
active_subs[i] = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
DBG("No subscription to remove");
|
||||
return false;
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user