我正在尝试通过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);
}
Run Code Online (Sandbox Code Playgroud)
看来您正在使用WiShield图书馆。下载中应该有一个示例文件夹,WiShield其中包含SocketApp示例UDPApp。这是一个很好的起点。
我在制作 UDP 应用程序时学到的一些东西。
在重新编译应用程序之前,您可能需要编辑一些#defines(例如APP_UDPAPPin apps-conf.h、UIP_CONF_UDPin )。uip-conf.h
如果您正在开发UDP应用程序,请记住您的接收缓冲区有限(UIP_CONF_BUFFER_SIZE将uip-conf.h其设置为400)。我的路由器正在发送大约700 字节的UDP 广播 XML 消息,这导致该缓冲区溢出并覆盖其他数据。我不认为 TCP 会有这个问题,因为它会协商一个不会超出缓冲区的 MSS。
最后我对示例handle_connection()中的函数进行了更改UDPapp。下面是一个片段(设置uip_ipaddr为255.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);
}
Run Code Online (Sandbox Code Playgroud)