/*
  Inkplate13SPECTRA_HTTP_POST_Request example for Soldered Inkplate 13SPECTRA
  For this example you will need USB cable, Inkplate 13SPECTRA and stable WiFi Internet connection.
  Select "Soldered Inkplate 13SPECTRA" from Tools -> Board menu.
  Don't have "Soldered Inkplate 13SPECTRA" option? Follow our tutorial and add it:

  /////////////////////////// INSERT BOARD DEF LINK HERE

  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 https://docs.soldered.com/inkplate/
21 January 2026 by Soldered Electronics
*/

// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#ifndef ARDUINO_INKPLATE13SPECTRA
#error "Wrong board selection for this example, please select Soldered Inkplate 13SPECTRA 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(INKPLATE_BLACK);
  display.setTextSize(4);

  // Display a message
  display.printf("HTTP POST request example\n\n");
  display.printf("Open Serial Monitor at \n115200 baud rate to see \nwhat'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());
}


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();

    // 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);

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