Global Twitter Mood Light

Hi - im pretty new to programming but have completed a few projects that turn data into physical actions. My latest project i really want to build is a RGB lamp that connects to this API http://wefeel.csiro.au/#/ in particular this http://wefeel.csiro.au/api/emotions/primary/totals and displays a colour based on the returned emotion eg:

{“love”:2008664,“other”:4499476,“sadness”:4328887,“anger”:1233666,“joy”:15646593,"*":24963396,“surprise”:1595201,“fear”:868048}

I would like the photon to take the highest value and return a colour so IF “love” is the highest number return ‘RED’ or if its “anger” = “FLASH ORANGE”

I created a similar project using @jvanier advice on http://hook.io/ i ried using this tutorial but cant really get my head around using it on this API https://www.hackster.io/monkbroc/step-by-step-guide-to-make-a-physical-dashboard-ae3b8a

Any pointers greatly appreciated

Thanks Al

Take a look at webhooks, since that might simplify things a bit :slight_smile: which part are you struggling with and what have you tried so far?

Ive managed to parse the API in hook.io using @jvanier tutorial from here but i dont know how to map the values to colors

module['exports'] = function WeFeel (hook) {
  var got = require('got');
  got('http://wefeel.csiro.au/api/emotions/primary/totals')
  .then(function (response) {
hook.res.end(response.body);
  })**_**This is the section im struggling with!_**
  .catch(function (error) {
hook.res.end("Error fetching data: " + error);
  });
};

These are the values from the API https://hook.io/ginandtronic/wefeel im trying to send to the photon - the photon would only display rank number ‘1’

This is the photon code

int setWeFeel(String value) {
    RGB.control(true);
    if(value == "pulse_green") {
        RGB.color(RGB_COLOR_GREEN);
        return 0;
    } else if(value == "pulse_blue") {
        RGB.color(RGB_COLOR_BLUE);
        return 0;
    } else if(value == "pulse_red") {
        RGB.color(RGB_COLOR_RED);
        return 0;
    } else if(value == "flash_green") {
        RGB.color(RGB_COLOR_GREEN);
        return 0;
    } else if(value == "flash_orange") {
        RGB.color(RGB_COLOR_ORANGE);
        return 0;
    } else if(value == "flash_red") {
        RGB.color(RGB_COLOR_RED);
        return 0;
    }
    return 1;
}

void setup() {
    Particle.function("we-feel", setWeFeel);
}

void loop() {
}

Any help i would be stoked! :slight_smile:
Cheers
Al

I’d agree with @Moors7, why not use Particle webhooks and do the whole lot on the device.
The webhook would only relay the data to your device and then you’ve got all the logic in one place.

1 Like

Thanks @ScruffR and @Moors7 cool ive spent a good few hours trying to get this working with webhooks

I created the webhook to read the data

My photon is running the below but im really stuck with how to pull the highest value out and display a colour on the onboard LED

void loop() {
  // Get the emotion
  String data = String(10);
  // Trigger the integration
  Particle.publish("get_emotion", data, PRIVATE);
  // Wait 60 seconds
  delay(60000);
}
      

    
    void setup() {
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/get_emotion", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}
void myHandler(const char *event, const char *data) {
  // Handle the integration response
}

you get data from the event, so you need to process that into usable data.

One way would be to add a Response Template (mustache template) to your Integration/WebHook and get just deal with the numbers(if that is all you are really interested in).

Another approach would to be to use the JSON parser library (a couple available if you search this site) to extract the values from the JSON object you received in data.

2 Likes

I tried using mustache but soon realised i need the ‘emotion’ as well as the value. This is what the webhook is returning

Requesting Emotion
47hook-response/get_emotion/0, data: {"love":2078091,"other":4099889,"sadness":4038821,"anger":1114953,"joy":15067171,"*":23838244,"surprise":1468112,"fear":840305}

Unfortunately my knowledge of programming is very limited so im unsure how to start ‘sorting’ the numbers and emotions so i can display the highest value as a LED colour. Ive spent hours searching and trying to learn more and came across this brilliant tutorial http://www.connected-displays.com/tutorial/photon-rain-led-tutorial/ i have tried my best to configure it to the API im working with and its given me a great understanding of how to extract specific data using webhooks & mustache.

This is my modified version of the tutorial above https://pastebin.com/6PRvf6dV

You could get the full response and either parse that JSON string yourself or use this lib
https://build.particle.io/libs/SparkJson/0.0.2/tab/example/JsonParserExample.ino

To extract the moods. Then convert the values using atoi() and then just find the max.