DHT11 DATA IN ANDROID

OVERALL PROCEDURE

Step 1: DHT-11 is interfaced with the Arduino.

Step 2 : Arduino sends data to a php file putdata.php which writes data in a text file in json format .

Step 3 : In Android when the button get values is pressed the android calls a php file getdata.php which reads the text file and sends it to android.

Step 4 : Then android extracts the information in the json format and puts it in the required textboxes.

1.ARDUINO PART :

ARDUINO            NODEMCU
5v          -      5v
Gnd         -      Gnd
D4          -      Data pin

Upload the following program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<DHT.h>
#include <ESP8266WiFi.h>	
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "Username";//Username
const char* password = "Password";//Password
 
const char* host = "";//Webaddress
 
DHT dht(DHTPIN,DHTTYPE);
 
void setup() {
 Serial.begin(9600);
 delay(10);
 
// We start by connecting to a WiFi network
 
Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 
 WiFi.begin(ssid, password);
 
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 
Serial.println("");
 Serial.println("WiFi connected"); 
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
}
 
int value = 69;
 
void loop() {
 delay(5000);
 ++value;
static boolean data_state = false; 
int temperature = dht.readTemperature(); 
 int humidity = dht.readHumidity(); 
 Serial.print("Temperature Value is :"); 
 Serial.print(temperature); 
 Serial.println("C"); 
 Serial.print("Humidity Value is :"); 
 Serial.print(humidity); 
 Serial.println("%");
 
Serial.print("connecting to ");
 Serial.println(host);
 
 // Use WiFiClient class to create TCP connections
 WiFiClient client;
 const int httpPort = 80;
 if (!client.connect(host, httpPort)) {
 Serial.println("connection failed");
 return;
 }
 //temperature=40;
 //humidity=79;
 // We now create a URI for the request
 String url = "/putdata.php";
 url += "?temp=";
 url += temperature;
 url += "&amp;hum=";
 url += humidity;
 
 Serial.print("Requesting URL: ");
 Serial.println(url);
 
 // This will send the request to the server
 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
 "Host: " + host + "\r\n" + 
 "Connection: close\r\n\r\n");
 unsigned long timeout = millis();
 while (client.available() == 0) {
 if (millis() - timeout &gt; 5000) {
 Serial.println("&gt;&gt;&gt; Client Timeout !");
 client.stop();
 return;
 }
 }
 
 // Read all the lines of the reply from server and print them to Serial
 while(client.available()){
 String line = client.readStringUntil('\r');
 Serial.print(line);
 }
 
 Serial.println();
 Serial.println("closing connection");
}
const char* ssid = "Username"; const char* password = "Password";
  • Change the above lines with your Wi-Fi Username and Password.
  • Make sure that you select the correct COM port.
const char* host = "";//Webaddress
  • Change it with your webaddress after creating one shown below.

2.SERVER PART :

There are two possibilities for server. They are

  •           Localhost
  •           Online server

In Localserver like xampp the data can be accessed only connected in same network. If you are interested installing localserver click here.

In myproject Iam using onlinefree server namely 000webhost.com. In my video below I will explain how to create a free server and upload all the files in it .

PUTDATA.PHP
1
2
3
4
5
6
7
8
9
10
11
<!--?php //GETting values $var1 = $_GET['temp']; $var2 = $_GET['hum']; $v1=(string)$var1; $v2=(string)$var2; echo $var1; echo $var2; $data=array('temp' =&gt; $v1,'hum' =&gt;$v2);
$file=fopen("value.text","w");
fwrite($file,json_encode($data));
fclose($file);
 
$dt = new DateTime('now', new DateTimezone('Asia/Kolkata'));
$date= $dt-&gt;format('F j, Y');
$time= $dt-&gt;format('g:i a');
echo $date;
echo $time;
?-->
GETDATA.PHP
1
<!--?php $res=file_get_contents('value.text'); echo $res; ?-->


ANDROID PART:

I have made a app specially for this project where it is enough if you specify the webaddress and click Get the values the temperature and humidity data will be displayed.im1

                                 Thankyou
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

1 Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.