我尝试从here修改基本的Arduino代码,以便将HTTP请求发送到AWS API Gateway。虽然链接中的示例代码有效,但我无法与AWS API Gateway成功建立连接。
我尝试了一些组合,例如从服务器[] 中移除 https:// ,将端口更改为 443 80 ,使用 client.connectSSL 而不是客户端从服务器[] 中删除 / beta .connect ,但到目前为止这些都没有。
该行: int err = client.connect(server,80); 返回值为0。
AWS API网关没有设置证书,所以我不认为这是一个问题。 Wifi工作得很好。
非常感谢任何帮助!
#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");
}
答案 0 :(得分:0)
提示:
(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
我希望这会有所帮助。