Arduino Ethernet problem

Hi, I have been having some trouble getting arduino Ethernet to POST on my SQL server database. I can't really find the issue.

Because of my lack of experience in this sort of things, I have spent loads of time on this problem.

I have been working through this tutorial, except i am using ds18b20 as my sensor.

Below I posted all the code I have been using.

I have been getting also this sort of error, this value just shows up, and after that nothing.

http://failiem.lv/u/tgtkjho

Here is my Arduino code.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Ethernet.h> 
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte ip[] = { 10, 0, 0, 177 }; 
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 10, 0, 0, 1 };
EthernetClient client;
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
int t =0;
String data;
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  Serial.begin(9600);

Ethernet.begin(mac, ip, gateway, subnet);


  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  sensors.begin();
  delay(200);
  sensors.requestTemperatures();
  delay(200);
  t = (int)sensors.getTempCByIndex(0);
  data = "";
}
 
 
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 t = (int) sensors.getTempCByIndex(0);

 Serial.println(t);
 
 data = "temp1=" + t;
 Serial.print(data);

 if (client.connect("10.0.0.50",80)) { // REPLACE WITH YOUR SERVER ADDRESS
		client.println("POST /add.php HTTP/1.1"); 
		client.println("Host:10.0.0.50"); // 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(10000);
}

Here is my add.php, which receives the data

<?php
   	include("connect.php");
   	
   	$link=Connection();

	$temp1=$_POST["temp1"];

	$query = "INSERT INTO `tempLog` (`temperature`) 
		VALUES ('".$temp1."')"; 
   	
   	mysql_query($query,$link);
	mysql_close($link);

   	header("Location: index.php");
?>

I am also inserting index.php for viewing and connect.php for database connection, althought all the connections within the server(my pc) seems to be working fine. I am using WAMP.

<?php

	include("connect.php"); 	
	
	$link=Connection();

	$result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);
?>

<html>
   <head>
      <title>Sensor Data</title>
   </head>
<body>
   <h1>Temperature / moisture sensor readings</h1>

   <table border="1" cellspacing="1" cellpadding="1">
		<tr>
			<td>&nbsp;Timestamp&nbsp;</td>
			<td>&nbsp;Temperature 1&nbsp;</td>
		</tr>

      <?php 
		  if($result!==FALSE){
		     while($row = mysql_fetch_array($result)) {
		        printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td></tr>", 
		           $row["timeStamp"], $row["temperature"]);
		     }
		     mysql_free_result($result);
		     mysql_close();
		  }
      ?>

   </table>
</body>
</html>
<?php

	function Connection(){
		$server="localhost";
		$user="root";
		$pass="";
		$db="temperatura";
	   	
		$connection = mysql_connect($server, $user, $pass);

		if (!$connection) {
	    	die('MySQL ERROR: ' . mysql_error());
		}
		
		mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

		return $connection;
	}
?>

First, this is not correct. You omitted the dns server parameter.

// replace this
Ethernet.begin(mac, ip, gateway, subnet);
// with this
Ethernet.begin(mac, ip, gateway, gateway, subnet);

Second, this is probably going to fail. It will try to use dns to resolve the server "10.0.0.50".

 if (client.connect("10.0.0.50",80)) { // REPLACE WITH YOUR SERVER ADDRESS

Try this instead. It will not use dns.

 IPAddress server(10,0,0,50);

 if (client.connect(server,80) == 1) {[code]

edit: Third, you should read the server response until the server closes the connection, or the connection times out.

 if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
    client.println("POST /add.php HTTP/1.1");
    client.println("Host:10.0.0.50"); // 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()) {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
    Serial.println("\r\ndisconnected");
 }

Thanks, that seems to solve a lot of problems, which I have had, but it also brings a few new ones up. Yet still I am not getting any output at index.php on the browser.
Here is the code I am using now on Arduino.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Ethernet.h> 
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte ip[] = { 10, 0, 0, 177 }; 
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 10, 0, 0, 1 };
EthernetClient client;
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
int t =0;
String data;
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  Serial.begin(9600);

Ethernet.begin(mac, ip, gateway, gateway, subnet);


  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  sensors.begin();
  delay(200);
  sensors.requestTemperatures();
  delay(200);
  t = (int)sensors.getTempCByIndex(0);
  data = "";
}
 
 
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 t = (int) sensors.getTempCByIndex(0);

 Serial.println(t);
 
 data = "temp1=" + t;
 Serial.print(data);
IPAddress server(10,0,0,50);
 if (client.connect(server,80)==1) { // REPLACE WITH YOUR SERVER ADDRESS
		client.println("POST /add.php HTTP/1.1"); 
		client.println("Host:10.0.0.50"); // 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()) {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
    Serial.println("\r\ndisconnected");
 }
 
 delay(10000);
}

Also no data on the database showing up. Up above I also have the connection and Index PHP file.

This is the output from Serial:

 Requesting temperatures...DONE
Temperature for Device 1 is: 23.9423
TP/1.1HTTP/1.1 302 Found
Date: Sun, 12 Apr 2015 16:15:15 GMT
Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12
X-Powered-By: PHP/5.5.12
Location: index.php
Content-Length: 2553
Content-Type: text/html



<font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\connect.php on line <i>9</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>236976</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\add.php' bgcolor='#eeeeec'>..\add.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>241560</td><td bgcolor='#eeeeec'>Connection(  )</td><td title='C:\wamp\www\add.php' bgcolor='#eeeeec'>..\add.php<b>:</b>4</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>241928</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a>
(  )</td><td title='C:\wamp\www\connect.php' bgcolor='#eeeeec'>..\connect.php<b>:</b>9</td></tr>
</table></font>


<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: temp1 in C:\wamp\www\add.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>236976</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\add.php' bgcolor='#eeeeec'>..\add.php<b>:</b>0</td></tr>
</table></font>

disconnected

The worst part is I am getting this error from PHP.

removed in the future: use mysqli or PDO instead in C:\wamp\www\connect.php on line 9

[12-Apr-2015 18:28:47 Europe/Paris] PHP Stack trace:

[12-Apr-2015 18:28:47 Europe/Paris] PHP 1. {main}() C:\wamp\www\add.php:0

[12-Apr-2015 18:28:47 Europe/Paris] PHP 2. Connection() C:\wamp\www\add.php:4

[12-Apr-2015 18:28:47 Europe/Paris] PHP 3. mysql_connect() C:\wamp\www\connect.php:9

[12-Apr-2015 18:28:47 Europe/Paris] PHP Notice: Undefined index: temp1 in C:\wamp\www\add.php on line 6

[12-Apr-2015 18:28:47 Europe/Paris] PHP Stack trace:

[12-Apr-2015 18:28:47 Europe/Paris] PHP 1. {main}() C:\wamp\www\add.php:0

I tried changing the code from above to the one below, yet it still seems to bringing errorrs up

<?php
   	include("connect.php");
   	
   	$link=Connection();
        $temp1=isset($_POST["temp1"]) ? $_POST['value'] : '';

	$query = "INSERT INTO `tempLog` (`temperature`) 
		VALUES ('".$temp1."')"; 
   	
   	mysql_query($query,$link);
	mysql_close($link);

   	header("Location: index.php");
?>

I think this is causing the 302 messsage.
header("Location: index.php");

Try this.
$temp1=$_POST["temp1"];

The rest I can't tell.

I tried disabling the Firewall and I removed the header("Location:index.php").

Still no results.

It is giving me now the 200 message.

Thanks, for the effort.

linkers:
Still no results.

It is giving me now the 200 message.

What is the rest of the 200 message?

It is showing 200 OK, something like that. For the rest I think it's the same.

Something is wrong here...

 data = "temp1=" + t;
 Serial.print(data);

...if the output is this:
TP/1.1HTTP/1.1 302 Found

Data is a string variable it adds temp1 + the temperature read from sensor.
I tried outputting the data to a serial port, as you can see the line below.
It is showing me 0.5(definetly not the right temperature) and sometimes not showing anything at all, so a huge error there.

Any advice what to do about it?

I don't use the String data type. I prefer a character array and sprintf.

Woohoo baby thanks mr guru SuferTim I realised that Ds18b20 had an output of float value, so I used dtostrf function to solve this.

Also in my SQL database i didn't have a timestamp correctly set, so it didn't add data, but that I solved in a few minutes.

So yeah, this is the final code, probably I will tune it up, but atleast I know it works now :).

Next goal, setting up several ds18b20 sensors and reading them, should be more fun.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Ethernet.h> 
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
byte ip[] = { 10, 0, 0, 177 }; 
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 10, 0, 0, 1 };
EthernetClient client;
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
float t =0;
String data;
char charVal[10];
String stringVal = "";
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  Serial.begin(9600);

Ethernet.begin(mac, ip, gateway, gateway, subnet);


  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  sensors.begin();
  delay(200);
  sensors.requestTemperatures();
  delay(200);
  t = (float)sensors.getTempCByIndex(0);
  data = "";
  String stringVal = "";
}
 
 
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 t = (float) sensors.getTempCByIndex(0);

stringVal= dtostrf(t, 4, 3, charVal);
 Serial.print("StringVal: "); Serial.println(stringVal);
 data = "temp1=" + stringVal;
 Serial.println(data);
IPAddress server(10,0,0,50);
 if (client.connect(server,80)==1) { // REPLACE WITH YOUR SERVER ADDRESS
		client.println("POST /add.php HTTP/1.1"); 
		client.println("Host:10.0.0.50"); // 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()) {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
    Serial.println("\r\ndisconnected");
 }
 
 delay(10000);
}