From 6217eb4112f9bccb8c05523d67c7d53b234c2942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Toledo?= Date: Sat, 8 Aug 2020 22:58:12 +0000 Subject: [PATCH] update example to work with the v1.3 library --- ...ws-mqtt-websocket-example-pubsubclient.ino | 67 +++++++++++++++++-- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/examples/aws-mqtt-websocket-example-pubsubclient/aws-mqtt-websocket-example-pubsubclient.ino b/examples/aws-mqtt-websocket-example-pubsubclient/aws-mqtt-websocket-example-pubsubclient.ino index f5853cd..e00064c 100644 --- a/examples/aws-mqtt-websocket-example-pubsubclient/aws-mqtt-websocket-example-pubsubclient.ino +++ b/examples/aws-mqtt-websocket-example-pubsubclient/aws-mqtt-websocket-example-pubsubclient.ino @@ -1,6 +1,10 @@ +/** +* Example working with library v1.3 +* +*/ #include #include - +#include #include #include @@ -35,6 +39,37 @@ char aws_region[] = "eu-west-1"; const char* aws_topic = "$aws/things/your-device/shadow/update"; int port = 443; +//AWS root certificate - expires 2037 +const char ca[] PROGMEM = R"EOF( +-----BEGIN CERTIFICATE----- +MIIEkjCCA3qgAwIBAgITBn+USionzfP6wq4rAfkI7rnExjANBgkqhkiG9w0BAQsF +ADCBmDELMAkGA1UEBhMCVVMxEDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNj +b3R0c2RhbGUxJTAjBgNVBAoTHFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4x +OzA5BgNVBAMTMlN0YXJmaWVsZCBTZXJ2aWNlcyBSb290IENlcnRpZmljYXRlIEF1 +dGhvcml0eSAtIEcyMB4XDTE1MDUyNTEyMDAwMFoXDTM3MTIzMTAxMDAwMFowOTEL +MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv +b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj +ca9HgFB0f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xyvkQAiSr6w10kY17RSlQOYiypok1JR4U +akcjMS9cmvqtmg5iUaQqqcT5NJ0hGA== +-----END CERTIFICATE----- +)EOF"; + //MQTT config const int maxMQTTpackageSize = 512; const int maxMQTTMessageHandlers = 1; @@ -72,9 +107,6 @@ void callback(char* topic, byte* payload, unsigned int length) { //connects to websocket layer and mqtt layer bool connect () { - - - if (client.connected()) { client.disconnect (); } @@ -120,7 +152,24 @@ void sendmessage () { } -void setup() { +void setClock() { + configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); + + Serial.print("Waiting for NTP time sync: "); + time_t now = time(nullptr); + while (now < 8 * 3600 * 2) { + delay(500); + Serial.print("."); + now = time(nullptr); + } + Serial.println(""); + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + Serial.print("Current time: "); + Serial.print(asctime(&timeinfo)); +} + +void setup() { wifi_set_sleep_type(NONE_SLEEP_T); Serial.begin (115200); delay (2000); @@ -134,13 +183,19 @@ void setup() { Serial.print ("."); } Serial.println ("\nconnected"); - + + setClock(); // Required for X.509 certificate validation + //fill AWS parameters awsWSclient.setAWSRegion(aws_region); awsWSclient.setAWSDomain(aws_endpoint); awsWSclient.setAWSKeyID(aws_key); awsWSclient.setAWSSecretKey(aws_secret); awsWSclient.setUseSSL(true); + awsWSclient.setCA(ca); + //as we had to configurate ntp time to validate the certificate, we can use it to validate aws connection as well + awsWSclient.setUseAmazonTimestamp(false); + if (connect ()){ subscribe ();