Arduino内部网页代理,网页穿透,公网访问Arduino内部网页
Arduino内部网页代理,网页穿透,公网访问Arduino内部网页项目地址:https://github.com/IoTServ/esp8266-Arduino-web-proxy/tree/master
看下效果:
好了上代码:
#include <ESP8266WiFi.h>
const char* id = "id";//http://www.mcunode.com/proxy/id/LEDif id==4567then url:http://www.mcunode.com/proxy/4567/LED
const char* ssid = "ssid";
const char* password = "password";
int ledPin = 13;
const char* host = "www.mcunode.com";
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 8001;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
if (client.connected())
{
client.write(id);
delay(1000);
while (1)
{
String request = client.readStringUntil('\r');
Serial.print(request);
client.flush();
int value = LOW;
if (request.indexOf("/LED=ON") != -1){
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(ledPin, LOW);
value = LOW;
}
Serial.println(value);
if(value == HIGH) {
client.print("<h1> ESP8266 Arduino Web Server</h1>Led pin is now:on <br><br><a href=\"LED=ON\"\"><button>Turn On </button></a><a href=\"LED=OFF\"\"><button>Turn Off </button></a><br />");
} else {
client.print("<h1> ESP8266 Arduino Web Server</h1>Led pin is now:off<br><br><a href=\"LED=ON\"\"><button>Turn On </button></a><a href=\"LED=OFF\"\"><button>Turn Off </button></a><br />");
}
}
}
}
自己要运行需要修改2,3,4行,id为自己自定义的字符串,3,4行的wifi名和密码自己设置,要是esp8266 Arduino可以连上的,运行成功后访问:
http://www.mcunode.com/proxy/yourid/LED其中yourid换成你在Arduino程序第二行自定义的id 加了一个串口提示公网URL
#include <ESP8266WiFi.h>
const char* id = "id";//http://www.mcunode.com/proxy/id/LEDif id==4567then url:http://www.mcunode.com/proxy/4567/LED
const char* ssid = "ssid";
const char* password = "password";
int ledPin = 13;
const char* host = "www.mcunode.com";
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Please open http://www.mcunode.com/proxy/");
Serial.print(id);
Serial.println("/LED");
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 8001;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
if (client.connected())
{
client.write(id);
delay(1000);
while (1)
{
String request = client.readStringUntil('\r');
Serial.print(request);
client.flush();
int value = LOW;
if (request.indexOf("/LED=ON") != -1){
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(ledPin, LOW);
value = LOW;
}
Serial.println(value);
if(value == HIGH) {
client.print("<h1> ESP8266 Arduino Web Server</h1>Led pin is now:on <br><br><a href=\"LED=ON\"\"><button>Turn On </button></a><a href=\"LED=OFF\"\"><button>Turn Off </button></a><br />");
} else {
client.print("<h1> ESP8266 Arduino Web Server</h1>Led pin is now:off<br><br><a href=\"LED=ON\"\"><button>Turn On </button></a><a href=\"LED=OFF\"\"><button>Turn Off </button></a><br />");
}
}
}
} 这个厉害。有空研究研究。 不知道是否需要公网固定IP? haguna 发表于 2017-4-24 23:44
不知道是否需要公网固定IP?
估计不是公网IP,用的代理转发 还是物联网云平台,何来网页穿透。实际上在路由器做个端口转发,再搞个动态域名就好了,就算没有动态域名现在路由器多数都是常开,ip不变,直接ip访问就好,用不着云服务 :lol文章的意思就是靠网站转发,好像网站当了,不稳定啊 支持一下支持一下支持 Ansifa 发表于 2017-5-10 02:14
文章的意思就是靠网站转发,好像网站当了,不稳定啊
自己可以搭一个这个网站:https://github.com/IoTServ/McuNode-server Vast 发表于 2017-5-10 16:08
支持一下支持一下支持
完全实现mcunode.com的服务端:https://github.com/IoTServ/McuNode-server 运行之后访问服务器的ip就行,也有安卓版服务器(没错,网站跑在安卓手机上),git大小限制,没传,而且是初始版本
页:
[1]