【问题标题】:Trouble with Sending HTTP 1.1 GET request发送 HTTP 1.1 GET 请求时遇到问题
【发布时间】:2016-02-16 23:21:50
【问题描述】:

我正在尝试使用 PubNub 向频道发布一条消息,但我总是收到“连接失败!”。

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

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

我已经在谷歌浏览器上测试过这个网址,它工作得很好。

以下是在 Arduino UNO + Adafruit HUZZAH CC3000 WIFI Breakout 上运行的 C 风格代码。这本身不是 Arduino 硬件问题,因为我相信我的电路没有问题。我的问题是使用 Adafruit CC3000 Library 创建的 HTTP GET 请求的结构。

我不得不发送 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 请求有问题。任何人都可以看看这个并提供任何关于可能出错的提示吗?

【问题讨论】:

  • 您的 GET 请求很好,除了一个小细节。您的短信是通过 URL 传递的,因此您必须对保留字符(包括空格)进行 url 编码,例如:client.print("Hello%20World%20from%20Arduino!"); 但这不是您的问题,因为您的代码在 connectTCP() 上失败。您是否事先将 pubsub.pubnub.com 解析为正确的 IP 地址?
  • 您是否尝试过直接调用Adafruit_CC3000_Client::connect(const char *host, uint16_t port) 而不是使用Adafruit_CC3000::connectTCP(uint32_t destIP, uint16_t destPort)
  • @CraigConover:我从来没有说过它是 PubNub,实际上我主要指的是硬件和我的代码。谢谢。
  • PubNub 确实支持使用 ESP8266 发布订阅 - 你可以在这里查看 - pubnub.com/blog/…。我们没有针对 CC3000 的特定库。
  • @CraigConover:正确,但它是为 ESP8266 列出的。我正在寻找一种适用于 Adafruit HUZZAH CC3000 WIFI 的订阅方法

标签: http-headers arduino wifi pubnub


【解决方案1】:

我终于可以使用下面的代码连接到 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;
}

【讨论】:

  • 这是您请求第三方网站的内容,我该如何使用自己的网络应用程序?
猜你喜欢
  • 2019-09-04
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2014-11-02
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多