Basic Authentication, Webserver, and GSM not working when merged

Hello Guys,

This is John a newbie in Arduino programming. Seeking your advice and help with my project work. I am working to create a web server with Basic Authentication and GSM to send messages when the button is press in the webserver. I also implemented automation in this project, wherein if it reached the set temperature and battery level, it would send a message to the recipient. Upon compiling and uploading the codes, i did not encounter any errors when i uploaded it to the Atmega324. However, the webserver is not showing up, and even the other functions are not working, like basic authentication. The web server cannot be reached. Here's the code for reference.


//For ethernet
#include <SPI.h>
#include <Ethernet.h>
//For relay
#include <Wire.h>
#include <Relay.h>
//For Temp
#include <OneWire.h>
#include <DallasTemperature.h>
//For GSM
#include <SoftwareSerial.h>

//For GSM---------------------------------------------------------------------------------
SoftwareSerial mySerial(2, 3);
char Rx_data[150];
unsigned char Rx_index = 0; 
int i = 0;
char msg[160];
int sig;

#define DEBUG true

//-----------------------------------------------------------------------------------------
int passFlag = 0;
int passFlag1 = 0;
int passFlag2 = 0;
int passFlag3 = 0;
//Millis timeout---------------------------------------------------------------------------

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

//Batt pin---------------------------------------------------------------------------------
#define SIGNAL_PIN A0   //battery level monitoring using 4.7K & 2.2K ohm resistor
//-----------------------------------------------------------------------------------------


//Temp Pin---------------------------------------------------------------------------------
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp;
//-----------------------------------------------------------------------------------------


//Batt computation-------------------------------------------------------------------------
float adc_voltage = 0.0;
float in_voltage = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float ref_voltage = 5.0;
int adc_value = 0;
//-----------------------------------------------------------------------------------------


//Ethernet---------------------------------------------------------------------------------
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,18, 12);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
//-----------------------------------------------------------------------------------------

// Relay state and pin---------------------------------------------------------------------
String relay1State1 = "ON";
String relay2State2 = "ON";
const int  TempCon= 6;
const int ChgRlyCon = 7;

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


// Client variables------------------------------------------------------------------------ 
char linebuf[80];
int charcount=0;
boolean authentificated=false;
//-----------------------------------------------------------------------------------------

void dashboardPage(EthernetClient &client) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connnection: close");
  client.println();
  client.println("<!DOCTYPE HTML><html>");
  client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>");
  client.println("<center><a href=\"/\">Refresh</a>");
  client.println("<h3>SERVER TEMPERATURE</a></h3>");
  client.println("<br>");
  client.println(temp);
  client.println("</br>");
  // Generates buttons to control the relay 
  client.println("<a href=\"/ACON\"><button>ON</button></a>&nbsp;&nbsp");
  client.println("<a href=\"/ACOFF\"><button>OFF</button></a>");
  client.println("<h3>SERVER BATTERY LEVEL</h3>");
  client.println("<br>");
  client.println(in_voltage);
  client.println("</br>");
  // Generates buttons to control the relay       
  client.println("<a href=\"/DCON\"><button>ON</button></a>&nbsp;&nbsp");
  client.println("<a href=\"/DCOFF\"><button>OFF</button></a></center>"); 

  client.println("</body></html>"); 
}

void SendAuthentificationpage(EthernetClient &auth)
{
  auth.println("HTTP/1.1 401 Authorization Required");
  auth.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
  auth.println("Content-Type: text/html");
  auth.println("Connnection: close");
  auth.println();
  auth.println("<!DOCTYPE HTML>");
  auth.println("<HTML>  <HEAD>   <TITLE>Error</TITLE>");
  auth.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}

void setup()
{
  // For GSM
  Serial.begin(9600);
  mySerial.begin(19200);


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

  //For ethernet
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //---------------------------------------------------------------------------------------

  //For relay pins
  pinMode(ChgRlyCon, OUTPUT);
  digitalWrite(ChgRlyCon, HIGH);
  pinMode(TempCon, OUTPUT);
  digitalWrite(TempCon, HIGH);
  //---------------------------------------------------------------------------------------


}


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

