중간 --분할선까지 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µî ¾Ë°í ÀÖ´Â ÇÁ·Î±×·¡¹Ö ¾ð¾î°¡ ´Ü ÇÑ ÀÚµµ ¾ø¾î¼ ¸·¸·ÇÕ´Ï´Ù¤¾¤¾
°ü·Ã¼ÀûÀº ±¸Çص×Áö¸¸ ¾îµð¿¡¼ºÎÅÍ ºÁ¾ßÇÒÁö..¤»¤»
Äڵ常 º¸°í ´Ü¼ø ¸ÓÁö´Â Çغôµ¥.. ÄÄÆÄÀÏ ¿À·ù¶ó´øÁö À̽´¶ó°í »ý°¢µÇ´Â ±¸Á¶ÀûÀÎ ¹®Á¦´Â ´çÀå µµ¿òÀÌ ¾ÈµÇ³×¿ä. 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¹ø ¼Ò½ºÀÇ À¥¼¹öµµ óÀ½¿£ ¿·È´Ù°¡ Á¦¾î¹öÆ° ´©¸£¸é »¸¾î¹ö·Á¼ ¿ì³ëÀÇ ÇÑ°èÀΰÇÁö ±×·± »ý°¢ÀÌ µé±âµµÇÕ´Ï´Ù~