Problem Sending Data from Arduino to Online Database using EtherCard

Greetings!

Hope everybody is doing well.

I am seeking help with my project. I am using an Arduino Mega to send data, from an Ultrasonic Sensor, to an online database (phpmyadmin) with the help of an ENC28J60 Module. I have used the EtherCard Library and I am having trouble trying to send the data to the database. I can view the data on a website with respect to my IP Address but I want the data to be stored in the online database.

Could you please guide me and help me through this.

Thanks in advance

Best Wishes
Bob

Here is my arduino code

#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,2 };
static byte gwip[] = { 192,168,1,1 };
byte Ethernet::buffer[500];
BufferFiller bfill;

#define trigPin 23
#define echoPin 22

long duration;
int distance;

void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
   pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
    Serial.println( "Failed to access Ethernet controller");
  ether.staticSetup(myip, gwip);

}

static void Sensor()
{
   digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2;

}

static word homePage() {
digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2;
  bfill = ether.tcpOffset();
  bfill.emit_p(PSTR(
  "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n"
    "\r\n"
    "<meta http-equiv='refresh' content='5'/>"
    "<title>Temp server</title>" 
    "<h1>Distance $D cm</h1>"),
  distance);
  return bfill.position();
}

void loop(){
  digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2;
  Serial.print(distance);
word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos)  // check if valid tcp data is received
  {
    Sensor();
    ether.httpServerReply(homePage()); // send web page data
  }

}

Why is there code in loop() that is also in Sensor()? Why do you have such a useless name for a function? Why is that function declared static?

Your code is acting as a server. What is acting as the client, to make the Arduino serve up the page?

There is nothing in your code that makes any attempt to make a GET, or POST, request, which is how you push data to a server, where the specified script could store the pushed data to a database.

Thank you for your response!

I believe that my PHP code at the INSERT ('..') VALUES ('..') must consist of a $_GET or $_PUT or $_POST with respect to the value declared in the Arduino program.

In that case, how do I make a connection between my database and the Arduino program, and declare a GET/POST request?

Best Wishes
Bob

The EtherCard library comes with examples. Surely one of them makes a GET request.