Weather API webhook RGB led code issue

Hi All,

I am new to programming and the particle system, pardon if my jargon is incorrect or if I am using incorrect wording (feel free to correct me) . I am attempting to create a light that changes based on the weather. I have seen a few similar projects and believe I am close to having this one working.

I have created a webhook that is using Dark Sky’s API. I have it pulling only one specific field and publishing an event based on that field. The event name is “summary”. Then I have subscribed to the event. My problem is that my command and set color do not seem to be talking/reading the event. Does anyone have an advice on how to make certain Pins turn on or off based on what the event is?

int redPin = D1;
int greenPin = D2;
int bluePin = D3;

int colorOne(String command);

void setup() {
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/summary", myHandler, MY_DEVICES);
  Serial.begin(9600);
  Particle.function("summary", colorOne); // Expose the color function

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}

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

int i = 0;
void myHandler(const char *event, const char *data) {
  // Handle the integration response
  i++;
  Serial.print(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data)
    Serial.println(data);
  else
    Serial.println("NULL");
}

int colorOne(String command){
    if(command == "Clear") {
        setColor(255, 255, 255);
        return 1;
    }
    else 
        return -1;
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

have you checked console.particle.io/integrations whether your webhook is correctly sent and the response received?
Can you share your webhook definition (make sure to obfuscate privat data)?

Hey ScruffR,

Thank you so much for replying I appreciate it immensely!

I have checked my web hook via the integrations and it looks like it is being correctly sent and received I have attached a photo of the event log.

Here is my webhook definition. I have set lat and long to 100 and removed location data. Is this the definition you are looking for?

{"latitude":100.000,"longitude":-100.000,"timezone":"Location","currently":
{"time":1579578709,"summary":"Overcast","icon":"cloudy","nearestStormDistance":2,
"nearestStormBearing":227,"precipIntensity":0,"precipProbability":0,"temperature":31.8,
"apparentTemperature":31.8,"dewPoint":13.35,"humidity":0.46,"pressure":1025.7,
"windSpeed":2.22,"windGust":3.37,"windBearing":198,"cloudCover":0.91,"uvIndex":0,
"visibility":10,"ozone":269.8},"offset":-7}

Cheers and thanks again

Not really.
I was actually looking for the definition of the webhook itself which you can obtain in console.particle.io/integrations when editing your webhook.

However, since we have established that the webhook works correctly, we can move on to the next steps

  • are you seeing the Serial.print() output of your myHandler()?
  • when that works, we have also established that the subscription works
  • finally (and that is the actual reason for your problem) you are not acting on the response you catch with myHandler(). You are merely printing the incoming data but don’t use it to call your setColor() - webhooks don’t call a Particle.function() even if they are called the same - so no surprise the colour doesn’t change :wink:

Background:
A webhook provides a way to request data from a third party server via a slim interface offloading most work to the cloud but they are not meant to get active on their own.
But this might be what you expected when you named your Particle.function() the same as the event to trigger the webhook.
Particle.function(), Particle.publish() and (for completeness) Particle.variable() are completely independent from eachother and one will never interact with the other unless you explicitly code it (either on your device or at some remote server).
The only direct interaction exists between Particle.publish() and Particle.subscribe() when both share a common event and webhooks can act as a (mere) relay between an outgoing and a related incoming event.

Hey ScruffR,

Thank you for the comprehensive run through of webhooks! It makes so much more sense now. I was misunderstanding the Particle.function()

I’m thinking if I make the addition to myhandler() section of my code it will look something like this and should call my setColor() correctly.

void myHandler (const char *event, const char *data) {

if (strcmp(data,"Clear")==0) {
   setColor(255, 255, 255);
    return 1;
}   else 
return -1;
}
1 Like

You cannot use return x in a void function tho’

On the other hand you can keep the Particle.function() as you had it but inside your subscription handler just call colorOne(data).
This way you can have your webhook and a user callable function do the same thing.

Thanks so much Scruff I got it working!

You are a legend!

Cheers.

1 Like