本文介绍了从Serial获取Xbee响应并发送到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Arduino,Ethernet Shield和Xbee Shield进行一些实验.我这样演示我的设置板:

第1组:Arduino Uno + Xbee防护罩:广播信号

第2组:Arduino Uno + Xbee防护板+以太网防护板:接收来自第1组,从AT命令获取信号强度并将其打印到浏览器中.

这里的问题是,我将ATDB命令发送到Serial后,我无法获得结果,实际上,我不确定它是否按预期工作了.

这是我用来检索信号强度的代码.

 int data;

void setup()
{
  Serial.begin(9600);
}

void receiver_checker(){
  delay(1200);
  Serial.print("+++");
  delay(1200);
  bool bOK = false;
  while (Serial.available() > 0) {
    Serial.write(Serial.read());
     bOK = true;
  }

  if(bOK)
  {
    Serial.println();
    Serial.println("ATDB");
    delay(100);
    while (Serial.available() > 0) {
    Serial.write(Serial.read());
    }
    Serial.println();
  }

  Serial.println();
}

void loop()
{
  while(Serial.available() > 0){
    data = Serial.read();
    if(data == '1'){
      // Broadcaster 1
      //Serial.println("1------------------");
      receiver_checker();
    }
  }
}
 

这部分工作符合我的预期,它以十六进制数字打印出它收到的最后一个包装的信号强度.

这是我将上一个代码和Web Server教程中的服务器部分结合在一起的代码:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xCA, 0xFE, 0x00, 0x00, 0x00, 0x02
};
IPAddress ip(1, 1, 1, 2);
int data;
int count = 0;
char result;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  // Serial.print("server is at ");
  // Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          if (Serial.available() > 0) {
            // read the oldest byte in the serial buffer:
            data = Serial.read();
            // if it's a capital H (ASCII 72), turn on the LED:
            if (data == '1') {
              count += 1;
              client.print("The number of times:");
              client.print(count);
              result = receiver_checker();
              client.print("========================");
              client.print(result);
              client.print("========================");
            }
          }

          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

char receiver_checker(){
  char signal;
  delay(1200);
  Serial.print("+++");
  delay(1200);
  bool bOK = false;
  while (Serial.available() > 0) {
    Serial.write(Serial.read());
     bOK = true;
  }

  if(bOK)
  {
    Serial.println();
    Serial.println("ATDB");
    delay(100);
    while (Serial.available() > 0) {
     signal = Serial.read();
    }
    Serial.println();
  }
  Serial.println();
  return signal;
}
 

如果还有另一种与Xbee防护罩进行交互的方式无法像我所要求的那样直接通过Serial并直接获得响应,请告诉我!

解决方案

您的receiver_checker()函数正在从XBee模块读取字符,并仅返回接收到的最后一个字符,这很可能是回车或换行./p>

更新该函数以返回int,并用以下内容替换您的while (Serial.available() > 0):

signal = (int) strtoul(Serial.readString().c_str(), 0, 16);

这是XBee返回十六进制值的时间.如果返回的是十进制值,请将16参数更改为10.

I am trying to do some experiments with Arduino, Ethernet Shield and Xbee Shield.I demonstrate my set up board like this:

Group 1: Arduino Uno + Xbee shield : broadcast the signal

Group 2: Arduino Uno + Xbee shield + Ethernet shield: receive the signal fromgroup 1, get the signal strength from AT command and print it into the browser.

The problem here is I can't get the result after sending to the Serial my ATDB command, actually, I am not sure it did worked as I expected.

Here is the code that I used to retrieve the signal strength.

int data;

void setup()
{
  Serial.begin(9600);
}

void receiver_checker(){
  delay(1200);
  Serial.print("+++");
  delay(1200);
  bool bOK = false;
  while (Serial.available() > 0) {
    Serial.write(Serial.read());
     bOK = true;
  }

  if(bOK)
  {
    Serial.println();
    Serial.println("ATDB");
    delay(100);
    while (Serial.available() > 0) {
    Serial.write(Serial.read());
    }
    Serial.println();
  }

  Serial.println();
}

void loop()
{
  while(Serial.available() > 0){
    data = Serial.read();
    if(data == '1'){
      // Broadcaster 1
      //Serial.println("1------------------");
      receiver_checker();
    }
  }
}

This part worked as I expected, it printed out in hex number the signal strength of the last package that it received.

Here is the code I combined the previous one and the server part from Web Server tutorial:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xCA, 0xFE, 0x00, 0x00, 0x00, 0x02
};
IPAddress ip(1, 1, 1, 2);
int data;
int count = 0;
char result;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  // Serial.print("server is at ");
  // Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          if (Serial.available() > 0) {
            // read the oldest byte in the serial buffer:
            data = Serial.read();
            // if it's a capital H (ASCII 72), turn on the LED:
            if (data == '1') {
              count += 1;
              client.print("The number of times:");
              client.print(count);
              result = receiver_checker();
              client.print("========================");
              client.print(result);
              client.print("========================");
            }
          }

          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

char receiver_checker(){
  char signal;
  delay(1200);
  Serial.print("+++");
  delay(1200);
  bool bOK = false;
  while (Serial.available() > 0) {
    Serial.write(Serial.read());
     bOK = true;
  }

  if(bOK)
  {
    Serial.println();
    Serial.println("ATDB");
    delay(100);
    while (Serial.available() > 0) {
     signal = Serial.read();
    }
    Serial.println();
  }
  Serial.println();
  return signal;
}

If there is another way to interact with the Xbee shield not go through Serial like I ask and get response directly, please let me know!

解决方案

Your receiver_checker() function is reading characters back from the XBee module, and just returning the last character received, which is likely a carriage return or line feed.

Update the function to return an int, and replace your while (Serial.available() > 0) with the following:

signal = (int) strtoul(Serial.readString().c_str(), 0, 16);

That's for when the XBee returns a hexadecimal value. If it's returning a decimal value, change the 16 parameter to a 10.

这篇关于从Serial获取Xbee响应并发送到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 15:23