Arduino返回错误的整数值

时间:2017-04-09 09:34:58

标签: http arduino arduino-uno

我有一些代码,根据网站上的值打开/关闭LED(包含数字的空白页。页面上的数字表示LED应闪烁的次数。

问题是循环继续运行 我可以通过手动设置整数值来解决问题(int c = 3)。

不确定我的问题是什么 也许你们其中一个人可以指出我正确的方向。

网址:http://b2b.as/lan.php?pid=8855

代码:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 104 };
char server[] = "b2b.as";

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println(Ethernet.localIP());
  Serial.println();

  // Set digital pin as output
  // 5V
  pinMode(8, OUTPUT);
}

void loop()
{

  //
  Serial.print("\n-----\n");

  // Connect to the server
  Serial.print("connecting to URL ...");

  // Start LAN connection
  EthernetClient client;
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /lan.php?pid=8855");
    client.println();
  } else {
    Serial.println("connection failed");
  }

  // Wait a moment for data to arrive
  // Note: This is NOT a reliable way of doing this!
  delay(1000);

  if (client.available() > 0) {

    char c = atoi(client.read());

    Serial.print("page value (pick): ");
    Serial.print(c, DEC);
    Serial.print("\n");

    for (int x = 1; x <= int(c); x++) {
      Serial.print("picking: #");
      Serial.println(x);
      digitalWrite(8, HIGH);
      Serial.println("8 HIGH ...");
      delay(5000); // Add switch
      digitalWrite(8, LOW);
      Serial.println("8 LOW ...");
      delay(1000);
    }

    Serial.print("end");

  }

  // Disconnect the client
  if (client.connected()) {
    //Serial.println();
    Serial.print("disconnecting");
    client.stop();
  }

  // Wait another 9s, which will give us a delay of roughly 10s
  delay(9000);
}

2 个答案:

答案 0 :(得分:0)

我认为对lan.php?pid=8855的调用只返回没有任何格式的值,例如HTML,XML,JSON。然后你的代码基本上将ASCII字符3转换为一个整数,它给你整数值33(见ASCII Table)。因此,你的循环不会停止。

<强>解决方案

只需使用atoi函数将其转换为整数。

char c = atoi(client.read());

答案 1 :(得分:0)

似乎toInt()是我正在寻找的功能。它将字符串转换为整数并修复循环。

https://www.arduino.cc/en/Reference/StringToInt

代码已更新,似乎有效:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 104 };
char server[] = "b2b.as";

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println(Ethernet.localIP());
  Serial.println();

  // Set digital pin as output
  // 5V
  pinMode(8, OUTPUT);
}

void loop()
{

  //
  Serial.print("\n-----\n");

  // Connect to the server
  Serial.print("connecting to URL ...");

  // Start LAN connection
  EthernetClient client;
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /lan.php?pid=8855");
    client.println();
  } else {
    Serial.println("connection failed");
  }

  // Wait a moment for data to arrive
  // Note: This is NOT a reliable way of doing this!
  delay(1000);

  if (client.available() > 0) {

    String pickNum;

    while (client.available()) {
        char c = client.read();  // gets one byte from serial buffer
        pickNum += c; // count
        delay(2);  // delay for buffer
    }

    Serial.print("page value (pick): ");
    Serial.println(pickNum);

    for (int x = 1; x <= pickNum.toInt(); x++) {
      Serial.print("picking: #");
      Serial.println(x);
      digitalWrite(8, HIGH);
      Serial.println("8 HIGH ...");
      delay(1000); // Add switch
      digitalWrite(8, LOW);
      Serial.println("8 LOW ...");
      delay(1000);
    }

    Serial.print("end");

  }

  // Disconnect the client
  if (client.connected()) {
    //Serial.println();
    Serial.print("disconnecting");
    client.stop();
  }

  // Wait another 9s, which will give us a delay of roughly 10s
  delay(9000);
}