WiFlyHQ 库- tcpclient 示例问题
WiFlyHQ Library- tcpclient Example issue
我使用的是 Arduino Mega 2560,我有一个 seed studio WiFi shield。我已经将它们都连接起来了。现在,我一直在尝试 运行 来自 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{
如果有人能帮助我,我将不胜感激。告诉我问题是什么?或者为什么会这样?
这样的输出是波特率不匹配的典型表现。
确保您在串行监视器中选择了 57600,或者在 Serial.begin(rate);
中输入所需的速度。
我使用的是 Arduino Mega 2560,我有一个 seed studio WiFi shield。我已经将它们都连接起来了。现在,我一直在尝试 运行 来自 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{
如果有人能帮助我,我将不胜感激。告诉我问题是什么?或者为什么会这样?
这样的输出是波特率不匹配的典型表现。
确保您在串行监视器中选择了 57600,或者在 Serial.begin(rate);
中输入所需的速度。