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

// How many leds in your strip?
#define NUM_LEDS 64
#define DATA_PIN 2

// Define the array of leds
CRGB leds[NUM_LEDS];

// TMP PIXEL-ART
byte strip[] = {0,0,0,183,62,67,183,62,67,183,62,67,0,0,0,0,0,0,0,0,0,0,0,0,246,211,45,0,0,0,183,62,67,183,62,67,183,62,67,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,183,62,67,183,62,67,183,62,67,183,62,67,0,0,0,0,0,0,0,0,0,0,0,0,246,245,244,246,245,244,246,245,244,246,245,244,0,0,0,0,0,0,0,0,0,0,0,0,226,142,142,0,0,0,226,142,142,0,0,0,226,142,142,0,0,0,0,0,0,0,0,0,226,142,142,226,142,142,226,142,142,226,142,142,226,142,142,0,0,0,0,0,0,0,0,0,246,245,244,246,245,244,246,245,244,246,245,244,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,246,245,244,246,245,244,0,0,0,0,0,0,0,0,0};


// WIFI SETUP
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//Your Domain name with URL path or IP address with path
String serverName = "https://www.google.it";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
unsigned long timerDelay = 60000;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed
  Serial.println("FastLED lib initialized!");

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  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() {
  // Draw the image
  for(int i=0, p=0;i<NUM_LEDS*3;i+=3,p++){
    byte r = strip[i];
    byte g = strip[i+1];
    byte b = strip[i+2];
  	leds[p] = CRGB(r, g, b);
  }
  FastLED.show(); // This sends the updated pixel color to the hardware.
  
  delay(10); // this speeds up the simulation

  //Send an HTTP POST request every 1 minute
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // If you need Node-RED/server authentication, insert user and password below
      //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}