13
13
#include < WiFi101.h>
14
14
#include < MQTTClient.h>
15
15
#include < ArduinoBearSSL.h>
16
+ #include < utility/ECC508.h>
17
+ #include < utility/ECC508Cert.h>
16
18
17
19
// ssid and pass are the wifi settings
18
20
const char ssid[] = " XXX" ;
@@ -22,19 +24,16 @@ const char pass[] = "XXX";
22
24
const char server[] = " xxxxxxxxxxxxxx.iot.xx-xxxx-x.amazonaws.com" ;
23
25
24
26
// id is the ThingName in aws IOT
25
- const char id[] = " XXX"
27
+ const String id = " XXX" ;
26
28
27
29
// Get the cert data by:
28
30
// 1) Creating a CSR using the ArduinoBearSSL -> Tools -> ECC508CSR example for key slot 0
29
- // 2) Creating a new thing and uploading the CSR for it
30
- // 3) Downloading the public key for the thing in AWS IoT
31
- // 4) Convert the base64 encoded cert to binary
32
- const byte cert[] = {
33
- // ...
34
- };
31
+ // 2) Use the "Go tool" to generate a public cert from the CSR
32
+ // 3) Store the cert params in 1)
33
+ // 4) Activate the cert in AWS IoT and attach policy + thing
35
34
36
35
WiFiClient wifiClient;
37
- BearSSLClient net (wifiClient, 0 , cert, sizeof (cert) );
36
+ BearSSLClient net (wifiClient);
38
37
MQTTClient client;
39
38
40
39
unsigned long lastMillis = 0 ;
@@ -45,8 +44,37 @@ unsigned long getTime() {
45
44
46
45
void setup () {
47
46
Serial.begin (115200 );
47
+ while (!Serial);
48
+
49
+ if (!ECC508.begin ()) {
50
+ Serial.println (" No ECC508 present!" );
51
+ while (1 );
52
+ }
48
53
49
54
ArduinoBearSSL.onGetTime (getTime);
55
+
56
+ ECC508Cert.begin (0 , 9 , 10 );
57
+ ECC508Cert.setIssuerCountryName (" US" );
58
+ ECC508Cert.setIssuerOrganizationName (" Arduino LLC US" );
59
+ ECC508Cert.setIssuerOrganizationalUnitName (" IT" );
60
+ ECC508Cert.setIssuerCommonName (" Arduino" );
61
+ ECC508Cert.setSubjectCommonName (ECC508.serialNumber ());
62
+ ECC508Cert.uncompress ();
63
+
64
+ const byte* certData = ECC508Cert.bytes ();
65
+ int certLength = ECC508Cert.length ();
66
+
67
+ for (int i = 0 ; i < certLength; i++) {
68
+ byte b = certData[i];
69
+
70
+ if (b < 16 ) {
71
+ Serial.print (' 0' );
72
+ }
73
+ Serial.print (b, HEX);
74
+ }
75
+ Serial.println ();
76
+
77
+ net.setEccSlot (0 , ECC508Cert.bytes (), ECC508Cert.length ());
50
78
51
79
WiFi.begin (ssid, pass);
52
80
@@ -68,7 +96,7 @@ void connect() {
68
96
}
69
97
70
98
Serial.print (" \n connecting..." );
71
- while (!client.connect (id)) {
99
+ while (!client.connect (id. c_str () )) {
72
100
Serial.print (" ." );
73
101
delay (1000 );
74
102
}
@@ -94,4 +122,4 @@ void loop() {
94
122
95
123
void messageReceived (String &topic, String &payload) {
96
124
Serial.println (" incoming: " + topic + " - " + payload);
97
- }
125
+ }
0 commit comments