{"id":472,"date":"2015-04-29T17:24:47","date_gmt":"2015-04-30T00:24:47","guid":{"rendered":"http:\/\/internetofhomethings.com\/homethings\/?p=472"},"modified":"2017-07-24T10:14:57","modified_gmt":"2017-07-24T17:14:57","slug":"a-tiny-footprint-esp8266-arduino-ide-json-encoder","status":"publish","type":"post","link":"https:\/\/internetofhomethings.com\/homethings\/?p=472","title":{"rendered":"A Tiny Footprint ESP8266 Arduino IDE JSON Encoder"},"content":{"rendered":"<p>As I recently migrated from the ESP8266 SDK to the Arduino IDE, I was disappointed to discover that many of the SDK features were not supported with the simplified Arduino IDE. Notably, the JSON encoding\/decoding library.<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/04\/wordle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-573 size-medium\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/04\/wordle-300x297.png\" alt=\"wordle\" width=\"300\" height=\"297\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/04\/wordle-300x297.png 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/04\/wordle-150x150.png 150w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/04\/wordle.png 529w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>I tried to add pre-built libraries from several internet repositories. But none of them would work in the Arduino environment. And all I needed was a simple encoder, built from the values captured from my sensors. So I ended up creating a simple, flat encoder myself. One with minimal sketch code. I hope this may also suite someone else&#8217;s needs as well.<\/p>\n<p><strong>Here it is. Two short sketch functions, that&#8217;s it:<\/strong><\/p>\n<pre>#define\u00a0ONEJSON   1\r\n#define FIRSTJSON\u00a02\r\n#define NEXTJSON\u00a0 3\r\n#define LASTJSON\u00a0 4\r\n\r\n<span style=\"line-height: 1.5;\">void jsonAdd(String *s, String key,String val) {<\/span>\r\n    *s += '\"' + key + '\"' + \":\" + '\"' + val + '\"'; \r\n} \r\nvoid jsonEncode(int pos, String * s, String key, String val) { \r\n    switch (pos) { \r\n        case ONEJSON: \r\n        case FIRSTJSON: \r\n            *s += \"{\\r\\n\"; \r\n            jsonAdd(s,key,val); \r\n            *s+= (pos==ONEJSON) ? \"\\r\\n}\" : \",\\r\\n\"; \r\n            break; \r\n        case NEXTJSON: \r\n            jsonAdd(s,key,val); \r\n            *s+= \",\\r\\n\"; \r\n            break; \r\n        case LASTJSON: \r\n            jsonAdd(s,key,val); \r\n            *s+= \"\\r\\n}\"; \r\n            break; \r\n     } \r\n}<\/pre>\n<p><strong>And now for some\u00a0example calls to these functions:<\/strong><\/p>\n<pre>\/\/First build the \u00a0response header\r\nString s = \"HTTP\/1.1 200 OK\\r\\n\";\r\ns += \"Access-Control-Allow-Origin: *\\r\\n\";\r\ns += \"Content-Type: application\/json\\r\\n\\r\\n\";\r\n\r\n\/\/Then add the JSON string to the response\r\n\/\/Last parameter read from sensor\r\njsonEncode(FIRSTJSON,&amp;s,\"B_Pressure\", bprs);\r\njsonEncode(NEXTJSON,&amp;s,\"DS_TempInside\", tin);\r\njsonEncode(NEXTJSON,&amp;s,\"DS_TempOutside\", tout);\r\njsonEncode(NEXTJSON,&amp;s,\"DS_TempAttic\", tatt);\r\njsonEncode(NEXTJSON,&amp;s,\"DH_Humidity\", dhhumi);\r\nv = system_get_free_heap_size();\r\njsonEncode(NEXTJSON,&amp;s,\"SYS_Heap\", v);\r\nv = millis()\/1000;\r\njsonEncode(LASTJSON,&amp;s,\"SYS_Time\", v);<\/pre>\n<p><strong>The resulting response message header and JSON string:<\/strong><\/p>\n<pre>HTTP\/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: application\/json\r\n\r\n{\r\n\"B_Pressure\":\"29.0\",\r\n\"DS_TempInside\":\"86.4\",\r\n\"DS_TempOutside\":\"90.1\",\r\n\"DS_TempAttic\":\"111.6\",\r\n\"DH_Humidity\":\"30.0\",\r\n\"DH_Temperature\":\"91.4\",\r\n\"SYS_Heap\":\"32568\",\r\n\"SYS_Time\":\"1610\"\r\n}<\/pre>\n<p>Notice that I added<\/p>\n<p><strong>Access-Control-Allow-Origin: *<\/strong><br \/>\nto the response header. This is needed if you use Javascript AJAX to read the ESP8266 JSON string. Javascript will not allow cross-domain access without this in the response header. This example opens access to all domains. You can replace the * with the domain you wish to limit access to your ESP8266 from.<\/p>\n<p>Hope you find this information useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I recently migrated from the ESP8266 SDK to the Arduino IDE, I was disappointed to discover that many of the SDK features were not supported with the simplified Arduino &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,10,19],"tags":[18,17],"class_list":["post-472","post","type-post","status-publish","format-standard","hentry","category-alltheposts","category-esp8266","category-internet-of-things","tag-esp8266-arduino-ide-json","tag-esp8266-json"],"_links":{"self":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=472"}],"version-history":[{"count":12,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/472\/revisions"}],"predecessor-version":[{"id":1870,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/472\/revisions\/1870"}],"wp:attachment":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}