将esp8266连接到AWS IoT

时间:2016-11-02 11:16:18

标签: arduino esp8266 aws-iot

我正在尝试使用https://github.com/heskew/aws-sdk-arduino将基于ESP8266的WeMos D1 mini连接到Amazon Web Service AWS IoT。

然而,当我刷新设备时,我得到403回复,并显示以下消息: “凭证应该用于纠正服务:'execute-api'。”

更改

this->awsService = "iotdata";

this->awsService = "execute-api";
AmazonIOTClient.cpp 中的

导致404: “找不到与http方法POST相匹配的路由事件/ my-thing / shadow的方法。” 并且,根据this thread,服务应该是'iotdata'以便成功请求。

是否有人遇到同样的问题并找到了让它运行的方法?如果是这样,将非常感谢帮助。谢谢!

这里是示例的完整代码:

#include <AmazonIOTClient.h>
#include <Esp8266AWSImplementations.h>
#include <AWSFoundationalTypes.h>
#include "keys.h"

const int sleepTimeS = 30;

void printWiFiData();
void printCurrentNetwork();
void publish(const char *topic, String data);
void publishToAWS();

void setup() {

  Serial.begin(9600);

  Serial.println("Started!");

  publishToAWS();
  ESP.deepSleep(sleepTimeS * 1000000);
}

void loop() {
}

void printWiFiData() {

  // IP address
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5], HEX);
  Serial.print(":");
  Serial.print(mac[4], HEX);
  Serial.print(":");
  Serial.print(mac[3], HEX);
  Serial.print(":");
  Serial.print(mac[2], HEX);
  Serial.print(":");
  Serial.print(mac[1], HEX);
  Serial.print(":");
  Serial.println(mac[0], HEX);
}

void printCurrentNetwork() {

  // SSID
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // signal strength:
  Serial.print("signal strength (RSSI): ");
  Serial.println(WiFi.RSSI());
}

void publish(const char *topic, String data) {

  AmazonIOTClient iotClient;
  ActionError actionError;

  Esp8266HttpClient httpClient;
  Esp8266DateTimeProvider dateTimeProvider;

  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(wifiSsid);
  Serial.println("...");

  WiFi.begin(wifiSsid, wifiPwd);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(50);
  }

  Serial.println("");
  Serial.println("WiFi connected");

  printCurrentNetwork();
  printWiFiData();

  delay(50);

  Serial.println("Initializing IoT client...");

  iotClient.setAWSRegion(awsIotRegion);
  iotClient.setAWSEndpoint(awsIotEndpoint);
  iotClient.setAWSDomain(awsIotDomain);
  iotClient.setAWSPath("/things/my-thing/shadow");
  iotClient.setAWSKeyID(awsKeyID);
  iotClient.setAWSSecretKey(awsSecKey);
  iotClient.setHttpClient(&httpClient);
  iotClient.setDateTimeProvider(&dateTimeProvider);

  delay(50);

  Serial.println("Updating thing shadow...");

  MinimalString shadow = ("{\"state\":{\"reported\":{\"text\":" + data + "}}}").c_str();
  char* result = iotClient.update_shadow(shadow, actionError);

  Serial.print("result: ");
  Serial.println(result);
}

void publishToAWS() {

  Serial.println("Publishing to AWS IoT Broker");

  publish("my-thing/text", "Hello World!");
}

keys.cpp 文件:

#include "keys.h"

// AWS User Credentials
const char* awsKeyID = "XXXXXXXXXXXXXXXXXXXX";
const char* awsSecKey = "X1xxx23xxxxXXXX34XXxxxxX56xXxxxxxxXx789x";

// AWS IoT
const char* awsIotRegion = "eu-central-1";
const char* awsIotEndpoint = "xxxxxxxxxxxxxx";
const char* awsIotDomain = "iot.eu-central-1.amazonaws.com";

// Init and connect WiFi to local WLAN
char* wifiSsid = "mySSID";
char* wifiPwd = "password";

1 个答案:

答案 0 :(得分:3)

我终于可以做出贡献了:)

我在几天前就得到了同样的例子。但是我使用相同的库只是一个不同的分支iot-get-shadow-and-cleanup。我不记得要像你提到的那样做任何改变:

this->awsService = "iotdata";this->awsService = "execute-api";

以下是适用于keys.cpp的AWS端点的正确设置

  awsIotRegion = "us-east-1";
  awsIotEndpoint = "amazonaws.com";
  awsIotDomain = "axxxxs2pxxxrlx.iot.us-east-1.amazonaws.com";`

同时将delete[] result;添加到publish()的末尾以节省一些宝贵的堆空间。

我建议使用iot-get-shadow-and-cleanup分支,因为它修复了内存泄漏。

我再做一次更改,以便在连续更新阴影时解决堆空间不足的所有问题。在AWSClient4.cpp中,我将// delete[] server;更改为delete[] data; - 我不是100%确定是否有必要但是加上delete[] result;我能够连续每分钟更新一次shadown一个小时没有丢失任何堆。

希望这会有所帮助。