Commit 6ae78c3c authored by Eric Duminil's avatar Eric Duminil
Browse files

Slowly replacing Strings by C strings

parent 58daf119
...@@ -22,7 +22,7 @@ namespace sensor { ...@@ -22,7 +22,7 @@ namespace sensor {
uint16_t co2 = 0; uint16_t co2 = 0;
float temperature = 0; float temperature = 0;
float humidity = 0; float humidity = 0;
String timestamp = ""; char timestamp[23];
int16_t stable_measurements = 0; int16_t stable_measurements = 0;
uint32_t waiting_color = color::blue; uint32_t waiting_color = color::blue;
bool should_calibrate = false; bool should_calibrate = false;
...@@ -168,7 +168,7 @@ namespace sensor { ...@@ -168,7 +168,7 @@ namespace sensor {
if (freshData) { if (freshData) {
// checkTimerDeviation(); // checkTimerDeviation();
timestamp = ntp::getLocalTime(); ntp::getLocalTime(timestamp);
co2 = scd30.getCO2(); co2 = scd30.getCO2();
temperature = scd30.getTemperature(); temperature = scd30.getTemperature();
humidity = scd30.getHumidity(); humidity = scd30.getHumidity();
......
...@@ -22,7 +22,7 @@ namespace sensor { ...@@ -22,7 +22,7 @@ namespace sensor {
extern uint16_t co2; extern uint16_t co2;
extern float temperature; extern float temperature;
extern float humidity; extern float humidity;
extern String timestamp; extern char timestamp[23];
void initialize(); void initialize();
bool processData(); bool processData();
......
...@@ -144,7 +144,7 @@ ...@@ -144,7 +144,7 @@
*/ */
# define NTP_SERVER "pool.ntp.org" # define NTP_SERVER "pool.ntp.org"
# define UTC_OFFSET_IN_SECONDS 3600 // [s] 3600 for UTC+1 # define UTC_OFFSET_IN_SECONDS 7200 // [s] 3600 for UTC+1, 7200 for UTC+1 and daylight saving time
/** /**
* Others * Others
......
...@@ -6,7 +6,7 @@ namespace config { ...@@ -6,7 +6,7 @@ namespace config {
} }
namespace csv_writer { namespace csv_writer {
unsigned long last_written_at = 0; unsigned long last_written_at = 0;
String last_successful_write = ""; char last_successful_write[23];
#if defined(ESP8266) #if defined(ESP8266)
/** /**
...@@ -145,7 +145,7 @@ namespace csv_writer { ...@@ -145,7 +145,7 @@ namespace csv_writer {
} else { } else {
Serial.print(F("CSV - Wrote : ")); Serial.print(F("CSV - Wrote : "));
Serial.print(csv_line); Serial.print(csv_line);
last_successful_write = ntp::getLocalTime(); ntp::getLocalTime(last_successful_write);
} }
updateFsInfo(); updateFsInfo();
delay(50); delay(50);
......
...@@ -20,7 +20,7 @@ namespace config { ...@@ -20,7 +20,7 @@ namespace config {
extern uint16_t csv_interval; // [s] extern uint16_t csv_interval; // [s]
} }
namespace csv_writer { namespace csv_writer {
extern String last_successful_write; extern char last_successful_write[23];
void initialize(); void initialize();
void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity); void logIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temperature, const float &humidity);
int getAvailableSpace(); int getAvailableSpace();
......
...@@ -33,7 +33,7 @@ void os_getDevKey(u1_t *buf) { ...@@ -33,7 +33,7 @@ void os_getDevKey(u1_t *buf) {
namespace lorawan { namespace lorawan {
bool waiting_for_confirmation = false; bool waiting_for_confirmation = false;
bool connected = false; bool connected = false;
String last_transmission = ""; char last_transmission[23] = "";
void initialize() { void initialize() {
Serial.println(F("Starting LoRaWAN. Frequency plan : " LMIC_FREQUENCY_PLAN " MHz.")); Serial.println(F("Starting LoRaWAN. Frequency plan : " LMIC_FREQUENCY_PLAN " MHz."));
...@@ -65,8 +65,10 @@ namespace lorawan { ...@@ -65,8 +65,10 @@ namespace lorawan {
} }
void onEvent(ev_t ev) { void onEvent(ev_t ev) {
char current_time[23];
ntp::getLocalTime(current_time);
Serial.print("LoRa - "); Serial.print("LoRa - ");
Serial.print(ntp::getLocalTime()); Serial.print(current_time);
Serial.print(" - "); Serial.print(" - ");
switch (ev) { switch (ev) {
case EV_JOINING: case EV_JOINING:
...@@ -113,7 +115,7 @@ namespace lorawan { ...@@ -113,7 +115,7 @@ namespace lorawan {
Serial.println(F("EV_REJOIN_FAILED")); Serial.println(F("EV_REJOIN_FAILED"));
break; break;
case EV_TXCOMPLETE: case EV_TXCOMPLETE:
last_transmission = ntp::getLocalTime(); ntp::getLocalTime(last_transmission);
Serial.println(F("EV_TXCOMPLETE")); Serial.println(F("EV_TXCOMPLETE"));
break; break;
case EV_TXSTART: case EV_TXSTART:
......
...@@ -39,7 +39,7 @@ namespace config { ...@@ -39,7 +39,7 @@ namespace config {
namespace lorawan { namespace lorawan {
extern bool waiting_for_confirmation; extern bool waiting_for_confirmation;
extern bool connected; extern bool connected;
extern String last_transmission; extern char last_transmission[];
void initialize(); void initialize();
void process(); void process();
void preparePayloadIfTimeHasCome(const int16_t &co2, const float &temp, const float &hum); void preparePayloadIfTimeHasCome(const int16_t &co2, const float &temp, const float &hum);
......
...@@ -25,7 +25,7 @@ namespace mqtt { ...@@ -25,7 +25,7 @@ namespace mqtt {
String publish_topic; String publish_topic;
const char *json_sensor_format; const char *json_sensor_format;
String last_successful_publish = ""; char last_successful_publish[23] = "";
void initialize(String &topic) { void initialize(String &topic) {
json_sensor_format = PSTR("{\"time\":\"%s\", \"co2\":%d, \"temp\":%.1f, \"rh\":%.1f}"); json_sensor_format = PSTR("{\"time\":\"%s\", \"co2\":%d, \"temp\":%.1f, \"rh\":%.1f}");
...@@ -51,7 +51,7 @@ namespace mqtt { ...@@ -51,7 +51,7 @@ namespace mqtt {
// Topic is the same as clientID. e.g. 'CO2sensors/ESP3d03da' // Topic is the same as clientID. e.g. 'CO2sensors/ESP3d03da'
if (mqttClient.publish(publish_topic.c_str(), payload)) { if (mqttClient.publish(publish_topic.c_str(), payload)) {
Serial.println(F("OK")); Serial.println(F("OK"));
last_successful_publish = ntp::getLocalTime(); ntp::getLocalTime(last_successful_publish);
} else { } else {
Serial.println(F("Failed.")); Serial.println(F("Failed."));
} }
......
...@@ -11,7 +11,7 @@ namespace config { ...@@ -11,7 +11,7 @@ namespace config {
extern uint16_t mqtt_sending_interval; // [s] extern uint16_t mqtt_sending_interval; // [s]
} }
namespace mqtt { namespace mqtt {
extern String last_successful_publish; extern char last_successful_publish[];
extern bool connected; extern bool connected;
void initialize(String &topic); void initialize(String &topic);
void keepConnection(); void keepConnection();
......
...@@ -164,7 +164,7 @@ String NTPClient::getFormattedTime(unsigned long secs) { ...@@ -164,7 +164,7 @@ String NTPClient::getFormattedTime(unsigned long secs) {
} }
// Based on https://github.com/PaulStoffregen/Time/blob/master/Time.cpp // Based on https://github.com/PaulStoffregen/Time/blob/master/Time.cpp
String NTPClient::getFormattedDate(unsigned long secs) { void NTPClient::getFormattedDate(char * formatted_date, unsigned long secs) {
unsigned long rawTime = (secs ? secs : this->getEpochTime()) / 86400L; // in days unsigned long rawTime = (secs ? secs : this->getEpochTime()) / 86400L; // in days
unsigned long days = 0, year = 1970; unsigned long days = 0, year = 1970;
uint8_t month; uint8_t month;
...@@ -187,11 +187,8 @@ String NTPClient::getFormattedDate(unsigned long secs) { ...@@ -187,11 +187,8 @@ String NTPClient::getFormattedDate(unsigned long secs) {
month++; // jan is month 1 month++; // jan is month 1
rawTime++; // first day is day 1 rawTime++; // first day is day 1
char formatted_date[23]; snprintf(formatted_date, 23, "%4lu-%02d-%02lu %s%+03d",
snprintf(formatted_date, sizeof(formatted_date), "%4lu-%02d-%02lu %s%+03d",
year, month, rawTime, this->getFormattedTime(secs).c_str(), this->_timeOffset / 3600); year, month, rawTime, this->getFormattedTime(secs).c_str(), this->_timeOffset / 3600);
return String(formatted_date);
} }
void NTPClient::end() { void NTPClient::end() {
......
...@@ -91,7 +91,7 @@ class NTPClient { ...@@ -91,7 +91,7 @@ class NTPClient {
* @return secs argument (or 0 for current date) formatted to ISO 8601 * @return secs argument (or 0 for current date) formatted to ISO 8601
* like `2004-02-12T15:19:21+00:00` * like `2004-02-12T15:19:21+00:00`
*/ */
String getFormattedDate(unsigned long secs = 0); void getFormattedDate(char *formatted_date, unsigned long secs = 0);
/** /**
* Stops the underlying UDP client * Stops the underlying UDP client
......
...@@ -33,8 +33,8 @@ namespace ntp { ...@@ -33,8 +33,8 @@ namespace ntp {
timeClient.update(); timeClient.update();
} }
String getLocalTime() { void getLocalTime(char *timestamp) {
return timeClient.getFormattedDate(); timeClient.getFormattedDate(timestamp);
} }
} }
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
namespace ntp { namespace ntp {
void initialize(); void initialize();
void update(); void update();
String getLocalTime(); void getLocalTime(char* timestamp);
} }
namespace util { namespace util {
......
...@@ -226,15 +226,15 @@ namespace web_server { ...@@ -226,15 +226,15 @@ namespace web_server {
// Body // Body
snprintf_P(content, sizeof(content), body_template, SENSOR_ID.c_str(), sensor::co2, sensor::temperature, snprintf_P(content, sizeof(content), body_template, SENSOR_ID.c_str(), sensor::co2, sensor::temperature,
sensor::humidity, sensor::timestamp.c_str(), config::measurement_timestep, sensor::humidity, sensor::timestamp, config::measurement_timestep,
#ifdef AMPEL_CSV #ifdef AMPEL_CSV
csv_writer::last_successful_write.c_str(), config::csv_interval, csv_writer::getAvailableSpace() / 1024, csv_writer::last_successful_write, config::csv_interval, csv_writer::getAvailableSpace() / 1024,
#endif #endif
#ifdef AMPEL_MQTT #ifdef AMPEL_MQTT
mqtt::connected ? "Yes" : "No", mqtt::last_successful_publish.c_str(), config::mqtt_sending_interval, mqtt::connected ? "Yes" : "No", mqtt::last_successful_publish, config::mqtt_sending_interval,
#endif #endif
#if defined(AMPEL_LORAWAN) && defined(ESP32) #if defined(AMPEL_LORAWAN) && defined(ESP32)
lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission.c_str(), lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission,
config::lorawan_sending_interval, config::lorawan_sending_interval,
#endif #endif
config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", SENSOR_ID.c_str(), SENSOR_ID.c_str(), config::temperature_offset, config::auto_calibrate_sensor ? "Yes" : "No", SENSOR_ID.c_str(), SENSOR_ID.c_str(),
......
Markdown is supported
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