#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#include <HTTPClient.h>
#include <ArduinoJson.h>
String jsonBuffer;
const int pinLED = 2;
#include "secrets.h"
//Buzy Wait Timers
// unsigned long t_lastRead = 0;
// unsigned long T_read = 10000;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("CS452 Rapid API");
WiFi.mode(WIFI_MODE_STA);
for (int i = 0; i < sizeof(wifi_cred) / sizeof(wifi_cred_t); i++) {
if (wifi_cred[i].password == NULL) {
wifiMulti.addAP(wifi_cred[i].ssid);
} else {
wifiMulti.addAP(wifi_cred[i].ssid, wifi_cred[i].password);
}
}
Serial.println("Connecting...");
connectWiFi();
}
//**** NEED to use the %20 for the space character ' '
// String dish = "chicken%20soup";
void loop() {
// unsigned long t_now = millis();
// if (t_now >= t_lastRead + T_read) {
// t_lastRead = t_now;
//read line from console
//message buffer
char message[256];
int i_msg = Serial.readBytesUntil('\n', message, 255);
message[i_msg] = '\0';
char dish[768];
urlencode(dish, message);
if (i_msg > 0) {
if (WiFi.status() == WL_CONNECTED) {
//**** NEEDS SERVER PATH
String serverPath =
String("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com?q=") +
dish;
Serial.println(serverPath);
jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
// Allocate the JSON jsonDocument
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON jsonDocument.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<2048> jsonDoc;
// Deserialize the JSON jsonDocument
DeserializationError error = deserializeJson(jsonDoc, jsonBuffer);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// double temperature = jsonDoc["main"]["temp"]; //we directly convert it to double without checking
// double pressure = jsonDoc["main"]["pressure"];
// double humidity = jsonDoc["main"]["humidity"];
// double windSpeed = jsonDoc["wind"]["speed"];
// String windDir = jsonDoc["wind"]["deg"]; //we are using a String for this
// String windForce = jsonDoc["wind"]["force"]; //this is not there
// Serial.print("Temperature: ");
// Serial.println(temperature);
// Serial.print("Pressure: ");
// Serial.println(pressure);
// Serial.print("Humidity: ");
// Serial.println(humidity);
// Serial.print("Wind Speed: ");
// Serial.println(windSpeed);
// Serial.print("Wind Direction: ");
// Serial.println(windDir);
// Serial.print("Wind Force: ");
// if (windForce != "null") //here we check for null
// Serial.println(windForce);
// else
// Serial.print("joson field not found");
String ing = jsonDoc["ingredients"];
Serial.println("Ingredients are: ");
Serial.println(ing);
} else {
Serial.println("WiFi Disconnected");
connectWiFi();
}
}
}
String httpGETRequest(const char* serverName) {
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(serverName);
//**** NEEDS FOLLOWING HEADERS
// http.addHeader("Accept", "application/json");
http.addHeader("X-RapidAPI-Key", RapidApiKey);
http.addHeader("X-RapidAPI-Host", "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com");
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
void connectWiFi() {
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
while (wifiMulti.run() != WL_CONNECTED) {
delay(250);
Serial.print(".");
digitalWrite(pinLED, !digitalRead(pinLED));
}
digitalWrite(pinLED, HIGH);
Serial.println();
Serial.println();
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
inline char hex_digit(char c) {
return "01234567890ABCDEF"[c & 0x0F];
}
char *urlencode(char *dst,char *src) {
const char specials[] = "$&+,/:;=?@ \"<>#%{}|\^~[]'"; /* String containing chars you want encoded */
char c;
char*d = dst;
while (c = *src++) {
if (strchr(specials,c)) {
*d++ = '%';
*d++ = hex_digit(c >> 4);
c = hex_digit(c);
}
*d++ = c;
}
*d = '\0';
return dst;
}