极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11806|回复: 3

用W5100控制LED的时候,总是会有个闪烁/闪断

[复制链接]
发表于 2017-8-10 06:53:02 | 显示全部楼层 |阅读模式
本帖最后由 Stormer 于 2017-8-10 07:01 编辑

我用的是IDE里的SAMPLE代码---WebClientRepeating。

代码的目的就是不断刷新读取指定网页的内容,然后根据内容控制LED是点亮还是关闭。

目标网页的内容只有一个字符 1 ,表示打开LED。

然而每当再次请求的时候(httpRequest()),LED会熄灭一下,这不是我想要的效果。我需要这个LED在网页内容为0的时候才会熄灭。

LED只是一个最简单的测试,闪烁一下还不要紧,但如果以后变成其他设备的时候情况肯定会变得很诡异不正常。

下面是代码。




  1. #include <SPI.h>
  2. #include <Ethernet.h>

  3. // assign a MAC address for the ethernet controller.
  4. // fill in your address here:
  5. byte mac[] = {
  6.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  7. };
  8. // fill in an available IP address on your network here,
  9. // for manual configuration:
  10. IPAddress ip(192, 168, 0, 177);

  11. // fill in your Domain Name Server address here:
  12. //IPAddress myDns(1, 1, 1, 1);

  13. // initialize the library instance:
  14. EthernetClient client;

  15. char server[] = "www.anjinkeji.com";
  16. //IPAddress server(64,131,82,241);

  17. #define LED_OUT 2

  18. int lightState = 0; //LED开启状态

  19. bool isConnection = true;

  20. unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
  21. //const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
  22. const unsigned long postingInterval = 2L * 1000L;
  23. // the "L" is needed to use long type numbers

  24. void setup() {
  25.   // start serial port:
  26.   Serial.begin(9600);
  27.   while (!Serial) {
  28.     ; // wait for serial port to connect. Needed for native USB port only
  29.   }

  30.   // give the ethernet module time to boot up:
  31.   delay(1000);
  32.   // start the Ethernet connection using a fixed IP address and DNS server:
  33.   Ethernet.begin(mac, ip);
  34.   // print the Ethernet board/shield's IP address:
  35.   Serial.print("My IP address: ");
  36.   Serial.println(Ethernet.localIP());
  37. }

  38. void loop()
  39. {

  40.   // if there's incoming data from the net connection.
  41.   // send it out the serial port.  This is for debugging
  42.   // purposes only:
  43.   if (client.available() )
  44.   {
  45.     char c = client.read();
  46.     Serial.write(c);

  47.     if (c == '1')
  48.     {
  49.       digitalWrite(LED_OUT, HIGH);
  50.     }
  51.     else
  52.     {
  53.       if (c == '0')
  54.       {
  55.         digitalWrite(LED_OUT, LOW);
  56.       }
  57.     }
  58.   }



  59.   // if ten seconds have passed since your last connection,
  60.   // then connect again and send data:
  61.   if (millis() - lastConnectionTime > postingInterval)
  62.   {
  63.     httpRequest();
  64.   }

  65. }



  66. // this method makes a HTTP connection to the server:
  67. void httpRequest()
  68. {


  69.   // close any connection before send a new request.
  70.   // This will free the socket on the WiFi shield
  71.   client.stop();

  72.   // if there's a successful connection:
  73.   if (client.connect(server, 80)) {
  74.     Serial.println("connecting...");
  75.     // send the HTTP GET request:
  76.     client.println("GET /t.html HTTP/1.1");
  77.     client.println("Host: www.anjinkeji.com");
  78.     //    client.println("User-Agent: arduino-ethernet");
  79.     client.println("Connection: close");
  80.     client.println();

  81.     // note the time that the connection was made:
  82.     lastConnectionTime = millis();
  83.   } else {
  84.     // if you couldn't make a connection:
  85.     Serial.println("connection failed");
  86.   }

  87. }
复制代码



