From 2d3906b7ae47c8a59b8a6e4da155d6c5d58a1f4e Mon Sep 17 00:00:00 2001 From: Amal Abeygunawardana Date: Fri, 4 Dec 2020 21:55:51 +1100 Subject: [PATCH 1/3] Allow passing port number when connecting --- src/CumulocityClient.cpp | 13 +++++++++++-- src/CumulocityClient.h | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/CumulocityClient.cpp b/src/CumulocityClient.cpp index c456ceb..bbcbf7e 100644 --- a/src/CumulocityClient.cpp +++ b/src/CumulocityClient.cpp @@ -22,10 +22,14 @@ bool CumulocityClient::reconnect() { } bool CumulocityClient::connect(char* host, char* tenant, char* user, char* password) { + return connect(host, 1883, tenant, user, password); +} +bool CumulocityClient::connect(char* host, uint16_t port, char* tenant, char* user, char* password) { Serial.printf("connect(%s,%s,%s)\n", host, tenant, user); _host = host; + _port = port; _credentials.tenant = tenant; _credentials.username = user; _credentials.password = password; @@ -36,15 +40,20 @@ bool CumulocityClient::connect(char* host, char* tenant, char* user, char* passw _clientId = (char*) malloc(myClientId.length() +1); strcpy(_clientId,myClientId.c_str()); - _client.setServer(_host, 1883); + _client.setServer(_host, _port); return connectClient(); } bool CumulocityClient::connect(char* host, char* tenant, char* user, char* password, char* defaultTemplate) { + return connect(host, 1883, tenant, user, password, defaultTemplate); +} + +bool CumulocityClient::connect(char* host, uint16_t port, char* tenant, char* user, char* password, char* defaultTemplate) { _host = host; + _port = port; _credentials.tenant = tenant; _credentials.username = user; _credentials.password = password; @@ -57,7 +66,7 @@ bool CumulocityClient::connect(char* host, char* tenant, char* user, char* passw _clientId = (char*) malloc(myClientId.length() +1); strcpy(_clientId,myClientId.c_str()); - _client.setServer(_host, 1883); + _client.setServer(_host, _port); return connectClient(); diff --git a/src/CumulocityClient.h b/src/CumulocityClient.h index 1e52c9f..e52d051 100644 --- a/src/CumulocityClient.h +++ b/src/CumulocityClient.h @@ -39,7 +39,9 @@ class CumulocityClient { bool reconnect(); bool connect(char* host, char* tenant, char* user, char* password); + bool connect(char* host, uint16_t port, char* tenant, char* user, char* password); bool connect(char* host, char* tenant, char* user, char* password, char* defaultTemplate); + bool connect(char* host, uint16_t port, char* tenant, char* user, char* password, char* defaultTemplate); void disconnect(); void setDeviceCredentials(char* tenant, char* user, char* password); @@ -65,6 +67,7 @@ class CumulocityClient { PubSubClient _client; char* _host; + uint16_t _port; Credentials _credentials; char* _clientId; bool _credentialsReceived; From 6086929a56b1eb7506487cd3ab53748ed69ec72a Mon Sep 17 00:00:00 2001 From: Amal Abeygunawardana Date: Sat, 5 Dec 2020 19:50:04 +1100 Subject: [PATCH 2/3] Add secure mqtt example --- examples/SecureMqtt/SecureMqtt.ino | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 examples/SecureMqtt/SecureMqtt.ino diff --git a/examples/SecureMqtt/SecureMqtt.ino b/examples/SecureMqtt/SecureMqtt.ino new file mode 100644 index 0000000..6c8bcbb --- /dev/null +++ b/examples/SecureMqtt/SecureMqtt.ino @@ -0,0 +1,51 @@ +#include +#ifdef ESP8266 + #include +#else //ESP32 + #include +#endif + +const char *ssid = "........"; +const char *wifiPassword = "........"; +char *host = "xxx.cumulocity.com"; +char *username = "........"; // fixed credentials can be registered in the Administration section +char *c8yPassword = "........"; // create a user in usermanagement with the "device"role and fill the credentials here +char *tenant = "........"; //tenant ID can be found by clicking on your name in the top right corner of Cumulocity +char *clientId = "........."; //Should be a unique identifier for this device, e.g. IMEI, MAC address or SerialNumber +const char *root_ca = + "-----BEGIN CERTIFICATE-----\n" + ".....CERT CONTENT............\n" + "-----END CERTIFICATE-----\n"; + +WiFiClientSecure wifiClient; +CumulocityClient c8yClient(wifiClient, clientId); + +void setup() +{ + Serial.begin(115200); + + wifiClient.setCACert(root_ca); + WiFi.begin(ssid, wifiPassword); + + Serial.print("Connecting to WiFi"); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println("connected to wifi"); + + c8yClient.connect(host, 8883, tenant, username, c8yPassword); + c8yClient.registerDevice(clientId, "c8y_esp32"); +} + +void loop() +{ + delay(3000); + c8yClient.loop(); + + int8_t rssi = WiFi.RSSI(); + char rssiStr[10]; + sprintf(rssiStr, "%d", rssi); + c8yClient.createMeasurement("SignalStrength", "T", rssiStr, "*db"); +} \ No newline at end of file From 2972aaeef4b3e87d08f3a910d9656655030d1b91 Mon Sep 17 00:00:00 2001 From: Amal Abeygunawardana Date: Sat, 5 Dec 2020 19:51:42 +1100 Subject: [PATCH 3/3] Remove unnecessary root ca cert --- examples/SecureMqtt/SecureMqtt.ino | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/examples/SecureMqtt/SecureMqtt.ino b/examples/SecureMqtt/SecureMqtt.ino index 6c8bcbb..3d19f69 100644 --- a/examples/SecureMqtt/SecureMqtt.ino +++ b/examples/SecureMqtt/SecureMqtt.ino @@ -12,10 +12,6 @@ char *username = "........"; // fixed credentials can be registered in the Ad char *c8yPassword = "........"; // create a user in usermanagement with the "device"role and fill the credentials here char *tenant = "........"; //tenant ID can be found by clicking on your name in the top right corner of Cumulocity char *clientId = "........."; //Should be a unique identifier for this device, e.g. IMEI, MAC address or SerialNumber -const char *root_ca = - "-----BEGIN CERTIFICATE-----\n" - ".....CERT CONTENT............\n" - "-----END CERTIFICATE-----\n"; WiFiClientSecure wifiClient; CumulocityClient c8yClient(wifiClient, clientId); @@ -23,11 +19,9 @@ CumulocityClient c8yClient(wifiClient, clientId); void setup() { Serial.begin(115200); + Serial.print("Connecting to WiFi"); - wifiClient.setCACert(root_ca); WiFi.begin(ssid, wifiPassword); - - Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(500);