//https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>


const char *ssid = "TP-Link_688A";
const char *password = "debarbamiguel";
const int http_port = 8080;

int httpCode;
String payload;

WiFiClient client;
#define pushAlunoUM    5 //pushbuttomAlunoUM - D1
#define pushAlunoDOIS    0 //pushbuttomAlunoDOIS - D3



void setup() {

  pinMode(pushAlunoUM,INPUT);
  pinMode(pushAlunoDOIS,INPUT);
  delay(1000);
  Serial.begin(115200);
  WiFi.mode(WIFI_OFF);delay(1000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);Serial.println(".");
  Serial.print("Conectando...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);Serial.print(".");}
    Serial.println ("");
    Serial.print ("Conectado a ");Serial.println (ssid);
    Serial.print ("Endereço IP: ");Serial.println (WiFi.localIP ());
    Serial.print("MAC: ");    Serial.println(WiFi.macAddress());    
}
void envia(String postData){
    HTTPClient http;

    // Lista de servidores de destino
    String servidores[2] = {
        "http://modulo4.migueldebarba.com.br/grupoa/miguel/insertleitura.php",
        "http://modulo4.migueldebarba.com.br/grupoa/otavio/insertleitura.php"
    };

    for (int i = 0; i < 2; i++) {
        http.begin(client, servidores[i]);
        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        int httpCode = http.POST(postData);
        String payload = http.getString();

        Serial.println("Servidor: " + servidores[i]);
        Serial.println("httpCode: " + String(httpCode));
        Serial.println("payload: " + payload);

        http.end();
    }
}


  
void loop() {
    if (digitalRead(pushAlunoUM)==HIGH){
        String mac_idmac=WiFi.macAddress();    
        int sensor_idsensor=1;
        float valor=analogRead(A0);
        String from="ESP";
        String nome="Aula dia 28/08";
        String postData = 
        "mac_idmac="+String(mac_idmac)
        +"&valor="+String(valor)
        +"&nome="+String(nome)
        +"&from="+String(from);
        envia(postData); 
    }
}