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

improvements (hopefully)

parent c8e6c63f
No related branches found
No related tags found
5 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!3Merge Branch `develop` into `sensor_readout`,!1Espnow
......@@ -15,6 +15,7 @@ downloads/
eggs/
.eggs/
lib/
!client/client/lib
lib64/
parts/
sdist/
......
......@@ -4,7 +4,7 @@
#include "espnow.hpp"
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
uint8_t host_mac[6];
bool host_defined = false;
esp_now_peer_info_t hostInfo;
Preferences preferences;
......@@ -13,14 +13,16 @@ 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){
// sync time
config new_config;
memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac
// put the host address in flash mem
preferences.begin("config", false);
if(!preferences.isKey("host")){
preferences.putBytes("host", new_config.host, sizeof(uint8_t) * 6);
}
}// host change shouldn't be an issue
preferences.end();
// sync time
}
......@@ -31,13 +33,22 @@ esp_err_t espnow_setup(){
result = esp_now_init();
if(result != ESP_OK){
//initialization failed
return result;
return result; // not sure about this
}
memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
preferences.begin("config", true);
if(preferences.isKey("host")){
// add host from flash mem as peer
preferences.getBytes("host", hostInfo.peer_addr, sizeof(uint8_t) * 6);
}
else{
// add Broadcast Mac as default
memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
}
preferences.end();
hostInfo.channel = 0;
hostInfo.encrypt = true; //if we encrypt espnow we can have no more than 10 clients per host
result = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here
hostInfo.encrypt = true;
result = esp_now_add_peer(&hostInfo);
if(result != ESP_OK){
//peer couldn't be added
return result;
......@@ -50,31 +61,38 @@ esp_err_t espnow_setup(){
}
void Message::add_data(float value, int identifier){
data.values[amountData] = value;
data.identifiers[amountData] = identifier;
amountData++;
if(amountData < NUM_SENSORS){
data->values[amountData] = value;
data->identifiers[amountData] = identifier;
amountData++;
}
if(data.timestamp == NULL){
if(data->timestamp == 0){
//add timestamp
}
}
// BROADCAST_MAC is to be changed
esp_err_t Message::send(){
esp_err_t success;
if(host_mac == NULL){
success = esp_now_send(BROADCAST_MAC, (uint8_t *) &data, sizeof(data));
}// if no host defined broadcast message
success = esp_now_send(host_mac, (uint8_t *) &data, sizeof(data));
success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data));
// TODO: cache data before resetting
data_struct empty_data;
data = empty_data;
amountData = 0;
free((void*) data);
return success;
}
// maybe do this in constructor
void Message::set_timestamp(long millis){
data.timestamp = millis;
data->timestamp = millis;
}
Message :: Message(){
data = (data_struct*) malloc(sizeof(data_struct));
preferences.begin("config", true);
preferences.getBytes("host", recipient, sizeof(uint8_t) * 6);
preferences.end();
amountData = 0;
data->timestamp = 0; // I am assuming we are not sending data from Unix Epoch
}
\ No newline at end of file
......@@ -9,8 +9,9 @@ typedef struct data_struct{
int identifiers[NUM_SENSORS];
float values[NUM_SENSORS];
long timestamp; //maybe make this array
}data;
}data_struct;
// Format of the message sent from host to client
// if more things are sent from the host the name might not be accurate anymore
typedef struct config{
uint8_t host[6];
......@@ -19,13 +20,15 @@ typedef struct config{
class Message{
public:
Message();
void add_data(float value, int identifier);
void set_timestamp(long millis);
esp_err_t send();
private:
data_struct data;
int amountData = 0;
data_struct *data;
int amountData;
uint8_t recipient[6];
};
......
......@@ -7,9 +7,12 @@ void setup() {
Serial.begin(115200);
while(!Serial);
int result = espnow_setup();
esp_err_t result = espnow_setup();
}
void loop() {
// put your main code here, to run repeatedly:
// Message* new_data = new Message();
// new_data->send();
// delete new_data;
}
\ No newline at end of file
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