Community
OpenEnergyMonitor

Community

How to change the min_pulsewidth in emonPi/emonCMS?

Hello,

I’m using an emonPi with the optical pulse sensor on a 10.000 times/kWh Landis+Gyr utility meter. This works perfectly to about 6kW, if the power usage in the house goes higher than that it seems that the pulse sensor can’t keep up resulting in indication 0 Watts.

I’ve read the forum that you can change the minimum width of interrupt pulse (min_pulsewidth= 1) as optical switches do not have contact bounce.

As I’m not using a sketch nor arduino. Where/how can I change this in emonCMS on the emonPi ?

Thx,
Cobalt

Unfortunately, you can’t. You need to make the change in the sketch that runs in the Atmel ATMega 328P processor that forms the “emon” part of the emonPi. If you wish to try to change that, I can tell you what needs changing and point you to what you need to do to implement the change. But it’s a somewhat tortuous process.

Hi Robert,
That’s fast! Thx for the quick reply.

Yes please, your help is very much appreciated.
I’ve found the other posts for Arduino on this topic with your comments but have not been able to find the right files/location on the emonPi (yet).

Basically, there are three steps:

  1. Install the Arduino IDE and libraries.
  2. Get the sketch for the '328P front end and change it.
  3. Upload it.

The way you write, you might already have the Arduino IDE. If not, the full instructions are in the ‘Learn’ section here: Learn→Electricity Monitoring→Using the Arduino IDE

The sketch is on GitHub, under emonpi/firmware/src/:


(NOT emonpi/Atmega328/ )

The file itself where you make the change is src.ino, line 96:
const byte min_pulsewidth= 60;

Here is the tricky part - uploading it. If you do all of the above on the Raspberry Pi, the compiled file you upload will be on there somewhere. If you did it on another computer, you must find it on that computer and transfer it to the Raspberry Pi. The file has the .hex extension, where it is depends on your operating system - there’s a lot more detail on the Arduino website.

Once it’s on the RPi, there’s a script to move it

or, if that doesn’t work for you and the file isn’t where that script expects it to be, stop emonHub, then do
$ avrdude -v -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 115200 -U flash:w:sketch_name.hex
(Don’t forget to restart emonHub.)

(I cheat: I disassemble the emonPi, take the “emon” board off and plug in a programmer and 5 V d.c., and load the sketch from my computer directly.)

Yes, I’m already using the Arduino IDE for other projects.

I’ve added all the libraries as referenced in the src and I’m using the Atmel atmega328p (Xplained mini) for the board.

I’m running into an issue with verification. It breaks on lcd_serial.ino

/Users/cobalt/Documents/github/emonpi-master/firmware/src/lcd_serial.ino: In function 'void serial_print_startup(int)':
lcd_serial:104:5: error: 'showString' was not declared in this scope
 showString(helpText1);
 ^~~~~~~~~~
/Users/cobalt/Documents/github/emonpi-master/firmware/src/lcd_serial.ino:104:5: note: suggested alternative: 'String'
 showString(helpText1);
 ^~~~~~~~~~
 String

I saw your comments on a previous post (Arduino IDE problem - showString( )) which showed something similar but could not quite figure out how to apply it here.

I had the same problem when I used the GitHub files.

The “main file” in your case is src.ino
(For a reason nobody seems to know, all principal files except one are now called “src.ino” - for maximum confusion when, as I have, there are several editor windows open all called “src.ino” but belonging to different sketches.)

I’ve put the line in just above the definition of the Help Text

bool quiet_mode = true;

static void showString (PGM_P s);

const char helpText1[] PROGMEM =

And it compiles for me. I don’t have an emonPi available (hence it failed to load the file :roll_eyes: ) so I can’t prove the change.

Great, that worked so change proven :slight_smile:

It creates two hex files:

  • src.ino.atmega328p-xmini.hex
  • src.ino.with_bootloader.atmega328p-xmini.hex

Seems to match when I look into emonpi/firmware/compiled (latest.hex and latest_bootloader.hex).

Just to verify, do I need to upload both or just one?

Oh wait, I see you only use latest.hex

As I said, I write direct to the “emon” processor, bypassing the RPi. I’ve never done it this way.

I have programmer laying around here somewhere but I’ll try it this way first :slight_smile:

Allright, I changed the const byte min_pulsewidth to 0 and copied the src.ino.atmega328p-xmini.hex file to the Rpi as latest.hex under /home/pi/emonpi/firmware/compiled/

Stopped emonhub, ran the avrdude cmd, started emonhub. I got the following with a couple of broken pipes but it seemed to work.

avrdude-original: Version 6.3-20171130
Copyright © 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright © 2007-2014 Joerg Wunsch

              System wide configuration file is "/etc/avrdude.conf"
              User configuration file is "/root/.avrduderc"
              User configuration file does not exist or is not a regular file, skipping

              Using Port                    : /dev/ttyAMA0
              Using Programmer              : arduino
              Overriding Baud Rate          : 115200

avrdude-original: Using autoreset DTR on GPIO Pin 7
AVR Part : ATmega328P
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PC2
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

                                       Block Poll               Page                       Polled
                Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
                ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
                eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
                flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
                lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

              Programmer Type : Arduino
              Description     : Arduino
              Hardware Version: 3
              Firmware Version: 4.4
              Vtarget         : 0.3 V
              Varef           : 0.3 V
              Oscillator      : 28.800 kHz
              SCK period      : 3.3 us

avrdude-original: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude-original: Device signature = 0x1e950f (probably m328p)
avrdude-original: safemode: lfuse reads as 0
avrdude-original: safemode: hfuse reads as 0
avrdude-original: safemode: efuse reads as 0
avrdude-original: NOTE: “flash” memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude-original: erasing chip
avrdude-original: reading input file “/home/pi/emonpi/firmware/compiled/latest.hex”
avrdude-original: input file /home/pi/emonpi/firmware/compiled/latest.hex auto detected as Intel Hex
avrdude-original: writing flash (18356 bytes):

Writing | ################################################## | 100% 2.57s

avrdude-original: 18356 bytes of flash written
avrdude-original: verifying flash memory against /home/pi/emonpi/firmware/compiled/latest.hex:
avrdude-original: load data flash data from input file /home/pi/emonpi/firmware/compiled/latest.hex:
avrdude-original: input file /home/pi/emonpi/firmware/compiled/latest.hex auto detected as Intel Hex
avrdude-original: input file /home/pi/emonpi/firmware/compiled/latest.hex contains 18356 bytes
avrdude-original: reading on-chip flash data:

Reading | ################################################## | 100% 1.98s

avrdude-original: verifying …
avrdude-original: 18356 bytes of flash verified

avrdude-original: safemode: lfuse reads as 0
avrdude-original: safemode: hfuse reads as 0
avrdude-original: safemode: efuse reads as 0
avrdude-original: safemode: Fuses OK (E:00, H:00, L:00)
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe

avrdude-original done. Thank you.

strace: |autoreset: Broken pipe

The webinterface showed everything in order. And yessss, the pulse counter can now keep up with the real usage. A waterkettle, portable roomheater and all four of the ceramic stove pits on full will easily go to 10kW which is now perfectly showing up on the emonPi.

Robert, thx for all the quick replies and solution!

1 Like