void loop() {
  
//----GSM------------------------------------------------------------------------------------  
   if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
  // listen for incoming clients
//------------------------------------------------------------------------------------------  
  
//----TEMP SENSOR---------------------------------------------------------------------------  
  sensors.requestTemperatures(); // Send the command to get temperatures
  temp = sensors.getTempCByIndex(0);
  sensors.requestTemperatures(); // Send the command to get temperatures
  temp = sensors.getTempCByIndex(0);
//------------------------------------------------------------------------------------------  
  
  
//----BATT SENSOR---------------------------------------------------------------------------
// Why "byIndex"? 
  // You can have more than one IC on the same bus. 
  // 0 refers to the first IC on the wire
  // Read the Analog Input
  adc_value = analogRead(SIGNAL_PIN);

  // Determine voltage at ADC input
  adc_voltage  = (adc_value * ref_voltage) / 1024.0;

  // Calculate voltage at divider input
  in_voltage = adc_voltage / (R2 / (R1 + R2)) ;
//------------------------------------------------------------------------------------------


//----BASIC AUTHENTICATION------------------------------------------------------------------
 // listen for incoming clients
  EthernetClient auth = server.available();
  EthernetClient client = server.available();
  if (auth) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (auth.connected()) {
      if (auth.available()) {
        char a = auth.read();
        linebuf[charcount]=a;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(a);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (a == '\n' && currentLineIsBlank) {
          if (authentificated)
            dashboardPage(client);
          else
            SendAuthentificationpage(auth);  
          break;
        }
        if (a == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"YWRtaW46ZGF5d2Fsa2VyMDAy")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        else if (a != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
  }
 //------------------------------------------------------------------------------------------
 
 
 //----BUTTONS IN WEB------------------------------------------------------------------------ 
  if (client) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          dashboardPage(client);
          break;
        }
        if (c == '\n') {
            
            if (strstr(linebuf,"GET /ACON") > 0){
            digitalWrite(TempCon, HIGH);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED ON BY ADMIN");

          }
          else if (strstr(linebuf,"GET /ACOFF") > 0){
            digitalWrite(TempCon, LOW);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED OFF BY ADMIN");

          }

          if (strstr(linebuf,"GET /DCON") > 0){
            digitalWrite(ChgRlyCon, HIGH);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED BY ADMIN");

          }

          if (strstr(linebuf,"GET /DCOFF") > 0){
            digitalWrite(ChgRlyCon, LOW);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED BY ADMIN");

          }
          currentLineIsBlank = true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;          
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
  }
//------------------------------------------------------------------------------------------ 


//----TEMP CONDITION------------------------------------------------------------------------ 
  if( temp >= 35){
    digitalWrite(TempCon, HIGH);
    if(passFlag == 0){
      init_GSM();
      send_msg("***********", "AIRCON HAS BEEN TURNED ON");
      passFlag = 1;
      passFlag1 = 0;
    }
  }
  if (temp <= 34){
    digitalWrite(TempCon, LOW);
    if(passFlag1 == 0){
      init_GSM();
      send_msg("***********", "AIRCON HAS BEEN TURNED OFF");
      passFlag1 = 1;
      passFlag = 0;
    } 
//------------------------------------------------------------------------------------------


//----BATT CONDITION------------------------------------------------------------------------
  //--------------------------- bms 0, batt lt 13.4v--------------
  if(in_voltage >= 15){
    digitalWrite(ChgRlyCon, LOW);
    if(passFlag2 == 0){
      init_GSM();
      send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED");
      passFlag2 = 1;
      passFlag3 = 0;
    }
  }
  //--------------------------- bms 0, batt is charged at 13.4V -------
  if(in_voltage <= 6){
    digitalWrite(ChgRlyCon, HIGH);
    if(passFlag3 == 0){
      init_GSM();
      send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED");
      passFlag3 = 1;
      passFlag2 = 0;
    }
  }
 }
//------------------------------------------------------------------------------------------
} 


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


//---SEND GSM STRING----

void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {
    '\0'  };
  char msg1[160] = {
    '\0'  };
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

void sendGSM(char *string){
  mySerial.write(string);
  delay(90);
}

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(200);
    readSerialString(Rx_data);
    Serial.write(Rx_data);

    stat = strstr(Rx_data, "OK");
    Serial.println("Success");
    delay(50000);
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}

void init_GSM(){


  sendData("AT\r\n",1000,DEBUG); // AT

  sendData("AT+CMGF=1\r\n",1000,DEBUG); //AT+CMGF=1
  sendData("AT+CMGD=1\r\n",1000,DEBUG); //AT+CMGD=1

  delay(1000);
  delay(1000);
  delay(1000);
}

void readSerialString (char *strArray) {

  if(!mySerial.available()) {
    return;
  }

  while(mySerial.available()) {
    strArray[i] = mySerial.read();
    i++;
  }
}

