找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 3683|回復(fù): 4
收起左側(cè)

ardunio仿真實(shí)現(xiàn)網(wǎng)頁控制廣告燈閃爍程序

[復(fù)制鏈接]
ID:744206 發(fā)表于 2020-5-5 09:50 | 顯示全部樓層 |閱讀模式
效果說明:HTML制作一個簡易網(wǎng)頁,網(wǎng)頁上有兩個按鈕ON,OFF。
建立Arduino和所制作的網(wǎng)頁之間的通訊,但按下ON時,廣告燈閃爍,當(dāng)按下OFF時,廣告燈熄滅。
電路圖:
圖片1.png
代碼:
  1. #include<EtherCard.h>
  2. #include<SPI.h>
  3. static byte myip[] = {192,168,43,21};//設(shè)置本機(jī)同網(wǎng)段ip【根據(jù)你自己的IP設(shè)置】
  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()//設(shè)置開燈
  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()//設(shè)置關(guān)燈
  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. }
復(fù)制代碼


效果:
圖片2.png
網(wǎng)頁控制led.zip (36.41 KB, 下載次數(shù): 15)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

ID:85865 發(fā)表于 2020-5-5 20:47 | 顯示全部樓層
這個可用外網(wǎng)來控制嗎
回復(fù)

使用道具 舉報(bào)

ID:396751 發(fā)表于 2020-5-22 11:38 | 顯示全部樓層
仿真部分是如何實(shí)現(xiàn)的呢?
回復(fù)

使用道具 舉報(bào)

ID:396751 發(fā)表于 2020-5-22 11:39 | 顯示全部樓層
請問一下  仿真部分是如何實(shí)現(xiàn)的呢?
回復(fù)

使用道具 舉報(bào)

ID:396751 發(fā)表于 2020-5-22 11:41 | 顯示全部樓層
樓主  藍(lán)牙智能小車APP的下載鏈接失敗了
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表