Skip to content

Commit

Permalink
Add secure mqtt example
Browse files Browse the repository at this point in the history
  • Loading branch information
amalabey committed Dec 5, 2020
1 parent 2d3906b commit 6086929
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/SecureMqtt/SecureMqtt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#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
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");
}

0 comments on commit 6086929

Please sign in to comment.