发送 HTTP 1.1 GET 请求时出现问题

Trouble with Sending HTTP 1.1 GET request

我正在尝试使用 PubNub 向频道发布 一条消息,但我总是得到 "Connection Failed!"。

我知道发布URL的结构如下:

http://pubsub.pubnub.com/publish/<PUB-KEY>/<SUB-KEY>/0/<CHANNEL>/0/%22<MESSAGE>%22

我已经在 Google Chrome 上测试了 URL,它工作正常。

以下是 C 风格的代码 运行 Arduino UNO + Adafruit HUZZAH CC3000 WIFI Breakout. This is not an Arduino hardware question per se, because I believe there is nothing wrong with my circuit. My issue is with the structure of the HTTP GET request created using Adafruit CC3000 Library

我不得不发送 GET 而不是使用 PubNub Arduino Library 因为它似乎不支持 CC3000 WIFI 模块。他们有一个 JSON WIFI 和以太网示例,但两者都不与 CC3000 WIFI 模块通信。

  char PUBKEY[] = "XXXXXXX";
  char SUBKEY[] = "XXXXXXX";

  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  if (client.connected())
  {
    client.print("GET /publish/");
    client.print(PUBKEY);
    client.print("/");
    client.print(SUBKEY);
    client.print("/0/");
    client.print("MyPubChannel");  // Channel Name
    client.print("/0/%22");
    client.print("Hello World from Arduino!"); // Msg to publish
    client.print("%22");
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println("pubsub.pubnub.com"); 
    client.println();
  } 
  else 
  {
    Serial.println(F("Connection failed"));    
  }

我已阅读 this 页面并了解正确的 GET 请求应采用以下形式:

GET /pub/WWW/TheProject.html HTTP/1.1
Host: www.w3.org

我很确定我的 GET 请求有问题。任何人都可以看看这个并提供任何关于可能出错的提示吗?

我终于可以使用下面的代码连接到 PubNub。

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"

#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
#define WLAN_SSID             "XXXWifi"           
#define WLAN_PASS             "XXX"
#define WLAN_SECURITY         WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS       3      

uint32_t ip;

// PubNub Setup
#define WEBSITE  "pubsub.pubnub.com"
#define WEBPAGE  "/publish/"
char PUBKEY[] = "pub-XXXX";
char SUBKEY[] = "sub-XXXX";

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS,     ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_CC3000_Client www;

void setup(void)
{
  Serial.begin(115200);

  /* Initialize the module */
  Serial.print(F("\n Initializing WIFI Adapter..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
  Serial.print(F("Ready!\n"));
  Serial.print(F("Connecting to [")); Serial.print(WLAN_SSID);
  Serial.print(F("]..."));
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.print(F("Connected!\n"));

  //Wait for DHCP to complete 
  Serial.print(F("Waiting for DHCP..."));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  
  Serial.print(F("Done!\n"));
  Serial.print(F("Resolving PubNub..."));

  ip = 0;

  // Try looking up the website's IP address
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  Serial.print(F("Resolved!\n"));
  www = cc3000.connectTCP(ip, 80);

  for(int i=0;i<=50;i++)
  {
    char buffer[4];
    dtostrf(i, 4, 0, buffer);
    Publish("ch2", buffer);
  }

  /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  /* the next time your try to connect ... */
  Serial.println(F("\n\nDisconnecting"));
  www.close();
  cc3000.disconnect();
}

void loop(void)
{
   delay(1000);
}

void Publish(char* CH, char* MSG)
{
   if (www.connected()) {
    www.fastrprint(F("GET "));
    www.fastrprint(WEBPAGE);
    www.fastrprint(PUBKEY);
    www.fastrprint(F("/"));
    www.fastrprint(SUBKEY);
    www.fastrprint(F("/0/"));
    www.fastrprint(CH);
    www.fastrprint(F("/0/%22"));
    www.fastrprint(MSG);
    www.fastrprint(F("%22"));
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: "));     
    www.fastrprint(WEBSITE); 
    www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }

  //Read data until either the connection is closed, or the idle timeout is reached.
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    while (www.available()) {
      char c = www.read();
      //Serial.print(c);
      lastRead = millis();
    }
  }
}

char* clean(char* MSG)
{
  return MSG;
}