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

esp32 host and esp32 client now communicate as they should

parent 98e3cff6
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
......@@ -4,7 +4,7 @@
#include "WiFi.h"
#include "espnow.hpp"
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
uint8_t A_MAC[] = {0x58, 0xCF, 0x79, 0x04, 0x37, 0x38};
bool host_defined = false;
esp_now_peer_info_t hostInfo;
Preferences preferences;
......@@ -15,13 +15,15 @@ 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){
Serial.println("message recieved");
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);
preferences.putBytes("host", new_config.host, sizeof(new_config.host));
Serial.println("host mac saved to flash");
}// host change shouldn't be an issue
preferences.end();
// sync time
......@@ -38,24 +40,20 @@ esp_err_t espnow_setup(){
//initialization failed
return result; // not sure about this
}
preferences.begin("config", true);
preferences.begin("config", false);
if(preferences.isKey("host")){
// add host from flash mem as peer
preferences.getBytes("host", hostInfo.peer_addr, sizeof(uint8_t) * 6);
preferences.getBytes("host", hostInfo.peer_addr, sizeof(hostInfo.peer_addr));
Serial.println("Mac used from flash");
}
else{
// add Broadcast Mac as default
memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
memcpy(hostInfo.peer_addr, A_MAC, sizeof(hostInfo.peer_addr));
}
preferences.end();
hostInfo.channel = 0;
hostInfo.encrypt = true;
result = esp_now_add_peer(&hostInfo);
if(result != ESP_OK){
//peer couldn't be added
return result;
}
hostInfo.encrypt = 0;
esp_now_add_peer(&hostInfo);
esp_now_register_recv_cb(on_data_recv);
esp_now_register_send_cb(on_data_sent);
......@@ -64,39 +62,44 @@ esp_err_t espnow_setup(){
}
void Message::add_data(float value, int identifier){
if(amountData < NUM_SENSORS){
data->values[amountData] = value;
data->identifiers[amountData] = identifier;
amountData++;
}
if(data->timestamp == 0){
//add timestamp
data->timestamp = rtc.getMillis();
}
}
if(data->amountData < NUM_SENSORS){
data->values[data->amountData] = value;
data->identifiers[data->amountData] = identifier;
data->amountData++;
}
}
esp_err_t Message::send(){
Serial.println("sending Message");
esp_err_t success;
success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data));
success = esp_now_send(recipient, (uint8_t *) &data, sizeof(data));
for(int i=0; i<data->amountData; i++){
Serial.println(data->values[i]);
}
// TODO: cache data before resetting
free((void*) data);
Serial.println((String) "time sent: " + data->timestamp);
Serial.println((String) "Send status: " + success);
Serial.println();
Serial.flush();
free((void*) data);
return success;
}
// maybe do this in constructor
void Message::set_timestamp(long 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);
if(preferences.isKey("host")){
preferences.getBytes("host", recipient, sizeof(uint8_t) * 6);
}
else{
memcpy(recipient, A_MAC, sizeof(A_MAC));
Serial.println("backup mac used");
}
preferences.end();
amountData = 0;
data->timestamp = 0; // I am assuming we are not sending data from Unix Epoch
data->amountData = 0;
data->timestamp = rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch
}
\ No newline at end of file
......@@ -8,6 +8,7 @@
typedef struct data_struct{
int identifiers[NUM_SENSORS];
float values[NUM_SENSORS];
int amountData;
long timestamp; //maybe make this array
}data_struct;
......@@ -22,15 +23,15 @@ 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;
uint8_t recipient[6];
};
esp_err_t espnow_setup();
bool is_host_defined();
#endif
\ No newline at end of file
......@@ -6,13 +6,21 @@ void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while(!Serial);
Serial.flush();
esp_err_t result = espnow_setup();
}
int counter = 0;
void loop() {
// put your main code here, to run repeatedly:
// Message* new_data = new Message();
// new_data->send();
// delete new_data;
Message* new_data = new Message();
new_data->add_data(++counter * 1.1, 0);
new_data->add_data(counter * 1.2, 1);
new_data->add_data(counter * 1.3, 2);
new_data->send();
delete new_data;
delay(5000);
}
\ No newline at end of file
......@@ -6,19 +6,25 @@
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup
esp_now_peer_info_t hostInfo;
data_struct lastData;
ESP32Time rtc;
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status){
Serial.println((String) "Message sent: " + status);
}
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len){
data_struct lastData;
memcpy(&lastData, incomingData, sizeof(lastData));
for(int i=0; i<NUM_SENSORS; i++){
Serial.println(lastData.identifiers[i] + lastData.values[i]);
}
Serial.println("at " + lastData.timestamp);
Serial.println("message copied");
// for(int i=0; i<lastData.amountData; i++){
// Serial.print((String) lastData.identifiers[i] + ": ");
// Serial.println((String) lastData.values[i]);
// }
Serial.println((String) lastData.values[0]);
Serial.println((String) "sent time: " + lastData.timestamp);
Serial.println((String) "RTC time: " + rtc.getMillis());
Serial.flush();
}
esp_err_t espnow_setup(){
......@@ -30,13 +36,10 @@ esp_err_t espnow_setup(){
return result; // not sure about this
}
memcpy(hostInfo.peer_addr, BROADCAST_MAC, sizeof(BROADCAST_MAC));
hostInfo.channel = 0;
hostInfo.encrypt = true;
result = esp_now_add_peer(&hostInfo);
if(result != ESP_OK){
//peer couldn't be added
return result;
}
hostInfo.encrypt = 0;
esp_now_add_peer(&hostInfo);
esp_now_register_recv_cb(on_data_recv);
esp_now_register_send_cb(on_data_sent);
......@@ -45,7 +48,7 @@ esp_err_t espnow_setup(){
}
Message :: Message(){
memcpy(BROADCAST_MAC, recipient, sizeof(BROADCAST_MAC));
memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC));
config = (config_struct*) malloc(sizeof(config_struct));
esp_wifi_get_mac(WIFI_IF_STA, config->host);
config->time_millis = rtc.getMillis();
......
......@@ -6,6 +6,7 @@
typedef struct data_struct{
int identifiers[NUM_SENSORS];
float values[NUM_SENSORS];
int amountData;
long timestamp; //maybe make this array
}data_struct;
......
......@@ -12,4 +12,5 @@
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
monitor_speed=115200
lib_deps = fbiego/ESP32Time@^2.0.0
......@@ -7,14 +7,19 @@ void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while(!Serial);
WiFi.mode(WIFI_STA);
Serial.flush();
espnow_setup();
Message* msg = new Message;
msg->send();
}
int counter = 0;
void loop() {
// put your main code here, to run repeatedly:
if(counter++ % 10 == 0){
Message* msg = new Message;
msg->send();
delete msg;
}
Serial.println("No message...");
}
\ No newline at end of file
delay(1000);
}
\ 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