esp8266 aws iot资料收集 可以上传

1 相关网页

[未解决] 求助 用esp8266 sketch data upload tool 上传aws的cert和key报错
https://www.arduino.cn/thread-82379-1-1.html

亚马逊云物联网AWS IoT初体验
https://blog.csdn.net/jo_joe/article/details/77975319

[esp8266]-新ESP8266-RTOS-SDK(v3.0)开发环境搭建
https://blog.csdn.net/areswhy/article/details/81144864

2

板卡参数没选对,你选的是512k no spiffs,当然不能上传了,这个按钮的工具是生成spiffs的固件。你要选择正确的内存布局,esp8266 12f之类的推荐使用4m(3m spiffs)

Part 0: Getting started with AWS IoT and Arduino (+ESP8266)
https://anwaarullah.wordpress.com/2015/07/30/getting-started-with-iot-on-aws-and-arduino-esp8266/
https://github.com/esp8266/Arduino/issues/3944

3 MQTT之 PubSubClient 库

esp8266 aws iot资料收集 可以上传_第1张图片

4 例子:

源码来源:
https://github.com/HarringayMakerSpace/awsiot/blob/master/Esp8266AWSIoTExample/Esp8266AWSIoTExample.ino

修改后的源码:

/* ESP8266 AWS IoT
 *  
 * Simplest possible example (that I could come up with) of using an ESP8266 with AWS IoT.
 * No messing with openssl or spiffs just regular pubsub and certificates in string constants
 * 
 * Note: For this to work you do need to do this:
 *       https://github.com/esp8266/Arduino/issues/3944
 *
 * Author: Anthony Elder 
 * License: Apache License v2
 */
#include 
#include 
extern "C" {
#include "libb64/cdecode.h"
}

const char* ssid = "XXXX";
const char* password = "xxxxxxxxx";

const char* awsEndpoint = "a3vw7fvq64xaow-ats.iot.us-west-2.amazonaws.com";

// For the two certificate strings below paste in the text of your AWS 
// device certificate and private key, comment out the BEGIN and END 
// lines, add a quote character at the start of each line and a quote 
// and backslash at the end of each line:

// xxxxxxxxxx-certificate.pem.crt
const String certificatePemCrt = \
//-----BEGIN CERTIFICATE-----
"MIIDWjCCAkKgAwIBAgIVAJNKVSaQLsc/Hu1QuzPUxJciSe+pMA0GCSqGSIb3DQEB" \
"CwUAME0xSzBJBgNVBAsMQkFtYXpvbiBXZWIgU2VydmljZXMgTz1BbWF6b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dFP3ihzK5BHaBsIqLXb45FwAaio8Q=="; 
//-----END CERTIFICATE-----

