I have a setup of 9 inputs, each input is measured once per second. The data is coming in on a raspberry Pi through the GPIO serial RX line.
At first I received a lot of garbled data, so I started to investigate. First thing I saw was this: Somehow the data was coming in way slower than when I watched the serial monitor directly. So I checked the serial buffer. In the emonhub_interfacer.py file I added a
print(self._ser.inWaiting())
call in the beginning of the read function of the serial interfacer to debug (the inWaiting() function of pyserial returns the number of bytes in the buffer). It turned out the serial buffer was overflowing quite soon after startup.
I solved it by decreasing the delay in the read loop in emonhub.py, in the run function there is
time.sleep(0.2)
which causes the program to wait 0.2 seconds between each reading (=line in the serial input). So 9 lines coming in each second: that is 1.8 seconds of processing time needed each second.
I am using a raspberry Pi 2 (which has increased processing power over the early models) the processor load of 1 cores goes up to 100% when you remove the delay completely. When I left it in at 0.05 seconds I have a load of 10%.
Some extra background info (why I have 9 inputs):
I have a setup where I read the power of 9 inputs. 3 inputs (CT sensor) on PCB A and 6 inputs (hall efect sensors) on PCB B. The TX line of PCB B is connected to the RX of PCB A. The TX line of PCB A is connected to the RX on the GPIO of a raspberry Pi.
PCB A contains a call in the main loop to the function forward_data() which is defined as follows:
void forward_data(){
while (Serial.available()) {
{
String str = Serial.readStringUntil('\r\n');
Serial.println(str);
}
}}
Re: Emonhub on Pi - serial buffer overflow - read loop iteration speed
Interesting to know, The interval in emonhub.py will become detached from the interfacer loop times in the next version as the interfacers will each be operating in their own thread. The read speed is still important so this info is good to know even if 9 payloads a second is a bit on the extreme end.
Out of interest, what baud are you running at and what size is an average payload.
Paul
Re: Emonhub on Pi - serial buffer overflow - read loop iteration speed
I am using a baud rate of 9600, average payload size is 30-40 characters.