|
|
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Ethernet.h>
int tempPin = 2;//wendu
float temperature = 0;
int wetPin = 3;//shidu
float wet = 0;
int deepPin =1;//shenidu
float deep = 0;
class function{
public:
void sendHTML(EthernetClient client);
void sendHTML2(EthernetClient client);
void sendHTML21(EthernetClient client);
void sendHTML3(EthernetClient client);
void sendHTML4(EthernetClient client);
};
function f;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,111);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80);
LiquidCrystal_I2C lcd(0x27,20,4);
void setup()
{
pinMode( 2 , OUTPUT);//wendu
pinMode( 3 , OUTPUT);//shidu
pinMode( 4 , OUTPUT);//shendu
lcd.init();
lcd.backlight();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip,gateway,subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.begin(9600);
}
void loop ()
{
EthernetClient client = server.available();
tempTest();
wetTest();
deepTest();
lcd.setCursor(2,0);
lcd.print("Temp:");
lcd.setCursor(8,0);
lcd.print(temperature);
lcd.setCursor(4,1);
lcd.print("RH:");
lcd.setCursor(8,1);
lcd.print(wet);
lcd.setCursor(2,2);
lcd.print("Deep:");
lcd.setCursor(8,2);
lcd.print(deep);
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
f.sendHTML(client);
f.sendHTML2(client);
f.sendHTML21(client);
f.sendHTML3(client);
f.sendHTML4(client);
String POST = "";
while(client.available()){
c = client.read();
// save the variables somewhere
POST += c;
}//while
if(POST !=""){
Serial.println(POST);
char webCmd = POST[POST.length()-1];
executeCmd(webCmd,client);
}
break;
} //if (c == '\n' && currentLineIsBlank)
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}//if (client.available())
}//while (client.connected())
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}//if(client)
delay(500);
}
void executeCmd(char cmd ,EthernetClient client){
switch (cmd){
case 1:Serial.println("cccc");break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
default:break;
}
client.println("<script language='javascript'>alert('Done Successfully!')</script>");
}
void tempTest(){
long valtemp = 0;
valtemp=analogRead(tempPin);
temperature = (valtemp*0.0048828125*100);
Serial.print("Tep= ");
Serial.print(temperature);
Serial.println(" C");
if ( temperature > 25 )
{
digitalWrite( 2 , HIGH );
}
else
{
digitalWrite( 2 , LOW );
}
}
void wetTest(){
float valwet = 0;
valwet=analogRead(wetPin);
wet = (valwet*0.0048828125*100);
Serial.print("shidu= ");
Serial.println(wet);
if ( wet > 23 )
{
digitalWrite( 3 , LOW );
}
else
{
digitalWrite( 3, HIGH );
}
}
void deepTest(){
long valdeep = 0;
valdeep=analogRead(deepPin);
deep = valdeep/100;
Serial.print("Deep= ");
Serial.print(deep);
Serial.println(" cm");
if ( deep > 8 )
{
digitalWrite( 4 , LOW );
}
else
{
digitalWrite( 4, HIGH );
}
}
void function::sendHTML(EthernetClient client){
client.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
client.println("<head><meta http-equiv='Content-Type' content='text/html; charset=gb2312' />");
client.println("<head><meta http-equiv='refresh' content='20'/>");
client.println("<title>Smart vagetables shed management system</title></head>");
client.println("<style type='text/css'>#titletext {position: absolute;");
client.println("font-family: 'Times New Roman', Times, serif;");
client.println("font-size: 40px;top: 38px;left: 300px;}");
client.println("#tablee{position:absolute;");
client.println("font-family: 'Times New Roman', Times, serif;");
client.println(" top: 189px;left: 445px;}");
client.println(" #formm{position:absolute;");
client.println("font-family: 'Times New Roman', Times, serif;");
client.println("top: 250px;left: 445px;}</style>");
}
void function::sendHTML2(EthernetClient client){
client.println("<body>");
client.println("<div id='titletext'> <font id='biaoti' color='#000000'>");
client.println("<b>Smart vagetables shed management");
}
void function::sendHTML21(EthernetClient client){
client.println("</b></font></div>");
client.print("<div id='tablee'>");
client.println("<table border=' green ''3px' id='info'>");
client.println("<tr><td><font color='#FF00CC'>Temp:</font></td>");
client.println("<td><font color='#FF00CC'>NN</font></td> </tr>");
client.println("<tr> <td><font color='#FF00CC'>HR:</font></td>");
client.println("<td><font color='#FF00CC'>NN</font></td> </tr>");
}
void function::sendHTML3(EthernetClient client){
client.println("<tr> <td><font color='#FF00CC'>Deep:</font></td>");
client.println("<td><font color='#FF00CC'>NN</font></td></tr></table></div>");
client.println("<div id='formm'>");
client.println("<form action='http://192.168.1.111:80' method='post'>");
client.println("<input type='hidden' name='cmd' value='1' /><br /><br />");
}
void function::sendHTML4(EthernetClient client){
client.println("<input type='submit' value='OPEN...' /></form>");
client.println("<form action='' method='post'>");
client.println("<input type='hidden' name='cmd' value='2' />");
client.println("<input type='submit' value='CLOSE...' /></form></div></body></html>");
}
|
|