How can I Loop my HTTP Request using Ethernet Shield

Hello! I'm new to arduino and I am having a problem looping a httprequest function in my code
my code works, it fetches data from my database in phpmyadmin but it only runs and fetches data once from the database but stops fetching data the second time.

like If I retrieve a name from my database it will fetch the name in the 1st try
but if I go for a 2nd time it won't fetch anymore

can anyone help me thank you in advance.
I'm using Arduino Mega w/ Ethernet Shield

Here's my Code Sorry for Bad Coding :sweat_smile:

#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Ethernet.h>
#include <String.h>

Servo parceldrop;
Servo mmoneyslot1;
Servo mmoneyslot2;
Servo mmoneyslot3;
Servo mmoneyslot4;
Servo mmoneyslot5;

// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetClient client;

int    HTTP_PORT   = 80;
String HTTP_METHOD = "GET";
char   HOST_NAME[] = "********"; // change to your PC's IP address

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //three columns
LiquidCrystal_I2C lcd(0x27,16,2);
int cursorColumn = 0;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 10, 3, 2 };
Keypad mykeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String keypadstring; //trackingid
int RedpinLock = 12;
int GreenpinUnlock = 11;
int LCDRow = 0;

void setup() {
  Serial.begin(9600);
  pinMode(RedpinLock, OUTPUT);
  pinMode(GreenpinUnlock, OUTPUT);
  parceldrop.attach(A3);
  mmoneyslot1.attach(A4);
  mmoneyslot2.attach(A5); 
  mmoneyslot3.attach(A7);
  mmoneyslot4.attach(A8); 
  mmoneyslot5.attach(A9);    
  lcd.init();
  lcd.backlight();
  lcd.setCursor(LCDRow, 1);
  keypadstring.reserve(5);
  options();
}

void loop() {
  digitalWrite(RedpinLock,HIGH);
  digitalWrite(GreenpinUnlock,LOW);
  char key = mykeypad.getKey();
  if (key){
    Serial.print(key);// print pressed button in serial
    lcd.print(key);// print pressed button in lcd
    lcd.setCursor(++LCDRow, 1);//move to nextline for new characters
    if(key == '*') {
      keypadstring = ""; // reset the pressed button
      delay(1000);                  
      lcd.clear();
      options();
    }
    else if(key == '#') {
      if (keypadstring == ""){
        lcd.clear();
        lcd.setCursor(0,0);
        Serial.println("Try Again!");
        lcd.print("Try Again!");
        lcd.setCursor(0,1);
        Serial.println("InvalidID");
        lcd.print("Invalid ID");
        delay(2000);
        lcd.clear();
        options();
      }
      else{
        lcd.setCursor(0,0);
        Serial.println("Scanning Data");
        lcd.print("Scanning Data");
        lcd.setCursor(0,1);
        
         // initialize the Ethernet shield using DHCP:
        if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to obtaining an IP address using DHCP");
        while(true);
        }
        
        lcd.clear();
        Serial.println(keypadstring);
        lcd.print(keypadstring);
        client.connect(HOST_NAME, HTTP_PORT);
        httpsrequest();
        client.stop();
      }
       
      } else {
      keypadstring += key; // append new character to input password string
     }
  }
}

void httpsrequest(){
 
  // connect to web server on port 80:
  if(client.connected()) {
    // if connected:
    Serial.println("Connected to server");
    // make a HTTP request:
    // send HTTP header
    client.println("GET /practice/sec.php?keypadstring="+keypadstring);
    client.println("Host: " + String(HOST_NAME));
    client.print("HTTP/1.1");
    client.println("Connection: close");
    client.println(); // end HTTP header
    while(client.connected()) {
      if(client.available()){
        // read an incoming byte from the server and print it to serial monitor:
        char c = client.read();
        Serial.print(c);
        switch (c){
          case '1':
          Serial.println(" Trackind ID: "+keypadstring);
          Serial.println("Open Moneyslot 1");
          moneyslot1();
          break;
          
          case '2':
          Serial.println(" Trackind ID: "+keypadstring);
          Serial.println("Open Moneyslot 2");
          moneyslot2();
          break;
          
          case '3':
          Serial.println(" Trackind ID: "+keypadstring);
          Serial.println("Open Moneyslot 3");
          moneyslot3();
          break;
          
          case '4':
          Serial.println(" Trackind ID: "+keypadstring);
          Serial.println("Open Moneyslot 4");
          moneyslot4();
          break;
          
          case '5':
          Serial.println(" Trackind ID: "+keypadstring);
          Serial.println("Open Moneyslot 5");
          moneyslot5();
          goto exitloop;
          break;

          default:
          break;
          Serial.println("Error Occured");
        }
      }
      
    }
    exitloop:
    // the server's disconnected, stop the client:
    Serial.println("disconnected");
    options();
  } else {// if not connected:
    Serial.println("connection failed");
  }
}
void options()
{
  keypadstring = "";
  parceldrop.write(90);
  mmoneyslot1.write(90);
  mmoneyslot2.write(90);
  mmoneyslot3.write(90);
  mmoneyslot4.write(90);
  mmoneyslot5.write(90);
  digitalWrite(RedpinLock,HIGH);
  digitalWrite(GreenpinUnlock,LOW);
  char key = mykeypad.getKey();
  lcd.setCursor(0, 0);
  lcd.print("A.Deliver Parcel");
  lcd.setCursor(0,1);
  if (key == 'A')
  {
    lcd.clear();
    standbyposition();
  }
  else
  {
    options();
  }
}

