The trouble with JSON

A project log for LUMOS

1m 12w RGB LED WiFi Artnet Controlled Cubes

charles-yarnoldCharles Yarnold 05/26/2016 at 05:190 Comments

I'm writing this after wrestling with json to save config settings to the esp file system and get them back out again, so I may be a little biased and reserve the right to remove this post!

I had initially chosen to use json as there is the ArduinoJSON library for parsing and creating json. I already have json in the sketch that gets sent as a udp packet as its heartbeat. This is just a sprintf that throws together a few variables and passes it to the udp send bits.

The main issues I was having with the ArduinoJSON library was due to it needing to use a small set of variable types, notably "const char*" but not "char*". Alot of the other libraries I use expect char* so I got into a hell of needing to recast variables all over the place. To the point where I ended up recasting around back in a circle without realising. Along with this I ran into the issues of arduino pushing structs below functions in the compile order so I needed to move the settings struct to its own h file.

Another big issue was that I needed to have my settings available right away to setup pin modes. After that I call my WiFiManager setups and this seems to interfere with my struct, wiping out my const char* variables! This meant having to run the function again to correct this after finishing with WiFiManager (I think this is to do with that lib using json parsing itself, something to look into another day).

This still caused issues, so instead of a global struct with the settings in I reverted back to my independent global variables and recasting at the function to parse the JSON so the rest of my sketch could go back to how it was.

Its a horrible mess I don't want to look at for a bit, but at least that mess is contained to the 2 functions dealing with the JSON and I can come back and fix it once I'm done with the rest of my todo list. I'm tempted to rip out the json and just store it as plain text...

Next on my list is adding a find me function so I can pick the esp I'm talking to out of a box of 20+ of them. Then build the internal server to set the settings these horrible json functions are using, maybe I'm revisiting this sooner than I thought :/

I'm making myself happy by hooking 10 of them up to a power supply and sending a mass ota update just to watch them turn cyan, reset then re-join the network at once. Its purdy to watch.