/*
   Inkplate4_HTTP_POST_Request example for Soldered Inkplate4
   For this example you will need a USB-C cable, Inkplate4 and stable WiFi Internet connection.
   Select "Soldered Inkplate4" from Tools -> Board menu.
   Don't have "Soldered Inkplate4" option? Follow our tutorial and add it:
   https://soldered.com/learn/add-inkplate-6-board-definition-to-arduino-ide/

   This example will show you how to connect to a WiFi network and send a POST request via HTTP.
   We will use ThingSpeak API to see post requests. It's a free API that allows you to store and retrieve data using
   HTTP.
   1. Go to the ThingSpeak.com and create a free account
   2. Open the Channels tab
   3. Create a new channel
   4. Create fields you want to use (this example uses 1 field called field1 and this name must be used when sending data)
   5. Open the channel, go to the API Keys tab and copy your Write API Key
   6. Enter your API key in the code below

   When you send a POST request, open your channel and you will see the graph where is your sent data.

   Want to learn more about Inkplate? Visit www.inkplate.io
   Looking to get support? Write on our forums: https://forum.soldered.com/
   4 April 2023 by Soldered
*/

// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#ifndef ARDUINO_INKPLATE4
#error "Wrong board selection for this example, please select Soldered Inkplate4 in the boards menu."
#endif

// Include needed libraries
#include "Inkplate.h"
#include "WiFi.h"

// Create objects from included libraries
Inkplate display;
WiFiClient client;

// Here you can change the interval of sending POST requests (minimum 15 seconds with a free license)
#define POSTING_INTERVAL_IN_SESCS 20

// Enter your WiFi credentials
const char *ssid = "";
const char *pass = "";

// ThingSpeak settings
char *server = "api.thingspeak.com";
String writeAPIKey = ""; // Enter your Write API Key

// Variable that holds last connection time
unsigned long lastConnectionTime = 0;


void setup()
{
    // Init serial communication
    Serial.begin(115200);

    // Init Inkplate library (you should call this function ONLY ONCE)
    display.begin();

    // Clear frame buffer of display
    display.clearDisplay();

    // Set text color and size
    display.setTextColor(BLACK, WHITE);
    display.setTextSize(3);

    // Display a message
    display.println("HTTP POST request\nexample");
    display.println();
    display.setTextSize(2);
    display.println("Open Serial Monitor at 115200");
    display.println("baud rate to see what's happening");
    display.display();

    // Connect to the WiFi network
    WiFi.mode(WIFI_MODE_STA);
    WiFi.begin(ssid, pass);
    Serial.print("Connecting to Wifi ");
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
    Serial.print("Connected to WiFi network with IP Address: ");
    Serial.println(WiFi.localIP());

    // Send request for the first time without waiting POSTING_INTERVAL_IN_SESCS
    sendRequest();
}


void loop()
{
    // Every POSTING_INTERVAL_IN_SESCS seconds make the POST request
    if ((unsigned long)(millis() - lastConnectionTime) > POSTING_INTERVAL_IN_SESCS * 1000LL)
    {
        // Clear frame buffer of display
        display.clearDisplay();

        // A function that does a request
        sendRequest();
    }
}

void sendRequest()
{
    // Connect the WiFi client to the server via port 80
    if (!client.connect(server, 80))
    {
        // If it fails, print a message, remember time, stop the client and reset the loop
        Serial.println("Connection failed");
        lastConnectionTime = millis();
        client.stop();
        return;
    }
    else
    {
        // If you have any sensor or something else, here you have to put data to send instead of a random number
        int field1Data = random(40);

        // Create data string to send to ThingSpeak
        String data = "field1=" + String(field1Data); // Shows how to include additional field data in http post

        // POST data to ThingSpeak
        if (client.connect(server, 80))
        {
            client.println("POST /update HTTP/1.1");
            client.println("Host: api.thingspeak.com");
            client.println("Connection: close");
            client.println("User-Agent: ESP32WiFi/1.1");
            client.println("X-THINGSPEAKAPIKEY: " + writeAPIKey);
            client.println("Content-Type: application/x-www-form-urlencoded");
            client.print("Content-Length: ");
            client.print(data.length());
            client.print("\n\n");
            client.print(data);
            delay(250);
            client.stop();

            Serial.print("The POST request is done: ");
            Serial.println(data);
            lastConnectionTime = millis();
        }
    }
}
