Hello, I need help. I am making such a web-based temperature and humidity monitoring application. The device that I use is DHT22, Arduino, and Ethernet shield for internet connection from the router. The scenario of my project where the sensor data will be sent to mysql webhost (I use 000webhost.com for hosting). To input data from Arduino to MySQL using PHP with the Get method.
From the experiments carried out on the arduino serial output it can connect to the web server, but the data doesn't go to mysql. Before I tried it with localhost (xampp) data from the sensor can enter mysql localhost. Please help me so that sensor data can be saved to mysql (webhost).
This my arduino code :
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
//byte ip[] = {192, 168, 1, 105 }; //Enter the IP of ethernet shield
char server[] = "www.rizkiprat070.000webhostapp.com"; //alamat hosting rizkiprat070.000webhostapp.com www.nyankosensei.pe.hu
EthernetClient client;
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP"); //DHCP
// Ethernet.begin(mac, ip); //static IP (uncommend ipaddress)
}
dht.begin();
delay(1000); // GIVE THE SENSOR SOME TIME TO START
//delay(1000);
Serial.println("connecting...");
}
void loop() {
float hum = dht.readHumidity(); //Reading the humidity and storing in hum
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
if (client.connect(server, 80)) { //localhost : IP 192.168.1.103
Serial.println("connected");
client.print("GET /data1.php?");
client.print("temperature=");
client.print(temp);
client.print("&humidity=");
client.println(hum);
client.println("HTTP/1.1");
client.println("Host: www.rizkiprat070.000webhostapp.com");
client.print("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println();
Serial.print("Temperature= ");
Serial.println(temp);
Serial.print("Humidity= ");
Serial.println(hum);
client.stop(); //Closing the connection
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(10000);
}
PHP code for connection to database (connection.php):
If you have doubts about your php/mySql/webserver configuration you can usually (if you are using the GET method) test with a normal web browser to simulate what the Arduino would be doing, for example:
thank you, but for the hostname, I think it's correct, because I tried to enter the manual through the web browser, the data can be entered into mysql.
6v6gt:
If you have doubts about your php/mySql/webserver configuration you can usually (if you are using the GET method) test with a normal web browser to simulate what the Arduino would be doing, for example:
rpm070:
thank you, but for the hostname, I think it's correct, because I tried to enter the manual through the web browser, the data can be entered into mysql.
I do not think you understand my suggestion from post #1. I am suggesting you change connection.php.
rpm070:
yes, I understand what you mean, if I change host (on connection.php) to rizkiprat070.000webhostapp.com, PHP can't connect to mysql.
Now that I think about it, both values of hostname in connection.php should work (assuming the database is hosted on the same system as the web server, which they usually are). So I don't know why my suggestion would stop it from working.
But the fact that you can insert data to the database from browser as suggested means that the problem must be in your Arduino code, I think. Can you post output you see on serial monitor? I suggest you copy every Client.print/ln() line and change the copied lines to Serial.print/ln() so you can see exactly what is being sent to the client.
PaulRB:
Now that I think about it, both values of hostname in connection.php should work (assuming the database is hosted on the same system as the web server, which they usually are). So I don't know why my suggestion would stop it from working.
But the fact that you can insert data to the database from browser as suggested means that the problem must be in your Arduino code, I think. Can you post output you see on serial monitor? I suggest you copy every Client.print/ln() line and change the copied lines to Serial.print/ln() so you can see exactly what is being sent to the client.
what do you think about my arduino program, if you think there is a mistake please let me know. thank you
PaulRB:
I though about that missing space also. But it is separated from the humidity value by a new line:
client.println(hum);
client.println("HTTP/1.1");
so I thought that was ok (the new line counts as whitespace). But maybe the HTTP/1.1 must be on the *same line* as the GET. Worth trying
Ah yes. I didn't notice the line feed before, which is almost certainly wrong as well.
The user has to find a known valid example and follow it very carefully with attention to that sort of detail.
Sometimes, looking in the Apache access log files also helps to show up such errors.
PaulRB:
I though about that missing space also. But it is separated from the humidity value by a new line:
client.println(hum);
client.println("HTTP/1.1");
so I thought that was ok (the new line counts as whitespace). But maybe the HTTP/1.1 must be on the *same line* as the GET. Worth trying:
client.print(hum);
client.println(" HTTP/1.1");
6v6gt:
Ah yes. I didn't notice the line feed before, which is almost certainly wrong as well.
The user has to find a known valid example and follow it very carefully with attention to that sort of detail.
Sometimes, looking in the Apache access log files also helps to show up such errors.
Thank you @PaulRB & @6v6gt my program was successful. thanks