Arduino inet_aton函数

时间:2014-10-27 16:09:05

标签: tcp arduino wifi server

我正在尝试在Arduino上构建TCP服务器,并且我使用https://github.com/sparkfun/CC3000_WiFi_Breakout库来获取CC3000 WiFi模块(Sparkfun)。

/********************************

Hardware Connections:

 Mega Pin   CC3000 Board    Function

 +5V        VCC or +5V      5V
 GND        GND             GND
 2          INT             Interrupt
 35         EN              WiFi Enable
 53         CS              SPI Chip Select
 51         MOSI            SPI MOSI
 50         MISO            SPI MISO
 52         SCK             SPI Clock

***********************************/

#include <SPI.h>

#include <SFE_CC3000.h>

#include <utility/socket.h>

#include <Ethernet.h>

#include <Dns.h>

// Pins
#define CC3000_INT      2   // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN       35   // Can be any digital pin
#define CC3000_CS       53  // Preferred is pin 10 on Uno

// Connection info data lengths
#define IP_ADDR_LEN     4   // Length of IP address in bytes
#define MAC_ADDR_LEN    6   // Length of MAC address in bytes

#define LOCALHOST 2130706433 // 127.0.0.1 in long
#define PORT 5100 // port to be used

// Constants
char ap_ssid[] = "HTC";                  // SSID of network
char ap_password[] = "123456789";          // Password of network
unsigned int ap_security = WLAN_SEC_WPA2; // Security of network
unsigned int timeout = 30000;             // Milliseconds
//const char static_ip_addr[] = "0.0.0.0";

sockaddr_in sck_addr, client_addr;
int client_addr_len;

// Global Variables
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);

void setup() {

  ConnectionInfo connection_info;

  // Initialize Serial port
  Serial.begin(115200);
  Serial.println();
  Serial.println("---------------------------------");
  Serial.println("Quadrocopter - BSD Socket Test");
  Serial.println("---------------------------------");

  // Initialize CC3000 (configure SPI communications)
  if ( wifi.init() ) {
    Serial.println("CC3000 initialization complete");
  } else {
    Serial.println("Something went wrong during CC3000 init!");
  }

  // Connect using DHCP
  Serial.print("Connecting to: ");
  Serial.println(ap_ssid);
  if(!wifi.connect(ap_ssid, ap_security, ap_password, timeout)) {
    Serial.println("Error: Could not connect to AP");
  }

  // Print out connection details
  if( !wifi.getConnectionInfo(connection_info) ) {
    Serial.println("Error: Could not obtain connection details");
  } else {
    Serial.println("Connected!");
    Serial.println();
  }

  memset(&sck_addr, 0, sizeof sck_addr);
  sck_addr.sin_family = AF_INET;
  inet_aton("127.0.0.1", &sck_addr.sin_addr);
  sck_addr.sin_port = htons(PORT);
  int sck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  // Disconnect
//  if ( wifi.disconnect() ) {
//    Serial.println("Disconnected");
//  } else {
//    Serial.println("Error: Could not disconnect from network");
//  }
}



void loop() {

  // Do nothing
  delay(1000);

}

utility / socket.h的代码片段

typedef struct _in_addr_t
{
    unsigned long s_addr;                   // load with inet_aton()
} in_addr;

typedef struct _sockaddr_t
{
    unsigned short int    sa_family;
    unsigned char     sa_data[14];
} sockaddr;

typedef struct _sockaddr_in_t
{
    short            sin_family;            // e.g. AF_INET
    unsigned short   sin_port;              // e.g. htons(3490)
    in_addr          sin_addr;              // see struct in_addr, below
    char             sin_zero[8];           // zero this if you want to
} sockaddr_in;

我需要设置sck_addr.sin_addr,但这个库缺少inet_aton函数。

我尝试从Arduino以太网库导入dns库(已定义inet_aton),但inet_aton仍未被识别。

0 个答案:

没有答案