String sendData(String command, const int timeout, boolean debug)
{
  String response = "";

  mySerial.print(command); 

  long int time = millis();

  while( (time+timeout) > millis())
  {
    while(mySerial.available())
    {


      char c = mySerial.read();
      response+=c;
    }  
  }

  if(debug)
  {
    Serial.print(response);
  }

  return response;
}



Seeking for your help and advice how to make this codes work.

Thank you.

what board are you using? An Uno or Nano use an atmega328.

Either way, you are wasting a ton of ram with all your client.print() functions of strings. Put those in PROGMEM. You may be running out of memory.

Hello @blh64,

Im using atmega328.

Thanks for the advice.

That means using the F macro.
See post 4 for how to use it.

Hello @Grumpy_Mike and blh64,

The page is now working. It is showing up the moment i used F macro. Thanks. However the buttons in the webserver are not working. Can you help me identify the problem? I would really appreciate it as i am frustrated to make this work.

//For ethernet
#include <SPI.h>
#include <Ethernet.h>
//For relay
#include <Wire.h>
#include <Relay.h>
//For Temp
#include <OneWire.h>
#include <DallasTemperature.h>
//For GSM
#include <SoftwareSerial.h>

#include <EEPROM.h>


//For GSM---------------------------------------------------------------------------------
SoftwareSerial mySerial(2, 3);
char Rx_data[150];
unsigned char Rx_index = 0; 
int i = 0;
char msg[160];
int sig;

#define DEBUG true

//-----------------------------------------------------------------------------------------
int passFlag = 0;
int passFlag1 = 0;
int passFlag2 = 0;
int passFlag3 = 0;
//Millis timeout---------------------------------------------------------------------------

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

//Batt pin---------------------------------------------------------------------------------
#define SIGNAL_PIN A0   //battery level monitoring using 4.7K & 2.2K ohm resistor
//-----------------------------------------------------------------------------------------


//Temp Pin---------------------------------------------------------------------------------
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp;
//-----------------------------------------------------------------------------------------


//Batt computation-------------------------------------------------------------------------
float adc_voltage = 0.0;
float in_voltage = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float ref_voltage = 5.0;
int adc_value = 0;
//-----------------------------------------------------------------------------------------


//Ethernet---------------------------------------------------------------------------------
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,18, 12);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
//-----------------------------------------------------------------------------------------

// Relay state and pin---------------------------------------------------------------------
String relay1State1 = "ON";
String relay2State2 = "ON";
const int  TempCon= 6;
const int ChgRlyCon = 7;

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


// Client variables------------------------------------------------------------------------ 
char linebuf[80];
int charcount=0;
boolean authentificated=false;
//-----------------------------------------------------------------------------------------

void dashboardPage(EthernetClient &client) {
  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html"));
  client.println(F("Connnection: close"));
  client.println();
   client.println(F("<!DOCTYPE HTML><html>"));
  client.println(F("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>"));
  client.println(F("<center><a href=\"/\">Refresh</a>"));
  client.println(F("<h3>SERVER TEMPERATURE</a></h3>"));
  client.println(F("<br>"));
  client.println(temp);
  client.println(F("</br>"));
  // Generates buttons to control the relay 
  client.println(F("<a href=\"/ACON\"><button>ON</button></a>&nbsp;&nbsp"));
  client.println(F("<a href=\"/ACOFF\"><button>OFF</button></a>"));
  client.println(F("<h3>SERVER BATTERY LEVEL</h3>"));
  client.println(F("<br>"));
  client.println(in_voltage);
  client.println(F("</br>"));
  // Generates buttons to control the relay       
  client.println(F("<a href=\"/DCON\"><button>ON</button></a>&nbsp;&nbsp"));
  client.println(F("<a href=\"/DCOFF\"><button>OFF</button></a></center>")); 
  client.println(F("<style>body {background-image: url(\"https://geloautomationworks.files.wordpress.com/2023/08/bb1.jpg\");  background-size: 100vw 100vh; background-repeat: no-repeat;  text-align: center; font-family: \"Trebuchet MS\", Arial;}"));
  client.println(F("</body></html>")); 

  client.println("</body></html>"); 
}

