#define ChannelID 7
#define TINAPA_API "http://api.olympusworld.online:1336/api/esp/"

// WiFi CONFIG
#define _SSID "Wokwi-GUEST"
#define _PASS ""


#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>

WiFiClient client;
HTTPClient http;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(_SSID, _PASS);

  while (WiFi.status() != WL_CONNECTED)
	{
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
	Serial.print("Connected to Wi-Fi network with IP Address: ");
	Serial.println(WiFi.localIP());

	String url = String(TINAPA_API) + String(ChannelID);
	http.setReuse(true); // reuse ang connection for ease post
	http.begin(client, url);
}

void accumulateTinapa()
{
	http.addHeader("Content-Type", "application/json");
  int randomData = random(1, 101);
	String data = "{\"accumulated\":" + String(randomData) + "}";
	String url = String(TINAPA_API) + String(ChannelID);

	if (!http.connected())
	{
		// reinitialize the HTTP client if wla nka connect
		http.begin(client, url);
		http.addHeader("Content-Type", "application/json");
		http.addHeader("Connection", "Keep-Alive");
  }

	int httpResponseCode = http.POST(data);
	if (httpResponseCode > 0)
		Serial.println("Successfully sent data to Tinapa");
	else {
		Serial.print("Error code: ");
		Serial.println(httpResponseCode);
	}

	//isRequestSent = true;

	http.end();
}

void loop()
{
  accumulateTinapa();
  delay(100);
}