top of page

Making It with an API

I haven't been running much since spring break for a number of reasons, and I thought about making a tool that would help encourage me to run. I had an idea to make a screen showing the latest 3 (or so) running activities from the ITP running group on Strava, and I thought "Strava must have an API, right?" They do!


I tried making curl requests, but wasn't having any luck, I wondered if my syntax was wrong, or I wasn't getting the keys right or something... For some reason I wasn't getting anything back. Upon reading a little further, I realized I might hit privacy blocks, and several of the API examples on the Strava documentation site were also broken! So I scrapped this idea...


A simple project that I would love to have is a weather clock on the wall in the bathroom because I'm not great at checking the weather. If I can see the weather when I brush my teeth in the morning, I will be more prepared when I leave the house!


I also googled "most reliable APIs" and Accuweather came up, which seemed promising to me.


Referencing Tom's Making Things Talk HTTP AQI sketch for the Arduino, I was able to connect to Accuweather but I wasn't getting any messages back. This is what shows up when I try to print the message:

Am I doing this right?




/* AccuWeather API 
Let's see if this works
*/

#include <WiFiNINA.h> //wifi library for Arduino Nano IoT 33
#include <ArduinoHttpClient.h> //library protocol for APIs
#include "arduino_secrets.h" //network username and password
#include <Arduino_JSON.h> //maybe we need this library to read the get request?

//set global variables
const int port = 443;
const long requestInterval = 3600000; //once an hour

WiFiClient netSocket;
const char serverAddress[] = "http://dataservice.accuweather.com";
String route = "/forecasts/v1/"; 
//use locations API to find the location you want: https://developer.accuweather.com/accuweather-locations-api/apis
long lastRequestTime = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(SECRET_SSID);               // print the network name (SSID)
    WiFi.begin(SECRET_SSID, SECRET_PASS);  // try to connect
   for (int b = 0; b < 3; b++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
    delay(200);
  }
  }

  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  for (int b = 0; b < 3; b++) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
    delay(200);
  }
  route += "/hourly"; // hourly or daily
  route += "/1hour"; // 1, 5, 10, 15 days / 1, 12, 24, 72, 120 hours
  route += "/4101_PC"; //location, this set to postal code 11216
  route += "?apikey=%";
  route += SECRET_API; //this is secret api key
  connectToServer();
}

void loop() {
  // put your main code here, to run repeatedly:
 if (millis() - lastRequestTime > requestInterval) {
    connectToServer();
  }
}

void connectToServer() {
  long temp;  
  String getReq;

  // make HTTP call:
  HttpClient http(netSocket, serverAddress, port);  // make an HTTPS client
  Serial.print("Connecting to ");
  Serial.print(serverAddress);
  Serial.println(route);
  getReq = http.get(route);            // make a GET request
  Serial.print("get Request: ");
  Serial.println(getReq);
  // http.skipResponseHeaders();  // ignore the HTTP headers
  // while connected to the server:
  Serial.println("http getting");
  if (http.connected()) {
    Serial.println("connected.");
    if (http.available()) {   
      Serial.println("available.");                       // if there's a response from the server,
      // bool iconPhrase = http.findUntil("IconPhrase", "\n");  // parse response for "PM2.5"
      // if (iconPhrase) {                                 // if there's a PM2.5 value,
      //   forecast = http.parseString();                       // read PM2.5 value from the response
      //   http.flush();                                // throw out the rest of the response
      //     Serial.println(iconPhrase);                   // print it out, and
      //   }
      // }
      bool getValue = http.findUntil("Value", ":");
      Serial.println("getting temp");
      if (getValue){
        temp = http.parseInt();
        Serial.print("Temp: ");
        Serial.println(temp); 
        http.flush();
      } else{
        Serial.println("failed!");
      }
      
      http.stop();  // close the request
    }
  }
  lastRequestTime = millis();  // save the time of this HTTP call
}


1 view0 comments

Recent Posts

See All

Plug router, Philips Bridge, lights into power. Connect ethernet cable to bridge and laptop Discover router IP on Network settings Discover bridge IP on Terminal: ping -c 5 XXX.XXX.255, then arp -a. L

Hmmmmmmm...... (Hrngh!!!!!!!!!!) Let's talk this through. I'm referencing Tom's Making Things Talk AQI Web Client sketch for the Arduino Nano IoT33 to connect to a weather API through an HTTP GET requ

bottom of page