[英]Arduino MKR1000 not able to connect to AWS API Gateway
我尝试从此处修改基本的Arduino代码,以便将HTTP请求发送到AWS API Gateway。 虽然该链接中的示例代码有效,但我无法与AWS API Gateway成功建立连接。
我尝试了多种方法,例如从server []删除https:// ,将端口更改为443而不是80 ,从server []删除/ beta ,使用client.connectSSL而不是client.connect ,但是没有到目前为止,这些方法已经奏效。
这行代码: int err = client.connect(server,80); 返回我的值为0。
AWS API Gateway没有设置证书,因此我认为这不是问题。 无线网络完美运行。
任何帮助将不胜感激!
#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");
}
提示:
(1)client.connect(fqdn,port)期望第一个参数使用FQDN。文档中的示例是client.connect(“ Arduino.cc”,80),这在我的测试中似乎运行良好。 文档说“ URL”,但它们表示FQDN。
(2)如果需要SSL,则必须首先使用固件更新程序加载证书。 如果您将非标准引脚用于WiFi101板,则必须使用wifi.setPins()设置引脚,否则固件更新程序将失败。 Adafruit Feather M0 1500的所有者会知道我在这里说什么。
参考: https : //www.arduino.cc/en/Reference/WiFi101ClientConnect
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.