Asp.net mvc ESP8266代码工作不正常
这是用于打开/关闭LED的ESP8266代码。我使用的是ArduinoIDE内置示例代码。LED正常工作,但我想向本地托管的网站(发送电子邮件)发送HTTP请求,但它不工作Asp.net mvc ESP8266代码工作不正常,asp.net-mvc,arduino,localhost,iot,esp8266,Asp.net Mvc,Arduino,Localhost,Iot,Esp8266,这是用于打开/关闭LED的ESP8266代码。我使用的是ArduinoIDE内置示例代码。LED正常工作,但我想向本地托管的网站(发送电子邮件)发送HTTP请求,但它不工作 已连接到我的本地Wifi 分配了一个静态IP地址 当我点击192.168.1.101/gpio/1时(Led亮起) 当我点击192.168.1.101/gpio/0(Led熄灭)时,它正在工作,但无法点击我的网站 当我点击192.168.1.101/gpio/1时,它应该点击我的本地托管URL(192.168.1.100/h
#include <ESP8266WiFi.h>
const char* ssid = "SMART";
const char* password = "123456789";
const char* host = "192.168.1.100"; // Your domain
IPAddress ip(192, 168, 1, 101); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO3
pinMode(3, OUTPUT);
digitalWrite(3, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip,gateway,subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1) {
val = 0;
if (client.connect(host, 80)) {
//starts client connection, checks for connection
Serial.println("connected to website/server");
client.println("GET /home/ardchk HTTP/1.1"); //Send data
client.println("Host: 192.168.1.100");
Serial.println("Email Sended");
client.println("Connection: close");
//close 1.1 persistent connection
client.println(); //end of get request
}
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
} else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(3, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}
#包括
const char*ssid=“智能”;
const char*password=“123456789”;
const char*host=“192.168.1.100”;//你的领域
ip地址ip(192、168、1、101);//其中xx是所需的IP地址
IP地址网关(192、168、1、1);//设置网关以匹配您的网络
IP地址子网(255、255、255、0);
WiFiServer服务器(80);
无效设置(){
序列号开始(115200);
延迟(10);
//制备GPIO3
pinMode(3,输出);
数字写入(3,0);
//连接到WiFi网络
Serial.println();
Serial.println();
串行打印(“连接到”);
序列号println(ssid);
WiFi.config(ip、网关、子网);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“WiFi连接”);
//启动服务器
server.begin();
Serial.println(“服务器已启动”);
//打印IP地址
Serial.println(WiFi.localIP());
}
void循环(){
//检查客户端是否已连接
WiFiClient=server.available();
如果(!客户端){
返回;
}
//等待客户端发送一些数据
Serial.println(“新客户”);
而(!client.available()){
延迟(1);
}
//阅读请求的第一行
String req=client.readStringUntil('\r');
串行打印LN(req);
//符合要求
int-val;
如果(请求索引(“/gpio/0”)!=-1){
val=0;
if(客户端连接(主机,80)){
//启动客户端连接,检查连接
Serial.println(“连接到网站/服务器”);
client.println(“GET/home/ardchk HTTP/1.1”);//发送数据
client.println(“主机:192.168.1.100”);
Serial.println(“发送电子邮件”);
client.println(“连接:关闭”);
//关闭1.1永久连接
client.println();//get请求结束
}
}else if(请求索引(“/gpio/1”)!=-1){
val=1;
}否则{
Serial.println(“无效请求”);
client.stop();
返回;
}
//根据请求设置GPIO2
数字写入(3,val);
client.flush();
//准备答复
String s=“HTTP/1.1 200确定\r\n内容类型:text/html\r\n\r\n\r\n\r\nGPIO现在是”;
s+=(val)?“高”:“低”;
s+=“\n”;
//将响应发送到客户端
客户。打印件;
延迟(1);
Serial.println(“客户端断开”);
//客户端实际上将断开连接
//当函数返回且“客户端”对象被销毁时
}
根据您的问题/评论,我假设client.connect(主机,80)
返回false
我相信您无法连接到您的主机
,因为您正在尝试两次连接同一客户端
您的代码如下所示:
// returns an already connected client, if available
WiFiClient client = server.available()
//...
if (client.connect(host, 80)) {/*...*/}
您可以看到,您正在使用已连接的客户端尝试连接到主机。相反,请尝试为该作业创建单独的WiFiClient
:
WiFiClient requesting_client = server.available();
//...
if (req.indexOf("/gpio/0") != -1) {
val = 0;
// create a fresh, new client
WiFiClient emailing_client;
if (emailing_client.connect(host, 80)) {
// ...
// don't forget that you need to close this client as well!
emailing_client.stop();
}
希望这有帮助 根据您的问题/评论,我假设
client.connect(主机,80)
返回false
我相信您无法连接到您的主机
,因为您正在尝试两次连接同一客户端
您的代码如下所示:
// returns an already connected client, if available
WiFiClient client = server.available()
//...
if (client.connect(host, 80)) {/*...*/}
您可以看到,您正在使用已连接的客户端尝试连接到主机。相反,请尝试为该作业创建单独的WiFiClient
:
WiFiClient requesting_client = server.available();
//...
if (req.indexOf("/gpio/0") != -1) {
val = 0;
// create a fresh, new client
WiFiClient emailing_client;
if (emailing_client.connect(host, 80)) {
// ...
// don't forget that you need to close this client as well!
emailing_client.stop();
}
希望这有帮助 请解释当你尝试点击url时会发生什么。“不工作”是不清楚的。你需要学习拼写(随机大写字母不能算是正确的拼写),并正确缩进你的代码。幸运的是,ArduinoIDE为您完成了这项工作:按ctrl-T…代码块:if(client.connect(host,80)){由于未连接到主机,因此此代码未运行}即使网站是在同一个网络上托管的,请解释当你们试图点击url时会发生什么。“不工作”是不清楚的。你需要学习拼写(随机大写字母不能算是正确的拼写),并正确缩进你的代码。幸运的是,ArduinoIDE为您完成了这项工作:按ctrl-T…代码块:if(client.connect(host,80)){由于未连接到主机,因此此代码未运行}甚至网站也托管在同一个网络上