// xxxxxxxxxx-private.pem.key
const String privatePemKey = \
//-----BEGIN RSA PRIVATE KEY-----
"MIIEpAIBAAKCAQEAwmcyqwxj3QCOtCL+7RLZk2DqPg1/Po2E8d5VVVcLB4r3v2fj" \
"Wugyr4SVLp+BrbhvHQ5wa5hilhubWklVWMoAtFGuYC14952VTSDO1R+BeN0MmKll" \
"/jUhQFChDIe+yW2pJeQXTLGh+GBhBxPwTfJlY8kKZFeAnoy11QESmjQU8Pij4xFG" \
"maSpM2nv+eIiDoDct2ntOYfyj4tvtg6jKsEh7MfBR4aMfBs8e0LSNyygsx0l6rlj" \
"7hz0tnHlnpE+Ys2OonGqsWwwZhiMNLPLNogFKx2/UH60Q99DtvJbSC6CQEjQTRZj" \
"s/SozoWrjeWfizKej62nP3VfnAU1mpUdM6iMAQIDAQABAoIBAQC+Y8NDsca70qfv" \
"07ZHCIb9x5ShYSaVZJQnLM/13GF46ajpVB1eErpRG0/fcWfXcyGfG10OgxdKW48y" \
"RU/IvU0jTDuzBdXK7BIkj1UBdPGgJR0JBh5VcEM4cb7IPsmjDDr0WQZYnj86uWlN" \
"PUHDlPmnWGVNEEVvAe+yxHIrg4Ze1+zqCblr+1tg6mG/L9YmbLuNVln/eikzpPSr" \
"3wq6DSZp5PH745le+mpeE4FQRVjm91r/xnMOFOJ0UhmKGkvyWZh3qzegrmD8NXw1" \
"Bk5vmfvOYBIVASBq8Itw9YYTrr0gumEdpMZCm2YnQnrE05uYOg7dOaA5/YFCvyKp" \
"R18GS6LhAoGBAPWo10942hn1rrhPKoeVvhaL5I0u21yEzSJJHR8zksE53GgW5oW6" \
"q26WUjsH+f0doo4VyJcm7Am9YWYy8pFQFA+/4AZJVuw+QUWjaV1B+WmHg+MQcR4U" \
"wRmh7rOlzjKnjnetPXSZfg8Sv39FOSmruai+Deg7eDHhbUNbeJpI9MavAoGBAMqW" \
"CCy2EioLAZCGbmJyn3nF+zcMWUVazv08npds2/0d9UnOjHpA1NIKWENQe8SfqBYg" \
"0Az9yU9BemC9YqoWTuD/do4OJEGoDe7PT3UMGotEyT4HXwbeKahmobtxyunHig/H" \
"Gly+HTjx86qEB46N/PflTzhI0vuhxG/NzAmcY4RPAoGBANr4ihtzvO0g2oG9R0qe" \
"BldwkOPr3WqFkZg7FbNHNEv6OGWr7AVN1Bt58Ihf3sxpCwmdjzR0ZDd2UIhZZ3Bx" \
"scAsJxNTciCYiXP6aiPPd/NB8SSH9lx64Xjop64IYFk0k8BHcp0paH0lF/7pBEAJ" \
"qy8nYZwczlGf/9XnhgP4ff9PAoGAUjNOXdYCyMxudhYV9BxOWTrcFBoOnvWZx2cx" \
"cn/ZQ3tEbA5DMdfcgt1vsuCVV9LwgQoPYTYY2O/YmChAQIOMvf7qO/nA/GMURDIa" \
"K4QdwSTIA0M6a675eDsspMInc3RjsMM5DyiU8uY52JZ5+n3ihzOkUVI7SZ759eOr" \
"RrsOJkECgYBprBPswAZhKGGZUYEE3pNqFapWTPuM+GdG/gr8XdLHAl24x1r9soja" \
"/I1EvdHUPZ9wzxcc1/ELZlgaO8kNVmyz77uobhu3N3672bXurE9x2I07fMSrPA3l" \
"T+aXSxYzxYEzsK5L3j26PTV6Db71pLSGFsipx4mY68O4hWQifYdaQQ==";
//-----END RSA PRIVATE KEY-----

WiFiClientSecure wiFiClient;
void msgReceived(char* topic, byte* payload, unsigned int len);
PubSubClient pubSubClient(awsEndpoint, 8883, msgReceived, wiFiClient); 

void setup() {
  Serial.begin(115200); Serial.println();
  Serial.println("ESP8266 AWS IoT Example");

  Serial.print("Connecting to "); Serial.print(ssid);
  WiFi.begin(ssid, password);
  WiFi.waitForConnectResult();
  Serial.print(", WiFi connected, IP address: "); Serial.println(WiFi.localIP());

  uint8_t binaryCert[certificatePemCrt.length() * 3 / 4];
  int len = b64decode(certificatePemCrt, binaryCert);
  wiFiClient.setCertificate(binaryCert, len);
  
  uint8_t binaryPrivate[privatePemKey.length() * 3 / 4];
  len = b64decode(privatePemKey, binaryPrivate);
  wiFiClient.setPrivateKey(binaryPrivate, len);
}

unsigned long lastPublish;
int msgCount;

void loop() {

  pubSubCheckConnect();

  if (millis() - lastPublish > 10000) {
    String msg = String("Hello from ESP8266: ") + ++msgCount;
    pubSubClient.publish("$aws/things/myTestThing/shadow/update", msg.c_str());
    Serial.print("Published: "); Serial.println(msg);
    lastPublish = millis();
  }
}

void msgReceived(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message received on "); Serial.print(topic); Serial.print(": ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void pubSubCheckConnect() {
  if ( ! pubSubClient.connected()) {
    Serial.print("PubSubClient connecting to: "); Serial.print(awsEndpoint);
    while ( ! pubSubClient.connected()) {
      Serial.print(".");
      pubSubClient.connect("myTestThing");
    }
    Serial.println(" connected");
    pubSubClient.subscribe("$aws/things/myTestThing/shadow/update");
  }
  pubSubClient.loop();
}

int b64decode(String b64Text, uint8_t* output) {
  base64_decodestate s;
  base64_init_decodestate(&s);
  int cnt = base64_decode_block(b64Text.c_str(), b64Text.length(), (char*)output, &s);
  return cnt;
}

运行log :
esp8266 aws iot资料收集 可以上传_第2张图片

esp8266 aws iot资料收集 可以上传_第3张图片

上面的证书手动 停用了,如果需要继续测试使用,需要重新激活证书:
esp8266 aws iot资料收集 可以上传_第4张图片

用到的设备为:
esp8266 aws iot资料收集 可以上传_第5张图片

你可能感兴趣的:(esp8266)