중간 --분할선까지 1번 소스입니다.
/*
PUMP CONTROL SYSTEM
*/
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#define DHTPIN A2 // what pin we're connected the DHT output
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int led4 = 4;
int led2 = 5;
int led3 = 3;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 66 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
void setup() {
dht.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(led4, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
int h = dht.readHumidity();
int t = dht.readTemperature();
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
client.println("<TITLE>PUMP CONTROL SYSTEM</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>PUMP CONTROL SYSTEM</H1>");
client.println("<hr />");
client.println("<br />");
client.println("<H2>169-1</H2>");
client.println("<br />");
client.println("<a href=\"/?button1off\"\">Turn OFF PUMP1</a>");
client.println("<a href=\"/?button1on\"\">Turn ON PUMP1</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?button2off\"\">Turn OFF PUMP2</a>");
client.println("<a href=\"/?button2on\"\">Turn ON PUMP2</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?button3off\"\">Turn OFF PUMP3</a>");
client.println("<a href=\"/?button3on\"\">Turn ON PUMP3</a><br />");
client.println("<center>");
client.println("<h1>");
client.print("FARM_1");
client.println("</h1>");
client.println("<h2>");
client.print("Temperature and Humidity");
client.println("</h2>");
client.println("<h4>");
client.print("Temperature : ");
client.print(t);
client.print("<sup>0</sup>");
client.print("C");
client.println("<br />");
client.print("Humidity : ");
client.print(h);
client.print("%");
client.println("</h4>");
client.println("</center>");client.println("<p>-</p>");
client.println("<br />");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") >0){
digitalWrite(led4, LOW);
}
if (readString.indexOf("?button1off") >0){
digitalWrite(led4, HIGH);
}
if (readString.indexOf("?button2on") >0){
digitalWrite(led2, LOW);
}
if (readString.indexOf("?button2off") >0){
digitalWrite(led2, HIGH);
}
if (readString.indexOf("?button3on") >0){
digitalWrite(led3, HIGH);
}
if (readString.indexOf("?button3off") >0){
digitalWrite(led3, LOW);
}
//clearing string for next read
readString="";
}
}
}
}
}
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2번 소스입니다.
(두 소스간 일부 중복되는 항목을 제외하고 깔끔하게 합치고 싶은데 어디서 어떤 구문이 들어가야할지 모르겠습니다.)
#include "DHT.h" | |
#include <SPI.h> | |
#include <Ethernet.h> | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Setting MAC Address | |
#define DHTPIN 2 | |
#define DHTTYPE DHT11 | |
DHT dht(DHTPIN,DHTTYPE); | |
float humidityData; | |
float temperatureData; | |
char server[] = "<Your Local IP>"; | |
IPAddress ip(192,168,0,177); | |
EthernetClient client; | |
/* Setup for Ethernet and RFID */ | |
void setup() { | |
Serial.begin(9600); | |
dht.begin(); | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
Ethernet.begin(mac, ip); | |
} | |
delay(1000); | |
} | |
//------------------------------------------------------------------------------ | |
/* Infinite Loop */ | |
void loop(){ | |
humidityData = dht.readHumidity(); | |
temperatureData = dht.readTemperature(); | |
Sending_To_phpmyadmindatabase(); | |
delay(30000); // 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="); | |
client.print("GET /testcode/dht.php?humidity="); //YOUR URL | |
Serial.println(humidityData); | |
client.print(humidityData); | |
client.print("&temperature="); | |
Serial.println("&temperature="); | |
client.print(temperatureData); | |
Serial.println(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"); | |
} | |
} |
ü ÷ο Լ ϴ Լ ˸ ġ°ž ٵ...
, ѷ Ÿ Լ, ϴ Լ... ˾Ƶ...
2ҽ ½ Ͱ õǾ ִ DB ϴ ҽԴϴ.
2 ҽ ̴ ϰ loop Ͽ ָ ;µ Ƶ̳ Ұ ֱ..
Ƶ̳볪 php c html ˰ ִ α ڵ մϴ٤
ü ص ..
ڵ常 ܼ غôµ.. ̽ Ǵ ȵdz. http://plus.inhyeok.com/aduino/pump.c
õغ ۸ ! ذغڽϴ~
if (readString.indexOf("?button1on") > 0)
{
digitalWrite(led4, LOW);
}
if (readString.indexOf("?button1off") > 0)
{
digitalWrite(led4, HIGH);
}
⼱ on low off high ε
if (readString.indexOf("?button3on") > 0)
{
digitalWrite(led3, HIGH);
}
if (readString.indexOf("?button3off") > 0)
{
digitalWrite(led3, LOW);
}
3.. on high off low ̳ .. 3 ° .. 1 2 Ʋ ѵ ..
On/offư ǵ鸸 Ȯ Դϴ.
On/off ݴ ۵ϴ°Űϴ ἱغ ϸ ɵմϴ.
2.. 30ʸ ½ ִ°ǰ ..
1 h, t ƿ° Ȱ ..
30ʸ Ƽ . ϴ κи ű Ǵ°ǰ?
̴ dht11 Ͽ ߺǴ Ϻ ̺귯 ְ void յ {}ָ Ѱ ͱ ѵ
κ ΰ ʿѵմϴ
Ʈ غ .. ׳ ¥⸸ ߽ϴ.
2 setup κ ̴ κ dhcp ƿ ׳ ѷִ κΰ ׳ ߱
setinterval ϱ ؼ(30ʸ ) . https://playground.arduino.cc/Code/SimpleTimer/ ϼż .. ̺귯
߰ Ͻø ɰ ϴ.
ð ʾ õغ ̰ ٲ㰡 غ Ʈ ص帮ڽϴ~
PUMP CONTROL SYSTEM
*/
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <SimpleTimer.h>
#define DHTPIN A2 // what pin we're connected the DHT output
#define DHTTYPE DHT11 // DHT 11
// the timer object
SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
int led4 = 4;
int led2 = 5;
int led3 = 3;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //physical mac address
byte ip[] = {192, 168, 0, 66}; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = {192, 168, 0, 1}; // internet access via router
byte subnet[] = {255, 255, 255, 0}; //subnet mask
EthernetServer server(80); //server port
String readString;
void getSensor()
{
int sh = dht.readHumidity();
int st = dht.readTemperature();
Sending_To_phpmyadmindatabase(sh, st);
}
void setup()
{
dht.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(led4, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
timer.setInterval(30000, getSensor);
}
void loop()
{
int h = dht.readHumidity();
int t = dht.readTemperature();
// Create a client connection
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100)
{
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n')
{
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
client.println("<TITLE>PUMP CONTROL SYSTEM</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>PUMP CONTROL SYSTEM</H1>");
client.println("<hr />");
client.println("<br />");
client.println("<H2>169-1</H2>");
client.println("<br />");
client.println("<a href=\"/?button1off\"\">Turn OFF PUMP1</a>");
client.println("<a href=\"/?button1on\"\">Turn ON PUMP1</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?button2off\"\">Turn OFF PUMP2</a>");
client.println("<a href=\"/?button2on\"\">Turn ON PUMP2</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?button3off\"\">Turn OFF PUMP3</a>");
client.println("<a href=\"/?button3on\"\">Turn ON PUMP3</a><br />");
client.println("<center>");
client.println("<h1>");
client.print("FARM_1");
client.println("</h1>");
client.println("<h2>");
client.print("Temperature and Humidity");
client.println("</h2>");
client.println("<h4>");
client.print("Temperature : ");
client.print(t);
client.print("<sup>0</sup>");
client.print("C");
client.println("<br />");
client.print("Humidity : ");
client.print(h);
client.print("%");
client.println("</h4>");
client.println("</center>");
client.println("<p>-</p>");
client.println("<br />");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") > 0)
{
digitalWrite(led4, LOW);
}
if (readString.indexOf("?button1off") > 0)
{
digitalWrite(led4, HIGH);
}
if (readString.indexOf("?button2on") > 0)
{
digitalWrite(led2, LOW);
}
if (readString.indexOf("?button2off") > 0)
{
digitalWrite(led2, HIGH);
}
if (readString.indexOf("?button3on") > 0)
{
digitalWrite(led3, HIGH);
}
if (readString.indexOf("?button3off") > 0)
{
digitalWrite(led3, LOW);
}
//clearing string for next read
readString = "";
}
}
}
}
}
void Sending_To_phpmyadmindatabase(float humidityData, float temperatureData) //CONNECTING WITH MYSQL
{
if (client.connect(server, 80))
{
Serial.println("connected");
// Make a HTTP request:
Serial.print("GET /testcode/dht.php?humidity=");
client.print("GET /testcode/dht.php?humidity="); //YOUR URL
Serial.println(humidityData);
client.print(humidityData);
client.print("&temperature=");
Serial.println("&temperature=");
client.print(temperatureData);
Serial.println(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");
}
}
{
if (client.connect(server, 80))
{
Serial.println("connected");
// Make a HTTP request:
2 ҽ if (client.connect(server, 80)) 'client' was not declared in this scope ϴ.
{} εմϴ.
־ֽð ..
if (client.connect(server, 80))
κп
EthernetClient client;
ֽð Ͻðų
EthernetClient client; loop ٱ ż Ͻø Ǵµ .. ׳
ʿ EthernetClient client; ߰ ϼż ø ɰ ϴ.
ϸ鼭 Ǹ Դ ư
loop client ϸ.. loop մϴ.
Լ
Sending_To_phpmyadmindatabase Լ ʿ client Դϴ.
ip .. ҽ2 ϴ ҽ ƴѰ ?
char server[] = "<Your Local IP>";
̷ ip · öִ .. ȴµ ۵ Ѵٸ
String readString;
Ʒʿ
char dbserver = "192.168.1.x" -> ip
ְ .. void Sending_To_phpmyadmindatabase Լ
if (client.connect(server, 80))
..
if (client.connect(dbserver, 80))
ּ.
뿩 ٰ ް ֹ߽ϴ~!
õõ ٽ غڽϴ٤
1 ҽ ó ȴٰ ư Ѱΰ մϴ~