2012-09-03 10 views
7

पर एक विशिष्ट आईपी पते पर एक वाई-फाई शील्ड के माध्यम से Arduino डेटा भेजें। मैं Arduino Uno से Copperhead Wi-Fi shield के माध्यम से एक विशिष्ट आईपी पते और एक लैन पर पोर्ट के लिए सेंसर डेटा भेजने का प्रयास कर रहा हूं।लैन

मैं काम करने के लिए कॉपरहेड वाई-फाई सर्वर उदाहरण sketch प्राप्त कर सकता हूं (नीचे चिपकाया गया)। हालांकि, मुझे HTML के माध्यम से सर्वर अनुरोधों का जवाब देने में कोई दिलचस्पी नहीं है। मुझे बस दिलचस्पी है कि एक सॉकेट-जैसे कनेक्शन स्थापित करना और टीसीपी या यूडीपी के माध्यम से आईपी एड्रेस 1 9 2.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); 
} 

उत्तर

2

ऐसा लगता है कि आप WiShield लाइब्रेरी का उपयोग कर रहे हैं। में SocketApp और UDPApp उदाहरण के साथ डाउनलोड करने के लिए एक उदाहरण फ़ोल्डर होना चाहिए। शुरुआत करने के लिए यह एक अच्छा स्थान है।

यूडीपी ऐप बनाने के दौरान मैंने कुछ सीखा।

  1. आप अपने ऐप recompiling करने से पहले कुछ #defines (जैसे apps-conf.h में APP_UDPAPP, UIP_CONF_UDPuip-conf.h में) संपादित करने के लिए हो सकता है।

  2. आप एक UDP एप्लिकेशन कर रहे हैं, यह ध्यान रखें आप एक सीमित प्राप्त बफर (UIP_CONF_BUFFER_SIZEuip-conf.h सेटों में यह करने के लिए) है। मेरा राउटर यूडीपी प्रसारण एक्सएमएल संदेश भेज रहा था जो ~ 700 बाइट्स था जिससे इस बफर को ओवरफ्लो और अन्य डेटा लिखने का कारण बन गया। मुझे नहीं लगता कि टीसीपी को यह समस्या होगी क्योंकि यह एक एमएसएस पर बातचीत करेगा जो बफर को खत्म नहीं करेगा।

अंत में मैं UDPapp उदाहरण में handle_connection() समारोह में परिवर्तन किए। नीचे एक स्निपेट है (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); 
} 
+0

बहुत उपयोगी! बफर आकार पर भी हेड-अप के लिए धन्यवाद! – Kappa

0

क्या आपको Arduino WiFiWebClient tutorial पर देखने का मौका मिला? यह उदाहरण दिखाता है कि वेबसर्वर से कैसे कनेक्ट करें और HTTP GET अनुरोध भेजें।

आप लैन पर किसी भी मशीन पर एक साधारण सर्वर बना सकते हैं और सर्वर से कनेक्ट करने के लिए क्लाइंट लाइब्रेरी का उपयोग कर सकते हैं और कार्यों के लेखन/प्रिंट/println सेट का उपयोग कर डेटा भेज सकते हैं। मुझे पता है कि यह करने से आसान कहा जाता है, लेकिन प्रोग्रामिंग का मजा नहीं है?

+0

लिंक के लिए धन्यवाद। मैं भी इस विकल्प का पता लगाऊंगा। – Kappa