Native ESP8266 code, published to MQTT and RPi calc engine

Hi eveyone,

Thought i'd share what I've been working on and get some help too. I wanted to use the ESP8266 with the Arduino IDE to run the transmitter modules for the open energy monitor. The ESP-12 doesn't seem to have the power (or RAM??) to process the power calculations in real time. Trying this just crashed the ESP. So the ESP and MCP3008 just reads the data and sends  a stream over MQTT to the Raspberry Pi. At the end of a stream of data points, the ESP sends a trigger message to start the RPi calculating.

The RPi can then fit a sine curve to the raw data with a least squares optimisation. The results give offset, magnitude, phase, amplitude. Everything we need for power calcs on a device with plenty of processing power.

My transmitter:

  • ESP-12 NodeMCU dev board
  • MCP3008 analog to digital converter (8 channel) on HSPI
  • The usual current and voltage Tx's for sensing.
  • Works just fine

My Raspberry Pi hub:

I have mqttwarn configured to dump the messages to a text file (comma separated, working well), and I think I have a script to process the raw data (needs testing).

The problem I have is mqttwarn related, mainly how to pass the file name to the processing script to do the sine curve fit. Anyone know how to do this in the mqttwarn code?

I have included the sketch and python code as attachments.

 

Thanks,

Mark

dBC's picture

Re: Native ESP8266 code, published to MQTT and RPi calc engine

I'm curious as to why you fit a sine wave to the data.  Do you realise the signal is rarely anything like a sinewave (ok, voltage is usually pretty close to one, but current is rarely anything like one)?  To accurately measure power you really want the samples to be as unmolested (both in time and value) as possible.

Any specifics on why you couldn't do the power calculations at the front end?  I would have thought storing all those samples and shipping them across the wifi would be way more resource intensive than calculating power up front and just shipping that.

mark0's picture

Re: Native ESP8266 code, published to MQTT and RPi calc engine

i hadn't really considered the effect of the sine curve on the calculation, just thought it was an efficient way of doing the calc. This could be changed in the python code to do any form of calculation really.

 

I had tried the calculation in the ESP, it crashed every boot once the calculations started. Might have been some other fault crashing the ESP though.

Bramco's picture

Re: Native ESP8266 code, published to MQTT and RPi calc engine

Mark, without seeing your original code, I'd guess the reason its crashing is that maybe you are trying to do the calculations in the interrupt routine. I've found that you need to just grab the data and set a flag so that the calculations are done in the main loop.

You'd certainly need to be doing this to do any PV diversion.

Is there any chance you can post your original code?

Simon

mark0's picture

Re: Native ESP8266 code, published to MQTT and RPi calc engine

So I had another go at modifying emontx firmware to run on the ESP, using mqtt to transmit the data

The 1.6.7 build of Arduino seems to support sqrt with the ESP which is a bonus. The pull request for emonLib doesn't seem like it is needed anymore. And the squareRoot function itself seemed to cause soft WDT resets.

Sketch seems to be running much better now. My own noob mistakes? probably.

Sketch is attached, with the modified emonLib.cpp (to use the external ADC).

I have tested the external ADC with the calc_rms function (AC-AC detection) which works well, so the sampling frequency is high enough. I have a different voltage Tx to the ones on the wiki pages though, so my calibration constant is very different. I have also been able to take raw data to excel to view the Voltage waveform in a graph.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.