MQTT String from ESP8266 for Optical Sensor

Dear forum,

I have built an ESP8266 based optical sensor which is working fine and sending MQTT strings to EmonPi. This will be for my gas meter. EmonPi is adding the new device and strings at it should but I’m unable to get it to count upwards. My ESP is setup on interrupt and sends a 1 every time it is triggered. How do I setup Emonpi to add them together please?

I could do this in the ESP and send the new value each time but that would seem a significant weakness in the lifetime of the system should it be rebooted and revert back to zero. Using the EEProm to store it would seem overkill too for what I would expect is handled within the Emonpi.

The string I’m sending (I have tried many formats) -
client.publish(“emon/gasmeter/pulse”, “1”);

Many thanks.

Welcome, James, to the OEM forum.

That in fact is how the emonTx handles it. But the “reset to zero” is recognised and handled correctly by the “Wh accumulator” (also the kWh accumulator) in the emonCMS Input process which you’re bypassing by sending via MQTT, I believe. In fact you’re not bypassing that when you use MQTT, it’s only emonHub that you bypass.

The very latest emonTx software does store its energy values in EEPROM - not every reading every 10 s, but only when there’s a significant change in one of the values. We calculated that on the trade-off between lost energy and EEPROM life, it ended up at a projected life of 60 years.

Robert,
Thank you for the prompt response and clear answer. That does make sense after all, I will give that a go. Its always nice when a seemingly straight forward question unearths hidden techniques for ensuring the products robustness.
Thank you,
James.

Just use the accumulator Input Process, I think.

You might need to handle Null or Zero (use if Skip) but probably not.

What is the max rate a ‘1’ would be triggered? You might find it better to send on a fixed cycle if it is quicker than 5s else you will miss readings.

A more robust way would be to send the accumulating total and the increment (then you can compare both to see if you are missing data). Send it as a JSON string to emon/gasmeter and

{"pulse": 1, "total": 500}

You can accumulate the ‘total’ and emoncms will (usually) account for any reset to zero (check the process info to find the right one).

Emoncms does assume accumulators are Wh rather than m³, but you can set the units in the Feed.