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
}
}