Skip to content
Snippets Groups Projects
Commit d88b8630 authored by Moritz Perschke's avatar Moritz Perschke
Browse files

implemented filtering of received messages at client by type

parent 05d00b43
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!6Espnow
......@@ -5,6 +5,15 @@ static const char *TAG = "ESPNOW";
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t hostInfo;
Preferences preferences;
bool msg_recv = false;
bool was_msg_received(){
if(msg_recv){
msg_recv = false;
return true;
}
return false;
}
void get_host_mac(uint8_t *destination)
{
......@@ -17,6 +26,16 @@ void get_host_mac(uint8_t *destination)
}
preferences.end();
}
esp_err_t add_host_to_peers(config received){
esp_now_peer_info_t host;
memset(&host, 0, sizeof(host));
memcpy(host.peer_addr, received.host, sizeof(received.host));
host.encrypt = false;
host.channel = 0;
return esp_now_add_peer(&host);
}
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
ESP_LOGE(TAG, "Message sent to");
......@@ -28,60 +47,49 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status)
// go to sleep
}
bool are_mac_addresses_equal(uint8_t first_mac[], uint8_t second_mac[]){
bool result = true;
for(int i=0; i<6; i++){
result && (first_mac[i] == second_mac[i]);
}
return result;
}
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
// is msg host -> yes -> set bool
// assume host change not happening, rare event
// => on host change, broadcast
ESP_LOGE(TAG, "Message recieved");
preferences.begin("config", false);
config new_config;
memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac
// all the esp32 macs so far use the same first 3(?) bytes so maybe use that
uint8_t stored[6];
preferences.getBytes("host", stored, sizeof(uint8_t) * 6);
// put the host address in flash mem
// this does not work as long as we don't know whether a message was received
// save received host address if there is none saved or received one is different than before
if (!preferences.isKey("host") || !are_mac_addresses_equal(stored, new_config.host)) {
preferences.putBytes("host", new_config.host, sizeof(new_config.host));
ESP_LOGI(TAG, "host MAC address saved to flash");
// add host to peers
esp_now_peer_info_t host;
memset(&host, 0, sizeof(host));
memcpy(host.peer_addr, new_config.host, sizeof(new_config.host));
host.encrypt = false;
host.channel = 0;
if(esp_now_add_peer(&host) != ESP_OK){
ESP_LOGE(TAG, "Adding host to peers failed");
config received_msg;
memcpy(&received_msg, incomingData, sizeof(received_msg)); // TODO: check for valid mac
// all the esp32 macs so far use the same first 3(?) bytes so maybe use that
switch (received_msg.type){
case dataAck:{
msg_recv = true;
Time::getInstance().setTime(
received_msg.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
Serial.println(Time::getInstance().getEpochSeconds());
if (!preferences.isKey("host")) {
preferences.putBytes("host", received_msg.host, sizeof(received_msg.host));
ESP_LOGI(TAG, "host MAC address saved to flash");
// add host to peers
add_host_to_peers(received_msg);
}
}
if(!are_mac_addresses_equal(stored, new_config.host)){
esp_now_del_peer(stored);
case hostChange:{
Time::getInstance().setTime(received_msg.epoch_seconds);
// delete old host
if(preferences.isKey("host")){
uint8_t old[6];
get_host_mac(old);
esp_now_del_peer(old);
}
// add new host
add_host_to_peers(received_msg);
}
} // changed Oct. 24th: now has the capability to change the mac in flash
else{
uint8_t stored[6];
preferences.getBytes("host", stored, sizeof(uint8_t) * 6);
if(!are_mac_addresses_equal(stored, new_config.host)){
default:{
return;
}
}
preferences.end();
// sync time
Time::getInstance().setTime(
new_config.epoch_seconds); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
}
esp_err_t espnow_setup()
{
esp_err_t result;
......
......@@ -11,7 +11,12 @@
#include <WiFi.h>
#include <esp_now.h>
enum MessageType{
dataAck,
hostChange
};
typedef struct config {
MessageType type;
uint8_t host[6];
long epoch_seconds;
} config;
......@@ -19,6 +24,7 @@ typedef struct config {
esp_err_t espnow_setup();
//esp_err_t espnow_send_message(const Message& message);
bool is_host_defined();
bool was_msg_received();
void get_host_mac(uint8_t *destination);
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status);
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len);
......
......@@ -23,4 +23,5 @@ class Message {
private:
ClientDataPackage clientDataPackage;
uint8_t recipient[6]{};
};
\ No newline at end of file
};
......@@ -20,6 +20,8 @@ build_flags =
-DCORE_DEBUG_LEVEL=5
-std=gnu++17
build_unflags = -std=gnu++11
monitor_port = /dev/ttyUSB0
upload_port = /dev/ttyUSB0
lib_deps =
sparkfun/SparkFun SCD30 Arduino Library@^1.0.18
Wire
......
......@@ -6,6 +6,7 @@
#include <drs26.hpp>
#include <ina219.hpp>
#include <scd30.hpp>
#include "ESPNow.hpp"
// #include "esp32-hal-log.h"
static const std::string TAG = "MAIN";
......@@ -35,8 +36,10 @@ void loop()
if(message.send() != ESP_OK){
RtcMemory::store(message.getMessageAsMinifiedJsonString());
}
// some sort of if(message received) is needed as well
// String test = RtcMemory::get_from_storage();
delay(5000);
if(!was_msg_received()){
RtcMemory::store(message.getMessageAsMinifiedJsonString());
}
}
} catch (const NoDataAvailableException &e) {
......
......@@ -13,8 +13,8 @@ platform = espressif32
board = esp32-c3-devkitm-1
monitor_speed = 115200
framework = arduino
; monitor_port = /dev/ttyUSB1
; upload_port = /dev/ttyUSB1
monitor_port = /dev/ttyUSB1
upload_port = /dev/ttyUSB1
build_flags =
-I include
-DCORE_DEBUG_LEVEL=5
......
......@@ -13,7 +13,12 @@ long time1 = 0;
//SoftwareSerial mySerial(RXD2, TXD2);
enum MessageType{
dataAck,
hostChange
};
typedef struct response{
MessageType type;
uint8_t mac[6];
long time;
}response;
......@@ -62,6 +67,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
// respond with own mac + time
response response;
response.type = dataAck;
esp_read_mac(response.mac, ESP_MAC_WIFI_STA);
response.time = 7 * time1++;
esp_err_t success = esp_now_send(mac, (uint8_t *) &response, sizeof(response));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment