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.
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> Timestamp </td>
<td> Temperature 1 </td>
</tr>
<?php
if($result!==FALSE){
while($row = mysql_fetch_array($result)) {
printf("<tr><td> %s </td><td> %s </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;
}
?>