void standbyposition()
{ 
    LCDRow = 0;
    digitalWrite(RedpinLock,HIGH);
    digitalWrite(GreenpinUnlock,LOW);
    lcd.setCursor(0,0);
    lcd.print("Tracking ID");
    lcd.setCursor(0,1);
}
void moneyslot1()
{
  digitalWrite(RedpinLock,LOW);
  digitalWrite(GreenpinUnlock,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.println("Hello");
  parceldrop.write(180);
  delay(1000);
  lcd.print("Success");
  lcd.setCursor(0,1);
  lcd.print("MoneySlot 1");
  mmoneyslot1.write(180);
  delay(5000);
  lcd.clear();
  options();
}
void moneyslot2()
{
  digitalWrite(RedpinLock,LOW);
  digitalWrite(GreenpinUnlock,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.println("HI");
  parceldrop.write(180);
  delay(1000);
  lcd.print("Success");
  lcd.setCursor(0,1);
  lcd.print("MoneySlot 2");
  mmoneyslot2.write(180);
  delay(5000);
  lcd.clear();
  options();
}
void moneyslot3()
{
  digitalWrite(RedpinLock,LOW);
  digitalWrite(GreenpinUnlock,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.println("Howdy");
  parceldrop.write(180);
  delay(1000);
  lcd.print("Success");
  lcd.setCursor(0,1);
  lcd.print("MoneySlot 3");
  mmoneyslot2.write(180);
  delay(5000);
  lcd.clear();
  options();
}
void moneyslot4()
{
  digitalWrite(RedpinLock,LOW);
  digitalWrite(GreenpinUnlock,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.println("Morning");
  parceldrop.write(180);
  delay(1000);
  lcd.print("Success");
  lcd.setCursor(0,1);
  lcd.print("MoneySlot 4");
  mmoneyslot4.write(180);
  delay(5000);
  lcd.clear();
  options();
}
void moneyslot5()
{
  digitalWrite(RedpinLock,LOW);
  digitalWrite(GreenpinUnlock,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.println("Night");
  Serial.println("Success");
  parceldrop.write(180);
  delay(1000);
  lcd.print("Success");
  lcd.setCursor(0,1);
  lcd.print("MoneySlot 5");
  mmoneyslot5.write(180);
  delay(5000);
  lcd.clear();
}

sec.php

<?php
require 'config.php';
if (isset($_GET['keypadstring'])){
$keypadstring = $_GET["keypadstring"];
$sql= "SELECT * FROM record WHERE trackingid LIKE '%$keypadstring%'";
echo"\n";
echo "Moneyslot: ";
	foreach($pdo->query($sql)as $row){
	echo "$row[moneyslot]";
	}
echo"\n";

echo "Tracking ID: ";
	foreach($pdo->query($sql)as $row){
	echo "$row[trackingid]";
	}
echo"\n";

echo "Payment: ";
	foreach($pdo->query($sql)as $row){
	echo "$row[payment]";
	}
echo"\n";

}
?>

As I have no knowledge about the flow in the code it's hard to tell.
Use serial monitor and Serial.print telling what the code is trying, and print when it succeeds. Like that key read in loop. Does that go through?

Yes, because I was able to run the program fine for the first time it just won't work for the 2nd time.

The flow is
When you enter a tracking ID in the arduino.

it searches the phpmyadmin database for a saved tracking ID then fetches and displays the tracking ID in the arduino LCD.

Then after displaying the servo motor does its thing it it goes back from the start where you should enter a tracking ID.
But the program only runs this flow for the 1st try and it won't fetch data for the 2nd time.

Its like my ethernet shield connection does not connect for the 2nd time

Because in my serial monitor when i ran it it shows that it is successful connection 1st but when I do it again it does not show anything more.

this is my serial monitor for my 1st try:

222#Scanning Data
222
Connected to server

Moneyslot: 5 Trackind ID: 222
Open Moneyslot 5
Night
Success
disconnected

but when I try it for the 2nd time it only shows this in the serial monitor:

222#Scanning Data

Have You heard about flow charts?
Word sallad is not the way to tell.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.