Skip to content

Commit

Permalink
Added switcheable mqtt debuging with temperature logging, CPU frequen…
Browse files Browse the repository at this point in the history
…zy limitation (Brunas#3)
  • Loading branch information
Brunas authored Feb 6, 2024
1 parent bd7e4b4 commit 294944a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
*.h.lv

settings.h
git.settings.h
5 changes: 5 additions & 0 deletions esp32_p1meter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ void setup() {
digitalWrite(LED_PIN, LOW);
blinkLed(1, 500);

// Force CPU frequency if constant is specified
if (CPU_FREQ > 0) {
setCpuFrequencyMhz(CPU_FREQ);
}

Serial.begin(BAUD_RATE);

#ifdef DEBUG
Expand Down
16 changes: 3 additions & 13 deletions read_p1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ bool decodeTelegram(int len) {
messageCRC[4] = 0; // * Thanks to HarmOtten (issue 5)
validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);

#ifdef DEBUG
if (validCRCFound)
Serial.println(F("CRC Valid!"));
else
Serial.println(F("CRC Invalid!"));
#endif
debug(validCRCFound ? "CRC valid!" : "CRC Invalid!");
currentCRC = 0;
} else {
currentCRC = crc16(currentCRC, (unsigned char *)telegram, len);
Expand All @@ -114,9 +109,7 @@ bool decodeTelegram(int len) {
telegramObjects[i].value = newValue;
telegramObjects[i].sendData = true;
}
#ifdef DEBUG
Serial.println((String) "Found a Telegram object: " + telegramObjects[i].name + " value: " + telegramObjects[i].value);
#endif
debug((String) "Found a Telegram object: " + telegramObjects[i].name + " value: " + telegramObjects[i].value);
break;
}
}
Expand All @@ -126,10 +119,7 @@ bool decodeTelegram(int len) {

bool readP1Serial() {
if (Serial2.available()) {
#ifdef DEBUG
Serial.println("Serial2 is available");
Serial.println("Memset telegram");
#endif
debug("Serial2 is available. Memset telegram.");
memset(telegram, 0, sizeof(telegram));
while (Serial2.available()) {
// Reads the telegram until it finds a return character
Expand Down
10 changes: 5 additions & 5 deletions readout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
and converts to telegramDecodedObjects MQTT vector.
*/
void setupDataReadout() {
DynamicJsonDocument _dsmrMapDocument(DYNAMIC_JSON_DOCUMENT_SIZE);
JsonDocument _dsmrMapDocument;
DeserializationError _error = deserializeJson(_dsmrMapDocument, DSMR_MAP);
if (_error) {
Serial.print("deserializeJson() failed: ");
Serial.println(_error.c_str());
debug("deserializeJson() failed: ");
debug(_error.c_str());
return;
}

Expand All @@ -24,9 +24,9 @@ void setupDataReadout() {
}

#ifdef DEBUG
Serial.println("MQTT Topics initialized (" + String(_count) + "):");
debug("MQTT Topics initialized (" + String(_count) + "):");
for (int i = 0; i < _count; i++) {
Serial.println(String(MQTT_ROOT_TOPIC) + "/" + telegramObjects[i].name);
debug(String(MQTT_ROOT_TOPIC) + "/" + telegramObjects[i].name);
}
#endif
}
18 changes: 12 additions & 6 deletions settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
//#define DEBUG
// Uncomment for debug messages sent to gmail
//#define EMAIL_DEBUGGING
// Uncomment for debug messages sent to mqtt
//#define MQTT_DEBUGGING
// Uncomment for testing
//#define TEST

// Update treshold in milliseconds, messages will only be sent on this interval
//#define UPDATE_INTERVAL 1000 // 1 second
#define UPDATE_INTERVAL 30000 // 30 seconds
//#define UPDATE_INTERVAL 60000 // 1 minute
//#define UPDATE_INTERVAL 30000 // 30 seconds
#define UPDATE_INTERVAL 60000 // 1 minute
//#define UPDATE_INTERVAL 300000 // 5 minutes

// Update treshold in milliseconds,
Expand All @@ -32,13 +34,12 @@

#define P1_MAXLINELENGTH 1050

// To calculate this use https://arduinojson.org/v6/assistant/
// This is for 84 readouts with some slack
#define DYNAMIC_JSON_DOCUMENT_SIZE 12288

#define MQTT_MAX_RECONNECT_TRIES 10
#define MQTT_ROOT_TOPIC "p1_meter/sensor"
#define MQTT_STATUS_TOPIC "p1_meter/status"
#ifdef MQTT_DEBUGGING
#define MQTT_DEBUG_TOPIC "p1_meter/debug"
#endif

#define WIFI_MAX_RECONNECT_TRIES 5

Expand All @@ -63,6 +64,11 @@ long VALUE_NUMERIC_MULTIPLIER = 1;
// Integers are handled automatically and no decimals are used
unsigned short VALUE_FLOAT_DECIMAL_PLACES = 3;

// Specify CPU frequenxy in MHz to reduce power usage
// 0, if frequency should be handled automatically using ArduinoIDE and board settings
// CAUTION: please make sure your CPU can handle frequency!
unsigned int CPU_FREQ = 80;

// Nothing to change below - globals to make it all work as simple as possible
char telegram[P1_MAXLINELENGTH];

Expand Down
15 changes: 15 additions & 0 deletions utils.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();

void blinkLed(int numberOfBlinks, int msBetweenBlinks) {
int msDurationForLow=200;
for (int i = 0; i < numberOfBlinks; i++) {
Expand Down Expand Up @@ -66,8 +75,14 @@ void sendEmailMessage(String subject, String message) {

void debug(String msg) {
#ifdef DEBUG
unsigned int _temp = (temprature_sens_read() - 32) / 1.8;
msg += " (T: " + String(_temp) + "C)";
Serial.println(msg);

#ifdef MQTT_DEBUGGING;
sendMQTTMessage(MQTT_DEBUG_TOPIC, msg.c_str());
#endif

#ifdef EMAIL_DEBUGGING
emailMessageDump += msg + "\r\n";

Expand Down

0 comments on commit 294944a

Please sign in to comment.