Skip to content

Commit

Permalink
Merge pull request #2 from amalabey/add-ssl-support
Browse files Browse the repository at this point in the history
Enable secure MQTT support by parameterising the port number
  • Loading branch information
elpinjo authored Dec 10, 2020
2 parents a945aae + 2972aae commit ae91ac4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
45 changes: 45 additions & 0 deletions examples/SecureMqtt/SecureMqtt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <CumulocityClient.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else //ESP32
#include <WiFiClientSecure.h>
#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

WiFiClientSecure wifiClient;
CumulocityClient c8yClient(wifiClient, clientId);

void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");

WiFi.begin(ssid, wifiPassword);
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");
}
13 changes: 11 additions & 2 deletions src/CumulocityClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions src/CumulocityClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -65,6 +67,7 @@ class CumulocityClient {
PubSubClient _client;

char* _host;
uint16_t _port;
Credentials _credentials;
char* _clientId;
bool _credentialsReceived;
Expand Down

0 comments on commit ae91ac4

Please sign in to comment.