//==================================================================================//
// SERVER                                                                           //
// 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 Project
//------------------------------------------------------------------------------------
#include <SPI.h>
//#include <ESP8266WiFi.h>                     // The Basic Function Of The ESP NOD MCU
#include <WiFi.h>

//------------------------------------------------------------------------------------
// WIFI Module Config
//------------------------------------------------------------------------------------
//char ssid[] = "FVML";                        // SSID of your ESP Server
//char pass[] = "fvml1234";                    // password of your ESP Server

char ssid[] = "Wokwi-GUEST";                // SSID of your ESP Server
char pass[] = "";                   // password of your ESP SEVER


WiFiServer server(80);

  IPAddress ip(192, 168, 10, 40);            // IP address of the server
 // IPAddress ip(192, 168, 4, 1);            // IP address of the server
  
  IPAddress gateway(192, 168, 10, 1);        // gateway of the server
  IPAddress subnet(255, 255, 255, 0);        // subnet mask of the server

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

//====================================================================================
void setup() {
  Serial.begin(115200);                      // Only for debug
  
  WiFi.mode(WIFI_AP_STA);                    // Need both to serve the webpage and take commands via tcp

  WiFi.softAPConfig(ip, gateway, subnet);
  WiFi.softAP(ssid, pass);                   // Access point password and identification
  delay(500);
  Serial.print("AP IP address: ");
  Serial.println(ip);
  server.begin();                            // Starts the server

//------------------------------------------------------------------------------------
// Serial Network parameters - Only for debug
//------------------------------------------------------------------------------------  
  Serial.println("ESP Server Initialized - FVML");
  Serial.print("IP: ");       Serial.println(WiFi.softAPIP());
  Serial.print("SSID: ");     Serial.println(WiFi.SSID());
  Serial.print("Signal: ");   Serial.println(WiFi.RSSI());

  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
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  String request = client.readStringUntil('\r');
  client.flush();

  if (request == "I am Transmitter") {
    digitalWrite(LedBoard, !digitalRead(LedBoard));
    Serial.print("Data Received: "); Serial.println(request);
    delay(200);
  }

  int reading = digitalRead(BUTTON);
  if (reading == LOW) {
    client.print("I am Receiver\r");
    delay(200);
  }
  client.println("Receiver\r");      // sends the answer to the client
  delay(100);
}
//============================================== www.fvml.com.br =============================================================