0

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

I have also followed the steps given in this https://forum.arduino.cc/index.php?topic=40448.0 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 https://forum.arduino.cc/index.php?topic=525947.0.

Hope someone can help me

Thanks in advance

#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 recieve 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);
}

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);
?>
  • 2
    How do you know till where it runs? What output does it produce? What have you tried to debug? Why do you stop the client twice? – tttapa Jan 31 '18 at 10:15
  • 1
    Also asked at: forum.arduino.cc/index.php?topic=525947 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. – per1234 Jan 31 '18 at 10:40
  • try to change ip to 127.0.0.1? maybe try NAT at your router. – hary kusuma Oct 29 '18 at 19:15
1

As far as I can see, when you call client.connect(server, 80), you call it with char[] variable that holds char server[] = "192.168.1.7". And from library code it seems that it will call function with those parameters which will try to resolve dns but can't do it and returns 0.

It calls: int EthernetClient::connect(const char * host, uint16_t port) ,
when it should call: int EthernetClient::connect(IPAddress ip, uint16_t port)
Source code: https://github.com/arduino-libraries/Ethernet/blob/master/src/EthernetClient.cpp

So you need to change char server[] = "192.168.1.7"; to IPAddress server(192,168,1,7); That way it will call the right connection -function and wont get stuck with dns.

Edit: added details

0

Check the server ip is correct and server is running. Then check the return value of client.connect() function. If it's a 0 then most likely your server is behind a firewall. If so, unblock the apache server to pass through it. I had this similar issue with wamp.

Although 0 is not a valid return code for client.connect() according to http://www.arduino.cc/en/Reference/ClientConnect , it weirdly returns a 0. Oherwise it will give you a clue.

Also use IPAddress class for assigning the server address instead of using char [].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.