#include <ESP32Servo.h>
#include "Arduino.h"
#include "WiFi.h"
#include <HTTPClient.h>

HTTPClient client;

char nomeWifi[] = "Wokwi-GUEST";
char senhaWifi[] = "";
char serverAddress[] = "https://api.tago.io/data";
char contentHeader[] = "application/json";
char tokenHeader[] = "0f31deac-da9b-4e2e-9394-520c42f52fac";

bool entrar;
bool sair;

int statusPorta;

String comando;
  
int Disparo=18;
int Eco=2;
long Duracao; 
long Distancia; 

int Disparo1=5;
int Eco1=4;
long Duracao1; 
long Distancia1;

Servo servo_6;
Servo servo_7;
//---------------------------------------------------------

void setup() {                
  Serial.begin (9600);  
  pinMode(Eco, INPUT);     
  pinMode(Disparo, OUTPUT);
  
  pinMode(Eco1, INPUT);     
  pinMode(Disparo1, OUTPUT);
  
  servo_6.attach(21);
  
  init_wifi();
} 

//---------------------------------------------------------
bool servoOn = false;

void loop() {
  Serial.print("\n\n\n\n\n\n\n\n");
  if (statusPorta == 1){
    Serial.println("Porta aberta");
  }else if(statusPorta == 0){
  	Serial.println("Porta fechada");
  }
  send_status();

//---------------------------------------------------------
  
  digitalWrite(Disparo,LOW);
  delayMicroseconds(2);
  digitalWrite(Disparo,HIGH);
  delayMicroseconds(10);
  digitalWrite(Disparo,LOW);
  Duracao=pulseIn(Eco,HIGH);
  Distancia=Duracao/58;
  delay(1000);

  digitalWrite(Disparo1,LOW);
  delayMicroseconds(2);
  digitalWrite(Disparo1,HIGH);
  delayMicroseconds(10);
  digitalWrite(Disparo1,LOW);
  Duracao1=pulseIn(Eco1,HIGH);
  Distancia1=Duracao1/58;  
  delay(1000);
  
//---------------------------------------------------------
   
    if (Distancia1 < 120 || Distancia < 120){
      servo_7.write(0);
      servo_6.write(0);
      servoOn = true; 
      statusPorta = servoOn;
      porta();
    }else if (Distancia1 > 120 && Distancia > 120){
      servo_7.write(90);
      servo_6.write(90);
      servoOn = false; 
      statusPorta = servoOn;
    }
  
  while(Serial.available() < 0){
  	comando = Serial.readString();
    if (comando == "A"){
    	servo_7.write(0);
    	servo_6.write(0);
    	servoOn = true; 
    	statusPorta = servoOn;
      	Serial.println("Abrindo...");
      	delay(3000);
      	
    }else if (comando == "F"){
    	servo_7.write(90);
    	servo_6.write(90);
    	servoOn = false; 
    	statusPorta = servoOn;
      	Serial.println("Fechando...");
      	delay(3000);
    }
  }
}
//---------------------------------------------------------

void init_wifi() {
  Serial.println("Conectando WiFi");
  Serial.print("Wifi: ");
  Serial.println(nomeWifi);
  WiFi.begin(nomeWifi, senhaWifi);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
  }
  Serial.println("WiFi Conectado");
  Serial.print("Meu IP eh: ");
  Serial.println(WiFi.localIP());
}

//---------------------------------------------------------

//---------------------------------------------------------

void send_status() {
  char anyData[30];
  char postData[300];
  char anyData1[30];
  char bAny[30];
  int statusCode = 0;

  strcpy(postData, "{\n\t\"variable\": \"statusPorta\",\n\t\"value\": ");
  dtostrf(statusPorta, 6, 2, anyData);
  strncat(postData, anyData, 100);
  strcpy(anyData1, ",\n\t\"unit\": \"C\"\n\t}\n");
  strncat(postData, anyData1, 100);
  Serial.println(postData);
  client.begin(serverAddress);
  client.addHeader("Content-Type", contentHeader);
  client.addHeader("Device-Token", tokenHeader);
  statusCode = client.POST(postData);
  delay(2000);

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.println("End of POST to TagoIO");
  Serial.println();
}

//---------------------------------------------------------

//---------------------------------------------------------
void porta(){
  if (Distancia1 < 89){
    sair = true;
    send_sair();
  }else if(Distancia < 89){
  	entrar = true;
    send_entrar();
  }
}

void send_sair() {
  char anyData[30];
  char postData[300];
  char anyData1[30];
  char bAny[30];
  int statusCode = 0;

  strcpy(postData, "{\n\t\"variable\": \"sair\",\n\t\"value\": ");
  dtostrf(sair, 6, 2, anyData);
  strncat(postData, anyData, 100);
  strcpy(anyData1, ",\n\t\"unit\": \"C\"\n\t}\n");
  strncat(postData, anyData1, 100);
  Serial.println(postData);
  client.begin(serverAddress);
  client.addHeader("Content-Type", contentHeader);
  client.addHeader("Device-Token", tokenHeader);
  statusCode = client.POST(postData);
  delay(2000);

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.println("End of POST to TagoIO");
  Serial.println();
}

void send_entrar() {
  char anyData[30];
  char postData[300];
  char anyData1[30];
  char bAny[30];
  int statusCode = 0;

  strcpy(postData, "{\n\t\"variable\": \"entrar\",\n\t\"value\": ");
  dtostrf(entrar, 6, 2, anyData);
  strncat(postData, anyData, 100);
  strcpy(anyData1, ",\n\t\"unit\": \"C\"\n\t}\n");
  strncat(postData, anyData1, 100);
  Serial.println(postData);
  client.begin(serverAddress);
  client.addHeader("Content-Type", contentHeader);
  client.addHeader("Device-Token", tokenHeader);
  statusCode = client.POST(postData);
  delay(2000);

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.println("End of POST to TagoIO");
  Serial.println();
}
$abcdeabcde151015202530354045505560fghijfghij