串行监视器:


  1. My IP address: 192.168.0.177
  2. connecting...
  3. HTTP/1.1 200 OK
  4. Server: nginx
  5. Date: Wed, 09 Aug 2017 22:52:16 GMT
  6. Content-Type: text/html
  7. Content-Length: 1
  8. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  9. Connection: close
  10. ETag: "598b8dba-1"
  11. Accept-Ranges: bytes

  12. 1connecting...
  13. HTTP/1.1 200 OK
  14. Server: nginx
  15. Date: Wed, 09 Aug 2017 22:52:18 GMT
  16. Content-Type: text/html
  17. Content-Length: 1
  18. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  19. Connection: close
  20. ETag: "598b8dba-1"
  21. Accept-Ranges: bytes

  22. 1connecting...
  23. HTTP/1.1 200 OK
  24. Server: nginx
  25. Date: Wed, 09 Aug 2017 22:52:20 GMT
  26. Content-Type: text/html
  27. Content-Length: 1
  28. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  29. Connection: close
  30. ETag: "598b8dba-1"
  31. Accept-Ranges: bytes

  32. 1connecting...
  33. HTTP/1.1 200 OK
  34. Server: nginx
  35. Date: Wed, 09 Aug 2017 22:52:22 GMT
  36. Content-Type: text/html
  37. Content-Length: 1
  38. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  39. Connection: close
  40. ETag: "598b8dba-1"
  41. Accept-Ranges: bytes

  42. 1connecting...
  43. HTTP/1.1 200 OK
  44. Server: nginx
  45. Date: Wed, 09 Aug 2017 22:52:25 GMT
  46. Content-Type: text/html
  47. Content-Length: 1
  48. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  49. Connection: close
  50. ETag: "598b8dba-1"
  51. Accept-Ranges: bytes

  52. 1connecting...
  53. HTTP/1.1 200 OK
  54. Server: nginx
  55. Date: Wed, 09 Aug 2017 22:52:27 GMT
  56. Content-Type: text/html
  57. Content-Length: 1
  58. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  59. Connection: close
  60. ETag: "598b8dba-1"
  61. Accept-Ranges: bytes

  62. 1
复制代码
回复

使用道具 举报

发表于 2017-8-11 02:06:05 | 显示全部楼层
没用过,说下我的猜测。
1、IO没有设置成输出模式,需要初始化一个状态。
2、0可能来自某个函数无返回时的输出,建议换个其他值试试,比如2。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-11 02:58:48 | 显示全部楼层
本帖最后由 Stormer 于 2017-8-11 05:07 编辑
通幽境 发表于 2017-8-11 02:06
没用过,说下我的猜测。
1、IO没有设置成输出模式,需要初始化一个状态。
2、0可能来自某个函数无返回时 ...


谢谢,我居然忘了加pinMode哈哈。

1、setup()中加入了pinMode(LED_OUT, OUTPUT);
2、设置LED状态为LOW的判断改成了2,if (c == '2')

问题依旧。LED还是在从新请求网络的时候闪烁一下。我再找找原因。

-----------------------

问题解决了。

问题在于网络请求的结果不仅仅是网页里的HTML内容,还包括了反馈给浏览器的信息。

client.read() 逐字读出的内容是:

  1. HTTP/1.1 200 OK
  2. Server: nginx
  3. Date: Wed, 09 Aug 2017 22:52:20 GMT
  4. Content-Type: text/html
  5. Content-Length: 1
  6. Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
  7. Connection: close
  8. ETag: "598b8dba-1"
  9. Accept-Ranges: bytes

  10. 1
复制代码


所以结果就很明显不正常了。

改了下代码现在只把 client.read() 里最后一位的内容读出来作为判断就可以了。


  1. void loop()
  2. {
  3.   

  4.   if (client.available() )
  5.   {   
  6.     cResult = client.read();

  7.     Serial.write(cResult);
  8.   }



  9.   // if 2 seconds have passed since your last connection,
  10.   // then connect again and send data:
  11.   if (millis() - lastConnectionTime > postingInterval)
  12.   {

  13.     //Serial.println(c);

  14.     switch (cResult)
  15.     {
  16.       case'0':
  17.         digitalWrite(LED_OUT, LOW);
  18.         lightState = 0;
  19.         break;

  20.       case'1':
  21.         if (lightState == 0)
  22.         {
  23.           digitalWrite(LED_OUT, HIGH);
  24.           lightState = 1;
  25.         }
  26.         break;


  27.     }

  28.     httpRequest();
  29.   }

  30. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2017-8-11 09:56:55 | 显示全部楼层
Stormer 发表于 2017-8-11 02:58
谢谢,我居然忘了加pinMode哈哈。

1、setup()中加入了pinMode(LED_OUT, OUTPUT);

学习了。。
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-3-30 00:00 , Processed in 0.041453 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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