How to efficiently fetch serial data only once in a while

Hello there,

I'm working on a small project to monitor my electricity consumption at home. First the happy news: my electricity meter has serial port providing live data such as current power, then the sad news: the device isn't in my flat but in the basement, where there's neither an AC plug nor WLAN signal.

My approach so far is to power my Arduino UNO (a pretty old one) with a battery and store data in an SD card. Then I'd go to the basement once in a while (hopefully no more often than once a week) and get the data, charge the battery, etc.

Because of this constraints, I really want to pay attention to the power usage. I think I'll be using a 6xAAA battery holder, providing around 6V, which is something the Arduino should be happy with. The main doubt I have is how to fetch the data from the serial. The electricity meter sends data around once per second, which is way more often than reasonable for this pet project... I think one a minute is more than enough for my purposes.

I see two approaches here:

  • Read serial data, go to sleep for a minute and then read data again. The problem with this approach is that, after reading, the buffer will get full of data (irrelevant in a minute) until it starts overflowing, so I guess I'll have to clear the buffer before fetching fresh data.
  • Open serial, read data, close serial and go to sleep. Rinse and repeat. I'm not sure if this incurs in some extra power drain, or if this will clear the buffer between calls.

Thanks a lot in advance!

PS: I'm all ears to other suggestions regarding saving energy.

A couple of questions : is it just ouputting instantaneous demand (i.e. kW) or is it putting out consumption in the last time period (i.e kWh) ?

Either of those have some interesting issues - if it's power you'll need to capture all the values and then average them (but the less frequently you sample the more dodgy your data will be). If it is consumption then you have to capture the data for every time period and you'll need to interpolate any data you miss.

I suspect you can only address a certain size of SD, but if you only need data/time and value then that shouldnt suck up much space. If it was giving you all sorts of other data you wanted then It might be possible to do some form of compression on the data perhaps ? (I've not ever looked into that, tbh. )

Can you please provide a link to a document that contains the UART Protocol of your Energy Meter?

It gives me both the "instant power" (W) and consumed energy (Wh)... plus a lot of other stuff.

Yes, it's true that instant power once a minute isn't ideal and should be averaged over some time but for sake of simplicity, I left it out in the question.

Sure, it's called SML, but it seems to be used only in Germany (?), and therefore all docs are in German: Smart Message Language – Wikipedia

However, the Tasmota project seems to interact with SML stuff... maybe it's more widely spread: Smart Meter Interface - Tasmota

Why do you think that the Serial input buffer is active during sleep?

The proper answer is because I'm an ignorant :sweat_smile:.

I'm not that versed in programming for constrained devices and I assumed the buffer gets filled independently from what happens in the microchip (I dunno, the UART thing runs on its own...or just magic).

So if I understand you well, when sleeping, the serial buffer doesn't receive any data, right? In that case I could just dump what's inside and then wait for fresh data.

Thanks a lot!

Be sure to include a switch or other device to tell your program to close the SD file so you will have the WHOLE file to access when you want to look at the data.

Wouldn't be enough if I flush each minute or at least once in a while?

Flush just updates the data in the file, it does not update the file header which contains the total number of bytes written.

All right, thank you very much. Would it be expensive to open and close the file often?

Potentially there could be lost data. but you would have to determine the value.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.