Php wemos d1(作为客户端)和Web服务器(在arduino或wemos d1上)之间通过LAN进行通信
所以我想做一些家庭自动化,所以我给家里的每个房间都分配了一个esp8266,所有房间都连接到一个wifi作为客户端,具有静态ip地址,所以我可以通过向每个esps ip发送get请求来控制我的灯光,所以我的问题是,我想让一个Web服务器运行在arduino或esp8266上,这样它就可以保存房子所有灯光的状态。我可以在wammp上轻松地执行此操作,php将处理客户端esp发出的所有get请求并将数据保存在数据库中,但Web服务器(在esp或arduino上)可以处理这些get请求并将状态保存在一些内存中。是的,可以使用arduino运行Web服务器。这是我们的例子Php wemos d1(作为客户端)和Web服务器(在arduino或wemos d1上)之间通过LAN进行通信,php,c++,http,arduino,arduino-esp8266,Php,C++,Http,Arduino,Arduino Esp8266,所以我想做一些家庭自动化,所以我给家里的每个房间都分配了一个esp8266,所有房间都连接到一个wifi作为客户端,具有静态ip地址,所以我可以通过向每个esps ip发送get请求来控制我的灯光,所以我的问题是,我想让一个Web服务器运行在arduino或esp8266上,这样它就可以保存房子所有灯光的状态。我可以在wammp上轻松地执行此操作,php将处理客户端esp发出的所有get请求并将数据保存在数据库中,但Web服务器(在esp或arduino上)可以处理这些get请求并将状态保存在一
/*
网络服务器
一个简单的web服务器,显示模拟输入引脚的值。
使用Arduino Wiznet以太网屏蔽。
线路:
*连接到引脚10、11、12、13的以太网屏蔽
*连接至引脚A0至A5的模拟输入(可选)
创建于2009年12月18日
大卫·A·梅利斯
2012年4月9日修改
汤姆·伊戈
2015年9月2日修改
阿图罗·瓜达卢皮
*/
#包括
#包括
//在下面输入控制器的MAC地址和IP地址。
//IP地址将取决于您的本地网络:
字节mac[]={
0xDE、0xAD、0xBE、0xEF、0xFE、0xED
};
ip地址ip(192、168、1177);
//初始化以太网服务器库
//具有要使用的IP地址和端口
//(HTTP的默认端口为80):
以太网络服务器(80);
无效设置(){
//打开串行通信并等待端口打开:
Serial.begin(9600);
而(!串行){
;//等待串行端口连接。仅本机USB端口需要
}
//启动以太网连接和服务器:
以太网开始(mac,ip);
server.begin();
串行打印(“服务器位于”);
Serial.println(Ethernet.localIP());
}
void循环(){
//侦听传入的客户端
EthernetClient=server.available();
如果(客户){
Serial.println(“新客户”);
//http请求以一个空行结束
布尔currentLineIsBlank=true;
while(client.connected()){
if(client.available()){
char c=client.read();
串行写入(c);
//如果你已经到了这一行的末尾(收到了一条新行)
//字符)且该行为空,则http请求已结束,
//所以你可以发送回复
如果(c='\n'&¤tLineIsBlank){
//发送标准http响应头
client.println(“HTTP/1.1200ok”);
client.println(“内容类型:text/html”);
client.println(“Connection:close”);//响应完成后将关闭连接
client.println(“Refresh:5”);//每5秒自动刷新一次页面
client.println();
客户。println(“”);
客户。println(“”);
//输出每个模拟输入引脚的值
对于(int-analogChannel=0;analogChannel<6;analogChannel++){
int sensorReading=模拟读取(模拟通道);
客户端打印(“模拟输入”);
客户端打印(模拟频道);
客户。打印(“is”);
客户端。打印(sensorReading);
client.println(“
”);
}
客户。println(“”);
打破
}
如果(c=='\n'){
//你在开一条新的生产线
currentLineIsBlank=true;
}如果(c!='\r'),则为else{
//当前行中有一个角色
currentLineIsBlank=false;
}
}
}
//为web浏览器提供接收数据的时间
延迟(1);
//关闭连接:
client.stop();
Serial.println(“客户端断开”);
}
}
将状态保存在全局变量中,并在setup()
中初始化它们。当客户机发送其请求时,它必须发送其id或ip,服务器可以使用状态进行响应
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
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 you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}