How to Connect Arduino WiFi to post data on the Database

Hi Everyone

iam trying to connect the arduino uno WiFi to the database but its giving this massage on the serial monitor.....' attemting to connect to the IoT (my WIFI router)"..

this is the code iam using for this..

/////#include <SPI.h>
#include <WiFi.h>

// EDIT: Change the 'ssid' and 'password' to match your network
char ssid[] = "IoT"; // wireless network name
char password[] = "IoT@2016"; // wireless password
int status = WL_IDLE_STATUS;
WiFiClient client;

// EDIT: 'Server' address to match your domain
char server[] = "192.168.1.6"; // This could also be 192.168.1.18/~me if you are running a server on your computer on a local network.

// This is the data that will be passed into your POST and matches your mysql column
int yourarduinodata = 999;
String yourdatacolumn = "yourdata=";
String yourdata;

void setup() {
Serial.begin(9600);

connectWifi();

// You're connected now, so print out the status
printWifiStatus();

postData();
}

void loop() {

}

void connectWifi() {
// Attempt to connect to wifi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, password);
// Wait 10 seconds for connection
delay(10000);
}
}

void printWifiStatus() {
// Print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// Print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// Print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

// This method makes a HTTP connection to the server and POSTs data
void postData() {
// Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
// (yourarduinodata) and package them into the String yourdata which is what will be
// sent in your POST request
yourdata = yourdatacolumn + yourarduinodata;

// If there's a successful connection, send the HTTP POST request
if (client.connect(server, 80)) {
Serial.println("connecting...");

// EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
client.println("POST /insert_mysql.php HTTP/1.1");

// EDIT: 'Host' to match your domain
client.println("Host: www.yourdomain.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(yourdata.length());
client.println();
client.println(yourdata);
}
else {
// If you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
}
}

////

can anyone please help me with this,,, to connect to post data on the database successfully..

thanks in advance..

What Wifi device are you using ?

You are convinced that these credentials are correct. You can use these successfully in another device (eg Smartphone) to connect to your wireless network ?

char ssid[] = "IoT"; // wireless network name
char password[] = "IoT@2016"; // wireless password

yes, the credentials are correct because iam using them on my phone and iam connected..

Positioning:
yes, the credentials are correct because iam using them on my phone and iam connected..

What protocol is your WiFi router using? Only some protocols are supported by WiFi shields.

Which WiFi shield are you using?

PaulS:
. . .
Which WiFi shield are you using?

I hope you have more success in getting an answer to this question than I did. :slight_smile:

:slight_smile: EDison.. yaa neh.. but the

Positioning:
yes, the credentials are correct because iam using them on my phone and iam connected..

i am usnig an Arduino uno WiFi board.. can anyone help... i want to pos data from a fingerprint module to a database of mySQL... please help guys.. thanks

please help guys

Forget it. You are not answering all the questions that have been asked.

[/quote]

PaulS:
Forget it. You are not answering all the questions that have been asked.

Hi PaulS

i am using Arduino uno WiFi which is ESP8266 WiFi intergrated, and my Router is using internet protocol version 4 (TCP/IPv4) .. hope i answered your question correct..

hope i answered your question correct..

You did not. I have no idea what "Arduino uno WiFi which is ESP8266 WiFi intergrated" means. As far as I know, not a single Arduino comes with an integrated WiFi device OF ANY KIND. Certainly NOT the Uno. Post a link, or links, to the device(s) you have.

WiFi routers use WEP, WPA, or some other protocols to determine who can connect. If your does not use one of the protocols that the WiFi class supports, you will be SOL.

Hi PaulS

hope your day is going well.

Here is the link to the device/ board I am using...Arduino - Home.

I don't know whether the codes I am using to post data on the database which I created are wrong or what is the problem...

your assistance will be highly appreciated.

Alright. I was not aware that anyone was making an Arduino with an integrated WiFi board. I see that you did accurately describe the hardware that you have.

The next step is for you to post your ACTUAL serial output, instead of trying to paraphrase it.

If the Arduino is failing to connect to your WiFi router, the usual problems are that you have identified the wrong SSID, the wrong password, or the router is not using a supported wireless protocol (WEP, WPA, etc.).

PaulS:
The next step is for you to post your ACTUAL serial output, instead of trying to paraphrase it.

Hi PaulS

Thank you very much PaulS .

The arduino is connecting to the WiFi router. The problem is to Post data on the Database which I created using MySQL.

the codes I am using to post data on the database is below:

how can I modify the following codes so that I can post data on the database (using the Default port80)
from the Arduino WiFi ?

*/

#include <SPI.h>

#include <Ethernet.h>

// Enter a MAC address for your controller below.

// Newer Ethernet shields have a MAC address printed on a sticker on the shield

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don't want to use DNS (and reduce your sketch size)

// use the numeric IP instead of the name for the server:

char server[] = "192.168.1.3"; // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign

IPAddress ip(192, 168, 1, 2);

// Initialize the Ethernet client library

// with the IP address and port of the server

// that you want to connect to (port 80 is default for HTTP):

EthernetClient client;

int eid=0;

void setup() {

// Open serial communications and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for native USB port only

}

// start the Ethernet connection:

if (Ethernet.begin(mac) == 0) {

Serial.println("Failed to configure Ethernet using DHCP");

// try to congifure using IP address instead of DHCP:

Ethernet.begin(mac, ip);

}

// give the Ethernet shield a second to initialize:

delay(1000);

Serial.println("connecting...");

// Connect to the server (your computer or web page)

if (client.connect(server, 80)) {

client.print("GET /write_data.php?"); // This

//...............................................................................

Serial.println("connected");

// Make a HTTP request:

//client.println("GET /write_data.php HTTP/1.1");

//...............................................................................

client.print("id="); // This

client.print(eid); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor

client.println(" HTTP/1.1"); // Part of the GET request

client.println("Host: 192.168.1.3"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")

client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message

client.println(); // Empty line

client.println(); // Empty line

client.stop(); // Closing connection to server

} else {

// if you didn't get a connection to the server:

Serial.println("connection failed");

}

}

void loop() {

// if there are incoming bytes available

// from the server, read them and print them:

if (client.available()) {

char c = client.read();

Serial.print(c);

}

// if the server's disconnected, stop the client:

if (!client.connected()) {

Serial.println();

Serial.println("disconnecting.");

client.stop();

// do nothing forevermore:

while (true);

}

}

//////////////////////////////////////////////////////////////////////////////

how can I modify the following codes so that I can post data on the database (using the Default port80)
from the Arduino WiFi ?

Since you can't even learn to post code correctly, nor do you seem to be able to even connect to your local network, I don't think you have a hope in hell of making that code POST anything anywhere.

There is more then one solution to a problem. Here is one potential solution.
Since Arduino have a very rudimentary functions to deal with web server output. The solution is at the web server you are connecting to.

Instead of attempting to connect directly to a database no matter which one it is. Try connecting a string to a web server page and that page will connect to the database posting the results in a return string.

Illustrating it through an example might be the best way.
In this example we will use an Arduino WiFi enabled and a web server supporting PHP and MySQL.

Arduino output string:

//obviously replace variables after ? with whatever you see fit
server.send(200, "text/html", "get_information_from_database.php?what=x&whatelse=y");

get_information_from_database.php then retrieves the information from the database using a redirect function posting it back to Arduino using the PHP function redirect

//database connectivity functions here based on the information received....
//IP address represents where Arduino is on the network
header('Location: http://192.168.1.18/?confirm_data=yes&x=123&y=456');
exit;

Remember! if you are sending or receiving information via open network then embedding a password is recommended (i.e. get_information_from_database.php?what=x&whatelse=y&password=123456)

Storing information into the database use the same exact sample but the output should result in say database_connection=true or database_connection=false...

This solution is far easier than attempting to conduct a direct database connectivity.

Realized a potential bug.

Instead of using:
server.send(200, "text/html", "get_information_from_database.php?what=x&whatelse=y");

Creates static information that will not go anywhere.

Use this instead:
client.println("Refresh: 4;URL='//192.168.1.8:80/get_information_from_database.php?what=x&whatelse=y'>");

Dynamic flow of information proceeds with a "wait" for a reply from a page call.

And of course don't forget to test for proper variables passing on information
if (server.hasArg("x")){....}