//Weather API:
//https://randomnerdtutorials.com/esp32-http-get-open-weather-map-thingspeak-arduino/
//http://api.openweathermap.org/data/2.5/weather?q=Kleinmond,za&APPID=ecc1d9b72d3955500709d76126597b09
//MCU = ESP32 DEVKIT V1
//=========================================================================
//ILI9341 OLED Display Connections
//=========================================================================
//MCU Display Pin
//----------------------------
//VIN 1 (VCC)
//GND 2 (GND)
//D15 3 (CS)
//D4 4 (RST)
//D2 5 (D/C)
//D23 6 (MOSI)
//D18 7 (SCK)
// 8 Not connected
//D19 9 (MISO)
//=========================================================================
//Includes
//=========================================================================
#include <WiFi.h> //Library to connect to Wifi
#include <HTTPClient.h> //Library to connect to Internet
#include <ArduinoJson.h> //Library for JSON
#include <Adafruit_GFX.h> //Library for ILI9341 Oled display
#include <Adafruit_ILI9341.h> //Library for ILI9341 Oled display
//=========================================================================
//Constants/Defines
//=========================================================================
//The wifi ssid to use while working in this simulator, later change to 'Jacques'
const char* ssid = "Wokwi-GUEST";
//The wifi password to use while working in this simulator, later change to 'kleinmond30100'
const char* password = "";
//const String url = "https://v2.jokeapi.dev/joke/Programming";
//const String url = "https://golfanywhere.co.za/WebService/Database.asmx/GetCountries?CountryID=";
const char* host = "golfanywhere.co.za";
const char* url = "/WebService/Database.asmx/GetCountries?CountryID=";
#define TFT_DC 2 //For display.
#define TFT_CS 15 //For display.
//=========================================================================
//Variable Declares
//=========================================================================
Adafruit_ILI9341 Display = Adafruit_ILI9341(TFT_CS, TFT_DC); //Display.
//===================================================================================
//S E T U P
//===================================================================================
void setup() {
Serial.begin(115200); //For debugging.
setupDisplay();
connectToWifi();
getJSON();
}
//===================================================================================
//M A I N L O O P.
//===================================================================================
void loop()
{
//tft.fillScreen(ILI9341_BLACK);
//tft.setCursor(0, 0);
}
//===================================================================================
void setupDisplay()
{
Display.begin();
Display.setRotation(1); //Landscape.
Display.setTextColor(ILI9341_WHITE);
Display.setTextSize(2);
}
//===================================================================================
void connectToWifi()
{
String str1;
String str2;
String str3;
Display.fillScreen(ILI9341_BLACK); //Clear screen.
Display.setCursor(0, 0);
str1 = "Connecting to ";
str2 = str1 + ssid + "...";
Display.print(str2);
WiFi.begin(ssid, password, 6); //Connect to Wifi.
while (WiFi.status() != WL_CONNECTED)
{}
Display.fillScreen(ILI9341_BLACK); //Clear screen.
Display.setCursor(0, 0);
IPAddress localIP = WiFi.localIP();
str1 = "Connected to ";
str2 = localIP.toString();
str3 = str1 + str2;
Display.print(str3);
}
//===================================================================================
void getJSON() {
HTTPClient http;
http.begin("https://" + String(host) + url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
//Response =
//{"Table":[{"id_GolfBookingCountry":1,"nvcGolfBookingCountry":"South Africa"},{"id_GolfBookingCountry":2,"nvcGolfBookingCountry":"South African Agents"}]}
Serial.println(payload);
// Parse the JSON response
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.println("Deserialization failed: " + String(error.c_str()));
//return;
}
JsonArray table = doc["Table"];
for (int i = 0; i < table.size(); i++) {
JsonObject country = table[i];
int id = country["id_GolfBookingCountry"];
String name = country["nvcGolfBookingCountry"];
Serial.println("id: " + String(id) + ", name: " + name);
}
}
else
{
Serial.println("Error on HTTP request");
}
http.end();
}