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

created message class, started implementing configuration

parent 3032f591
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
#include <esp_now.h> #include <esp_now.h>
#include <Preferences.h>
#include "WiFi.h" #include "WiFi.h"
#include "espnow.hpp" #include "espnow.hpp"
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //probably shouldn't be defined here, works for now uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
uint8_t host_mac[6];
esp_now_peer_info_t hostInfo; esp_now_peer_info_t hostInfo;
Preferences preferences;
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){
// go to sleep // go to sleep
...@@ -11,31 +14,67 @@ void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){ ...@@ -11,31 +14,67 @@ 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){ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len){
// sync time // sync time
} config new_config;
memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac
int send_data(dataMessage data){ preferences.begin("config", false);
esp_err_t result = esp_now_send(BROADCAST_MAC, (uint8_t *) &data, sizeof(data)); if(!preferences.isKey("host")){
return result; preferences.putBytes("host", new_config.host, sizeof(uint8_t) * 6);
}
} }
int espnow_setup(){
esp_err_t espnow_setup(){
esp_err_t result;
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
if(esp_now_init() != ESP_OK){ result = esp_now_init();
if(result != ESP_OK){
//initialization failed //initialization failed
return 1; return result;
} }
memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC)); memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
hostInfo.channel = 0; hostInfo.channel = 0;
hostInfo.encrypt = true; //if we encrypt espnow we can have no more than 10 clients per host hostInfo.encrypt = true; //if we encrypt espnow we can have no more than 10 clients per host
esp_err_t peerAddStatus = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here result = esp_now_add_peer(&hostInfo); //there are a couple errors that can happen here
if(peerAddStatus != ESP_OK){ if(result != ESP_OK){
//peer couldn't be added //peer couldn't be added
return 2; return result;
} }
esp_now_register_recv_cb(on_data_recv); esp_now_register_recv_cb(on_data_recv);
esp_now_register_send_cb(on_data_sent); esp_now_register_send_cb(on_data_sent);
return 0; return ESP_OK;
} }
void Message::add_data(float value, int identifier){
data.values[amountData] = value;
data.identifiers[amountData] = identifier;
amountData++;
if(data.timestamp == NULL){
//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));
// TODO: cache data before resetting
data_struct empty_data;
data = empty_data;
amountData = 0;
return success;
}
void Message::set_timestamp(long millis){
data.timestamp = millis;
}
\ No newline at end of file
#ifndef _ESPNOW
#define _ESPNOW
#include <string>
#define NUM_SENSORS 10 #define NUM_SENSORS 10
// I originally wanted to define the mac addresses here, but i got a "multiple definition" error? // having the data be a struct of basic types makes sending easier,
typedef struct dataMessage{ // otherwise we would have to serialize the data before sending
std::string identifiers[NUM_SENSORS]; typedef struct data_struct{
int identifiers[NUM_SENSORS];
float values[NUM_SENSORS]; float values[NUM_SENSORS];
}dataMessage; long timestamp; //maybe make this array
}data;
int send_data(dataMessage data); // if more things are sent from the host the name might not be accurate anymore
typedef struct config{
uint8_t host[6];
long time_millis;
}config;
int espnow_setup(); class Message{
\ No newline at end of file public:
void add_data(float value, int identifier);
void set_timestamp(long millis);
esp_err_t send();
private:
data_struct data;
int amountData = 0;
};
esp_err_t espnow_setup();
#endif
\ 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