void SendAuthentificationpage(EthernetClient &auth)
{
  auth.println(F("HTTP/1.1 401 Authorization Required"));
  auth.println(F("WWW-Authenticate: Basic realm=\"Secure Area\""));
  auth.println(F("Content-Type: text/html"));
  auth.println("Connnection: close");
  auth.println();
  auth.println(F("<!DOCTYPE HTML>"));
  auth.println(F("<HTML>  <HEAD>   <TITLE>Error</TITLE>"));
  auth.println(F(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>"));
}

void setup()
{
  // For GSM
  Serial.begin(9600);
  mySerial.begin(19200);


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

  //For ethernet
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //---------------------------------------------------------------------------------------

  //For relay pins
  pinMode(ChgRlyCon, OUTPUT);
  digitalWrite(ChgRlyCon, HIGH);
  pinMode(TempCon, OUTPUT);
  digitalWrite(TempCon, HIGH);
  //---------------------------------------------------------------------------------------


}


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

void loop() {
  
//----GSM------------------------------------------------------------------------------------  
   if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
  // listen for incoming clients
//------------------------------------------------------------------------------------------  
  
//----TEMP SENSOR---------------------------------------------------------------------------  
  sensors.requestTemperatures(); // Send the command to get temperatures
  temp = sensors.getTempCByIndex(0);
  sensors.requestTemperatures(); // Send the command to get temperatures
  temp = sensors.getTempCByIndex(0);
//------------------------------------------------------------------------------------------  
  
  
//----BATT SENSOR---------------------------------------------------------------------------
// Why "byIndex"? 
  // You can have more than one IC on the same bus. 
  // 0 refers to the first IC on the wire
  // Read the Analog Input
  adc_value = analogRead(SIGNAL_PIN);

  // Determine voltage at ADC input
  adc_voltage  = (adc_value * ref_voltage) / 1024.0;

  // Calculate voltage at divider input
  in_voltage = adc_voltage / (R2 / (R1 + R2)) ;
//------------------------------------------------------------------------------------------


//----BASIC AUTHENTICATION------------------------------------------------------------------
 // listen for incoming clients
  EthernetClient auth = server.available();
  EthernetClient client = server.available();
  if (auth) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (auth.connected()) {
      if (auth.available()) {
        char a = auth.read();
        linebuf[charcount]=a;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(a);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (a == '\n' && currentLineIsBlank) {
          if (authentificated)
            dashboardPage(client);
          else
            SendAuthentificationpage(auth);  
          break;
        }
        if (a == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"YWRtaW46ZGF5d2Fsa2VyMDAy")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        else if (a != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
  }
 //------------------------------------------------------------------------------------------
 
 
 //----BUTTONS IN WEB------------------------------------------------------------------------ 
  if (client) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          dashboardPage(client);
          break;
        }
        if (c == '\n') {
            
            if (strstr(linebuf,"GET /ACON") > 0){
            digitalWrite(TempCon, HIGH);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED ON BY ADMIN");

          }
          else if (strstr(linebuf,"GET /ACOFF") > 0){
            digitalWrite(TempCon, LOW);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED OFF BY ADMIN");

          }

          if (strstr(linebuf,"GET /DCON") > 0){
            digitalWrite(ChgRlyCon, HIGH);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED BY ADMIN");

          }

          if (strstr(linebuf,"GET /DCOFF") > 0){
            digitalWrite(ChgRlyCon, LOW);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED BY ADMIN");

          }
          currentLineIsBlank = true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;          
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
  }
//------------------------------------------------------------------------------------------ 


//----TEMP CONDITION------------------------------------------------------------------------ 
  if( temp >= 35){
    digitalWrite(TempCon, HIGH);
    if(passFlag == 0){
      init_GSM();
      send_msg("***********", "AIRCON HAS BEEN TURNED ON");
      passFlag = 1;
      passFlag1 = 0;
    }
  }
  if (temp <= 34){
    digitalWrite(TempCon, LOW);
    if(passFlag1 == 0){
      init_GSM();
      send_msg("***********", "AIRCON HAS BEEN TURNED OFF");
      passFlag1 = 1;
      passFlag = 0;
    } 
//------------------------------------------------------------------------------------------


//----BATT CONDITION------------------------------------------------------------------------
  //--------------------------- bms 0, batt lt 13.4v--------------
  if(in_voltage >= 15){
    digitalWrite(ChgRlyCon, LOW);
    if(passFlag2 == 0){
      init_GSM();
      send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED");
      passFlag2 = 1;
      passFlag3 = 0;
    }
  }
  //--------------------------- bms 0, batt is charged at 13.4V -------
  if(in_voltage <= 6){
    digitalWrite(ChgRlyCon, HIGH);
    if(passFlag3 == 0){
      init_GSM();
      send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED");
      passFlag3 = 1;
      passFlag2 = 0;
    }
  }
 }
//------------------------------------------------------------------------------------------
} 


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


//---SEND GSM STRING----

void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {
    '\0'  };
  char msg1[160] = {
    '\0'  };
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

void sendGSM(char *string){
  mySerial.write(string);
  delay(90);
}

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(200);
    readSerialString(Rx_data);
    Serial.write(Rx_data);

    stat = strstr(Rx_data, "OK");
    Serial.println("Success");
    delay(50000);
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}

void init_GSM(){


  sendData("AT\r\n",1000,DEBUG); // AT

  sendData("AT+CMGF=1\r\n",1000,DEBUG); //AT+CMGF=1
  sendData("AT+CMGD=1\r\n",1000,DEBUG); //AT+CMGD=1

  delay(1000);
  delay(1000);
  delay(1000);
}

void readSerialString (char *strArray) {

  if(!mySerial.available()) {
    return;
  }

  while(mySerial.available()) {
    strArray[i] = mySerial.read();
    i++;
  }
}

String sendData(String command, const int timeout, boolean debug)
{
  String response = "";

  mySerial.print(command); 

  long int time = millis();

  while( (time+timeout) > millis())
  {
    while(mySerial.available())
    {


      char c = mySerial.read();
      response+=c;
    }  
  }

  if(debug)
  {
    Serial.print(response);
  }

  return response;
}

Where did you get this code from? ChatGPT?

Do you understand it?

What ethernet module do you use? What gsm module do you use? How is the ethernet card connected? To your laptop or router? What IP adress do you set your computer at?

Not easy project for a "newbee"

Hello @Tordens

i am using W5100 Lan shield, SIM800L shield, the Ethernet is connected to the router directly.

OK! So the IP on the W5100 match your LAN IP in your router? Does the obsolete SIM800L shield realy work in your country?

Yeah. i can access the webserver and it replies to my ping request. The problem is the buttons in the page, there are not working as mentioned in my reply to @Grumpy_Mike and @blh64. And yes, SIM800L is working on our country.

Do you have a w5100 uno module with microSD card? They share the same SPI bus. In that case, you need to disable the microSD card or remove it. If an uninitialized SD card is left in the SD card socket of the shield, it can cause problems with code in the sketch that is accessing the Ethernet chip. This may cause symptoms such as the sketch running once or twice, then hanging up.

When you press a button, it generates something like this:

GET /ACON HTTP/1.1
Authorization: Basic YXNkZjo=
Host: <yourhosthere>

So you need to parse it correctly. It looks like you pasted two bits of code together:

  EthernetClient auth = server.available();
  EthernetClient client = server.available();

You only have 1 connection, not 2. I would suggest that your just print out to the Serial port whatever you receive to help you see what exactly is being received and then you can parse it correctly.

Hello @blh64,

Let me try it. i separate it as the EthernetClient function will do two things, first for the Basic authentication, and second is for the button actions. Thanks for your help.. ill try your suggestion. Thank you so much as always @blh64

Hello @blh64,

I tried implementing only one EthernetClient for both Basic Authentication and the web buttons, but unfortunately, it is not working. I guess based on the result from the Serial Monitor, the Etherclient client = server.available var c is only applying in the basic authentication loop codes and not in the buttons. Please see data received from Serial monitor and the code used.

Readings from Serial Monitor
"server is at 192.168.18.12
GET / HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Accept-Encoding: gzip, deflate

GET / HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Cache-Control: max-age=0
Authorization: Basic YWRtyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Accept-Encoding: gzip, deflate

GET /DCON HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Authorization: Basic YWRtyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Referer: http://192.168.18.12/
Accept-Encoding: gzip, deflate

GET /DCON HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Cache-Control: max-age=0
Authorization: Basic YWRtyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Referer: http://192.168.18.12/
Accept-Encoding: gzip, deflate

GET /ACON HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Authorization: Basic YWRtyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Referer: http://192.168.18.12/DCON
Accept-Encoding: gzip, deflate

Here's the code that has been used:

type or paste code here
//For ethernet
#include <SPI.h>
#include <Ethernet.h>
//For relay
#include <Wire.h>
#include <Relay.h>
//For Temp
#include <OneWire.h>
#include <DallasTemperature.h>
//For GSM
#include <SoftwareSerial.h>

#include <EEPROM.h>


//For GSM---------------------------------------------------------------------------------
SoftwareSerial mySerial(2, 3);
char Rx_data[150];
unsigned char Rx_index = 0; 
int i = 0;
char msg[160];
int sig;

#define DEBUG true

//-----------------------------------------------------------------------------------------
int passFlag = 0;
int passFlag1 = 0;
int passFlag2 = 0;
int passFlag3 = 0;
//Millis timeout---------------------------------------------------------------------------

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

//Batt pin---------------------------------------------------------------------------------
#define SIGNAL_PIN A0   //battery level monitoring using 4.7K & 2.2K ohm resistor
//-----------------------------------------------------------------------------------------


//Temp Pin---------------------------------------------------------------------------------
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp;
//-----------------------------------------------------------------------------------------


//Batt computation-------------------------------------------------------------------------
float adc_voltage = 0.0;
float in_voltage = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float ref_voltage = 5.0;
int adc_value = 0;
//-----------------------------------------------------------------------------------------


//Ethernet---------------------------------------------------------------------------------
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,18, 12);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
//-----------------------------------------------------------------------------------------

// Relay state and pin---------------------------------------------------------------------
String relay1State1 = "ON";
String relay2State2 = "ON";
const int  TempCon= 6;
const int ChgRlyCon = 7;

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


// Client variables------------------------------------------------------------------------ 
char linebuf[80];
int charcount=0;
boolean authentificated=false;
//-----------------------------------------------------------------------------------------

void dashboardPage(EthernetClient &client) {
  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html"));
  client.println(F("Connnection: close"));
  client.println();
   client.println(F("<!DOCTYPE HTML><html>"));
  client.println(F("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>"));
  client.println(F("<center><a href=\"/\">Refresh</a>"));
  client.println(F("<h3>SERVER TEMPERATURE</a></h3>"));
  client.println(F("<br>"));
  client.println(temp);
  client.println(F("</br>"));
  // Generates buttons to control the relay 
  client.println(F("<a href=\"/ACON\"><button>ON</button></a>&nbsp;&nbsp"));
  client.println(F("<a href=\"/ACOFF\"><button>OFF</button></a>"));
  client.println(F("<h3>SERVER BATTERY LEVEL</h3>"));
  client.println(F("<br>"));
  client.println(in_voltage);
  client.println(F("</br>"));
  // Generates buttons to control the relay       
  client.println(F("<a href=\"/DCON\"><button>ON</button></a>&nbsp;&nbsp"));
  client.println(F("<a href=\"/DCOFF\"><button>OFF</button></a></center>")); 
  client.println(F("<style>body {background-image: url(\"https://geloautomationworks.files.wordpress.com/2023/08/bb1.jpg\");  background-size: 100vw 100vh; background-repeat: no-repeat;  text-align: center; font-family: \"Trebuchet MS\", Arial;}"));
  client.println(F("</body></html>")); 

  client.println("</body></html>"); 
}

void SendAuthentificationpage(EthernetClient &auth)
{
  auth.println(F("HTTP/1.1 401 Authorization Required"));
  auth.println(F("WWW-Authenticate: Basic realm=\"Secure Area\""));
  auth.println(F("Content-Type: text/html"));
  auth.println("Connnection: close");
  auth.println();
  auth.println(F("<!DOCTYPE HTML>"));
  auth.println(F("<HTML>  <HEAD>   <TITLE>Error</TITLE>"));
  auth.println(F(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>"));
}

void setup()
{
  // For GSM
  Serial.begin(9600);
  mySerial.begin(19200);


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

  //For ethernet
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //---------------------------------------------------------------------------------------

  //For relay pins
  pinMode(ChgRlyCon, OUTPUT);
  digitalWrite(ChgRlyCon, HIGH);
  pinMode(TempCon, OUTPUT);
  digitalWrite(TempCon, HIGH);
  //---------------------------------------------------------------------------------------


}


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

void loop() {
  
//----GSM------------------------------------------------------------------------------------  
   if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
  // listen for incoming clients
//------------------------------------------------------------------------------------------  
  
//----TEMP SENSOR---------------------------------------------------------------------------  
  sensors.requestTemperatures(); // Send the command to get temperatures
  temp = sensors.getTempCByIndex(0);
  sensors.requestTemperatures(); // Send the command to get temperatures
  temp = sensors.getTempCByIndex(0);
//------------------------------------------------------------------------------------------  
  
  
//----BATT SENSOR---------------------------------------------------------------------------
// Why "byIndex"? 
  // You can have more than one IC on the same bus. 
  // 0 refers to the first IC on the wire
  // Read the Analog Input
  adc_value = analogRead(SIGNAL_PIN);

  // Determine voltage at ADC input
  adc_voltage  = (adc_value * ref_voltage) / 1024.0;

  // Calculate voltage at divider input
  in_voltage = adc_voltage / (R2 / (R1 + R2)) ;
//------------------------------------------------------------------------------------------


//----BASIC AUTHENTICATION------------------------------------------------------------------
 // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          if (authentificated)
            dashboardPage(client);
          else
            SendAuthentificationpage(client);  
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"YWRtaW46ZGF5d2Fsa2VyMDAy")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        else if ( c  != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
  }
 //------------------------------------------------------------------------------------------
 
 
 //----BUTTONS IN WEB------------------------------------------------------------------------ 
  if (client) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          dashboardPage(client);
          break;
        }
        if (c == '\n') {
            
            if (strstr(linebuf,"GET /ACON") > 0){
            digitalWrite(TempCon, HIGH);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED ON BY ADMIN");

          }
          else if (strstr(linebuf,"GET /ACOFF") > 0){
            digitalWrite(TempCon, LOW);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED OFF BY ADMIN");

          }

          if (strstr(linebuf,"GET /DCON") > 0){
            digitalWrite(ChgRlyCon, HIGH);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED BY ADMIN");

          }

          if (strstr(linebuf,"GET /DCOFF") > 0){
            digitalWrite(ChgRlyCon, LOW);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED BY ADMIN");

          }
          currentLineIsBlank = true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;          
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
  }
//------------------------------------------------------------------------------------------ 


//----TEMP CONDITION------------------------------------------------------------------------ 
  if( temp >= 35){
    digitalWrite(TempCon, HIGH);
    if(passFlag == 0){
      init_GSM();
      send_msg("***********", "AIRCON HAS BEEN TURNED ON");
      passFlag = 1;
      passFlag1 = 0;
    }
  }
  if (temp <= 34){
    digitalWrite(TempCon, LOW);
    if(passFlag1 == 0){
      init_GSM();
      send_msg("***********", "AIRCON HAS BEEN TURNED OFF");
      passFlag1 = 1;
      passFlag = 0;
    } 
//------------------------------------------------------------------------------------------


//----BATT CONDITION------------------------------------------------------------------------
  //--------------------------- bms 0, batt lt 13.4v--------------
  if(in_voltage >= 15){
    digitalWrite(ChgRlyCon, LOW);
    if(passFlag2 == 0){
      init_GSM();
      send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED");
      passFlag2 = 1;
      passFlag3 = 0;
    }
  }
  //--------------------------- bms 0, batt is charged at 13.4V -------
  if(in_voltage <= 6){
    digitalWrite(ChgRlyCon, HIGH);
    if(passFlag3 == 0){
      init_GSM();
      send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED");
      passFlag3 = 1;
      passFlag2 = 0;
    }
  }
 }
//------------------------------------------------------------------------------------------
} 


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


//---SEND GSM STRING----

void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {
    '\0'  };
  char msg1[160] = {
    '\0'  };
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

void sendGSM(char *string){
  mySerial.write(string);
  delay(90);
}

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(200);
    readSerialString(Rx_data);
    Serial.write(Rx_data);

    stat = strstr(Rx_data, "OK");
    Serial.println("Success");
    delay(50000);
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}

