对之前单机模式 只能用温度控制舵机 进行了更新 加入新的模块--网络模块
可以通过手机端和电脑端 访问自己连在同一个局域网下的 w5100
Turn on 打开 舵机旋转角度在90度 按钮变成Turn off 可以再关掉舵机 归位旋转到0度
硬件方面 与第一版多了w5100 模块 其他连线同第一版
大致思路:使用adruino 的网络模块 <Ethernet.h> 作为服务器端 等待手机或其他同一局域网下的电脑客户端访问
客户端收到回复 得到网页 如图1 就可以访问了
贴出代码:
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
} //摄氏温度度转化为华氏温度
double Kelvin(double celsius)
{
return celsius + 273.15;
} //摄氏温度转化为开氏温度
// 露点(点在此温度时,空气饱和并产生露珠)
// 参考: http://wahiduddin.Net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// 快速计算露点,速度是5倍dewPoint()
// 参考: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
#include <Servo.h>
#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,106);
EthernetServer server(80);
EthernetClient client;
String readString= "" ;
dht11 DHT11;
int Sensor=A0;
#define DHT11PIN 2
Servo servo_pin_3;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print( "Server is at " );
Serial.println(Ethernet.localIP());
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
servo_pin_3.attach(3);
}
void loop()
{
static int a=0;
static int b=0;
int chk = DHT11.read(DHT11PIN);
client = server.available();
if(client)
{
Serial.println( "new client" );
boolean currentLineIsBlank = false ;
while (client.connected()) {
if (client.available()) {
// client.write((float)DHT11.temperature);
char c = client.read();
readString += c;
if (c == '\n' ) {
Serial.println(readString);
Serial.println((float)DHT11.temperature, 2);
Serial.println((float)DHT11.humidity, 2);
//检查收到的信息中是否有”on”,有则开
if (readString.indexOf( "?on" ) >0||(float)DHT11.temperature>30) {
a=90;
servo_pin_3.write( a );
delay(1000);
Serial.println( "Duo ji On" );
break ;
}
//检查收到的信息中是否有”off”,有则关灯
if (readString.indexOf( "?off" ) >0) {
servo_pin_3.write( b );
delay(1000);
Serial.println( "Duoji Off" );
break ;
}
SendHTML();
break;
}
}
}
delay(1000);
client.stop();
Serial.println( "client disonnected" );
readString= "" ;
}
}
void SendHTML()
{
client.println( "HTTP/1.1 200 OK" );
client.println( "Content-Type: text/html" );
client.println( "Connection: close" );
client.println();
client.println( "<!DOCTYPE HTML>" );
client.println( "<html><head><meta charset=\"UTF-8\"><title>OpenJumper!Arduino Web Server</title><script type=\"text/javascript\">" );
client.println( "function send2arduino(){var xmlhttp;if (window.XMLHttpRequest)xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");element=document.getElementById(\"light\");if (element.innerHTML.match(\"Turn on\")){element.innerHTML=\"Turn off\"; xmlhttp.open(\"GET\",\"?on\",true);}else{ element.innerHTML=\"Turn on\";xmlhttp.open(\"GET\",\"?off\",true); }xmlhttp.send();}" );
client.println( "</script>" );
client.println( "</head><body><div align=\"center\"><h1>Arduino Web Server</h1><div>brightness:</div><div id=\"brightness\">" );
client.println( "</div><button id=\"light\" type=\"button\" οnclick=\"send2arduino()\">Turn on</button><button type=\"button\" οnclick=\"alert('OpenJumper Web Server')\">About</button></div></body></html>" );
}
转载注明出处。