Converting a working call in python to nanopb

20 views
Skip to first unread message

Renegade

unread,
Jan 30, 2025, 1:24:03 AMJan 30
to nanopb
Hi everyone, I am new to protobuf.

I have an application which sends 25 bytes written in python using a protobuf and the call works correctly, I get a response from the other end working well. The call I am using as a reference here is the rust+.py library and specifically the gettime() function.


I want to perform the same request from my ESP32. So I install configure and use nanopb to generate the pb.h and pb.c files, encode my request and send the data.

The problem is, the python code sends 25 bytes, whereas the Nanopb code only encodes a message of size 23. There is 2 bytes missing and I cannot determine where they are.

The python code seems to send the following bytes, as an example :
0000   08 04 10 da c1 a2 80 90 80 80 88 01 18 8c e2 fe   ................
0010   bc fa ff ff ff ff 01 4a 00                        .......J.

When I encode my request with nanopb, every byte is exactly the same apart from the last 2, in this case they are '4a' and '00'. I cannot see what they are meant to represent, or where they come from.

The code I use after generating my headers with nanopb's generators to make my request is as follows:

 rustplus_AppRequest appRequest = rustplus_AppRequest_init_zero;
        appRequest.seq = 0;
        appRequest.playerId=76561197960831194;
        appRequest.playerToken=-1482706676;
        rustplus_AppEmpty appEmpty = rustplus_AppEmpty_init_zero;
        appRequest.getTime =appEmpty;
        appRequest.has_entityId = true;
        uint8_t buffer[25];
     
       
        /* Create a stream that will write to our buffer. */
        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
        /* Now we are ready to encode the message! */
        status = pb_encode(&stream, rustplus_AppRequest_fields, &appRequest);
       
        message_length = stream.bytes_written;
        /* Then just check for any errors.. */
        if (!status)
        {
            Serial.printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
                 Serial.println();   
        }else{
          Serial.printf("Encoding of %i bytes complete",message_length);
        }
        b.length = message_length;
        b.memory = buffer;

I then write b.memory, the buffer, to my websocket connection which was opened successfully using the websocket library from arduino (https://github.com/Links2004/arduinoWebSockets)

My code says "Encoding of 23 bytes complete". Unsurprisingly, the server disconnects our socket after the request is sent as clearly something is missing.

Can anyone spot some simple error I am making here? Why does the correctly working python library send 25 bytes, when we appear to encode the same request and end up with only 23 bytes. I suspect I am missing something simple!

Hope you can help!

Petteri Aimonen

unread,
Jan 30, 2025, 1:30:03 AMJan 30
to nan...@googlegroups.com
Hi,

Here are some pointers:

You can use this tool to decode the hex messages for comparing:
https://protobuf-decoder.netlify.app/

It looks like the last two bytes are a submessage in the field 9,
which I think is AppRequest.getTime.

You could try setting appRequest.has_getTime = true;, maybe that
makes a difference.

--
Petteri
> --
> You received this message because you are subscribed to the Google Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanopb+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/nanopb/653e2653-5400-433d-8c31-bcf1b9a2e898n%40googlegroups.com.
> --
> You received this message because you are subscribed to the Google
> Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]nanopb+un...@googlegroups.com.
> To view this discussion visit
> [2]https://groups.google.com/d/msgid/nanopb/653e2653-5400-433d-8c31-bcf
> 1b9a2e898n%40googlegroups.com.
>
> References
>
> 1. mailto:nanopb+un...@googlegroups.com
> 2. https://groups.google.com/d/msgid/nanopb/653e2653-5400-433d...@googlegroups.com?utm_medium=email&utm_source=footer


Renegade

unread,
Jan 31, 2025, 2:13:48 AMJan 31
to nanopb
Hi Petteri,

I think you are onto something! When I add appRequest.has_getTime = true; the result is bytes that look as folllows from the esp32:

080010DAC1A2809080808801188CE2FEBCFAFFFFFFFF014A0000

This is now identical to the python.

At this point I use webSocket.sendBIN with our buffer, and I do actually get a response! HOwever the response is 'strange' suggesting something is wrong with the request still!

0a 1d 08 00 3a 19 0d 00 00 70 42 15 00 00 80 3f 1d 16 24 e5 40 25 d8 88 a0 41 2d ea 23 40 41

Thank you for helping to get a response and spot the has_getTime which I think has brought me closer to the aim!
Reply all
Reply to author
Forward
0 new messages