Hello!
Lately I’ve been working on a project with arduino (with ethernet shield) which sends temperature (we’re using Dallas Temperature sensors) to mysql database (named “test”) via php scrpit. Here is the arduino code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC };
byte ip[] = { 172, 16, 3, 115 };
byte server[] = { 172, 16, 0, 201 }; // MySQL
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
EthernetClient client;
void setup(void)
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println("Starting connection to MySQL");
Serial.println();
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET /update_db.php?celsius=36 HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
sensors.setResolution(insideThermometer, 9);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.println(tempC);
}
void loop(void)
{
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
printTemperature(insideThermometer);
delay(2000);
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
And here is PHP script:
<?php
$celsius = $_GET['celsius'];
//Connect to database
$con = mysql_connect("serveraddress", "username", "Password");
if(!$con)
die('Could not connect: ' .mysql_error());
mysql_select_db("test", $con);
mysql_query("INSERT INTO temp(Temperatuur) VALUES ('$celsius')");
mysql_close($con);
?>
The problem is that the variable only saves itself if the command is like client.println("GET /update_db.php?celsius=36 HTTP/1.0");
but I’d like to save the temperature from Dallas sensor.