Skip to content
Snippets Groups Projects
Commit 81b58350 authored by Zoe Pfister's avatar Zoe Pfister :speech_balloon:
Browse files

move Time to Singleton pattern, fix bug where no data would be sent in ESPNow.cpp

parent b385e000
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!5fipy host broadcasts its mac + timestamp on start, main function of client...
......@@ -7,7 +7,7 @@ Preferences preferences;
void get_host_mac(uint8_t *destination)
{
preferences.begin("config", true);
if (preferences.isKey("host")) {
if (!preferences.isKey("host")) {
preferences.getBytes("host", destination, sizeof(uint8_t) * 6);
} else {
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
......@@ -36,7 +36,7 @@ void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
}// host change shouldn't be an issue
preferences.end();
// sync time
esptime::rtc.setTime(
Time::getInstance().setTime(
new_config.time_millis); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
Serial.println("Saved Time: " + (String) new_config.time_millis);
Serial.flush();
......@@ -64,25 +64,25 @@ esp_err_t espnow_setup()
return ESP_OK;
}
esp_err_t espnow_send_message(Message message){
esp_err_t espnow_send_message(const Message& message){
Serial.println("sending Message");
esp_err_t success;
ClientDataPackage* dataP = message.get_client_data_package();
ClientDataPackage dataP = message.get_client_data_package();
uint8_t recipient;
get_host_mac(&recipient);
success = esp_now_send(&recipient, (uint8_t *) dataP, sizeof(ClientDataPackage));
success = esp_now_send(&recipient, (uint8_t *) &dataP, sizeof(ClientDataPackage));
// if(success != ESP_OK){
// if(!ram_cache_is_full()){
// ram_cache_push(*data);
// }
// }
for (int i = 0; i < dataP->amountData; i++) {
Serial.println(dataP->values[i]);
for (int i = 0; i < dataP.amountData; i++) {
Serial.println(dataP.values[i]);
}
Serial.println((String) "time sent: " + dataP->timestamp);
Serial.println((String) "time sent: " + dataP.timestamp);
Serial.println((String) "Send status: " + success);
Serial.println();
Serial.println("done");
......
......@@ -16,7 +16,7 @@ typedef struct config {
} config;
esp_err_t espnow_setup();
esp_err_t espnow_send_message(Message message);
esp_err_t espnow_send_message(const Message& message);
bool is_host_defined();
void get_host_mac(uint8_t *destination);
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status);
......
......@@ -9,8 +9,9 @@ void Message::add_data(float value, int identifier)
}
}
ClientDataPackage* Message ::get_client_data_package(){
return &data;
ClientDataPackage Message ::get_client_data_package() const
{
return data;
}
Message ::Message()
......@@ -18,7 +19,7 @@ Message ::Message()
// check for existing host mac address, use broadcast otherwise
data.amountData = 0;
data.timestamp = esptime::rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch
data.timestamp = Time::getInstance().getMillis(); // I am assuming we are not sending data from Unix Epoch
}
Message ::Message(ClientDataPackage old_data)
......
......@@ -13,7 +13,7 @@ class Message {
Message();
Message(ClientDataPackage old_data);
void add_data(float value, int identifier);
ClientDataPackage* get_client_data_package();
ClientDataPackage get_client_data_package() const;
private:
ClientDataPackage data;
......
#include "Time.hpp"
#include <utility>
void Time::setTime(long epoch, int ms)
{
this->rtc.setTime(epoch, ms);
}
tm Time::getTimeStruct()
{
return this->rtc.getTimeStruct();
}
String Time::getDateTime(bool mode)
{
return this->rtc.getDateTime(mode);
}
String Time::getTime(String format)
{
return this->rtc.getTime(std::move(format));
}
long Time::getEpochSeconds()
{
return this->rtc.getEpoch();
}
long Time::getMillis()
{
return this->rtc.getMillis();
}
#pragma once
#ifndef ESPTIME
#define ESPTIME
#include <ESP32Time.h>
namespace esptime {
// internal linkage only
static ESP32Time rtc; // global variable for the RTC (i don't think this is good practice)
} // namespace esptime
// namespace esptime
class Time {
public:
static Time &getInstance()
{
static Time instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
/*!
@brief set the internal RTC time
@param epoch
epoch time in seconds
@param ms
microseconds (optional)
*/
void setTime(long epoch, int ms = 0);
/*!
@brief get the internal RTC time as a tm struct
*/
tm getTimeStruct();
/*!
@brief get the time and date as an Arduino String object
@param mode
true = Long date format
false = Short date format
*/
String getDateTime(bool mode);
/*!
@brief get the time as an Arduino String object with the specified format
@param format
time format
http://www.cplusplus.com/reference/ctime/strftime/
*/
String getTime(String format = "%H:%M:%S");
/*!
@brief get the current epoch seconds as long
*/
long getEpochSeconds();
/*!
@brief get the current milliseconds as long
*/
long getMillis();
private:
Time() {} // Constructor? (the {} brackets) are needed here.
ESP32Time rtc = ESP32Time{};
// C++ 11
// =======
// We can use the better technique of deleting the methods
// we don't want.
public:
Time(Time const &) = delete;
void operator=(Time const &) = delete;
// Note: Scott Meyers mentions in his Effective Modern
// C++ book, that deleted functions should generally
// be public as it results in better error messages
// due to the compilers behavior to check accessibility
// before deleted status
};
#endif
\ No newline at end of file
......@@ -16,17 +16,6 @@ void setup()
Serial.flush();
esp_err_t result = espnow_setup();
esptime::rtc = ESP32Time();
StaticJsonDocument<96> doc;
doc["sensor"] = "gps";
doc["time"] = 1351824120;
JsonArray data = doc.createNestedArray("data");
data.add(48.75608);
data.add(2.302038);
}
int counter = 0;
......@@ -46,7 +35,8 @@ void loop()
// resend_message->send();
// delete resend_message;u
// }
Serial.println(esptime::rtc.getMillis());
Serial.println("Saved Time Loop: " + Time::getInstance().getTime("%c"));
Serial.println("delaying...");
Serial.flush();
delay(5000); // 5 second receive window
......
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