How do I do a HTTP Post and receive the JSON data to set the colour of an RGB LED?

Hi

Am trying to build a DIY Philips Hue Lamp. What I’m trying to achieve:

  1. User posts on Instagram
  2. IFTTT recipe fires
  3. NodeMCU receives the PUBLIC URL
  4. NodeMCU appends the URL to a service like Haku
  5. Receives info (This part is really confusing. How to parse JSON data? How to format incoming data to see it etc)
  6. Sets the LED Strip color to reflect the 6 most dominant colours in the image

I tried a few things and was able to get the Instagram URL printed on my serial monitor, but struggling to figure out how to do a HTTP post to get all this data. If anybody could be, it would be greatly appreciated.

#define _DEBUG_
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>

#define USERNAME "username"
#define DEVICE_ID "nodemcu"
#define DEVICE_CREDENTIAL "nodemcu"

#define SSID "SSID"
#define SSID_PASSWORD "password"
const char* url;
//const char* OutUrl;
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void call_instagram_endpoint(){
  //digitalWrite(BUILTIN_LED, LOW);
  pson colr;
  colr["rgb"] = rgb;
  data["hex"] = hex;
  thing.call_endpoint("instagram_rgb", colr);
  //digitalWrite(BUILTIN_LED, HIGH);
}

void setup() {
  Serial.begin(115200);
  //pinMode(BUILTIN_LED, OUTPUT);

  thing.add_wifi(SSID, SSID_PASSWORD);

// digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
//  thing["notiCloud"] << [](pson& in) {
//      const char* url = in["url"];
//      Serial.println((const char*)in["url"]);
//  };

  thing["notiCloud"] << [](pson& in){
      url = in["url"];
      Serial.println((const char*)url);
  };

  thing["notiCloud"] << [](pson& out) {
      const char* Value1 = url;
      Serial.println(Value1);
  };

  call_instagram_endpoint();
  Serial.println("Instagram Called");
}

void loop() {
  thing.handle();
}

Hi, not sure what is the information you need to pass to a device resource. But here is an example for receiving RGB colors:

  thing["rgb"] << [](pson& in){
      int r = in["r"];
      int g = in["g"];
      int b = in["b"];
      // change your rgb light here
  };

For calling this resource using a third party service like IFTTT, you must create a JSON like the following::slightly_smiling:

{
  "in":{
    "r": 100,
    "g": 200,
    "b": 0
  }
}

Hope this help!

Thank you so much for the reply. Honestly am still struggling to understand what to do as am a complete newbie. There are two things am struggling with. Let me ask you the simpler one first. The JSON data am getting from the URL when I do a HTTP POST is given below. How do I extract only the values I want, and maybe print it on my serial console so I can see the values am receiving? The values I want are of the 6 hex values or the 6 rgb values. Thanks for all the help.

