//==================================================================================//
// CLIENT                                                                           //
// Communication Between 2 ESPs8266 Peer-to-Peer - No Router                        //
// Adapted by: Engineer Jemerson Marques, On: 21.09.2019 - FVM Learning website     //
// Available at: https://www.fvml.com.br and on Youtube channel                     //
// https://www.youtube.com/c/FVMLearning - I hope you have fun - Good luck          //
//----------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------
// Libraries Needed For This 
//------------------------------------------------------------------------------------
#include <SPI.h>
//#include <ESP8266WiFi.h>                             // The Basic Function Of The ESP NODEMCU
#include <WiFi.h>

//------------------------------------------------------------------------------------
// Defining I/O Pins
//------------------------------------------------------------------------------------
#define       LedBoard   2                           // WIFI Module LED
#define       BUTTON     4                           // NodeMCU Button

//------------------------------------------------------------------------------------
// WIFI Authentication Variables
//------------------------------------------------------------------------------------
//char ssid[] = "FVML";                     // SSID of your ESP Server
//char pass[] = "fvml1234";                 // password of your ESP SEVER
 
char ssid[] = "Wokwi-GUEST";                // SSID of your ESP Server
char pass[] = "";                   // password of your ESP SEVER

//------------------------------------------------------------------------------------
// WIFI Module Mode & IP
//------------------------------------------------------------------------------------
IPAddress server(192,168,10,40);                      // the fix IP address of the server
WiFiClient client;

//====================================================================================
void setup() {
  pinMode(LedBoard, OUTPUT);                           // Initiate the Onboard Led Output
  pinMode(BUTTON, INPUT_PULLUP);                       // Initiate the ESP Pin: INPUT_PULLUP - Its mean that you no need put a resistor
  digitalWrite(LedBoard, HIGH);                        // Initiate the Onboard Led Off
 
  Serial.begin(115200);                                // only for debug
  Serial.println("");
  Serial.print("Awaiting connection: ");
  WiFi.begin(ssid, pass);                              // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  digitalWrite(LedBoard, LOW);
  delay(250);
  digitalWrite(LedBoard, HIGH);
  delay(250);
  }
  digitalWrite(LedBoard, HIGH);
  
//------------------------------------------------------------------------------------
// Network parameters - Only for debug
//------------------------------------------------------------------------------------
  Serial.println("");
  Serial.println("ESP Client Connected - FVML");
  Serial.print("IP: ");       Serial.println(WiFi.softAPIP());
  Serial.print("SSID: ");     Serial.println(WiFi.SSID());
  Serial.print("Signal: ");   Serial.println(WiFi.RSSI());
}
//====================================================================================
void loop() {
 
  ContinuousConnection();
}
//====================================================================================

 void ContinuousConnection(){
  client.connect(server, 80);                          // Connection to the server
  ReadButton();                                        // Read Button from Transmitter
 }
//====================================================================================

void ReadButton() {
  int reading = digitalRead(BUTTON);                   // Read the Button State
  if (reading == LOW) {                                // If the button pressed
    client.print("I am Transmitter\r");                // Send messege "I am Transmitter" To Server
    delay(200);
   }else{
   ClientContinue(); 
  } 
}

//====================================================================================
void ClientContinue(){
  client.println("Transmmiter");                      // sends the message to the server
  String answer = client.readStringUntil('\r');       // receives the answer from the sever
  client.flush();
  
  if (answer == "I am Receiver") {                    // compares if the response of the receiver is equal to 'I am Receiver'
    digitalWrite(LedBoard, !digitalRead(LedBoard));   // if it changes the status of the LED
    Serial.println("Data Received: " + answer);
    delay(200);                                       // client will trigger the communication 200 milliseconds
  }
}
//============================================== www.fvml.com.br =============================================================