#define TINY_GSM_MODEM_ESP8266

#define SerialMon Serial
#define SerialAT Serial1

// Increase RX buffer to capture the entire response
// Chips without internal buffering (A6/A7, ESP8266, M590)
// need enough space in the buffer for the entire response
// else data will be lost (and the http library will fail).
#if !defined(TINY_GSM_RX_BUFFER)
#define TINY_GSM_RX_BUFFER 650
#endif

// See all AT commands, if wanted
//#define DUMP_AT_COMMANDS

// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon

// Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200

// Your WiFi connection credentials, if applicable
const char wifiSSID[]  = "Idodaniel";
const char wifiPass[] = "0526530657";

// Server details
const char server[] = "128.199.144.129";
const char resource0[] = "/external/api/update?token=C-pNT9PjlIPoFNlhk4cd_Bb__FLeGYES&V4=0";
const char resource1[] = "/external/api/update?token=C-pNT9PjlIPoFNlhk4cd_Bb__FLeGYES&V4=1";

#include <TinyGsmClient.h>

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

TinyGsmClient client(modem);
const int  port = 80;

void setupModem() {
  SerialMon.begin(115200);
  delay(1000);
  //  SerialMon.println("Wait...");
  //  TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  //  delay(2000);
  SerialAT.begin(115200);
  SerialMon.println("Initializing modem...");
  modem.restart();
  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);
   // Wifi connection parameters must be set before waiting for the network
  SerialMon.print(F("Setting SSID/password..."));
  if (!modem.networkConnect(wifiSSID, wifiPass)) {
    SerialMon.println(" fail");
    delay(5000);
    return;
  }
  SerialMon.println(" success");
  SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork()) {
    SerialMon.println(" fail");
    delay(5000);
    return;
  }
  SerialMon.println(" success");

  if (modem.isNetworkConnected()) {
    SerialMon.println("Network connected");
  }
}

void dogHungry(bool hungry) {

  SerialMon.print("Connecting to ");
  SerialMon.println(server);
  if (!client.connect(server, port)) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" success");

  // Make a HTTP GET request:
  SerialMon.println("Performing HTTP GET request...");
  if(hungry){
     client.print(String("GET ") + resource1 + " HTTP/1.1\r\n");
  }else{
      client.print(String("GET ") + resource0 + " HTTP/1.1\r\n");
  }

  client.print(String("Host: ") + server + "\r\n");
  client.print("Connection: close\r\n\r\n");
  client.println();
  String output;
  uint32_t timeout = millis();
  while (client.connected() && millis() - timeout < 10000L) {
    // Print available data
    while (client.available()) {
      char c = client.read();
      output.concat(c);
      SerialMon.print(c);
      timeout = millis();
    }
  }
  SerialMon.println("#############################");
  SerialMon.println(output);
  SerialMon.println();

}
