#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
DHT dht;
const int sensor_pin = A1;
unsigned int ldr;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,33);
char server[] = "192.168.1.7"; // 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. "www.yourwebpage.com")
EthernetClient client;
void setup() {
Serial.begin(9600); // Serial.begin starts the serial connection between computer and Arduino
Ethernet.begin(mac, ip);
Serial.println("Ethernet configured via DHCP");
delay(1000);
dht.setup(2);
}
void loop() {
delay(dht.getMinimumSamplingPeriod()); /* Delay of amount equal to sampling period */
float soilmoisture;
int sensor_analog;
sensor_analog = analogRead(sensor_pin);
soilmoisture = ( 100 - ( (sensor_analog/1023.00) * 100 ) );
float temperature = dht.getTemperature();
float humidity = dht.getHumidity();
ldr=analogRead(A0);
Serial.println("Connecting..");
// Connect to the server (your computer or web page)
if (client.connect(server, 80)) {
Serial.println("--> Connected");
client.print("GET /write_data.php?"); // This
client.print("temperature="); // This
client.print(temperature); // 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.print("&humidity="); // This
client.print(humidity); // 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.print("&soilmoisture="); // This
client.print(soilmoisture); // 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.7"); // 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( "Content-Type: application/x-www-form-urlencoded" );
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
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
client.flush();
client.stop();
// Give the server some time to receive the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
delay(10000);
}
I'm using XAMPP(version-7.2.1) server in my PC to publish my arduino sensor data in a webpage. I have tried the code above and it shows an error like:
Ethernet configured via DHCP
Connecting...
-->connection failed
My php code is as follows:
<?php
// Prepare variables for database connection
$dbusername = "x"; // enter database username, I used "arduino" in step 2.2
$dbpassword = "y"; // enter database password, I used "arduinotest" in step 2.2
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
$dbname = "arduinotest1";
// Connect to your database
$dbconnect = mysqli_connect($server, $dbusername, $dbpassword,$dbname);
if (!$dbconnect) {
die("Database connection failed: " . mysqli_connect_error());
}
$dbselect = mysqli_select_db($dbconnect, "arduinotest1");
if (!$dbselect) {
die("Database selection failed: " . mysqli_connect_error());
}
// Prepare the SQL statement
$sql = "INSERT INTO arduinotest1.tabletest2 (temperature,humidity,soilmoisture) VALUES ('".$_GET["temperature"]."','".$_GET["humidity"]."','".$_GET["soilmoisture"]."')";
// Execute SQL statement
mysqli_query($dbconnect,$sql);
?>
I have also followed the steps given in this Arduino Uno+Ethernet Shield+XAMPP - Interfacing - Arduino Forum but it doesn't work.
The code runs till client.connect() after that it doesn't get inside the if(client.connect(server,80)) loop.
The arduino's ethernet shield is assigned an IP by the PC via ICS.
I believe that the problem is in connecting the arduino to the localhost but I don't know how to resolve it. The XAMPP server seems to run properly. I have checked it logs.
I have also posted at arduino uno - client.connect() problem - Arduino Stack Exchange.
Hope someone can help me
Thanks in advance