@@ -505,6 +505,24 @@ void Connect2MQTTbroker() {
505
505
}
506
506
507
507
508
+ template <typename L> void loadFromFile (const char * fname, L&& load) {
509
+ if (SPIFFS.exists (fname)) {
510
+ File f = SPIFFS.open (fname);
511
+ load (f, f.size ());
512
+ f.close ();
513
+ }
514
+ }
515
+
516
+
517
+ void loadCertificates (WiFiClientSecure* client) {
518
+ SPIFFS.begin ();
519
+ loadFromFile (WATSON_IOT_PLATFORM_CA_PEM, [client](Stream& stream, size_t size){return client->loadCACert (stream, size);});
520
+ // loadFromFile("/client.cert.pem", [client](Stream& stream, size_t size){return client->loadCertificate(stream, size);});
521
+ // loadFromFile("/private.key.pem", [client](Stream& stream, size_t size){return client->loadPrivateKey(stream, size);});
522
+ SPIFFS.end ();
523
+ }
524
+
525
+
508
526
void Send10Seconds2Cloud () {
509
527
// DynamicJsonDocument is stored on the heap
510
528
// Allocate a ArduinoJson buffer large enough to 10 seconds of Accelerometer trace data
@@ -756,30 +774,7 @@ void setup() {
756
774
#else
757
775
sprintf (MQTT_HOST," %s.messaging.internetofthings.ibmcloud.com" ,MQTT_ORGID); // Centrally managed
758
776
759
- if ( SPIFFS.begin (true ) ) {
760
- Serial.printf (" Opening Watson IoT Root CA PEM Chain : %s\r\n " , WATSON_IOT_PLATFORM_CA_PEM);
761
- File pemfile = SPIFFS.open ( WATSON_IOT_PLATFORM_CA_PEM );
762
- if ( pemfile ) {
763
- char *RootCAPemChain = nullptr ;
764
- size_t pemSize = pemfile.size ();
765
- RootCAPemChain = (char *)malloc (pemSize);
766
-
767
- if ( pemSize != pemfile.readBytes (RootCAPemChain, pemSize) ) {
768
- Serial.printf (" Reading %s pem server certificate chain failed.\r\n " ,WATSON_IOT_PLATFORM_CA_PEM);
769
- } else {
770
- Serial.printf (" Read %s Root CA server certificate chain\r\n " ,WATSON_IOT_PLATFORM_CA_PEM);
771
- // Serial.println( RootCAPemChain );
772
- wifiClient.setCACert (RootCAPemChain);
773
- }
774
- free ( RootCAPemChain );
775
- } else {
776
- Serial.println (" Failed to open server pem chain." );
777
- }
778
- pemfile.close ();
779
- } else {
780
- Serial.println (" An error has occurred while mounting SPIFFS" );
781
- }
782
-
777
+ loadCertificates ( &wifiClient ); // Load the Watson IoT messaging.pem CA Cert from SPIFFS
783
778
#endif
784
779
785
780
char mqttparams[100 ]; // Allocate a buffer large enough for this string ~95 chars
0 commit comments