Retrieving Data from APIs

Retrieving Data from APIs

Overview

  • APIs provide useful data we can use in our device (e.g. weather and location data)
  • Each API will be configured slightly differently but the process to connect is generally similar

##

Screenshot 2024-06-22 at 1.05.34 AM

##

Screenshot 2024-06-29 at 10.43.49 PM

##

Screenshot 2024-06-29 at 10.44.46 PM

##

Screenshot 2024-06-29 at 10.46.13 PM

##

Screenshot 2024-06-29 at 10.47.42 PM

Steps to Connect Photon 2 and API

  1. Determine how to use the API
  2. Create an integration -> webhook on Particle console
  3. Use Particle.publish to trigger webhook
  4. Use Particle.subscribe to “listen” for response from webhook
  5. Create Mustache template that tells the Particle Cloud to which relevant data from the response should be to the Photon 2 (and the rest of the data will be ignored)
  6. Create function handler that is used by Particle.subscribe to process JSON

Step 0: How to use the API

  • Each API is different, but they will usually have documentation that describes how to connect
  • Typically, this will include
    • Endpoint (URL you communicate with)
    • Parameters to include in your request (e.g. name of city you want weather data for)
    • How to obtain an API key (if necessary)

Step 0: How to Use the API

Example: WeatherStack

image-20201017181859930

Step 1: Create Webhook In Particle Console

Particle integration settings

image-20200405005837141

Part 2: Publish Event to Trigger Webhook

Photon 2 firmware

void loop() {
  String data = "90089";	//USC zip code
  
   // Trigger the integration
  Particle.publish("JSONWeatherStack", data, PRIVATE);
 
   // Wait 60 seconds
  delay(60000);
}

Part 3: Subscribe to JSON response from Weather Stack

Photon 2 firmware

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

##

Screenshot 2024-07-01 at 10.11.13 PM

##

Screenshot 2024-07-01 at 10.15.08 PM

##

Screenshot 2024-07-01 at 10.16.55 PM

##

Screenshot 2024-07-01 at 10.19.36 PM

##

Screenshot 2024-07-01 at 10.22.14 PM

Part 4 : Create Mustache template

  • Often we might only want a few items from the JSON, but the webserver sends the entire message
  • This extra data can waste time, bandwidth, power, and the response size can create errors
  • Instead, we can have Particle webserver send us only the data we actually want by creating Mustache templates

Example: Entire Weather Stack JSON Response

image-20200405005641533

Example: What if we only want the temperature?

right:50%

Creating Mustache Webhook Response Templates

Particle Console Webhook

image-20231012134414124

Example: Mustache Format

right:50%

  • If we are only interested in the temperature value which is nested in the current object, we could create a template like the following
  {"temp":"}"}
  • Now instead of the server sending entire JSON response, it will only send the following
    {"temp":"61"}
    
  • For webhook response templates, make sure the template will always result in valid JSON (i.e. {"name":"value"})

Part 5: Creating the function handler to receive and parse the JSON

Resources

Credits

Updated: