Compiles/runs in Arduino 1.85 but not PlatformIO

I compile and run a program that parses JSON from a serial input within Arduino 1.85 and it parses JSON no problem. Cut and paste the code into PlatformIO and it compiles and uploads okay but serial is garbled and it doesn’t parse correctly. I’ve checked it with an external serial monitor which works with the program uploaded from Arduino. The ArduinoJSON library is the same version in both IDEs. I am using a genuine Arduino Mega fresh out of the box. Thank you.

Thinking back over the past six months if this is a real issue and not user error it could be the reason for other unexplained serial issues I’ve been having. BTW I’m using PlatformIO on Xubuntu 16.04LTS

Apparently I can’t attach program source files. How do I give more info in this case for troubleshooting without pasting a hundred lines of identical code twice? Thanks.

Strange indeed, if you are using the exact same library versions and everything you should get the same behaviour.

Did you try different baud rates?

You can upload your code to pastebin.com and give is the link if it’s just one code file. For re-producing we also need your platformio.ini. If it’s a bigger project I suggest zipping it and uploading it to dropbox/google drive. You’d also need to give us a testcase of input plus expected output.

It shouldn’t make a difference, but… However, I want to try this on my old dev laptop first. I said I had serial issues previously which might be similar. I never experienced any on my old dev laptop which is Windows 7.

I will post code etc if the above does not result in anything interesting. Thanks.

Changing the baud rate did have an effect. Lowering the baud rate made the received (to computer) text more garbled while increasing it made it better. Either way the program appears to not parse a simple json packet correctly. Again this seems to follow the pattern I experienced during the summer with a different program.

I ran the exact same program on my other machine at home (Windows 7) and had the exact same problem. Again there are no problems using the Arduiono 1.85 IDE on either machine. Use of an external serial monitor does not make a difference either. I’ve posted the code to pastebin. This is an exact copy of the demo code except that I put setup and loop at the end so I would not need function prototypes when using PlatformIO. Arduino does not care. I also added the #include <Arduino.h> just for PlatformIO

[env:megaatmega2560]
platform = atmelavr
board = megaatmega2560
framework = arduino

I can reproduce the fault but have no idea why that’s happening… In PIO the firmware faults on an Arduino Nano, an ESP8266 and on an ESP32. On the ESP32 a WDT timeout is triggered (Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU1)), as well as on the ESP8266 (wdt reset). Nano just sometimes spits out garbage over the Serial an continues, but never prints the value of the JSON object. Using an Arduino Nano with the Arduino IDE and the exact same ArduinoJSON version works, at least it prints the JSON value and doesn’t appear to get stuck.

I’ve compared the build flags in all g++/gcc commands and there is no visible difference. C++ version, optimization flags and other flags match up. Will check and try to debug on different hardware later.

Well at least I’m not crazy. Thanks. Interesting that you mention the ESP8266. I’ve had problems with the WDT triggering for no good reason. Should this be raised to the level of a bug report?

1 Like

What the actual hell. I found the solution.

Add all the prototypes of the dump functions at the beginning of the sketch after the includes.

void dump(const JsonVariant &variant, int nesting);
void indent(int nesting);
void dump(bool value, int nesting);
void dump(long value, int nesting);
void dump(double value, int nesting);
void dump(const char *str, int nesting);
void dump(const JsonObject &obj, int nesting);
void dump(const JsonArray &arr, int nesting);

I was being suspicious about those. They are using parameter overloading. I’m not sure what exactly happened here but maybe it called a different function for a different parameter type because at the time one function was compiled the other was not known and the signature was still applicable? C++ magic.

This also explains why this was working in the Arduino IDE – the pre-processing run of “collect all function prototypes and write them at the top” helped solve this problem.

Now works on my Arduino Nano flawlessly. One mystery less.

Thanks very much. Wish I could mail you a beer. Is this a bug on either platform or just programmer error?

Well the sketch works in Arduino IDE which does insertion of all function prototypes at the beginning, which is why this works. This is done by-design in Arduino IDE. So if you’re strictly using Arduino IDE that sketch does work. On PIO in this configuration it didn’t, maybe because of reasons above. The programmer relied on Arduino IDE features here in which it’s supposed to be used, so it’s not strictly a ‘bug’.

At least such an error can’t happen in the rest of the library because those a .cpp/h files which compile the same on both platforms. Only ino files get that extra treatment in Arduino IDE. You should be safe there if you always write out function prototypes at the top when you convert Arduino sketches to cpp files in PIO.

Try to switch to CPP which will resolve all issues with converting INO → CPP.