Arduino Projects DISCUSSION

How can Arduino Mega2560+Wifi Shield 2.0 (seeedstudio) receive data from database in xampp server on my pc

Started by syedzainnasir How can Arduino Mega2560+Wifi Shield 2.0 (seeedstudio) receive data from database in xampp server on my pc
2 replies 248 views 2 participants
Latest activity · 20 Feb 2017

How can Arduino Mega2560+Wifi Shield 2.0 (seeedstudio) receive data from database in xampp server on my pc

syedzainnasir Arduino Projects Forum
#1
I'm trying to connect my Arduino Mega via wi-fi to my xampp server. My main objective is get a value at my database and store it on a variable in Arduino code. With this new variable I'll be able to control the rest of the program... so for that matter I need your help. Can someone give-me advice?

I already try every thing that i found in the internet but it just dosen't work.

I think that the simple way is to send a GET request to the server and read his response... but for some reason i cant send the GET request trought...

I test it on the browser directly and the value show up, so i think that the server side is allright.

Here is my Arduino Code:
[code]#include <Arduino.h> #include <SoftwareSerial.h> #include "WiFly.h" #include "HTTPClient.h" #define SSID "mySSID" #define KEY "myKEY" #define AUTH WIFLY_AUTH_WPA2_PSK #define HTTP_GET_URL "http://192.168.1.80/xampp/training/send_data_db.php/?temp=0" // Pins' connection // Arduino WiFly // 2 <----> TX // 3 <----> RX SoftwareSerial uart(10, 11); WiFly wifly(uart); HTTPClient http; char get; void setup() { Serial.begin(9600); Serial.println("------- WIFLY HTTP --------"); uart.begin(9600); // WiFly UART Baud Rate: 9600 // Wait WiFly to init // delay(3000); // check if WiFly is associated with AP(SSID) if (!wifly.isAssociated(SSID)) { while (!wifly.join(SSID, KEY, AUTH)) { Serial.println("Failed to join " SSID); Serial.println("Wait 0.1 second and try again..."); delay(100); } wifly.save(); // save configuration, } Serial.println("\r\nTry to get url - " HTTP_GET_URL); Serial.println("------------------------------"); while (http.get(HTTP_GET_URL, 10000) < 0) { } while (wifly.receive((uint8_t *)&get, 1, 1000) == 1) { Serial.print(get); } if (wifly.commandMode()) { Serial.println("\r\n\r\nEnter command mode. Send \"exit\"(with \\r) to exit command mode"); } } void loop() { int c; while (wifly.available()) { c = wifly.read(); if (c > 0) { Serial.write((char)c); } } while (Serial.available()) { c = Serial.read(); if (c >= 0) { wifly.write((char)c); } } }[/code] And here is my PHP code:
[code]<?php include("./connection/database_connect.php"); include("./top_table.php"); if((isset($_SESSION["ID"])) AND (isset($_GET["temp"]))) { $id=$_SESSION["ID"]; $sql="SELECT temp FROM perfil, client WHERE perfil.ID ='$id'"; $consult=mysqli_query($connect,$sql); $result=mysqli_num_rows($consult); if(($result==1)) { $person_data=mysqli_fetch_array($consult, MYSQLI_ASSOC); $temp=$person_data["temp"]; header("HTTP/1.1" . " " . 200 . "OK"); header("Content-Type: text/html; charset=UTF-8"); header("Content-Length: 1112"); header("Connection: close"); header("/r/n"); echo $temp; } else { //se user dosen't exist on data base header("Location: http://192.168.1.80/xampp/training/login.php"); exit; } } else { header("Location: http://192.168.1.80/xampp/training/login.php"); exit; } ?>[/code]

Community replies 2

Re: How can Arduino Mega2560+Wifi Shield 2.0 (seeedstudio) receive data from database in xampp server on my pc

#2
[quote=Eugene post_id=90 time=1487591464 user_id=72] I'm trying to connect my Arduino Mega via wi-fi to my xampp server. My main objective is get a value at my database and store it on a variable in Arduino code. With this new variable I'll be able to control the rest of the program... so for that matter I need your help. Can someone give-me advice?

I already try every thing that i found in the internet but it just dosen't work.

I think that the simple way is to send a GET request to the server and read his response... but for some reason i cant send the GET request trought...

I test it on the browser directly and the value show up, so i think that the server side is allright.

Here is my Arduino Code:
[code]#include <Arduino.h> #include <SoftwareSerial.h> #include "WiFly.h" #include "HTTPClient.h" #define SSID "mySSID" #define KEY "myKEY" #define AUTH WIFLY_AUTH_WPA2_PSK #define HTTP_GET_URL "http://192.168.1.80/xampp/training/send_data_db.php/?temp=0" // Pins' connection // Arduino WiFly // 2 <----> TX // 3 <----> RX SoftwareSerial uart(10, 11); WiFly wifly(uart); HTTPClient http; char get; void setup() { Serial.begin(9600); Serial.println("------- WIFLY HTTP --------"); uart.begin(9600); // WiFly UART Baud Rate: 9600 // Wait WiFly to init // delay(3000); // check if WiFly is associated with AP(SSID) if (!wifly.isAssociated(SSID)) { while (!wifly.join(SSID, KEY, AUTH)) { Serial.println("Failed to join " SSID); Serial.println("Wait 0.1 second and try again..."); delay(100); } wifly.save(); // save configuration, } Serial.println("\r\nTry to get url - " HTTP_GET_URL); Serial.println("------------------------------"); while (http.get(HTTP_GET_URL, 10000) < 0) { } while (wifly.receive((uint8_t *)&get, 1, 1000) == 1) { Serial.print(get); } if (wifly.commandMode()) { Serial.println("\r\n\r\nEnter command mode. Send \"exit\"(with \\r) to exit command mode"); } } void loop() { int c; while (wifly.available()) { c = wifly.read(); if (c > 0) { Serial.write((char)c); } } while (Serial.available()) { c = Serial.read(); if (c >= 0) { wifly.write((char)c); } } }[/code] And here is my PHP code:
[code]<?php include("./connection/database_connect.php"); include("./top_table.php"); if((isset($_SESSION["ID"])) AND (isset($_GET["temp"]))) { $id=$_SESSION["ID"]; $sql="SELECT temp FROM perfil, client WHERE perfil.ID ='$id'"; $consult=mysqli_query($connect,$sql); $result=mysqli_num_rows($consult); if(($result==1)) { $person_data=mysqli_fetch_array($consult, MYSQLI_ASSOC); $temp=$person_data["temp"]; header("HTTP/1.1" . " " . 200 . "OK"); header("Content-Type: text/html; charset=UTF-8"); header("Content-Length: 1112"); header("Connection: close"); header("/r/n"); echo $temp; } else { //se user dosen't exist on data base header("Location: http://192.168.1.80/xampp/training/login.php"); exit; } } else { header("Location: http://192.168.1.80/xampp/training/login.php"); exit; } ?>[/code] [/quote]
You should forget about the $_SESSION["ID"]; on PHP side, because you are working stateless without any session! You should give a parameter for identifying your variable from arduino to php script - is this the temp=0? If so you need to exchange
[code]$sql="SELECT temp FROM perfil, client WHERE perfil.ID ='$id'";[/code] with
[code]$sql="SELECT temp FROM perfil, client WHERE perfil.ID =" . $_GET["temp"];[/code] assuming that perfil.id is only an integer - if it is a string, then add "'".

Read More: [url=http://www.theengineeringprojects.com/2014/10/getting-data-from-webserver-using-arduino-wifi.html]Getting Data From Webserver using Arduino Wifi[/url]

Re: How can Arduino Mega2560+Wifi Shield 2.0 (seeedstudio) receive data from database in xampp server on my pc

#3
[quote=Junaid_Shahid post_id=91 time=1487591515 user_id=48] [quote=Eugene post_id=90 time=1487591464 user_id=72] I'm trying to connect my Arduino Mega via wi-fi to my xampp server. My main objective is get a value at my database and store it on a variable in Arduino code. With this new variable I'll be able to control the rest of the program... so for that matter I need your help. Can someone give-me advice?

I already try every thing that i found in the internet but it just dosen't work.

I think that the simple way is to send a GET request to the server and read his response... but for some reason i cant send the GET request trought...

I test it on the browser directly and the value show up, so i think that the server side is allright.

Here is my Arduino Code:
[code]#include <Arduino.h> #include <SoftwareSerial.h> #include "WiFly.h" #include "HTTPClient.h" #define SSID "mySSID" #define KEY "myKEY" #define AUTH WIFLY_AUTH_WPA2_PSK #define HTTP_GET_URL "http://192.168.1.80/xampp/training/send_data_db.php/?temp=0" // Pins' connection // Arduino WiFly // 2 <----> TX // 3 <----> RX SoftwareSerial uart(10, 11); WiFly wifly(uart); HTTPClient http; char get; void setup() { Serial.begin(9600); Serial.println("------- WIFLY HTTP --------"); uart.begin(9600); // WiFly UART Baud Rate: 9600 // Wait WiFly to init // delay(3000); // check if WiFly is associated with AP(SSID) if (!wifly.isAssociated(SSID)) { while (!wifly.join(SSID, KEY, AUTH)) { Serial.println("Failed to join " SSID); Serial.println("Wait 0.1 second and try again..."); delay(100); } wifly.save(); // save configuration, } Serial.println("\r\nTry to get url - " HTTP_GET_URL); Serial.println("------------------------------"); while (http.get(HTTP_GET_URL, 10000) < 0) { } while (wifly.receive((uint8_t *)&get, 1, 1000) == 1) { Serial.print(get); } if (wifly.commandMode()) { Serial.println("\r\n\r\nEnter command mode. Send \"exit\"(with \\r) to exit command mode"); } } void loop() { int c; while (wifly.available()) { c = wifly.read(); if (c > 0) { Serial.write((char)c); } } while (Serial.available()) { c = Serial.read(); if (c >= 0) { wifly.write((char)c); } } }[/code] And here is my PHP code:
[code]<?php include("./connection/database_connect.php"); include("./top_table.php"); if((isset($_SESSION["ID"])) AND (isset($_GET["temp"]))) { $id=$_SESSION["ID"]; $sql="SELECT temp FROM perfil, client WHERE perfil.ID ='$id'"; $consult=mysqli_query($connect,$sql); $result=mysqli_num_rows($consult); if(($result==1)) { $person_data=mysqli_fetch_array($consult, MYSQLI_ASSOC); $temp=$person_data["temp"]; header("HTTP/1.1" . " " . 200 . "OK"); header("Content-Type: text/html; charset=UTF-8"); header("Content-Length: 1112"); header("Connection: close"); header("/r/n"); echo $temp; } else { //se user dosen't exist on data base header("Location: http://192.168.1.80/xampp/training/login.php"); exit; } } else { header("Location: http://192.168.1.80/xampp/training/login.php"); exit; } ?>[/code] [/quote]
You should forget about the $_SESSION["ID"]; on PHP side, because you are working stateless without any session! You should give a parameter for identifying your variable from arduino to php script - is this the temp=0? If so you need to exchange
[code]$sql="SELECT temp FROM perfil, client WHERE perfil.ID ='$id'";[/code] with
[code]$sql="SELECT temp FROM perfil, client WHERE perfil.ID =" . $_GET["temp"];[/code] assuming that perfil.id is only an integer - if it is a string, then add "'".

Read More: [url=http://www.theengineeringprojects.com/2014/10/getting-data-from-webserver-using-arduino-wifi.html]Getting Data From Webserver using Arduino Wifi[/url] [/quote]
Yes you are right!

Meanwhile i finished this project.

The problem was with the Wi-Fi shield that I was using. The Http request wasn't in a right format.
TEP COMMUNITY