void init_GSM(){


  sendData("AT\r\n",1000,DEBUG); // AT

  sendData("AT+CMGF=1\r\n",1000,DEBUG); //AT+CMGF=1
  sendData("AT+CMGD=1\r\n",1000,DEBUG); //AT+CMGD=1

  delay(1000);
  delay(1000);
  delay(1000);
}

void readSerialString (char *strArray) {

  if(!mySerial.available()) {
    return;
  }

  while(mySerial.available()) {
    strArray[i] = mySerial.read();
    i++;
  }
}

String sendData(String command, const int timeout, boolean debug)
{
  String response = "";

  mySerial.print(command); 

  long int time = millis();

  while( (time+timeout) > millis())
  {
    while(mySerial.available())
    {


      char c = mySerial.read();
      response+=c;
    }  
  }

  if(debug)
  {
    Serial.print(response);
  }

  return response;
}

Any advice how to make the webserver buttons work?

Thank you.

Do not try to read twice. When a request comes in, it ALL comes in the same request - the GET, the authorization, along with all the other headers

GET /DCON HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Authorization: Basic YWRtyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Referer: http://192.168.18.12/
Accept-Encoding: gzip, deflate

This entire thing will go into your linebuf (which might be more than the 80 chars you currently have allocated). Once all of that is read in, use that buffer to find your authorization. If present and correct, continue on and then check to see if it is a button (GET /DCON, etc) or whatever and act accordingly. If it is just the home page, serve up your dashboard, if bad or missing authorization, server up your error page.