{
   "stats" : {
      "hsv" : {
         "h" : {
            "min" : [
               "0"
            ],
            "avg" : [
               "34 0.00"
            ],
            "max" : [
               "360"
            ],
            "median" : [
               42
            ]
         },
         "v" : {
            "min" : [
               "15"
            ],
            "avg" : [
               63
            ],
            "max" : [
               "94"
            ],
            "median" : [
               69
            ]
         },
         "s" : {
            "min" : [
               "0"
            ],
            "avg" : [
               25
            ],
            "max" : [
               "75"
            ],
            "median" : [
               21
            ]
         }
      },
      "rgb" : {
         "r" : {
            "min" : [
               "36"
            ],
            "avg" : [
               154
            ],
            "max" : [
               "239"
            ],
            "median" : [
               165
            ]
         },
         "g" : {
            "min" : [
               "29"
            ],
            "avg" : [
               141
            ],
            "max" : [
               "235"
            ],
            "median" : [
               151
            ]
         },
         "b" : {
            "min" : [
               "37"
            ],
            "avg" : [
               126
            ],
            "max" : [
               "233"
            ],
            "median" : [
               129
            ]
         }
      },
      "lch" : {
         "l" : {
            "min" : [
               "12"
            ],
            "avg" : [
               59
            ],
            "max" : [
               "93"
            ],
            "median" : [
               64
            ]
         },
         "c" : {
            "min" : [
               "0"
            ],
            "avg" : [
               16
            ],
            "max" : [
               "65"
            ],
            "median" : [
               12
            ]
         },
         "h" : {
            "min" : [
               "1"
            ],
            "avg" : [
               "76 0.00"
            ],
            "max" : [
               "360"
            ],
            "median" : [
               85
            ]
         }
      },
      "lab" : {
         "l" : {
            "min" : [
               "12"
            ],
            "avg" : [
               59
            ],
            "max" : [
               "93"
            ],
            "median" : [
               64
            ]
         },
         "a" : {
            "min" : [
               "-26"
            ],
            "avg" : [
               4
            ],
            "max" : [
               "43"
            ],
            "median" : [
               4
            ]
         },
         "b" : {
            "min" : [
               "-9"
            ],
            "avg" : [
               10
            ],
            "max" : [
               "55"
            ],
            "median" : [
               8
            ]
         }
      }
   },
   "file" : {
      "w" : 50,
      "w0" : 1080,
      "filename" : "/home/martink/www/htdocs/color-summarizer/tmp/color_summarizer_oshZL5F.2",
      "h0" : 1080
   },
   "clusters" : {
      "4" : {
         "hsv" : [
            "24",
            "55",
            "81"
         ],
         "n" : 350,
         "cmyk" : [
            "0",
            "27",
            "44",
            "19"
         ],
         "tags" : "di:raw:schnapps:serria:sienna:sour:whiskey:peach",
         "num_neighbours_maxdE" : 5,
         "rgb" : [
            "207",
            "138",
            "94"
         ],
         "lch" : [
            "64",
            "40",
            "58"
         ],
         "f" : 0.14,
         "hex" : [
            "#CF8A5E"
         ],
         "xyz" : [
            "0.37",
            "0.32",
            "0.15"
         ],
         "neighbours" : "whiskey[1758][210,144,98](2.2):peach_schnapps[1581][198,128,89](4.4):raw_sienna[1699][214,138,89](4.5):di_serria[1821][212,145,93](4.9):whiskey_sour[1822][212,145,93](4.9):tan[1527][219,147,112](5.6):antique_brass[1630][200,138,101](5.8):beethoven[1728][209,148,108](6.1):burning_sand[1411][208,131,99](6.2):florentine_pink[1412][208,131,99](6.2)"
      },
      "1" : {
         "hsv" : [
            "166",
            "18",
            "69"
         ],
         "n" : 538,
         "cmyk" : [
            "12",
            "0",
            "3",
            "31"
         ],
         "tags" : "boulevard:cascade:nymph:sea:skeptic:sorrento:unwind",
         "num_neighbours_maxdE" : 7,
         "rgb" : [
            "145",
            "177",
            "170"
         ],
         "lch" : [
            "70",
            "12",
            "180"
         ],
         "f" : 0.2152,
         "hex" : [
            "#91B1AA"
         ],
         "xyz" : [
            "0.35",
            "0.4",
            "0.44"
         ],
         "neighbours" : "unwind[5476][152,183,175](2.5):sea_nymph[5469][138,174,164](2.6):cascade[5595][139,169,165](3.2):cascade[5456][140,168,160](3.2):skeptic[5349][157,180,170](3.3):sorrento[5557][154,176,172](4.4):boulevard[5533][142,165,160](5.0):pewter[5388][150,168,161](5.2):tower_grey[5348][156,172,165](5.7):ashanti[5400][166,187,179](5.8)"
      },
      "3" : {
         "hsv" : [
            "338",
            "18",
            "25"
         ],
         "n" : 428,
         "cmyk" : [
            "0",
            "5",
            "3",
            "75"
         ],
         "tags" : "afficionado:barbecue:blackadder:caffeine:chocolate:cowboy:eclipse:half:horoscope:legend:lounge:platypus:urban",
         "num_neighbours_maxdE" : 10,
         "rgb" : [
            "63",
            "52",
            "56"
         ],
         "lch" : [
            "23",
            "6",
            "353"
         ],
         "f" : 0.1712,
         "hex" : [
            "#3F3438"
         ],
         "xyz" : [
            "0.04",
            "0.04",
            "0.04"
         ],
         "neighbours" : "caffeine[8166][60,49,53](1.1):horoscope[16][67,55,58](1.2):half_barbecue[8245][60,53,55](1.9):platypus[223][61,51,52](2.1):afficionado[185][69,54,56](2.3):urban_legend[8062][66,51,58](2.6):blackadder[8074][56,45,50](3.0):eclipse[388][63,57,57](3.8):cowboy[641][68,55,54](3.8):chocolate_lounge[7889][69,60,67](3.9)"
      },
      "0" : {
         "hsv" : [
            "34",
            "16",
            "78"
         ],
         "n" : 736,
         "cmyk" : [
            "0",
            "6",
            "13",
            "22"
         ],
         "tags" : "antique:bison:craigieburn:cream:drought:eighth:half:hide:joss:malta:nullarbor:quarter:rickshaw:sandcastle:thorndon:triple:white",
         "num_neighbours_maxdE" : 10,
         "rgb" : [
            "200",
            "185",
            "167"
         ],
         "lch" : [
            "76",
            "11",
            "80"
         ],
         "f" : 0.2944,
         "hex" : [
            "#C8B9A7"
         ],
         "xyz" : [
            "0.48",
            "0.5",
            "0.44"
         ],
         "neighbours" : "quarter_malta[2469][198,184,166](1.0):quarter_rickshaw[2152][203,185,168](1.7):half_sandcastle[2512][194,180,161](2.0):half_drought[2681][202,189,169](2.0):half_joss[2308][198,185,171](2.1):eighth_nullarbor[2566][194,180,160](2.2):antique_white[2428][205,192,176](2.3):bison_hide[2892][193,183,164](2.5):triple_thorndon_cream[3132][197,189,170](2.6):quarter_craigieburn[3067][191,183,166](2.7)"
      },
      "2" : {
         "hsv" : [
            "18",
            "30",
            "54"
         ],
         "n" : 448,
         "cmyk" : [
            "0",
            "11",
            "16",
            "46"
         ],
         "tags" : "slate:cement:coffee:donkey:dune:peachpuff:puff:roman:russett:sand:wrangler:brown:peach",
         "num_neighbours_maxdE" : 9,
         "rgb" : [
            "138",
            "110",
            "97"
         ],
         "lch" : [
            "49",
            "15",
            "50"
         ],
         "f" : 0.1792,
         "hex" : [
            "#8A6E61"
         ],
         "xyz" : [
            "0.18",
            "0.17",
            "0.14"
         ],
         "neighbours" : "wrangler[1593][128,103,90](3.0):donkey_brown[2112][129,110,92](4.4):cement[2005][141,118,98](4.5):russett[1369][125,101,92](4.6):sand_dune[1594][130,111,101](4.7):peach_puff[2043][139,119,101](4.9):peachpuff[2044][139,119,101](4.9):roman_coffee[1847][125,103,87](4.9):slate_brown[1848][125,103,87](4.9):beaver[1624][146,111,91](5.1)"
      }
   }
}

