WiFlyHQ Library- tcpclient示例问题

时间:2015-03-19 11:46:30

标签: arduino wifi arduino-ide

我使用的是Arduino Mega 2560,而且我有一个看见的工作室WiFi盾牌。我已将它们连接起来了。现在,我一直在尝试从WiFlyHQ库运行此TCP客户端示例。我在示例中仅更改了软件序列引脚号和SSID以及密码。上传程序后,串口监视器给我一个奇怪的输出

示例是:

 #include <SoftwareSerial.h>
    SoftwareSerial wifiSerial(10,11);

    //#include <AltSoftSerial.h>
    //AltSoftSerial wifiSerial(8,9);

    #include <WiFlyHQ.h>

    /* Change these to match your WiFi network */
    const char mySSID[] = "ash";
    const char myPassword[] = "*****";

    void terminal();

    WiFly wifly;

    void setup()
    {
     char buf[64];

      Serial.begin(57600);
      Serial.println("Starting");
      Serial.print("Free memory: ");
      Serial.println(wifly.getFreeMemory(),DEC);

    wifiSerial.begin(9600);
    if (!wifly.begin(&wifiSerial, &Serial)) {
        Serial.println("Failed to start wifly");
 terminal();
    }

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
 /* Setup the WiFly to connect to a wifi network */
 Serial.println("Joining network");
 wifly.setSSID(mySSID);
 wifly.setPassphrase(myPassword);
 wifly.enableDHCP();

 if (wifly.join()) {
    Serial.println("Joined wifi network");
 } else {
    Serial.println("Failed to join wifi network");
    terminal();
 }
    } else {
        Serial.println("Already joined network");
    }

    Serial.println("WiFly ready");

    Serial.print("MAC: ");
    Serial.println(wifly.getMAC(buf, sizeof(buf)));
    Serial.print("IP: ");
    Serial.println(wifly.getIP(buf, sizeof(buf)));
    Serial.print("Netmask: ");
    Serial.println(wifly.getNetmask(buf, sizeof(buf)));
    Serial.print("Gateway: ");
    Serial.println(wifly.getGateway(buf, sizeof(buf)));

    Serial.println("Set DeviceID");
    wifly.setDeviceID("Wifly-TCP");
    Serial.print("DeviceID: ");
    Serial.println(wifly.getDeviceID(buf, sizeof(buf)));

    wifly.setIpProtocol(WIFLY_PROTOCOL_TCP);

    if (wifly.isConnected()) {
        Serial.println("Old connection active. Closing");
 wifly.close();
    }
}

uint32_t connectTime = 0;

void loop()
{
    int available;

    if (wifly.isConnected() == false) {
 Serial.println("Connecting");
 if (wifly.open("192.168.1.60",8042)) {
    Serial.println("Connected");
    connectTime = millis();
 } else {
    Serial.println("Failed to open");
 }
    } else {
 available = wifly.available();
 if (available < 0) {
    Serial.println("Disconnected");
 } else if (available > 0) {
    Serial.write(wifly.read());
 } else {
    /* Disconnect after 10 seconds */
    if ((millis() - connectTime) > 10000) {
 Serial.println("Disconnecting");
 wifly.close();
    }
 }

 /* Send data from the serial monitor to the TCP server */
 if (Serial.available()) {
    wifly.write(Serial.read());
 }
    }
}

void terminal()
{
    while (1) {
 if (wifly.available() > 0) {
    Serial.write(wifly.read());
 }


 if (Serial.available()) { // Outgoing data
    wifly.write(Serial.read());
 }
    }
}

串口监视器为我输出

C°rüèbþWu|Ô°9­{

如果有人能帮助我,我会非常感激。告诉我这是什么问题?或者为什么会这样?

1 个答案:

答案 0 :(得分:0)

这样的输出是典型的不匹配波特率。

确保在串行监视器中选择了57600,或在Serial.begin(rate);中输入所需的速度。