GET REQUEST on particle photon

Hello,

I would like to use my particle photon to get data concerning electricity prices. I would need to do a get request to “https://acc.ameo.app/api/v2/characteristics/590” with Authorization Bearer: REDACTED

I can’t find an example how to do such a get request. Can anyone help me out?

Thanks in advance,
Bart

Webhooks would be the way to go.

Setting up a webhook is straight forward via
https://console.particle.io/integrations

1 Like

Thanks, but I am using the code with blynk integrated. If I try the code below the BlynkTimer causes the error "no matching function for call to ‘BlynkTimer::setInterval(long int, void (&)(const char*, const char*))’ ". Any idea how to solve this?

Thanks in advance,
Bart

#define BLYNK_TEMPLATE_ID           "xxx"
#define BLYNK_DEVICE_NAME           "xxx"
#define BLYNK_AUTH_TOKEN            "xxx"
#define BLYNK_PRINT Serial
#include <blynk.h>

#include "Particle.h"

char auth[] = BLYNK_AUTH_TOKEN;

String data = String(10);

BlynkTimer timer;

BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0);
}

void setup() {
  Blynk.begin(auth);
  Particle.subscribe("hook-response/GetEnergyPrices", myHandler, MY_DEVICES);
  timer.setInterval(5000L, myHandler);
}

void myHandler(const char *event, const char *data) {
  Particle.publish("GetEnergyPrices", data, PRIVATE);
}

void loop() {
  Blynk.run();
  timer.run();
}


@mertensbart20, you have the timer call myHandler which is also the Particle.subscribe() callback. The callback will work fine for Particle.subscribe() but will not work with the timer since it expects a function with no arguments. You will need a separate callback for the timer.

Also, putting a Particle.publish() in a Particle.subscribe() callback that uses the passed data may not work as expected since both share the same buffer. You may want to copy the contents of data to a local buffer before publishing it. From the documentation:

NOTE 2: Particle.publish() and the Particle.subscribe() handler(s) share the same buffer. As such, calling Particle.publish() within a Particle.subscribe() handler will overwrite the subscribe buffer, corrupting the data! In these cases, copying the subscribe buffer’s content to a separate char buffer prior to calling Particle.publish() is recommended.

3 Likes

Hello peekay123,

Thanks for the quick response. I think I found a solution.

Bart

@mertensbart20, I’d like to understand what you are trying to do. Am I correct in assuming that every 5 seconds, you want to trigger a webhook by publishing a “GetEnergyPrices” event and then subscribing to the response of that webhook so you can catch the energy data and do something with it using Blynk?

Hello peekay123,

What I want to do is the following. I want to check the energyprices via a Get request to https://acc.ameo.app/api/v2/characteristics/590 . From there I want to charge my electric car when electricity prices are low. I already made a smartcharger with a particle photon that can charge my car on sunlight. Now I would like to add the functionality to charge when energyprices are low.

I followed the steps for making a webhook and the webhook integration says to integrate the following in my firmware. For the trigger integration:

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

for the Get integration response:

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

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

But do I need both, trigger integration and get integration? Maybe only get integration? I am very new at webhooks so this is not easy for me :slightly_smiling_face:

Thanks for all your advice,
Bart

@mertensbart20, can you post a screenshot of your webhook setup?

For a webhook to fire, you need to trigger it via a publish(). You subscribe for the response from the webhook request. I’m not sure what data is for in the publish() and so the request for the webhook screenshot.

In your case, myHandler() is the webhook response handler and it is in that function that you would parse the const char *data string to obtain the data you need. How large is your expected response (in characters)?

For now the problem is that the webhook is triggered but I think I don’t get a response. The company says I also have a personal token of type Bearer but I don’t see where to put this info in the webhook. Any idea where to put this token in the webhook?

Okay, it seems that the data you get from the api is very extensive. I think I better read the data to a google sheet, parse it and then read a specific cel with the photon from the google sheet.

When your raw data came in JSON form the webhook integrations can already help you extract the relevant data and only forward that.

If you had a sample set of your raw data we could have a look at it and advise.

2 Likes

I think it will be far simpler to manually check the energyprices and let my electric car charging during the low price hours. This is getting to complex to solve automatically. So thanks to everybody who helped but I am going to solve the charging problem with a low tech solution. :slightly_smiling_face:

if the circumstances are in your favour it may not be as complicated as it might seem.
I guess you already have an account with that provider and can download the raw data on your computer. This way is shouldn’t be too difficult to upload that sample set of data into a post here and from there we can see.

The manual check will always be there as a last resort after investigating at least the simplest approaches :wink:

2 Likes

Thanks, but I am going to leave it at this point. It is a hobby-project and it is taking to much time.