I have been trying to complete a tutorial (link below) that uses an Ethernet Shield to send temperature readings to a database via a php file.
I am very new to this game and I have spent the best part of a week trying to troubleshoot this. I have communication between add.php and the database and the Arduino is telling me that it is connected to the network, so it would appear that the break down is between the Arduino and the add.php file, but as I said I am new to this and may be wrong
Any help would be greatly appreciated,
Regards
Eoghan
Client2.ino
#include <DHT.h>
#include <Ethernet2.h>
#include <SPI.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,2}; //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(127,0,0,1);
EthernetClient client;
#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int t = 0;// TEMPERATURE VAR
int h = 0;// HUMIDITY VAR
String data;
void setup() {
Serial.begin(115200);
// start the Ethernet connection:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
dht.begin();
delay(4000); // GIVE THE SENSOR SOME TIME TO START
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
data = "";
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}
Serial.print("Current humidity = ");
Serial.print(h);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(t);
Serial.println("C ");
data = "&temp1=" + String(t)+ "&hum1="+ String(h);
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
Serial.println("Server connection OK");
client.println("GET /add.php");
client.println( " HTTP/1.1");
client.println(server); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop();// DISCONNECT FROM THE SERVER
}
delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
First, this won't work. You need an external IP, this IP can be used only in the server itself.
IPAddress server(127,0,0,1);
Second, you should use the correct evaluation for the connect function. It doesn't have any effect om your code, but later on it could if you use a domain name instead of an IP for the server.
// change this
if (client.connect(server,80)) {
// to this
if (client.connect(server,80) == 1) {
Third, this is incorrect. it should all be one line, and a POST. not a GET.
// change these
client.println("GET /add.php");
client.println( " HTTP/1.1");
// to just this
client.println("POST /add.php HTTP/1.1");
Fourth, you should read and display the response from the server.
// after this
client.print(data);
// use this
while(client.connected()) {
while(client.available()) {
Serial.write(client.read());
}
}
// then close the connection on the client end
client.stop();
I really appreciate your reply, and I am thankful for your help,
however it seems I am still going wrong somewhere.
in the line
client.println("Post /add.php HTTP/1.1");
is it enough to just have the add.php in the same folder as my client2.ino or should i include the path to it
my full code now reads after making your amemdments
#include <DHT.h>
#include <Ethernet2.h>
#include <SPI.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,2}; //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(192,168,0,7);
EthernetClient client;
#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int t = 0;// TEMPERATURE VAR
int h = 0;// HUMIDITY VAR
String data;
void setup() {
Serial.begin(115200);
// start the Ethernet connection:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
dht.begin();
delay(4000); // GIVE THE SENSOR SOME TIME TO START
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
data = "";
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}
Serial.print("Current humidity = ");
Serial.print(h);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(t);
Serial.println("C ");
data = "&temp1=" + String(t)+ "&hum1="+ String(h);
if (client.connect(server,80) == 1) { // REPLACE WITH YOUR SERVER ADDRESS
Serial.println("Server connection OK");
client.println("Post /add.php HTTP/1.1");
client.println(server); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
while(client.connected()) {
while(client.available()) {
Serial.write(client.read());
}
}
// then close the connection on the client end
client.stop();
}
if (client.connected()) {
client.stop();// DISCONNECT FROM THE SERVER
}
delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
Again
I will hugely appreciate any help you can provide
Thanks
Eoghan
is it enough to just have the add.php in the same folder as my client2.ino or should i include the path to it
The add.php script needs to be in the folder that inetd or httpd are looking for, depending on which server software is running on the server.
In my case, I use Apache, so httpd is expecting to find scripts in C:\Apache24\htdocs. You can use relative path information (relative to the root directory) if you script is not in the root directory.
It is generally easier to use GET, since the length of the data doesn't need to be known in advance. Of course, that means that the PHP script needs to be looking for $GET variables, not $POST variables.
Thanks for noticing the Post mistake i completely missed it,
In terms of the GET I amended the code and unfortunately still no connection
Thanks again
Eoghan
SurferTim, I am in college at the moment so cant screenshot it, but the serial monitor displays something along the lines of:
"Connected at 192.168.0.24 Temperature = 20c Humidity = 40%"
then the temperature and humidity will continue to post every 5 seconds. In regards to your second question the php files are not hosted anywhere, so no they are just on my server.
In regards to your question Paul, no connection between my arduino and the add.php file. I am not sure why i have my server root based in my user folder, I didnt know it would be an issue, so you would recommend relocating it?
Don't want a screenshot. Just post everything you see in the serial monitor. If you added my code suggestion about reading and displaying the server response, there will be a pretty verbose response, including the header values from the server. That is what I am interested in.
No problem you will have to bear with me for a little while as i am going into an exam now in an hours time,
But i will be straight back to you after wards when i am home
thanks again
No problem. I'm off to work in an hour, so it will be at least a few hours before I will get a chance to check in here again after that. Talk to you then!
Hi Tim,
sorry i am so late getting back to you just had a very busy day
I have amended all the code as you recommended, however what is being displayed on the serial monitor is the same before:
My IP address: 192.168.0.26.Current humidity = 43% temperature = 20C
nothing has seemed to change since updating the code for some strange reason, as the code should be perfect
#include <DHT.h>
#include <Ethernet2.h>
#include <SPI.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,2}; //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(192,168,0,7);
EthernetClient client;
#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int t = 0;// TEMPERATURE VAR
int h = 0;// HUMIDITY VAR
String data;
void setup() {
Serial.begin(115200);
// start the Ethernet connection:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
dht.begin();
delay(4000); // GIVE THE SENSOR SOME TIME TO START
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
data = "";
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}
Serial.print("Current humidity = ");
Serial.print(h);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(t);
Serial.println("C ");
data = "&temp1=" + String(t)+ "&hum1="+ String(h);
if (client.connect(server,80) == 1) { // REPLACE WITH YOUR SERVER ADDRESS
Serial.println("Server connection OK");
client.println("POST /add.php HTTP/1.1");
client.println(server); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
while(client.connected()) {
while(client.available()) {
Serial.write(client.read());
}
}
// then close the connection on the client end
client.stop();
}
if (client.connected()) {
client.stop();// DISCONNECT FROM THE SERVER
}
delay(5000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}
Add a little error checking. Find out if the connection is failing. Take a look at the end of this code for the new "else" addition.
if (client.connect(server, 80) == 1) { // REPLACE WITH YOUR SERVER ADDRESS
Serial.println("Server connection OK");
client.println("POST /add.php HTTP/1.1");
client.println(server); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
while (client.connected()) {
while (client.available()) {
Serial.write(client.read());
}
}
// then close the connection on the client end
client.stop();
}
else {
// if connection failed
Serial.println("Connection failed");
}