7

我正在尝试通过Copperhead Wi-Fi 屏蔽从Arduino Uno将传感器数据发送到 LAN 上的特定 IP 地址和端口。

我可以让 Copperhead Wi-Fi 服务器示例草图工作(粘贴在下面)。但是,我对通过 HTML 响应服务器请求不感兴趣。我感兴趣的只是建立一个类似套接字的连接并通过 TCP 或 UDP 将数据发送到 IP 地址 192.168.0.3,端口 1234。

我确信有一个简单的解决方案,但由于我是 Arduino 新手,我试图找到解决方案的尝试没有成功。

#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,0,2};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,0,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};  // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"WiFi_AP"};       // max 32 bytes

unsigned char security_type = 0;    // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};   // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,     0x0a, 0x0b, 0x0c, 0x0d, // Key 0
              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // Key 3
            };

// Setup the wireless mode
// Infrastructure - connect to AP
// Adhoc - connect to another Wi-Fi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------


// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {

    // Check if the requested URL matches "/"
    if (strcmp(URL, "/") == 0) {
        // Use WiServer's print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("Hello World");
        WiServer.print("</html>");

        // URL was recognized
        return true;
    }
    // URL not found
    return false;
}


void setup() {
    // Initialize WiServer and have it use the sendMyPage function to serve pages
    WiServer.init(sendMyPage);

    // Enable Serial output and ask WiServer to generate log messages (optional)
    Serial.begin(57600);
    WiServer.enableVerboseMode(true);
}

void loop(){
    // Run WiServer
    WiServer.server_task();

    delay(10);
}
4

2 回答 2

2

看起来你正在使用WiShield图书馆。下载中应该有一个示例文件夹,其中WiShield包含一个SocketAppUDPApp示例。这是一个很好的起点。

我在制作 UDP 应用程序时学到了一些东西。

  1. 在重新编译您的应用程序之前,您可能需要编辑一些#defines(例如APP_UDPAPPin apps-conf.hUIP_CONF_UDPin )。uip-conf.h

  2. 如果您正在做一个UDP应用程序,请记住您的接收缓冲区有限(UIP_CONF_BUFFER_SIZEuip-conf.h其设置为400)。我的路由器发送了一个大约 700 字节的 UDP 广播 XML 消息,这导致该缓冲区溢出并覆盖了其他数据。我认为 TCP 不会有这个问题,因为它会协商一个不会超出缓冲区的 MSS。

handle_connection()最后,我对示例中的函数进行了更改UDPapp。下面是一个片段(设置uip_ipaddr255.255.255.255)。

void send_state(void) {
    sprintf((char*)uip_appdata, "state %ld %ld %ld %c %d", 
    clock_time(), 
    state.sensors.ping[0].cm,
    state.sensors.ping[1].cm,
    state.actuators.chassis.direction,
    state.actuators.chassis.speed);
    uip_send(uip_appdata, strlen((char*)uip_appdata));
}

void send_beacon(void) {
    if(timer_expired(&beacon_timer)) {
        timer_reset(&beacon_timer);
        sprintf((char*)uip_appdata, "beacon %ld", clock_time());
        uip_send(uip_appdata, strlen((char*)uip_appdata));
        uip_log("beacon sent");
    }
}

boolean data_or_poll(void) {
    return (uip_newdata() || uip_poll());
}

static PT_THREAD(handle_connection(void)) {
    PT_BEGIN(&s.pt);
    PT_WAIT_UNTIL(&s.pt, data_or_poll());
    if(uip_newdata()) {
        uip_flags &= (~UIP_NEWDATA);
        send_state();
    } else if (uip_poll()) {
        uip_flags &= (~UIP_POLL);
        send_beacon();
    }

    PT_END(&s.pt);
}
于 2012-09-05T18:33:06.477 回答
0

您是否有机会查看Arduino WiFiWebClient 教程?此示例显示如何连接到 Web 服务器并发送 HTTP GET 请求。

您可以在 LAN 上的任何机器上创建一个简单的服务器,并使用客户端库连接到服务器并使用 write/print/println 函数集发送数据。我知道说起来容易做起来难,但这就是编程的乐趣不是吗?

于 2012-09-03T21:54:03.670 回答