Thanks for your help, @blh64 . Do you have any guides on how to properly do it? Im a bit confused about how this code will work, as i need the GET function for both the authentication and buttons. I would greatly appreciate it if you can help me pinpoint the issue and provide me with a sample how to make this code works. I manage to make the buttons work by moving the web button code inside the basic authentication, but it will return to its default state after pressing the button.

Here's the output from Serial Monitor:
server is at 192.168.1.12
GET /ACON HTTP/1.1
Host: 192.168.1.12
Connection: keep-alive
Authorization: Basic YWRtaW46RGF5d2Fsa2VyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Referer: http://192.168.1.12/
Accept-Encoding: gzip, deflate

GET /DCOFF HTTP/1.1
Host: 192.168.1.12
Connection: keep-alive
Authorization: Basic YWRtaW46RGF5d2Fsa2VyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Referer: http://192.168.1.12/ACON
Accept-Encoding: gzip, deflate

This is the code for the Basic Authentication and Web buttons:

//----BASIC AUTHENTICATION------------------------------------------------------------------
 // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          if (authentificated)
            dashboardPage(client);
          else
            SendAuthentificationpage(client);  
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"YWRtaW46RGF5d2Fsa2VyQDAwMg==")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        }

//----BUTTONS IN WEB-------------------------------------------------------------------------        
        else if (strstr(linebuf,"GET /ACON") > 0){
            digitalWrite(TempCon, HIGH);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED ON BY ADMIN");

          }
          else if (strstr(linebuf,"GET /ACOFF") > 0){
            digitalWrite(TempCon, LOW);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED OFF BY ADMIN");

          }

          else if (strstr(linebuf,"GET /DCON") > 0){
            digitalWrite(ChgRlyCon, HIGH);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED BY ADMIN");

          }

          else if (strstr(linebuf,"GET /DCOFF") > 0){
            digitalWrite(ChgRlyCon, LOW);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED BY ADMIN");

          }
