简体   繁体   English

Arduino MKR1000无法连接到AWS API Gateway

[英]Arduino MKR1000 not able to connect to AWS API Gateway

I tried modifying the basic Arduino code from here in order to send HTTP Requests to AWS API Gateway. 我尝试从此处修改基本的Arduino代码,以便将HTTP请求发送到AWS API Gateway。 While the example code from the link worked, I was not able to get a successful connection with AWS API Gateway. 虽然该链接中的示例代码有效,但我无法与AWS API Gateway成功建立连接。

I have tried a combination of things such as removing the https:// from server[] , changing the port to 443 instead of 80 , removing the /beta from server[] , using client.connectSSL instead of client.connect , but none of these have worked so far. 我尝试了多种方法,例如从server []删除https:// ,将端口更改为443而不是80 ,从server []删除/ beta ,使用client.connectSSL而不是client.connect ,但是没有到目前为止,这些方法已经奏效。

The line: int err = client.connect(server, 80); 这行代码: int err = client.connect(server,80); returns me a value of 0. 返回我的值为0。

There are no certificates set up with the AWS API Gateway, so I don't think it's a problem with that. AWS API Gateway没有设置证书,因此我认为这不是问题。 Wifi works perfectly. 无线网络完美运行。

Any help would be greatly appreciated! 任何帮助将不胜感激!

#include <SPI.h>
#include <WiFi101.h>
#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or 
use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
char server[] = "https://**********.execute-api.us-west-2.amazonaws.com/beta";    // name address for Google (using DNS)

WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  int err = client.connect(server, 80);
  Serial.println(err);
  if (err) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /beta HTTP/1.1");
    client.println("Host: https://**********.execute-api.us-west-2.amazonaws.com");
    client.println("Connection: close");
    client.println();
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}


void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Tips: 提示:

(1) client.connect(fqdn, port) is expecting an FQDN for the first parameter The example in the docs is client.connect("Arduino.cc", 80) this seems to work well in my tests. (1)client.connect(fqdn,port)期望第一个参数使用FQDN。文档中的示例是client.connect(“ Arduino.cc”,80),这在我的测试中似乎运行良好。 The docs say "URL" but they mean FQDN. 文档说“ URL”,但它们表示FQDN。

(2) If you need SSL then you MUST load up your certs using the firmware updater first. (2)如果需要SSL,则必须首先使用固件更新程序加载证书。 If you are using non-standard pins for the WiFi101 board then you MUST use wifi.setPins() to set the pins or the firmware updater will fail. 如果您将非标准引脚用于WiFi101板,则必须使用wifi.setPins()设置引脚,否则固件更新程序将失败。 Adafruit Feather M0 1500 owners will know what I am talking about here. Adafruit Feather M0 1500的所有者会知道我在这里说什么。

Reference: https://www.arduino.cc/en/Reference/WiFi101ClientConnect 参考: https : //www.arduino.cc/en/Reference/WiFi101ClientConnect

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM