4

我们正在尝试将基于 Arduino 的 wifi 蜜蜂(ATmega 328P)连接到 Cloudant 数据库。wifi 蜜蜂使用 wiwhield 库连接到网络。我们可以使用它来连接到 cosm(xively)并发布/获取数据。但是,使用 cloudant,我们无法这样做,并且不断收到 503 服务不可用请求。

我认为问题在于 Wishield 需要指定 IP 地址,而 Cloudant 使用 DNS。我正在使用 wishield 添加代码片段,该代码片段适用于从 weather.com 获取数据,但是当我们修改 cloudant 的变量时.com,它只是给了我一个 503 请求。

请找到不同的代码: 代码片段:GetDatafromCloudant.ino :我们刚刚更改了 Cloudant GET 请求的 IP 地址/端口详细信息。

    /*
 * A simple sketch that uses WiServer to get the hourly weather data from LAX and prints
 * it via the Serial API
 */

#include <WiServer.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,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 = {"BSNL_AP"};       // max 32 bytes

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

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"bhutoriafamily"}; // 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 WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

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


// Function that prints data from the server
void printData(char* data, int len) {

  // Print the data returned by the server
  // Note that the data is not null-terminated, may be broken up into smaller packets, and 
  // includes the HTTP header. 
  while (len-- > 0) {
    Serial.print(*(data++));
  } 
}


// IP Address for www.weather.gov  
//uint8 ip[] = {140,90,113,200};
uint8 ip[] = {50,23,104,75};
// A request that gets the latest METAR weather data for LAX
GETrequest getWeather(ip, 5984, "http://alfatek.cloudant.com", "/wifi/_all_docs");


void setup() {
    // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
  WiServer.init(NULL);

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

  // Have the processData function called when data is returned by the server
  getWeather.setReturnFunc(printData);
}


// Time (in millis) when the data should be retrieved 
long updateTime = 0;

void loop(){

  // Check if it's time to get an update
  if (millis() >= updateTime) {
    getWeather.submit();    
    // Get another update one hour from now
    updateTime += 1000 * 20 ;
  }

  // Run WiServer
  WiServer.server_task();

  delay(10);
}

代码片段:GetDatafromWeather.ino:这很好用,我们可以从weather.com 中提取数据以获取洛杉矶天气数据。

/*
 * A simple sketch that uses WiServer to get the hourly weather data from LAX and prints
 * it via the Serial API
 */

#include <WiServer.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,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 = {"BSNL_AP"};       // max 32 bytes

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

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"bhutoriafamily"}; // 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 WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

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


// Function that prints data from the server
void printData(char* data, int len) {

  // Print the data returned by the server
  // Note that the data is not null-terminated, may be broken up into smaller packets, and 
  // includes the HTTP header. 
  while (len-- > 0) {
    Serial.print(*(data++));
  } 
}


// IP Address for www.weather.gov  
uint8 ip[] = {140,90,113,200};

// A request that gets the latest METAR weather data for LAX
GETrequest getWeather(ip, 80, "www.weather.gov", "/data/METAR/KLAX.1.txt");


void setup() {
    // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
  WiServer.init(NULL);

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

  // Have the processData function called when data is returned by the server
  getWeather.setReturnFunc(printData);
}


// Time (in millis) when the data should be retrieved 
long updateTime = 0;

void loop(){

  // Check if it's time to get an update
  if (millis() >= updateTime) {
    getWeather.submit();    
    // Get another update one hour from now
    updateTime += 1000 * 20 ;
  }

  // Run WiServer
  WiServer.server_task();

  delay(10);
}
4

1 回答 1

3

您是对的,Cloudant 根据提供的标头路由您的请求。您可以通过指定 Host 标头来解决此问题,如下所示:Host: USERNAME.cloudant.com

您从 Cloudant 获得的 503 状态代码意味着您登陆的负载均衡器不知道如何路由您的请求,因为它没有您的帐户名称。

您可以像这样获取要连接的 IP(Bash 终端):host USERNAME.cloudant.com

但是,这不是一个好主意。

Cloudant 使用 DNS 的原因是 IP 可以随服务的变化而变化。此外,故障转移到不同的负载平衡服务器和/或数据中心由 DNS 处理(数据中心内的本地故障转移由负载平衡器处理)。因此,您不会创建一个稳定的环境 - Cloudant 可以并且将会更改您的 IP,从而破坏您的设置。

抱歉,但听起来最好的解决方案是让 DNS 在您的环境中工作。

[来源:我在 Cloudant 工作。]

于 2013-06-19T19:02:03.997 回答