Stormer 发表于 2017-8-10 06:53:02

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

本帖最后由 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

通幽境 发表于 2017-8-11 02:06:05

没用过,说下我的猜测。
1、IO没有设置成输出模式,需要初始化一个状态。
2、0可能来自某个函数无返回时的输出,建议换个其他值试试,比如2。

Stormer 发表于 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() 逐字读出的内容是:

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

1

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

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


void loop()
{


if (client.available() )
{   
    cResult = client.read();

    Serial.write(cResult);
}



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

    //Serial.println(c);

    switch (cResult)
    {
      case'0':
      digitalWrite(LED_OUT, LOW);
      lightState = 0;
      break;

      case'1':
      if (lightState == 0)
      {
          digitalWrite(LED_OUT, HIGH);
          lightState = 1;
      }
      break;


    }

    httpRequest();
}

}

通幽境 发表于 2017-8-11 09:56:55

Stormer 发表于 2017-8-11 02:58
谢谢,我居然忘了加pinMode哈哈。

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


学习了。。
页: [1]
查看完整版本: 用W5100控制LED的时候,总是会有个闪烁/闪断