找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2632|回复: 4
收起左侧

ardunio仿真实现网页控制广告灯闪烁程序

[复制链接]
ID:744206 发表于 2020-5-5 09:50 | 显示全部楼层 |阅读模式
效果说明:HTML制作一个简易网页,网页上有两个按钮ON,OFF。
建立Arduino和所制作的网页之间的通讯,但按下ON时,广告灯闪烁,当按下OFF时,广告灯熄灭。
电路图:
图片1.png
代码:
  1. #include<EtherCard.h>
  2. #include<SPI.h>
  3. static byte myip[] = {192,168,43,21};//设置本机同网段ip【根据你自己的IP设置】
  4. static byte gwip[] = {192, 168, 43, 1};
  5. static byte mymac[] = {0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0x95};
  6. byte Ethernet::buffer[700];
  7. BufferFiller bfill;
  8. int LED_PIN[] = {2, 3, 4, 5};
  9. int cnt = 0;
  10. boolean flag = false;
  11. char *on = "ON";
  12. char *off = "OFF";
  13. char *statusLabel;
  14. char *buttonLabel;
  15. void light_on()//设置开灯
  16. {
  17.     if (cnt % 2 == 0)
  18.     {
  19.         for (int i = 0; i < 4; i++)
  20.         {
  21.             digitalWrite(LED_PIN[ i], HIGH);
  22.             delay(100);
  23.         }
  24.     }
  25.     else
  26.     {
  27.         for (int i = 0; i < 4; i++)
  28.         {
  29.             digitalWrite(LED_PIN[ i], LOW);
  30.             delay(100);
  31.         }
  32.     }
  33. }

  34. void light_off()//设置关灯
  35. {
  36.     for (int i = 0; i < 4; i++)
  37.     {
  38.         digitalWrite(LED_PIN[ i], LOW);
  39.     }
  40. }

  41. void setup()
  42. {

  43.     Serial.begin(9600);
  44.     if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
  45.         Serial.println("Failed to access Ethernet controller");

  46.     if (!ether.staticSetup(myip, gwip))
  47.         Serial.println("Failed to set IP address");

  48.     ether.printIp("LocalIP: ", ether.myip);
  49.     ether.printIp("GWIP: ", ether.gwip);
  50.     Serial.println();

  51.     for (int i = 0; i < 4; i++)
  52.     {
  53.         pinMode(LED_PIN[ i], OUTPUT);
  54.     }
  55. }

  56. void loop()
  57. {

  58.     word len = ether.packetReceive();
  59.     word pos = ether.packetLoop(len);

  60.     if (pos)
  61.     {
  62.         char *data = (char *)Ethernet::buffer + pos;

  63.         if (strstr(data, "GET /?status=ON") != 0)
  64.         {
  65.             Serial.println("Received ON ");
  66.             flag = true;
  67.         }

  68.         if (strstr(data, "GET /?status=OFF") != 0)
  69.         {
  70.             Serial.println("Received OFF ");
  71.             flag = false;
  72.         }

  73.         if (flag)
  74.         {
  75.             statusLabel = on;
  76.             buttonLabel = off;
  77.         }
  78.         else
  79.         {
  80.             statusLabel = off;
  81.             buttonLabel = on;
  82.         }

  83.         bfill = ether.tcpOffset();
  84.         bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
  85.                           "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
  86.                           "<html><head><title>WebLed</title></head>"
  87.                           "<body>LED灯 Status: $S "
  88.                           "<a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a>"
  89.                           "</body></html>"),
  90.                      statusLabel, buttonLabel, buttonLabel);

  91.         ether.httpServerReply(bfill.position());
  92.     }
  93.     if (flag)
  94.     {
  95.         light_on();
  96.     }
  97.     else
  98.     {
  99.         light_off();
  100.     }
  101.     cnt += 1;

  102. }
复制代码


效果:
图片2.png
网页控制led.zip (36.41 KB, 下载次数: 14)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:85865 发表于 2020-5-5 20:47 | 显示全部楼层
这个可用外网来控制吗
回复

使用道具 举报

ID:396751 发表于 2020-5-22 11:38 | 显示全部楼层
仿真部分是如何实现的呢?
回复

使用道具 举报

ID:396751 发表于 2020-5-22 11:39 | 显示全部楼层
请问一下  仿真部分是如何实现的呢?
回复

使用道具 举报

ID:396751 发表于 2020-5-22 11:41 | 显示全部楼层
楼主  蓝牙智能小车APP的下载链接失败了
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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