Hi guys,
Im currently working on connecting my arduino to mysql database. I've attached a dht11 temperature and humidity sensor, and i want to send the data to mysql, something doesnt work i dont know what. Here's the sketch:
6v6gt:
Maybe only a single & here:
client.print("&&");
What happens if you enter the url from you GET statement in a web browser?
It adds the record to the base, so the php side works well. Here is my insert statement:
$sql = "INSERT INTO arduino (temperatura,wilgotnosc) VALUES ('".$_GET["temperature"]."','".$_GET["humidity"]."')";
If you are following an example, look very carefully at it. For example, there should probably be a space after the first double quote:
client.println( "HTTP/1.1");
Maybe post a link to the example sketch you are following.
thats the topic. Idk why but i cant see any mistakes here. Is there any way to echo temperature and humidity variables in the write_data file? At least i would check if the data even goes to this file. I was trying to echo it like this:
$temperatura=$_GET['temperature'];
$wilgotnosc=$_GET['humidity'];
echo $temperatura;
of course worked when i entered the site using url which i posted before
Should i add client.println( Host: www.hosthere.com")? i tried it. Still nothing. And what about IP Address, is it necessary? Or its enough if i declare mac
Overall, in my opinion mac is enough cause i got the connection checked by if condition. But whats happening with the variables i dont really know
Your problem is only in the Http GET statement otherwise you would have got a connection failed message.
Post the code you have tried, but maybe first try the example GET statement below with a fixed temperature of 99.
if (client.connect(server, 3306)) {
client.println("GET /test1/write_data.php?temperature=99");
client.println("Host: www.improveyourphp.pl");
client.println("HTTP/1.1");
client.println("Connection: close");
client.println();
with this code im getting connection failed
if (client.connect(server, 80)) {
Serial.print(client.connect(server,80));
Serial.println();
Serial.println("connected");
// Make a HTTP request:
client.println("GET /test1/write_data.php?temperature=100");
client.println("Host: www.improveyourphp.pl");
client.println("HTTP/1.1");
client.println("Connection: close");
client.println();
client.stop();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
with this im getting connected to the site, but nothing happends in DB. I noticed client.stop(); pretends if i can connect or not, if this line is missing im getting connection failed.
well after 10 hours spent on it still doesnt working and i penetrated almost whole internet, i think im either retarded or something is broken here
The echo statement in php looks ok but the results will be visible only in a browser which you anyway say works.
That is not true. The echo output is returned by the server to the client. If the client is a browser, the client has some clue how to read and display the data. If the client is an Arduino, the programmer may, or may not, have a clue how to read and display the data. But, it CAN be done.
with this im getting connected to the site, but nothing happends in DB.
I'm not in the least surprised. The GET request starts with "GET " and ends with " HTTP/1.1". ONLY the data for the get request goes in between. The Host statement FOLLOWS the GET request.
If the entire GET request is not sent using ONE statement, the parts are NOT sent using println(). Only the LAST part is sent using println(). The others are sent using print().
Since you seem to have some level of access to the server, check the web server logs on the server. Enable stupidity logging, if it is not already enabled. Oops, I meant error logging.
Then, check the logs. you will see exactly what the httpd daemon got to deal with, and exactly what it thought of what it got.
Have you actually read the HTTP specification? This is not a valid HTTP request, not even close. What's up with all the line endings? Why is there a header field in your request line?
RFC7239§3:
All HTTP/1.1 messages consist of a start-line followed by a sequence
of octets in a format similar to the Internet Message Format
[RFC5322]: zero or more header fields (collectively referred to as
the "headers" or the "header section"), an empty line indicating the
end of the header section, and an optional message body.
HTTP-message = start-line
*( header-field CRLF )
CRLF
[ message-body ]
RFC7230§3.1:
start-line = request-line / status-line
RFC7230§3.1.1:
A request-line begins with a method token, followed by a single space
(SP), the request-target, another single space (SP), the protocol
version, and ends with CRLF.
RFC7230§5.4:
The "Host" header field in a request provides the host and port
information from the target URI, enabling the origin server to
distinguish among resources while servicing requests for multiple
host names on a single IP address.
Host = uri-host [ ":" port ] ; Section 2.7.1
A client MUST send a Host header field in all HTTP/1.1 request
messages.
[...]
Since the Host field-value is critical information for handling a
request, a user agent SHOULD generate Host as the first header field
following the request-line.
For example:
GET /test1/write_data.php?temperature=100 HTTP/1.1\r\n
Host: www.improveyourphp.pl\r\n
Connection: close\r\n
\r\n
Note the use of spaces and line endings, and compare it to the spaces and line endings in your code.
mercenariussz:
. . .
well after 10 hours spent on it still doesnt working and i penetrated almost whole internet, i think im either retarded or something is broken here
Well, you picked a very bad example to follow. arduino uno - DHT11 with Mysql - Arduino Stack Exchange
The guy clearly didn't know what he was doing and that is why he was asking for help to get it all fixed in the first place. What he posted at the end, although he claims it works, is also a mess.
If, after reading the previous attempts to help you, you are still stuck, then google for "Arduino Web Client Get".
Dear users, thank you for your advices. Thats how code looks now. It inserts the records correctly, but there is one problem left to solve. It inserts only one record to the base, then keeps outputing connection failed untill i just reload the sketch and it inserts just one record again. Why?
/*
Web client from https://www.arduino.cc/en/Tutorial/WebClient
This sketch connects to a website(http://...)
using an Arduino Wiznet Ethernet shield.
Circuit:
Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <dht.h>
#include <SPI.h>
#include <Ethernet2.h>
dht DHT;
int interval = 5000; // Wait between dumps
#define DHT11_PIN 2
// 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:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.improveyourphp.pl"; // name address (using DNS)
// Set the static IP address to use if the DHCP fails to assign
// change this to a possible address in your installation:
IPAddress ip(192, 168, 0, 177); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// 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;
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...");
//
// If this url works in a browser: www.improveyourphp.pl/test1/write_data.php?temperature=100
// the following should work.
// If the URL you have given is NOT real (eg it uses HTTPS:// or adds humidity etc., it will not work
//
}
void loop() {
// READ DATA
//Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature = ");
Serial.print(DHT.temperature);
Serial.println(" *C");
Serial.print("Humidity = ");
Serial.print(DHT.humidity);
Serial.println(" ");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.print("GET /test1/write_data.php?temperature=" ) ;
client.print( DHT.temperature );
client.print("&");
client.print("humidity=");
client.print( DHT.humidity );
client.println( " HTTP/1.1"); // embedded space and println !
client.println("Host: www.improveyourphp.pl");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// 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);
}
delay(interval);
}
so the get statement should now look like: GET /test1/write_data.php?temperature=DHT.temperature&humidity=DHT.humidity