Misformed RX packet from Arduino to RPI gateway via serial

Hey all, Im having a strange problem here...

I have an arduino board monitoring two of its analog inputs, and sending the value of them to a Raspberry PI with OEM-Gateway installed via the instructions, over a serial connection to the PI. I have verified that oemgateway.conf is correctly setup, is listening to /dev/ttyAMA0 and does receive correct-looking data over that port.

For some reason, the RPI keeps telling me that it received a "malformed" RX string from the arduino (and does not attempt to post the info via HTTP), but then it shows an apparently correctly-formed set of data in the displayed str(received) string. 

This is the arduino code that is sending the 'malformed' string via serial. Can anyone see anything glaringly wrong with it? (besides maybe a missed semi-colon or something, I typed it here, not pasted it...)

 

[code]

#include "EmonLib.h"

EnergyMonitor emon1;

EnergyMonitor emon2;

void setup(){

Serial.begin(9600);

emon1.current(A7, 59); // Current transformers on A6 & A7; "59" is the calib.

emon2.current(A6, 59);

}

 

void loop(){

double Irms1 = emon1.calcIrms(10000);

double Irms2 = emon2.calcIrms(10000);

 

int a = 12;  //dummy sensor node number for now

 

Serial.print(a, DEC);

Serial.print(' ');

Serial.print('Irms1');

Serial.print(' ');

Serial.println('Irms2');

}

[/code]

 

seemingly simple, yet frustrating.... the RPI console will output something like :

 

INFO Serial RX: 12 0.05 9.8

WARNING Misformed RX packet: '12' '0.05' '9.8'

INFO Serial RX: 12 0.12 7.6

WARNING Misformed RX packet: ' 12' '0.12' '7.6'

 

(this is repeated every time the arduino sends a serial packet; '12' being nodeID, '0.05' and '9.8' are current in # amps measured from the two sensors)

the sensor values change as they normally would, so I know that the arduino is correctly sensing current, and sending the values, and the RPI is correctly seeing them come in over the GPIO serial port. If I plug the arduino into a PC and open serial port monitor, i see an apparently- correct string of data... I dont know why they are marked as 'misformed'... help?

Thanks

 

 

PS... this is the relevant section of oemgatewaylistener.py, I just dont know why

if (len(received) < 2)

is returning 'TRUE' when received() is supposedly '12' '0.05' '9.8'

 

[code]

def _process_frame(self, f):
        """Process a frame of data

        f (string): 'NodeID val1 val2 ...'

        This function splits the string into integers and checks their
        validity.

        'NodeID val1 val2 ...' is the generic data format. If the source uses
        a different format, override this method.

        Return data as a list: [NodeID, val1, val2]

        """

        # Log data
        self._log.info("Serial RX: " + f)

        # Get an array out of the space separated string
        received = f.strip().split(' ')

        # Discard if frame not of the form [node, val1, ...]
        # with number of elements at least 2
        if (len(received) < 2):
            self._log.warning("Misformed RX frame: " + str(received))

        # Else, process frame
        else:
            try:
                received = [int(val) for val in received]
            except Exception:
                self._log.warning("Misformed RX frame: " + str(received))
            else:
                self._log.debug("Node: " + str(received[0]))
                self._log.debug("Values: " + str(received[1:]))
                return received

 

[/code]

pb66's picture

Re: Misformed RX packet from Arduino to RPI gateway via serial

PS... this is the relevant section of oemgatewaylistener.py, I just dont know why

Are you sure it's that line causing the issue? that log message is used multiple time in OEM gateway, in the excerpt you show, it is used again further on in the exception for the try: received = [int(val) for val in received], which will fail due to the float values. Try changing int(val) to float(val).

Paul

 

pb66's picture

Re: Misformed RX packet from Arduino to RPI gateway via serial

In fact you should probably change that line from

received = [int(val) for val in received]

to

[float(val) for val in received]

so it doesn't get cast at all, just tested for all numeric content

Paul

asd978f's picture

Re: Misformed RX packet from Arduino to RPI gateway via serial

Winner, Winner. 

I just tried your first suggestion of switching 

               received = [int(val) for val in received]

for

               received = [float(val) for val in received]

 

and it is now posting correctly to my emoncms instance! Strange that all the arduino examples ive poured thru looking for this didnt mention the data type difference (or i missed it).

 

So now, which is more correct for the long run, this way, or 

[float(val) for val in received]

?

 

pb66's picture

Re: Misformed RX packet from Arduino to RPI gateway via serial

I don't think it's vitally important, but the purpose of the try: is to confirm all the values are numeric. the int(val) is left over from the int only days and float(int) works with both ints and floats, but if you use the received = float(val) that will cast all values to floats, which isn't necessary as a test will do.

If you look at your log entries now you will notice even the node id is a float, again it will work ok. I prefer the last way as it has no effect on the data, it only tests so floats remain floats and the node id and ints vals remain as ints - easier on the eye and the processor but only by a nano fraction :-)

either way is ok. The reason you probably didn't find any ref to this is that most data is piped to OEM gateway via an rfm2pi so most sketches are written for that application and then sometimes adapted for serial etc. to use rfm2pi & JeeLib, all values are transmitted as whole numbers and scaled at either end, a temperature of 19.5 is usually sent as 1950 and divided by 100 in emonCMS so floats just don't get used that often.

Paul

Comment viewing options

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