Save data of temperature and humidity on MySQL with Arduino Uno and Wifly

Picture of Save data of temperature and humidity on MySQL with Arduino Uno and Wifly
Hello guys, i make this instructable for the people that  liked the electronics and the botanic, with this you have the data about  the temperatura and the humidity of your orchard and register this in a data base MySQL.

For this proyect you need some knowledge of programing in PHP and MySQL, we will make three archive .php, but this I'll explain in the following steps.
Remove these adsRemove these ads by Signing Up

Step 1: You need this

Picture of You need this
For this project, you need:

Arduino UNO or similar board  ($29.95)

WiFly Shield ($89.95)

Grove - Moisture sensor ($4.99)

Six Jumper Wires ($4.50)

DS18B20 Temperature Sensor with metal tip ($10.59)

If only  need, One protoboar ($9.58)


you need a server WEB, with PHP and database MySQL. I used the NAS of QNAP TS-110, is perfect for this project.

Step 3: Connecting all in Arduino

Picture of Connecting all in Arduino
Now, we will connect the two sensors on the Arduino shield Wifly:

-For the temperature sensor, the pin order is: GND-VCC in 5v power - SIG in digital pin 3.

-For the Moisture sensor, the pin order is: GND - 3.3V VCC in power - SIG in the analog pin 0.

Step 4: Preparing the database Mysql

In the query of NySQL you need put this code:

CREATE TABLE `your_database`.`tempmoi` (
`temp1` VARCHAR( 255 ) NOT NULL ,
`moi1` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `id` )
Amirul922 months ago

hello. I just want to asking about the connect.php. What do you mean by "your_ipserver" ?

camilo.n1012 (author)  Amirul921 month ago
Hi, "your_ip server" is the address where you host the server MySQL. In this case, is "", to connect the localhost.

hai. I'm having a problem with the arduino coding. It said as shown in the image:

camilo.n1012 (author)  Amirul921 month ago

This error is because you have not included the OneWire library, I recommend you download the version 2.0 from here:

OmkarD4 months ago

Hi Camilo,

Nice tutorial !!