//------------------------------------------------------------------------------------------
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
        

    }
  }
 }
//------------------------------------------------------------------------------------------

Please help me.

just put it all inside the block where you have received the entire header. First, test to make you the authentication is present, then test for any of your buttons, then default to either the dashboard or your authentication page

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    memset(linebuf, 0, sizeof(linebuf));
    charcount = 0;
    authentificated = false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    // an HTTP request ends with a blank line
    bool currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount] = c;
        if (charcount < sizeof(linebuf) - 1) charcount++;
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the HTTP request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // decide what to do
          if (strstr(linebuf, "Authorization: Basic") > 0 && strstr(linebuf, "YWRtaW46RGF5d2Fsa2VyQDAwMg==") > 0)
            authentificated = true;

          if (strstr(linebuf, "GET /ACON") > 0 && authenticated) {
            digitalWrite(TempCon, HIGH);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED ON BY ADMIN");
          }

          else if (strstr(linebuf, "GET /ACOFF") > 0 && authenticated) {
            digitalWrite(TempCon, LOW);
            init_GSM();
            send_msg("***********", "AC HAS BEEN TURNED OFF BY ADMIN");
          }

          else if (strstr(linebuf, "GET /DCON") > 0 && authenticated) {
            digitalWrite(ChgRlyCon, HIGH);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED BY ADMIN");
          }

          else if (strstr(linebuf, "GET /DCOFF") > 0 && authenticated) {
            digitalWrite(ChgRlyCon, LOW);
            init_GSM();
            send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED BY ADMIN");
          }

          else if (authenticated) {
            dashboardPage(client);
          }
          else {
            SendAuthentificationpage(client);
          }

          break;

        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

Hello @blh64,

Thanks for your help. however, it wont redirect to the dashboard page when i enter the username and password. it stays in the login page eventhough i entered the correct username and password.

Here's the output in the Serial Monitor

server is at 192.168.18.12
GET / HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Cache-Control: max-age=0
Authorization: Basic YWRtaW46RGF5d2Fsa2VyQDAwMg==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Accept-Encoding: gzip, deflate

client disconnected
GET / HTTP/1.1
Host: 192.168.18.12
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8
Sec-GPC: 1
Accept-Language: en-GB,en
Accept-Encoding: gzip, deflate

client disconnected

After putting the username and password, the client has been disconnected instead of redirecting it to the Dashboard page.

Anyone can help me?

I think it is time you attempted to help yourself. What have you tried to fix this issue? This forum is for people trying to learn. I have given you much help and outlined how the headers arrive and need to be parsed. I'm sure you can figure out the logic you want.

Hello @blh64,

I managed to make it work. Thank you so much for your big help. Appreciate it a lot.