After reading through examples and projects, it has come to my notice that I can use the WebClient example in the EtherCard lib to store data in my MySQL database. But I am still not able to store the data. I am able to display the data on a webpage.
How do I store my data?
I have edited the example code for my project work to display the distance from an ultrasonic sensor to my database.
Could you please help?
Below is the Arduino Code
#include <EtherCard.h>
#define trigPin 23
#define echoPin 22
long duration;
int distance;
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
static byte session_id;
const char website[] PROGMEM = "localhost";
static void my_callback (byte status, word off, word len) {
Serial.println(">>>");
Ethernet::buffer[off+300] = 0;
Serial.print((const char*) Ethernet::buffer + off);
Serial.println("...");
}
void setup () {
Serial.begin(57600);
Serial.println(F("\n[webClient]"));
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
#if 1
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
#elif 2
// then use it directly. Note: the string can not be in PROGMEM.
char websiteIP[] = "192.168.1.2";
ether.parseIp(ether.hisip, websiteIP);
#else
// or provide a numeric IP address instead of a string
byte hisip[] = { 192,168,1,2 };
ether.copyIp(ether.hisip, hisip);
#endif
ether.printIp("SRV: ", ether.hisip);
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 5000;
Serial.println();
Serial.print("<<< REQ ");
ether.browseUrl(PSTR("/writedata.php/"), distance, website, my_callback);
}
}
Below is my php code
<?php
$link = mysqli_connect("localhost", "root", "microgo777", "test");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "INSERT INTO sensor (value) VALUES ('".$_GET["distance"]."')";
if(mysqli_query($link, $sql)){
echo "<center> Records inserted successfully</center>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
That means that your Arduino is acting as a server, and the browser is acting as a client (the only role it can have).
You store data in a database by making a GET request, which is something only a client does.
With a W5x00 chip, which handles a lot more of the work, you can have the Arduino act as server and still be able to act as a client when needed. I do not know the ENCJ ethernet shield can act as client and server, though I have my doubts.
Not that any of this matters, because your code proves that your Arduino is NOT acting as a server, so that first statement is irrelevant.
const char website[] PROGMEM = "localhost";
That is so dumb. localhost is a name that has special meaning. It means that the client and the server are running on the same machine. The Arduino is NOT running a MySQL database, php, and acting as a client.
Your client is NOT trying to connect to itself. So, you need to use a different name.
If you want the Arduino to request that a server store some data, it does that by making a GET request, which is a client role. So, no, the Arduino does not need to act as a server, so, no, you did not understand correctly.
Now the real question, how do I make the GET request and send the data to the server?
You need to ask yourself some questions. Is google.com a client or a server? Do you ask it for information, or does it ask for information? Which role does a client play? Which roles does a server play?
If you agree that google.com is a server, then you should be able to recognize that the code you posted is having the Arduino act as a client. And, you should be able to recognize that you need to replace that server's name with your server's name.
The ether.browseUrl() method call's first two arguments contain the name of the script to execute and the data to pass to the script, though the example does a piss-poor job of illustrating or explaining that.
The documentation that I found for the method makes it even worse, by referring to the arguments as path and file.
The first argument should be the name of the script to execute. The second argument should be the data to pass to the script.
If you were to enter http://my.server.com/saveData.php?temp=26&hum=81 in the address bar of a browser, to pass temp and hum, with values of 26 and 81, to the saveData.php scrip on my.server.com, making the Arduino make the same request would involve having the first argument to the browseUrl() method be "/saveData.php" and the second argument would be "temp=26&hum=81".
If you wanted to pass variable data to the script, you could have
int temp=26;
int hum = 81;
char scriptData[80];
sprintf(scriptData, "temp=%d&hum=%d, temp, hum);
and the second argument would be scriptData, rather than a hardcoded string.
Thank you again, for your time and patience. I am super grateful.
I do have more questions.
As for the server, I am using the Wamp Server for the database and not any website. When I type "Localhost" or the computer's IP "192.168.1.3" on the address bar which opens the Wamp Server Homepage.
In such a case, what should be the value of :
const char website[] PROGMEM = "";
192.168.1.3/writedata.php?distance=10 stores 10 into the database
My script is writedata.php.
I want to print distance values measured by the ultrasonic sensor HC-SR04.
Instead of setting distance values, I would like to print whatever value it senses at that moment.
I feel like the server is not listening to the Arduino or the Arduino is not connecting properly to the server. I also feel like my PHP code could be wrong. I am eating my fingers, ripping my face and breaking my head over this.
Clearly, you control the entire process - the client side, making GET requests, and the server side, where the PHP scripts run and the database lives.
Post the code you have now, and the serial monitor output.
Look at the web server logs. Does the Arduino make a connection attempt? What did the server do with the GET request?
You can debug the PHP script, with echo statements.
I presume that you have tried executing the PHP script by making a web browser make the equivalent call to the script. If not, THAT is the next thing to do.