I am trying to upload temperature from temperature sensor. I am using temperature sensor, arduino uno, arduino ethernet shield and RJ 45 cable. I am using Xampp( database running on my PC. I have created a table(temp) in this database(temp_database). I have saved php files in my computer at C:xampp/htdocs/temp_database. I have hooked up Ethernet Shield to one of internet ports in my home. After all this i don't find table getting updated by temperature values. Is there any mistake? Please guide me.Here is the source code for Arduino:

#include "SPI.h"

#include "Ethernet.h"

const int temperaturePin = A0;

byte mac[] = { 0x90, 0xA2, 0xDB, 0x0F, 0x3A, 0xDF };

byte ip[] = { 131, 247, 19, 140 }; this is the ip address for Ethernet Shield

byte gateway[] = { 131, 225, 3, 254 };

byte subnet[] = { 255, 255, 252, 0 };

EthernetClient client ;

char server[] = "";

void setup() {

// Open serial communications and wait for port to open:


while (!Serial) {

; // wait for serial port to connect. Needed for Leonardo only


// start the Ethernet connection:

if (Ethernet.begin(mac) == 0) {

Serial.println("Failed to configure Ethernet using DHCP");

// no point in carrying on, so do nothing forevermore:

// try to congifure using IP address instead of DHCP:

Ethernet.begin(mac, ip);


// give the Ethernet shield a second to initialize:




void loop()


if( client.connect(server, 80))


float tem = getTemp();

Serial.println( tem );

client.print( "GET /add.php?");


client.print( tem );

client.println( " HTTP/1.1");

client.println( "Host: localhost" );

client.println( "Content-Type: application/x-www-form-urlencoded" );

client.println( "Connection: close" );





delay( 5000 );


float getTemp() {

float voltage, V,t ;

voltage = analogRead(temperaturePin) ;

V = voltage * 0.004882814 ;

t = (V - 0.5) * 100.0;

return t;


Here is the conec.php file


function Conection(){

if (!($link=mysql_connect("localhost","root","8282"))) {



if (!mysql_select_db("temp_database",$link)){



return $link;



Here is the add.php file:




$Sql="INSERT INTO 'temp'('temp1') VALUE ('".$_GET["temp1"]."')";


header("Location: insertareg.php");


Here is the insertareg.php file:



<title>Data of Sensor</title>



<h1>Data from the temperature sensor</h1>

<form action="add.php" method="get">



<td>Temperature </td>

<td><input type="text" name="temp1" size="20" maxlength="30"></td>



<input type="submit" name="accion" value="Grabar">






$result=mysql_query("select * from temp order by id desc",$link);


<table border="1" cellspacing="1" cellpadding="1">


<td> Temperature  </td>



while($row = mysql_fetch_array($result)) {

printf("<tr><td>  %s </td></tr>", $row["temp1"]);







jbuenaseda5 months ago

Hi! I'm having an error in instareg.php It says

Warning: mysql_select_db() expects parameter 2 to be resource, object given in C:\xampp\htdocs\WEB2\php_files\conec.php on line 9

Can\'t use sensordata :

what should I do in here? anyone help?

jbuenaseda5 months ago

Hi! What will I write in this? Can you provide an example?

if (!($link=mysql_connect("your_ipserver","your_user","your_pass")))

fgoncalves46 months ago

Hi! very nice tutorial. thanks!

is there any Ethernet version?

Hi! Nice tutorial!

I have a question

Where specifically are suppose to be the add.php, conec.php, insertareg.php???

Thanks for your time! :)

camilo.n1012 (author)  thernandez colon7 months ago

Hi ! These files must be hosted on your serve to be consulted for the database.

Much success and regards !

Hi Camilo! Thanks for your answer.

Can I know what library for the WiFly Antenna did you used in this project?

I have try with two and the program and I get errors such as:

"byte has not been declared"

"Client has not been declared"

I think that must be a problem with the library I am using :)

Thanks a lot for your help! ;)

camilo.n1012 (author)  thernandez colon7 months ago

Hi ! c:

I recommend that you download it from Github, this link:

In all modes, being very honest, it is recommended that if you work with Arduino and Wifi, you better have your the Arduino YUN.

If you have further questions do not hesitate to say them c:

Regards !!

Hi Camilo,

Did you have the problem "Client has not been declared"?

I am using the same library that you used and im getting this problem. Please if you solve it help me :)

Thanks for your help ;)

I bought the Arduino UNO R3 Recently :(

Did the Arduino UNO gave you some problem in this project?

arduinowifly7 months ago

Hello I have a project of home automation and I am trying to program the wifly, I have configured this module .I want to actually insert the sensor values from the arduino and display it in a web interface and save them in the database mysql I have tested this example but the table is still empty.Also, i dont understand why the instruction of GET is shown in the serial !!! , Can you help me please?

I'am using arduino uno + WIFLY RN-171 + XBEE shield
this is the execution of the code :

Sans titre.png
darrigan8 months ago

Hi! Why did you write


and not



Thank you.

bkools2 years ago
(removed by author or community request)

How did u solve the problem?

Turbovix2 years ago
Dear, to perform the build I get the following message:

In file included from C:\Arduino\arduino-1.0.1\libraries\WiFly/WiFly.h:4,
from wifly_twosensors.cpp:13:
C:\Arduino\arduino-1.0.1\libraries\WiFly/SpiUart.h:62: error: conflicting return type specified for 'virtual void SpiUartDevice::write(byte)'
C:\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:48: error: overriding 'virtual size_t Print::write(uint8_t)'
In file included from C:\Arduino\arduino-1.0.1\libraries\WiFly/WiFly.h:8,
from wifly_twosensors.cpp:13:
C:\Arduino\arduino-1.0.1\libraries\WiFly/Client.h:21: error: conflicting return type specified for 'virtual void Client::write(byte)'
C:\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:48: error: overriding 'virtual size_t Print::write(uint8_t)'
C:\Arduino\arduino-1.0.1\libraries\WiFly/Client.h:23: error: conflicting return type specified for 'virtual void Client::write(const uint8_t*, size_t)'
C:\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:50: error: overriding 'virtual size_t Print::write(const uint8_t*, size_t)'

Hi, do u solve the problem? i also facing it ><

MAG0810 months ago

Great work. I tried it on xampp and it works. Thanks for your help :)

why did you put: client.print( "GET /agregar.php?"); it is just that I haven't seen any agregar.php file on the example. It is equal to add.php?


Splight2 years ago
Well i found this page looking tutorial on how to MYSQL + PHP, and i found this project!! It looks amazing! Congrats!
Knowing what you know now, would you again purchase the Grove Moisture Sensor or would you build your own?
camilo.n1012 (author)  Jason Bedard2 years ago
I buy the Grove Moisture Sensor in, in Chile. i recommend buy this sensor, because is designed for use in gardens.

This is the link :
where is this file you called by GET
client.print( "GET /agregar.php?");
none of those file has this name,

and where did you located php files?

thank you
Good to know. Thanks.
Awesome. I am wondering what kinds of modifications would be needed to save to an sd card instead of the wifi. The wifly is out of my price range for right now but I have all of the other parts. Thank you.
zmashiah2 years ago
Very nice.
See this: for similar thing, only I am saving to flat file on the PC and communicating to PC over XBee rather than Wifi.
nevenne2 years ago
where l can find a sensors
camilo.n1012 (author)  nevenne2 years ago
Hi, you can find the sensors in, they have shiping world. This are the links:


Helder4u2 years ago
I was just looking for an example of how to start a MySQL Database and Connect to a website ^^

Will study Your application :D
msatorius2 years ago
How long does this project run on a single 9v?
camilo.n1012 (author)  msatorius2 years ago
Hi, the project will last a one day and half (i tested with my duracell 9v), but I recommend using a battery of the modem of the ethernet, which has allrrededor of 8 to 11.4 V, you will last much because have it a higher amperage. Good luck