Skip to content

Commit

Permalink
connect and retrieve credentials working
Browse files Browse the repository at this point in the history
  • Loading branch information
elpinjo committed Apr 5, 2020
1 parent 6d915a5 commit b516cc6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
14 changes: 11 additions & 3 deletions examples/SendMeasurement/SendMeasurement.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const char* clientId = "........."; //Should be a unique identifier for this dev
//uint64_t chipid = ESP.getEfuseMac();

WiFiClient wifiClient;
CumulocityClient c8yClient(wifiClient, host, tenant, username, c8yPassword, clientId);
PubSubClient pubSubClient(host, 1883, wifiClient);
CumulocityClient c8yClient(pubSubClient, tenant, username, c8yPassword, clientId);

void setup() {

Expand All @@ -24,21 +25,28 @@ void setup() {
delay(500);
Serial.print(".");
}

Serial.println("connected to wifi");

c8yClient.connect();

Serial.println("Retrieving device credentials");

c8yClient.retrieveDeviceCredentials();
while (!c8yClient.checkCredentialsReceived()) {
Serial.print("#");
delay(1000);
}

c8yClient.disconnect();
c8yClient.connect();

c8yClient.registerDevice("ESP32 - Misja", "c8y_esp32");


}

void loop() {

delay(1000);
c8yClient.loop();

}
82 changes: 71 additions & 11 deletions src/CumulocityClient.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
#include "Arduino.h"
#include "CumulocityClient.h"

CumulocityClient::CumulocityClient(WiFiClient client, const char* host, char* tenant, char* user, char* password, const char* deviceId) {
CumulocityClient::CumulocityClient(PubSubClient client, char* tenant, char* user, char* password, const char* deviceId) {

PubSubClient _client(client);
_client.setServer(host, 1883);
Serial.printf("CumulocityClient(%s, %s, %s)\n", tenant, user, deviceId);

_client = client;
_deviceId = deviceId;

_credentials.tenant = tenant;
_credentials.username = user;
_credentials.password = password;

}

bool CumulocityClient::connect() {

sprintf(_clientId, "d:%s", _deviceId);
Serial.println("connect()");
_clientId = (char*) malloc(strlen(_deviceId) +3);
strcpy(_clientId, "d:");
_clientId = strcat(_clientId, _deviceId);

return connectClient();

}

bool CumulocityClient::connect(char* defaultTemplate) {

_clientId = (char *) malloc(strlen(_deviceId) + strlen(defaultTemplate) + 4);
sprintf(_clientId, "d:%s:%s", _deviceId, defaultTemplate);
return connectClient();

}

bool CumulocityClient::connectClient() {

char* user;
Serial.println("ConnectClient()");

sprintf(user, "%s/%s",_credentials.tenant, _credentials.username);
_client.setCallback(
[this](const char* topic, byte* payload, unsigned int length){
this->callbackHandler(topic, payload, length);
}
);

char* user = (char*) malloc(strlen(_credentials.tenant) + strlen(_credentials.username) + 2);

bool success = _client.connect(_clientId, user, _credentials.password, "s/us", 0, false, "400,c8y_ConnectionEvent,\"Device connection was lost.\"", true);
sprintf(user, "%s/%s",_credentials.tenant, _credentials.username);

bool success = _client.connect(_clientId, user, _credentials.password, "s/us", 0, false, "400,c8y_ConnectionEvent,\"Connections lost.\"", true);


if (!success) {
Serial.print("Unable to connect to Cumulocity as ");
Serial.println(user);
} else {
Serial.println("Connected to cumulocity.");
}

free(user);

return success;
}

void CumulocityClient::disconnect() {

_client.disconnect();
free(_clientId);
}

void CumulocityClient::retrieveDeviceCredentials() {
Expand All @@ -58,20 +76,62 @@ void CumulocityClient::retrieveDeviceCredentials() {

bool CumulocityClient::checkCredentialsReceived() {

if (_credentialsReceived) {
_client.unsubscribe("s/dcr");
} else {
_client.publish("s/ucr", "");
_client.loop();
}

return _credentialsReceived;
}

void CumulocityClient::setDeviceCredentials(char* user, char* password) {
Credentials CumulocityClient::getCredentials() {

return _credentials;
}

void CumulocityClient::setDeviceCredentials(char* user, char* password) {
_credentials.username = user;
_credentials.password = password;
}

void CumulocityClient::registerDevice(char* deviceName, char* deviceType){
char* mqttMessage;
char* mqttMessage = (char*) malloc(strlen(_deviceId) + strlen(deviceType)+6);
sprintf(mqttMessage, "100,%s,%s", deviceName, deviceType);
_client.publish("s/us", mqttMessage);
}

void CumulocityClient::createMeasurement() {
void CumulocityClient::createMeasurement(char* fragment, char* series, char* value) {

char* mqttMessage;
sprintf(mqttMessage, "200,%s,%s,%s", fragment, series, value);
_client.publish("s/us", mqttMessage);

}

void CumulocityClient::loop() {

_client.loop();
}

void CumulocityClient::callbackHandler(const char* topic, byte* payload, unsigned int length) {

Serial.printf("callbackHandler(topic: %s, payload: %s)\n", topic, payload);

//int firstComma =
char myPayload[length+1];

strncpy(myPayload, (char*)payload,length);
myPayload[length] = '\0';

Serial.printf("Input length = %d, payload = %d\n", length, strlen(myPayload));

if (strcmp(topic, "s/dcr") == 0 && length > 0) {
Serial.print("Device credentials received: ");
Serial.println(myPayload);
_credentialsReceived = true;
}

}

19 changes: 10 additions & 9 deletions src/CumulocityClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@
#define CUMULOCITY_CLIENT_H

#include "Arduino.h"
#include <WiFiClient.h>
#include <PubSubClient.h>

typedef struct Credentials {
char* tenant;
char* username;
char* password;
};

class CumulocityClient {

public:
CumulocityClient(WiFiClient client, const char* host, char* tenant, char* user, char* password, const char* deviceId);
CumulocityClient(PubSubClient client, char* tenant, char* user, char* password, const char* deviceId);
bool connect();
bool connect(char* defaultTemplate);
void disconnect();
void setDeviceCredentials(char* user, char* password);
void registerDevice(char* deviceName, char* deviceType);
void retrieveDeviceCredentials();
Credentials getCredentials();
bool checkCredentialsReceived();
void createMeasurement();
void createMeasurement(char* fragment, char* series, char* value);
void loop();

typedef struct Credentials {
char* tenant;
char* username;
char* password;
};

private:
bool connectClient();
void callbackHandler(const char* topic, byte* payload, unsigned int length);

PubSubClient _client;

Expand Down

0 comments on commit b516cc6

Please sign in to comment.