I do not recommend to parse this kind of complex JSON in a microcontroller, as it may get out of memory easily, but if you want, you can use any JSON parser available for the Arduino IDE like AJson.

Why do you need to get this information dynamically? Can you extract and save the most relevant information for later use? What do you need to use Haku? or what is its purpose? I cannot understand the application yet…

Ok. Here’s what I wanted. I want to build a lamp out of a WS2812B LED strip which changes colour to reflect the instagram post of the user I specify. I found a site - http://mkweb.bcgsc.ca/color-summarizer/?url=scontent.cdninstagram.com/t51.2885-15/sh0.08/e35/12519516_260430734298444_821990130_n.jpg?ig_cache_key=MTIxNDI3MjYyNDAxODMzNTA4MA%3D%3D.2 that’ll return a JSON file with the 6 different clusters of the most dominant colours in the image and they offer an API that I can POST.

So I want to trigger a IFTTT recipe when there is a new instagram post, send the URL of the post to my lamp, which then does a HTTP POST to the URL and gets the JSON data of the rgb for the 6 colours. At least that’s the idea in principle. Is it beyond a nodeMCU or am I doing it wrong?

Ok! Now I understand your setup, and understand the API response now.

I think that the ESP8266 can be enough for your purpose. For the project, before thinger interaction, I think that you must start by working in the HTTP request and response parsing. Try making the HTTP request to a url like the following, and parse the response using AJson I commented you before. If that works, then, you can try to extract the relevant information you want, like the RGB code, change the leds, etc. Once you have this working, then integrating with thinger and IFTTT will be the easiest part. I can help you on this integration. Sure that there are tons of examples in Google for getting the first step working.

http://mkweb.bcgsc.ca/color-summarizer/?url=static.flickr.com/37/88847543_d1eb68c5b9_m.jpg&precision=low&json=1

Hi balsimpson,

Did you ever figure this out?

I am trying to make a wake up light using IFTTT. The time/date trigger is “this” and my lights turning on is “that”. I can turn the lights on and off, but can’t figure out how to integrate IFTTT.

Any ideas?

Thanks

Hi

I did figure it out but then instagram changed their policies so i couldn’t really make the lamp. what kind of lamp are you working with? nodemcu with rgb?

whatever i learnt, i leant from this. Make your IoT things react to hundred of IFTTT Events. if you give me more details i’ll see how i can help.

I figured it out! As I thought the URL I was posting was incorrect. It took me a while to figure out what it needed to be.

Thanks