Hi guys. I am new to arduino and i want to make a project for my university, using Temperature Sensors and uploading the results to phpMyadmin. I am checking everywhere to find a solution but i have totally "crushed"!!!
The problem is that while it seems to be a connection between arduino sketch and phpMyadmin in my database fields, the sketch returns only 0.00 and i can't find the problem. I thank you all in advance. Every possible answer is thankfully accepted.
Arduino Sketch Code:
#include <SPI.h>
#include <Ethernet.h> //library for ethernet functions
#include <HTTPClient.h> //library for client functions
#include <DallasTemperature.h> //library for temperature sensors
#include <OneWire.h> //library for the onewire bus
#define ONE_WIRE_BUS 2 //the onewire bus is connected to pin 2 on arduino
#define TEMPERATURE_PRECISION 10 //resolution of the sensors is set to 10bit
// Ethernet settings
uint8_t hwaddr[6] = {XXXX, XXXX, XXXX, XXXX, XXXX, XXXX}; // mac-adress of arduino
uint8_t ipaddr[4] = {X, X, X, X }; // IP-adress of arduino
uint8_t gwaddr[4] = {X, X, X, X}; // IP-adress of gateway ( for later DNS implementation)
uint8_t subnet[4] = {255, 255, 255, 0}; // subnetmask
uint8_t serverip[4] = {X, X, X, X}; // IP-adress of server arduino sends data to
uint8_t serverport = 80; // the port the arduino talks to
EthernetClient client;
OneWire oneWire(ONE_WIRE_BUS); // setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature
int numSensors; // variable to store the number of sensors
bool connected = false; // yes-no variable (boolean) to store if the arduino is connected to the server
int i = 0; // variable to count the sendings to the server
void setup(void) // setup-function runs at the startup of the arduino
{
Serial.begin(9600); // start the serial port
Serial.println("I2C-to-Ethernet Bridge.");
Serial.println("Initializing Ethernet.");
Ethernet. begin(hwaddr, ipaddr); // start up ethernet
sensors.begin(); // start up the library
Serial.println("Enumerating and scanning for I2C sensors.");
numSensors = sensors.getDeviceCount(); // store the number of sensors to the variable //"sensors.getDeviceCount" is a function in the library
if(numSensors > 0) // if there is at least one sensor:
{
Serial.print("Enumerated "); //print the number of sensors to serial port
Serial.print(numSensors);
Serial.println( " sensors.");
}
else //if there is no sensor:
{
Serial.println("No sensors enumerated."); // tell the serial port
}
}
void loop(void) // loop function runs over and over again
{
if(!connected) { // if "not" connected print: not connected ;)
//Serial.println("Not connected");
if (client.connect(serverip,80)){ // if connected, set variable connected to "true" and
connected = true;
sensors.requestTemperatures(); // send the request for temperature to sensors (all sensors)
for(i=0; i<numSensors; i++) // as long as "i" ( chosen number, starts at 0) is smaller than
//"numSensors" (number of sensors) do the "for" cycle
{
float temp1 = sensors.getTempCByIndex(0); // take temperature reading from sensor "i" and store it to the variable "temp"
Serial.print("Temp is "); // just to see if the reading was succesful in serial console
Serial.println(temp1);
Serial.println("Sending to Server: "); // all the "Serial.print" is for debugging and to show other people what arduino does
client.print("GET http://localhost/mysql_connect.php?temp1="); // send this to apache on server to call "
Serial.print("GET http://localhost/mysql_connect.php?temp1="); //
client.print(temp1);
Serial.print(temp1);
client.println(" HTTP/1.1"); //
Serial.println(" HTTP/1.1"); //
client.println("Host: x.x.x.x"); //
Serial.println("Host: x.x.x.x"); //
client.println("User-Agent: arduino-ethernet"); // ethernet related stuff
Serial.println("User-Agent: arduino-ethernet"); //
client.println("Accept: text/html"); //
Serial.println("Accept: text/html"); //
client.println("Connection: close"); //
//Serial.println("Connection: close"); //
client.println(); //
Serial.println();
delay(20000); // send the temperature every 20 sec
}
}
else{
Serial.println("Cannot connect to Server"); // else block if the server connection fails (debugging)
}
}
else {
delay(500); //
while (client.connected() && client.available()) { // when connected and availabe:
char c = client.read(); // read the answer of the server and
Serial.print(c); // print it to serial port
} //
Serial.println(); //
client.stop(); // stop the connection and set
connected = false; // "connected" to false
}
}
Also i upload the php file that (i believe!) connects arduino and phpMyadmin (file name is mysql_connect.php)
P.S: At start my database(named temp_database) has only one table (named temperatures) and only one column (named temp1)
PHP CODE
<?php
if (isset($_GET['temp1']))
{
$temp1 = floatval ('temp1');
$temp1 = $_GET['temp1'];
$con = mysql_connect("host","username","password");
if (!$con)
{
die('could not connect: ' .mysql_error());
}
mysql_select_db("temp_database", $con);
$sql="INSERT INTO temperatures (temp1) VALUES ('$_GET[temp1]')";
echo "inserted correctly \n";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
if (mysql_query($sql,$con)){
echo "1 record added";
}
mysql_close($con);
}
?>
Thank you very much again