|
本帖最后由 Stormer 于 2017-8-10 07:01 编辑
我用的是IDE里的SAMPLE代码---WebClientRepeating。
代码的目的就是不断刷新读取指定网页的内容,然后根据内容控制LED是点亮还是关闭。
目标网页的内容只有一个字符 1 ,表示打开LED。
然而每当再次请求的时候(httpRequest()),LED会熄灭一下,这不是我想要的效果。我需要这个LED在网页内容为0的时候才会熄灭。
LED只是一个最简单的测试,闪烁一下还不要紧,但如果以后变成其他设备的时候情况肯定会变得很诡异不正常。
下面是代码。
-
- #include <SPI.h>
- #include <Ethernet.h>
- // assign a MAC address for the ethernet controller.
- // fill in your address here:
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- };
- // fill in an available IP address on your network here,
- // for manual configuration:
- IPAddress ip(192, 168, 0, 177);
- // fill in your Domain Name Server address here:
- //IPAddress myDns(1, 1, 1, 1);
- // initialize the library instance:
- EthernetClient client;
- char server[] = "www.anjinkeji.com";
- //IPAddress server(64,131,82,241);
- #define LED_OUT 2
- int lightState = 0; //LED开启状态
- bool isConnection = true;
- unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
- //const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
- const unsigned long postingInterval = 2L * 1000L;
- // the "L" is needed to use long type numbers
- void setup() {
- // start serial port:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- // give the ethernet module time to boot up:
- delay(1000);
- // start the Ethernet connection using a fixed IP address and DNS server:
- Ethernet.begin(mac, ip);
- // print the Ethernet board/shield's IP address:
- Serial.print("My IP address: ");
- Serial.println(Ethernet.localIP());
- }
- void loop()
- {
- // if there's incoming data from the net connection.
- // send it out the serial port. This is for debugging
- // purposes only:
- if (client.available() )
- {
- char c = client.read();
- Serial.write(c);
- if (c == '1')
- {
- digitalWrite(LED_OUT, HIGH);
- }
- else
- {
- if (c == '0')
- {
- digitalWrite(LED_OUT, LOW);
- }
- }
- }
- // if ten seconds have passed since your last connection,
- // then connect again and send data:
- if (millis() - lastConnectionTime > postingInterval)
- {
- httpRequest();
- }
- }
- // this method makes a HTTP connection to the server:
- void httpRequest()
- {
-
- // close any connection before send a new request.
- // This will free the socket on the WiFi shield
- client.stop();
- // if there's a successful connection:
- if (client.connect(server, 80)) {
- Serial.println("connecting...");
- // send the HTTP GET request:
- client.println("GET /t.html HTTP/1.1");
- client.println("Host: www.anjinkeji.com");
- // client.println("User-Agent: arduino-ethernet");
- client.println("Connection: close");
- client.println();
- // note the time that the connection was made:
- lastConnectionTime = millis();
- } else {
- // if you couldn't make a connection:
- Serial.println("connection failed");
- }
-
- }
复制代码
串行监视器:
-
- My IP address: 192.168.0.177
- connecting...
- HTTP/1.1 200 OK
- Server: nginx
- Date: Wed, 09 Aug 2017 22:52:16 GMT
- Content-Type: text/html
- Content-Length: 1
- Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
- Connection: close
- ETag: "598b8dba-1"
- Accept-Ranges: bytes
- 1connecting...
- HTTP/1.1 200 OK
- Server: nginx
- Date: Wed, 09 Aug 2017 22:52:18 GMT
- Content-Type: text/html
- Content-Length: 1
- Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
- Connection: close
- ETag: "598b8dba-1"
- Accept-Ranges: bytes
- 1connecting...
- HTTP/1.1 200 OK
- Server: nginx
- Date: Wed, 09 Aug 2017 22:52:20 GMT
- Content-Type: text/html
- Content-Length: 1
- Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
- Connection: close
- ETag: "598b8dba-1"
- Accept-Ranges: bytes
- 1connecting...
- HTTP/1.1 200 OK
- Server: nginx
- Date: Wed, 09 Aug 2017 22:52:22 GMT
- Content-Type: text/html
- Content-Length: 1
- Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
- Connection: close
- ETag: "598b8dba-1"
- Accept-Ranges: bytes
- 1connecting...
- HTTP/1.1 200 OK
- Server: nginx
- Date: Wed, 09 Aug 2017 22:52:25 GMT
- Content-Type: text/html
- Content-Length: 1
- Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
- Connection: close
- ETag: "598b8dba-1"
- Accept-Ranges: bytes
- 1connecting...
- HTTP/1.1 200 OK
- Server: nginx
- Date: Wed, 09 Aug 2017 22:52:27 GMT
- Content-Type: text/html
- Content-Length: 1
- Last-Modified: Wed, 09 Aug 2017 22:33:30 GMT
- Connection: close
- ETag: "598b8dba-1"
- Accept-Ranges: bytes
- 1
复制代码 |
|