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 Temperature and Humidity on MySQL 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.

Arduino Uno and Wifly Arduino Uno and Wifly

Step 1: You Need This

 You Need This 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 2: Setting the Temperature Sensor and Humidity

 Temperature Sensor and Humidity Temperature Sensor and Humidity Temperature Sensor and Humidity
DS18B20 sensor is connected as follows. The position pin of the sensor Grove

Step 3: Connecting All in Arduino

 Connecting All in Arduino 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` )

Step 5: Preparing for PHP Files

You make three files:

In this file will connect to our database


function Conection(){
if (!($link=mysql_connect(“your_ipserver”,”your_user”,”your_pass”)))  {
if (!mysql_select_db(“your_database”,$link)){
return $link;


This file allows us to send the data to our table “tempmoi” located in our database, where “temp1” is equivalent to the temperature data and “moi1” is equivalent to the humidity data. This file also allows us to Arduino send data to the base.


$Sql=”insert into tempmoi (temp1,moi1)  values (‘”.$_GET[“temp1″].”‘, ‘”.$_GET[“moi1″].”‘)”;
header(“Location: insertareg.php”);


In this file we can see all the data collected by the sensors and put into the database, in addition to add some data of moisture and temperature if necessary.


<title>Data of Sensor</title>
<h1>Data from the temperature and moisture sensors</h1>
<form action=”add.php” method=”get”>
<td>Temperature 1</td>
<td><input type=”text” name=”temp1″ size=”20″ maxlength=”30″></td>
<td>Moisture 1</td>
<td><input type=”text” name=”moi1″ size=”20″ maxlength=”30″></td>
<input type=”submit” name=”accion” value=”Grabar”>
$result=mysql_query(“select * from tempmoi order by id desc”,$link);
<table border=”1″ cellspacing=”1″ cellpadding=”1″>
<td>&nbsp;Temperature 1&nbsp;</td>
<td>&nbsp;Moisture 1&nbsp;</td>
while($row = mysql_fetch_array($result)) {
printf(“<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td></tr>”, $row[“temp1”], $row[“moi1”]);


Step 6: Codes for the Arduino

To use this code requires the following libraries:

– OneWire librarie

– Wifly librarie

Well, i will briefly explain the code for the arduino, use the client that offers us the shield Wifly to send the data entering the digital pin 3 and pin analogue 0, these are sent as a sentences to file “agregar.php” for this we use the client.print.

I must remember to modify the data written in bold in the code:


#ifndef __CREDENTIALS_H__
#define __CREDENTIALS_H__
char passphrase[] = “pass_WIFI“; //password Wi-Fi
char ssid[] = “name_WIFI“; //name Wi-Fi

#include “WiFly.h”

int sensorPin = A0;
int sensorValue = 0;

int DS18S20_Pin = 3;
OneWire ds(DS18S20_Pin);

Client client( “your_server_web“, 80 );

int looped = 1;

void setup()
Serial.begin( 9600 );

if( !WiFly.join( ssid, passphrase ) )
Serial.println( “Association failed.” );

while( 1 )
// Hang on failure.

void loop()

if( client.connect() )
sensorValue = analogRead(sensorPin);
float temp = getTemp();
Serial.println( temp );

client.print( “GET /add.php?”);
client.print( temp );
client.print( sensorValue );
client.println( ” HTTP/1.1″);
client.println( “Host: your_server_web” );
client.println( “Content-Type: application/x-www-form-urlencoded” );
client.println( “Connection: close” );
delay( 5000 );

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( ! {
//no more sensors on chain, reset search
return -1000;

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -1000;

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print(“Device is not recognized”);
return -1000;

ds.write(0x44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();;
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] =;


byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two’s compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;



Step 7: And Now … to Prove It !!

to Prove It to Prove It

Now we will check in “insertareg.php” data sent over Wi-Fi to your Mysql database PHP THROUGH.

This project can use on your crops, it is easy to use and manipulate, with the background light.

Hope you liked this instructable, if they have questions do not hesitate to make.

Good luck : D

Source: Save Data of Temperature and Humidity on MySQL With Arduino Uno and Wifly

0/5 (0 Reviews)

About The Author

Hassan Zaka

I am an expert in accounting and possess diverse experience in technical writing. I have written for various industries on topics such as finance, business, and technology. My writing style is clear and simple, and I utilize infographics and diagrams to make my writing more engaging. I can be a valuable asset to any organization in need of technical writing services.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top