极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 20470|回复: 9

Arduino内部网页代理,网页穿透,公网访问Arduino内部网页

[复制链接]
发表于 2017-4-24 17:36:10 | 显示全部楼层 |阅读模式
Arduino内部网页代理,网页穿透,公网访问Arduino内部网页
项目地址:https://github.com/IoTServ/esp8266-Arduino-web-proxy/tree/master
看下效果:

好了上代码:
  1. #include <ESP8266WiFi.h>
  2. const char* id     = "id";  //http://www.mcunode.com/proxy/id/LED  if id==4567  then url:http://www.mcunode.com/proxy/4567/LED
  3. const char* ssid     = "ssid";
  4. const char* password = "password";
  5. int ledPin = 13;
  6. const char* host = "www.mcunode.com";

  7. void setup() {
  8.   Serial.begin(115200);
  9.   delay(10);

  10.   Serial.println();
  11.   Serial.println();
  12.   Serial.print("Connecting to ");
  13.   Serial.println(ssid);
  14.   
  15.   WiFi.mode(WIFI_STA);
  16.   WiFi.begin(ssid, password);
  17.   
  18.   while (WiFi.status() != WL_CONNECTED) {
  19.     delay(500);
  20.     Serial.print(".");
  21.   }

  22.   Serial.println("");
  23.   Serial.println("WiFi connected");  
  24.   Serial.println("IP address: ");
  25.   Serial.println(WiFi.localIP());
  26. }

  27. int value = 0;

  28. void loop() {
  29.   delay(5000);
  30.   ++value;

  31.   Serial.print("connecting to ");
  32.   Serial.println(host);
  33.   
  34.   WiFiClient client;
  35.   const int httpPort = 8001;
  36.   if (!client.connect(host, httpPort)) {
  37.     Serial.println("connection failed");
  38.     return;
  39.   }


  40. if (client.connected())
  41. {
  42.   client.write(id);
  43.   delay(1000);
  44.   while (1)
  45.   {

  46.   String request = client.readStringUntil('\r');
  47.   Serial.print(request);
  48.   client.flush();

  49.   int value = LOW;
  50.   if (request.indexOf("/LED=ON") != -1)  {
  51.     digitalWrite(ledPin, HIGH);
  52.     value = HIGH;
  53.   }
  54.   if (request.indexOf("/LED=OFF") != -1)  {
  55.     digitalWrite(ledPin, LOW);
  56.     value = LOW;
  57.   }
  58. Serial.println(value);


  59.   if(value == HIGH) {
  60.     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 />");
  61.   } else {
  62.     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 />");
  63.   }
  64.   
  65.   }
  66. }
  67. }
复制代码


自己要运行需要修改2,3,4行,id为自己自定义的字符串,3,4行的wifi名和密码自己设置,要是esp8266 Arduino可以连上的,运行成功后访问:
http://www.mcunode.com/proxy/yourid/LED  其中yourid换成你在Arduino程序第二行自定义的id

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2017-4-24 17:57:23 | 显示全部楼层
加了一个串口提示公网URL
  1. #include <ESP8266WiFi.h>
  2. const char* id     = "id";  //http://www.mcunode.com/proxy/id/LED  if id==4567  then url:http://www.mcunode.com/proxy/4567/LED
  3. const char* ssid     = "ssid";
  4. const char* password = "password";
  5. int ledPin = 13;
  6. const char* host = "www.mcunode.com";

  7. void setup() {
  8.   Serial.begin(115200);
  9.   delay(10);

  10.   Serial.println();
  11.   Serial.println();
  12.   Serial.print("Connecting to ");
  13.   Serial.println(ssid);
  14.   
  15.   WiFi.mode(WIFI_STA);
  16.   WiFi.begin(ssid, password);
  17.   
  18.   while (WiFi.status() != WL_CONNECTED) {
  19.     delay(500);
  20.     Serial.print(".");
  21.   }

  22.   Serial.println("");
  23.   Serial.println("WiFi connected");  
  24.   Serial.println("IP address: ");
  25.   Serial.println(WiFi.localIP());
  26.   Serial.print("Please open http://www.mcunode.com/proxy/");
  27.   Serial.print(id);
  28.   Serial.println("/LED");
  29. }

  30. int value = 0;

  31. void loop() {
  32.   delay(5000);
  33.   ++value;

  34.   Serial.print("connecting to ");
  35.   Serial.println(host);
  36.   
  37.   WiFiClient client;
  38.   const int httpPort = 8001;
  39.   if (!client.connect(host, httpPort)) {
  40.     Serial.println("connection failed");
  41.     return;
  42.   }

  43. if (client.connected())
  44. {
  45.   client.write(id);
  46.   delay(1000);
  47.   while (1)
  48.   {

  49.   String request = client.readStringUntil('\r');
  50.   Serial.print(request);
  51.   client.flush();

  52.   int value = LOW;
  53.   if (request.indexOf("/LED=ON") != -1)  {
  54.     digitalWrite(ledPin, HIGH);
  55.     value = HIGH;
  56.   }
  57.   if (request.indexOf("/LED=OFF") != -1)  {
  58.     digitalWrite(ledPin, LOW);
  59.     value = LOW;
  60.   }
  61. Serial.println(value);

  62.   if(value == HIGH) {
  63.     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 />");
  64.   } else {
  65.     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 />");
  66.   }
  67.   
  68.   }
  69. }
  70. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2017-4-24 23:43:41 | 显示全部楼层
这个厉害。有空研究研究。
回复 支持 反对

使用道具 举报

发表于 2017-4-24 23:44:38 | 显示全部楼层
不知道是否需要公网固定IP?
回复 支持 反对

使用道具 举报

发表于 2017-4-25 13:09:58 | 显示全部楼层
haguna 发表于 2017-4-24 23:44
不知道是否需要公网固定IP?

估计不是公网IP,用的代理转发
回复 支持 反对

使用道具 举报

发表于 2017-4-26 00:02:40 | 显示全部楼层
还是物联网云平台,何来网页穿透。实际上在路由器做个端口转发,再搞个动态域名就好了,就算没有动态域名现在路由器多数都是常开,ip不变,直接ip访问就好,用不着云服务
回复 支持 反对

使用道具 举报

发表于 2017-5-10 02:14:49 | 显示全部楼层
文章的意思就是靠网站转发,好像网站当了,不稳定啊
回复 支持 反对

使用道具 举报

发表于 2017-5-10 16:08:44 | 显示全部楼层
支持一下支持一下支持
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-11 10:45:25 | 显示全部楼层
Ansifa 发表于 2017-5-10 02:14
文章的意思就是靠网站转发,好像网站当了,不稳定啊

自己可以搭一个这个网站:https://github.com/IoTServ/McuNode-server
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-11 10:47:16 | 显示全部楼层
Vast 发表于 2017-5-10 16:08
支持一下支持一下支持

完全实现mcunode.com的服务端:https://github.com/IoTServ/McuNode-server 运行之后访问服务器的ip就行,也有安卓版服务器(没错,网站跑在安卓手机上),git大小限制,没传,而且是初始版本
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 05:24 , Processed in 0.048716 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表