Go Down

Topic: client.connect() problem (Read 820 times) previous topic - next topic

LakshanaR

Jan 31, 2018, 10:41 am Last Edit: Feb 01, 2018, 02:17 am by LakshanaR
Code: [Select]
#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:
Code: [Select]
<?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 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://arduino.stackexchange.com/q/49323.
Hope someone can help me
Thanks in advance

pert

Also posted at:
https://arduino.stackexchange.com/q/49323
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. When you post links please always use the chain links icon on the toolbar to make them clickable.

PaulS

Quote
but it doesn't work.
That is like saying that it is raining in Seattle. You wouldn't have found it necessary to post, if the code did exactly what you wanted, would you?

So, the code does something. You expect it to do something. Yet, you failed to describe either thing. Hope you did better on stack exchange, but I'm not going to go there to look.
The art of getting good answers lies in asking good questions.

LakshanaR

I apologize for not mentioning my post on arduino stackexchange but it was obvious for me to post it in others too. I have no experience in posting questions at forums. I apologize if I have wasted your time on answering my question. I hope my question is clear now.

rw950431

I'm pretty sure the problem is between

Code: [Select]
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")

and

Code: [Select]
$server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"

In computer networking "localhost" has a very specific meaning and isnt the right address for this application. You are likely to have more success if you find the IP address of your XAMPP server (maybe its 192.168.1.7 or not) and use that in both places.




LakshanaR

Till now my network is being shared from my PC to arduino and I have assigned an IP to the arduino myself i.e 192.168.1.9.

I thought maybe this was a problem because the arduino was not getting internet access even though it was sharing the network with PC. The PC is getting internet but the arduino is not.

So I tried connecting arduino directly to the router and connecting my PC via WiFi to the same network that the arduino is connected to.
Yet the same problem persists.

Does anyone know the cause?

rw950431

Are you able to visit the URL http://192.168.1.7/write_data.php in the browser of the XAMPP PC?

Are you able to visit the same URL from another device on the same network?

Is there any XAMPP log entries when the arduino attempts to connect?

 

On the PC open a command window and enter the following command

Code: [Select]
netstat -an | find "LISTEN"


Look for a line with a :80 at the end of the second field

Hopefully you will see a line like

 
Code: [Select]
TCP    0.0.0.0:80            0.0.0.0:0              LISTENING


or
Code: [Select]
TCP    192.168.1.7:80            0.0.0.0:0              LISTENING


But if you see a line like
Code: [Select]
TCP    127.0.0.1:80            0.0.0.0:0              LISTENING


It means your XAMPP server is only responding to requests from other applications on the same PC so your arduino wont be able to reach it. 



Go Up