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

377,956

307

79

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.

Step 1: You Need This

For this project, you need:

Arduino UNO or similar board  ($29.95)

http://www.sparkfun.com/products/11021

WiFly Shield ($89.95)
http://www.sparkfun.com/products/9954

Grove - Moisture sensor ($4.99)
http://www.seeedstudio.com/depot/grove-moisture-sensor-p-955.html?cPath=144_147

Six Jumper Wires ($4.50)
http://www.sparkfun.com/products/9387

DS18B20 Temperature Sensor with metal tip ($10.59)
http://www.olimex.cl/product_info.php?cPath=21_138&products_id=872&product__name=Breadborad_jumper_wire_pack

If only  need, One protoboar ($9.58)
http://www.olimex.cl/product_info.php?currency=USD&products_id=170&product__name=Proto_Board_Bread_board


AND....

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

DS18B20 sensor is connected as follows. The position pin of the sensor Grove

Step 3: 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` (
`id` INT( 255 ) NOT NULL AUTO_INCREMENT ,
`temp1` VARCHAR( 255 ) NOT NULL ,
`moi1` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;
-------------------------------------------------------------------

Step 5: Preparing for PHP Files

You make three files:

conec.php 
In this file will connect to our database

CODE:

<?php
function Conection(){
   if (!($link=mysql_connect("your_ipserver","your_user","your_pass")))  {
      exit();
   }
   if (!mysql_select_db("your_database",$link)){
      exit();
   }
   return $link;
}
?>

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

add.php
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.

CODE:

<?php
   include("conec.php");
   $link=Conection();
$Sql="insert into tempmoi (temp1,moi1)  values ('".$_GET["temp1"]."', '".$_GET["moi1"]."')";     
   mysql_query($Sql,$link);
   header("Location: insertareg.php");
?>

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.

CODE:

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

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

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
#endif

#include
#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 );
WiFly.begin();

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 );
Serial.println(sensorValue);

client.print( "GET /add.php?");
client.print("temp1=");
client.print( temp );
client.print("&&");
client.print("moi1=");
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" );
client.println();
client.println();
client.stop();
}
delay( 5000 );
}

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

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

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.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.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

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


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

ds.reset_search();

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 !!

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

2 People Made This Project!

Recommendations

  • Big and Small Contest

    Big and Small Contest
  • First Time Author

    First Time Author
  • Toys Contest

    Toys Contest

79 Discussions

0
None
JohnE284

Question 8 weeks ago

Hi,

I have a problem while I try to verify:

____________

In file included from /Users/John/Documents/Arduino/libraries/WiFly/SpiUart.h:7:0,

from /Users/John/Documents/Arduino/libraries/WiFly/WiFly.h:4,

from /Users/John/Documents/Arduino/libraries/WiFly/examples/WiFly_WebServer/WiFly_WebServer.pde:9:

/Users/John/Documents/Arduino/libraries/WiFly/_Spi.h:8:22: fatal error: WProgram.h: No such file or directory

#include <WProgram.h>

^

compilation terminated.

exit status 1

Erreur de compilation pour la carte Arduino MKR WiFi 1010

____________

How can I do? Maybe this comme from this new MKR 1010...

Thank you

0
None
Yit HengT

11 months ago

The data from sensor will upload automatically into the database ?

0
None
RudiT5

1 year ago

Hi,

Did you use a remote server or did you work local.

Seems that all works fine except the GET command in the add.php.

Nothing comes in from my Wifi module.

(greekcell with RN171)

Any sug's ?

Regards,

R

0
None
MehmetK59

1 year ago

Hello! First of all, thank you for sharing this instructable! It is very useful and nice for starters such me.

However, can you please help me doing the same things with ESP8266, instead of WiFly?
Thank you in advance!

0
None
robot1972

1 year ago

How to change the PHP code to work with an Access Database?

Thanks!

0
None
Pavisindhu

1 year ago

please help me how to store data from arduino to database in ultrasonic sensor

0
None
Sovatna

2 years ago

Hi,

It does not work with my code. I meant it does not send the data to my local Mysql. Please kindly help. The code is here below:

#include <SPI.h>

#include <Ethernet.h>

#include <String.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

byte ip[] = {192,168,1,110}; //address of arduino

byte server[] = {192,168,1,1};//address of rooter

EthernetClient client;

float sensor = 0;

void setup() {

Serial.begin(115200);

Ethernet.begin(mac, ip);

}

void loop() {

char comend = Serial.read();

if(comend == 'a'){

if(client.connect(server,80)){

Serial.println("connected...");

sensor = sensor + 5;

client.print("GET /arduino/tempTest/templogin.php?");

client.print("sensor=");

client.print(sensor);

Serial.print("Sensor = ");

Serial.print(sensor);

Serial.println();

client.stop();

}else {

Serial.println("Not connected...");

client.stop();

}

}

}

0
None
Amirul92

4 years ago on Step 5

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

6 replies
0
None
camilo.n1012Amirul92

Reply 4 years ago on Step 5

Hi, "your_ip server" is the address where you host the server MySQL. In this case, is "127.0.0.1", to connect the localhost.

0
None
camilo.n1012JitendraR

Reply 3 years ago on Introduction

None of these data will be used , if you are using phpMyAdmin , go to the tab " Estado Actual" ( as from image ) and in the section of server, have the direction of you need.

Captura de pantalla 2015-07-22 a las 11.37.33.png
0
None
camilo.n1012Amirul92

Reply 4 years ago on Introduction

This error is because you have not included the OneWire library, I recommend you download the version 2.0 from here: http://www.pjrc.com/teensy/td_libs_OneWire.html

0
None
RinaldiA2

2 years ago

Hi Camilo i got some problem, i got some error with ur arduino codes. Here the message :

Arduino: 1.6.6 (Windows 10), Board: "Arduino Nano, ATmega328"

In file included from C:\Program Files\Arduino\libraries\WiFly/SpiUart.h:7:0,

from C:\Program Files\Arduino\libraries\WiFly/WiFly.h:4,

from C:\Users\Documents\Arduino\Coba\Coba.ino:8:

C:\Program Files\Arduino\libraries\WiFly/_Spi.h:8:22: fatal error: WProgram.h: No such file or directory

#include <WProgram.h>

^

compilation terminated.

exit status 1

Error compiling.

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences.

0
None
HugoM59

2 years ago

Hi XiongL. I think you need to set IP Address of your PHP Server (Web server + PHP + MySQL). In short means, in your ardunio, you will build a link address like this http://192.168.1.199/add.php?temp1=temp&moi1=sensorValue. Obviously, vars "temp" and "sensorValue" will contain measured values from sensors. In the file add.php you will catch this data and then run code PHP to insert them in MySQL.

0
None
XiongL

2 years ago

hi can tell me

Client client( "your_website", 80 );

your_website what should i put ?

0
None
XiongL

2 years ago

i`m using arduino yun via wifi can use the same code as yours ?

0
None
dnb.dinusha

2 years ago

Hi ,i use this code in arduino but nothin works

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

client.print("54");
client.println( " HTTP/1.1");
client.println( "Host: www.dnb1654.com" );
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
client.println();
client.println();

client.stop();

0
None
Ayeza

2 years ago

Hello,
I need help regarding sending my sensor's values to database. I am unable to send the values instead variable to which i am passing these values is showing me zero at database.