Commit 8372df49 authored by Eric Duminil's avatar Eric Duminil
Browse files

NTP time : Slowly replacing Strings by C strings

parent 6ae78c3c
......@@ -132,11 +132,11 @@ namespace csv_writer {
return csv_file;
}
void log(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity) {
void log(const char *timestamp, const int16_t &co2, const float &temperature, const float &humidity) {
led_effects::onBoardLEDOn();
File csv_file = openOrCreate();
char csv_line[42];
snprintf(csv_line, sizeof(csv_line), "%s;%d;%.1f;%.1f\r\n", timeStamp.c_str(), co2, temperature, humidity);
snprintf(csv_line, sizeof(csv_line), "%s;%d;%.1f;%.1f\r\n", timestamp, co2, temperature, humidity);
if (csv_file) {
size_t written_bytes = csv_file.print(csv_line);
csv_file.close();
......@@ -156,7 +156,7 @@ namespace csv_writer {
led_effects::onBoardLEDOff();
}
void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity) {
void logIfTimeHasCome(const char *timeStamp, const int16_t &co2, const float &temperature, const float &humidity) {
unsigned long now = seconds();
if (now - last_written_at > config::csv_interval) {
last_written_at = now;
......
......@@ -22,7 +22,7 @@ namespace config {
namespace csv_writer {
extern char last_successful_write[23];
void initialize();
void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity);
void logIfTimeHasCome(const char* timestamp, const int16_t &co2, const float &temperature, const float &humidity);
int getAvailableSpace();
extern const String filename;
......
......@@ -41,13 +41,13 @@ namespace mqtt {
" (Sends local IP and SSID via MQTT. Can be useful to find sensor)");
}
void publish(const String &timestamp, int16_t co2, float temperature, float humidity) {
void publish(const char *timestamp, int16_t co2, float temperature, float humidity) {
if (WiFi.status() == WL_CONNECTED && mqttClient.connected()) {
led_effects::onBoardLEDOn();
Serial.print(F("MQTT - Publishing message ... "));
char payload[75]; // Should be enough for json...
snprintf(payload, sizeof(payload), json_sensor_format, timestamp.c_str(), co2, temperature, humidity);
snprintf(payload, sizeof(payload), json_sensor_format, timestamp, co2, temperature, humidity);
// Topic is the same as clientID. e.g. 'CO2sensors/ESP3d03da'
if (mqttClient.publish(publish_topic.c_str(), payload)) {
Serial.println(F("OK"));
......@@ -120,12 +120,12 @@ namespace mqtt {
}
}
void publishIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temp, const float &hum) {
void publishIfTimeHasCome(const char *timestamp, const int16_t &co2, const float &temp, const float &hum) {
// Send message via MQTT according to sending interval
unsigned long now = seconds();
if (now - last_sent_at > config::mqtt_sending_interval) {
last_sent_at = now;
publish(timeStamp, co2, temp, hum);
publish(timestamp, co2, temp, hum);
}
}
......
......@@ -15,7 +15,7 @@ namespace mqtt {
extern bool connected;
void initialize(String &topic);
void keepConnection();
void publishIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temp, const float &hum);
void publishIfTimeHasCome(const char *timestamp, const int16_t &co2, const float &temp, const float &hum);
void setMQTTinterval(int32_t sending_interval);
void sendInfoAboutLocalNetwork();
......
......@@ -152,19 +152,17 @@ int NTPClient::getSeconds() {
return (this->getEpochTime() % 60);
}
String NTPClient::getFormattedTime(unsigned long secs) {
void NTPClient::getFormattedTime(char *formatted_time, unsigned long secs) {
unsigned long rawTime = secs ? secs : this->getEpochTime();
unsigned int hours = (rawTime % 86400L) / 3600;
unsigned int minutes = (rawTime % 3600) / 60;
unsigned int seconds = rawTime % 60;
char formatted_time[9];
snprintf(formatted_time, sizeof(formatted_time), "%02d:%02d:%02d", hours, minutes, seconds);
return String(formatted_time);
snprintf(formatted_time, 9, "%02d:%02d:%02d", hours, minutes, seconds);
}
// Based on https://github.com/PaulStoffregen/Time/blob/master/Time.cpp
void NTPClient::getFormattedDate(char * formatted_date, unsigned long secs) {
void NTPClient::getFormattedDate(char *formatted_date, unsigned long secs) {
unsigned long rawTime = (secs ? secs : this->getEpochTime()) / 86400L; // in days
unsigned long days = 0, year = 1970;
uint8_t month;
......@@ -187,8 +185,9 @@ void NTPClient::getFormattedDate(char * formatted_date, unsigned long secs) {
month++; // jan is month 1
rawTime++; // first day is day 1
snprintf(formatted_date, 23, "%4lu-%02d-%02lu %s%+03d",
year, month, rawTime, this->getFormattedTime(secs).c_str(), this->_timeOffset / 3600);
char formatted_time[9];
this->getFormattedTime(formatted_time, secs);
snprintf(formatted_date, 23, "%4lu-%02d-%02lu %s%+03d", year, month, rawTime, formatted_time, this->_timeOffset / 3600);
}
void NTPClient::end() {
......
......@@ -80,7 +80,7 @@ class NTPClient {
/**
* @return secs argument (or 0 for current time) formatted like `hh:mm:ss`
*/
String getFormattedTime(unsigned long secs = 0);
void getFormattedTime(char *formatted_time, unsigned long secs = 0);
/**
* @return time in seconds since Jan. 1, 1970
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment