-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdht-mysql.ino
91 lines (73 loc) · 2.18 KB
/
dht-mysql.ino
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
/* This arduino code is sending data to mysql server every 30 seconds.
Created By Embedotronics Technologies*/
#include "DHT.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SPI.h>
#define DHTPIN D2
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
float humidityData;
float temperatureData;
const char* ssid = "맞는 것으로 바꾸세요";//
const char* password = "맞는 것으로 바꾸세요";
//WiFiClient client;
char server[] = "맞는 것으로 바꾸세요"; //eg: 192.168.0.222
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();
// Connect to 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");
// Start the server
// server.begin();
Serial.println("Server started");
Serial.print(WiFi.localIP());
delay(1000);
Serial.println("connecting...");
}
void loop()
{
humidityData = dht.readHumidity();
temperatureData = dht.readTemperature();
Sending_To_phpmyadmindatabase();
delay(5000); // interval
}
void Sending_To_phpmyadmindatabase() //CONNECTING WITH MYSQL
{
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
Serial.print("GET /testcode/dht.php?humidity=");
Serial.println(humidityData);
Serial.println("&temperature=");
Serial.println(temperatureData);
client.print("GET http://localhost/testcode/dht.php?humidity="); //YOUR URL
client.print(humidityData);
client.print("&temperature=");
client.print(temperatureData);
client.print(" "); //SPACE BEFORE HTTP/1.1
client.print("HTTP/1.1");
client.println();
client.println("Host: Your Local IP");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}