Skip to content

Commit

Permalink
Update AWS infrastructure section in the README file
Browse files Browse the repository at this point in the history
  • Loading branch information
loginov-rocks committed Jul 18, 2024
1 parent 9ba2731 commit d4a0d59
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 85 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,29 @@ void loop()
}
```
## Infrastructure View
## AWS Infrastructure
![Infrastructure View](https://raw.githubusercontent.com/loginov-rocks/AwsIotWiFiClient/main/docs/Infrastructure%20View.png)
There are several additional AWS components shown on the right, beyond the AWS IoT Core, but they are just an example
of how you can further extend the solution, while we are going to focus on the central and left parts:
* AWS IoT Core configuration;
* Microcontroller (MCU) connection over MQTT to AWS IoT Core;
* Device certificate and policy that will grant necessary permissions to establish a connection, publish, and receive
messages from AWS IoT Core.
MQTT has gained popularity within the hobbyist community for DIY IoT projects due to its simplicity and efficiency. AWS
IoT leverages MQTT as a standard to enable seamless, real-time data exchange between IoT devices and the cloud, making
it ideal for connecting constrained devices like the NodeMCU.
Device certificate ensures that the communication between your microcontroller and AWS is encrypted and secure, but
also authenticates your device with AWS IoT, confirming its identity — this guarantees that only trusted devices can
connect to your AWS infrastructure, maintaining the integrity of your system.
* [CloudFormation](https://github.com/loginov-rocks/AwsIotWiFiClient/blob/main/docs/cloudformation.json)
* [Policy](https://github.com/loginov-rocks/AwsIotWiFiClient/blob/main/docs/policy.json)
## API
### `AwsIotWiFiClient`
Expand Down Expand Up @@ -125,8 +144,8 @@ Set certificates to establish secure communication.
| Parameter | Type | Description |
| ----------------------- | ------------- | --------------------------------------- |
| &trustAnchorCertificate | `X509List*` | Pointer to the trust anchor certificate |
| &clientCertificate | `X509List*` | Pointer to the Client certificate |
| &clientPrivateKey | `PrivateKey*` | Pointer to the Client private key |
| &clientCertificate | `X509List*` | Pointer to the client certificate |
| &clientPrivateKey | `PrivateKey*` | Pointer to the client private key |
---
Expand Down Expand Up @@ -191,7 +210,7 @@ Set the MQTT topic filter to subscribe to incoming messages.

#### `setDebugOutput(debugOutput)`

Enable or disable debug output. Enabled by default.
Enable or disable debug output. Disabled by default.

**Kind**: instance method of `AwsIotWiFiClient`

Expand Down
32 changes: 16 additions & 16 deletions include/AwsIotWiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@
class AwsIotWiFiClient
{
private:
WiFiClientSecure wiFiSecureClient;
PubSubClient *pubSubClient;
WiFiClientSecure wiFiSecureClient;
PubSubClient *pubSubClient;

boolean debugOutput = true;
const char *clientId;
const char *subscribeTopicFilter;
boolean debugOutput = false;
const char *clientId;
const char *subscribeTopicFilter;

void setupTime();
void setupTime();

public:
AwsIotWiFiClient();
AwsIotWiFiClient();

AwsIotWiFiClient &setCertificates(const X509List *, const X509List *, const PrivateKey *);
AwsIotWiFiClient &setEndpoint(const char *);
AwsIotWiFiClient &setReceiveMessageCallback(std::function<void(char *, uint8_t *, unsigned int)>);
AwsIotWiFiClient &setCertificates(const X509List *, const X509List *, const PrivateKey *);
AwsIotWiFiClient &setEndpoint(const char *);
AwsIotWiFiClient &setReceiveMessageCallback(std::function<void(char *, uint8_t *, unsigned int)>);

AwsIotWiFiClient &setDebugOutput(boolean);
AwsIotWiFiClient &setClientId(const char *);
AwsIotWiFiClient &setSubscribeTopicFilter(const char *);
AwsIotWiFiClient &setDebugOutput(boolean);
AwsIotWiFiClient &setClientId(const char *);
AwsIotWiFiClient &setSubscribeTopicFilter(const char *);

void connect();
void loop();
void connect();
void loop();

boolean publishMessage(const char *, const char *);
boolean publishMessage(const char *, const char *);
};

#endif
130 changes: 65 additions & 65 deletions src/AwsIotWiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,132 +2,132 @@

void AwsIotWiFiClient::setupTime()
{
time_t nowish = 1510592825;
time_t nowish = 1510592825;

if (debugOutput)
{
Serial.print("Setting time using SNTP");
}
if (debugOutput)
{
Serial.print("Setting time using SNTP");
}

// configTime(TIME_ZONE * 3600, 0 * 3600, "pool.ntp.org", "time.nist.gov");
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
time_t now = time(nullptr);
// configTime(TIME_ZONE * 3600, 0 * 3600, "pool.ntp.org", "time.nist.gov");
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
time_t now = time(nullptr);

if (debugOutput)
if (debugOutput)
{
while (now < nowish)
{
while (now < nowish)
{
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("done!");
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("done!");
}

struct tm timeinfo;
gmtime_r(&now, &timeinfo);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);

if (debugOutput)
{
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}
if (debugOutput)
{
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}
}

AwsIotWiFiClient::AwsIotWiFiClient() : wiFiSecureClient()
{
pubSubClient = new PubSubClient(wiFiSecureClient);
pubSubClient = new PubSubClient(wiFiSecureClient);
}

AwsIotWiFiClient &AwsIotWiFiClient::setDebugOutput(boolean _debugOutput)
{
debugOutput = _debugOutput;
debugOutput = _debugOutput;

return *this;
return *this;
}

AwsIotWiFiClient &AwsIotWiFiClient::setCertificates(const X509List *trustAnchorCertificate, const X509List *clientCertificate, const PrivateKey *clientPrivateKey)
{
setupTime();
setupTime();

wiFiSecureClient.setTrustAnchors(trustAnchorCertificate);
wiFiSecureClient.setClientRSACert(clientCertificate, clientPrivateKey);
wiFiSecureClient.setTrustAnchors(trustAnchorCertificate);
wiFiSecureClient.setClientRSACert(clientCertificate, clientPrivateKey);

return *this;
return *this;
}

AwsIotWiFiClient &AwsIotWiFiClient::setEndpoint(const char *endpoint)
{
pubSubClient->setServer(endpoint, 8883);
pubSubClient->setServer(endpoint, 8883);

return *this;
return *this;
}

AwsIotWiFiClient &AwsIotWiFiClient::setReceiveMessageCallback(std::function<void(char *, uint8_t *, unsigned int)> callback)
{
pubSubClient->setCallback(callback);
pubSubClient->setCallback(callback);

return *this;
return *this;
}

AwsIotWiFiClient &AwsIotWiFiClient::setClientId(const char *_clientId)
{
clientId = _clientId;
clientId = _clientId;

return *this;
return *this;
}

AwsIotWiFiClient &AwsIotWiFiClient::setSubscribeTopicFilter(const char *_subscribeTopicFilter)
{
subscribeTopicFilter = _subscribeTopicFilter;
subscribeTopicFilter = _subscribeTopicFilter;

return *this;
return *this;
}

void AwsIotWiFiClient::connect()
{
if (debugOutput)
{
Serial.println("Connecting to AWS IOT");
}
if (debugOutput)
{
Serial.println("Connecting to AWS IOT");
}

while (!pubSubClient->connect(clientId))
while (!pubSubClient->connect(clientId))
{
if (debugOutput)
{
if (debugOutput)
{
Serial.print(".");
}
delay(1000);
Serial.print(".");
}
delay(1000);
}

if (!pubSubClient->connected())
if (!pubSubClient->connected())
{
if (debugOutput)
{
if (debugOutput)
{
Serial.println("AWS IoT Timeout!");
}
return;
Serial.println("AWS IoT Timeout!");
}
return;
}

pubSubClient->subscribe(subscribeTopicFilter);
pubSubClient->subscribe(subscribeTopicFilter);

if (debugOutput)
{
Serial.println("AWS IoT Connected!");
}
if (debugOutput)
{
Serial.println("AWS IoT Connected!");
}
}

void AwsIotWiFiClient::loop()
{
if (!pubSubClient->connected())
{
connect();
}
if (!pubSubClient->connected())
{
connect();
}

pubSubClient->loop();
pubSubClient->loop();
}

boolean AwsIotWiFiClient::publishMessage(const char *topicName, const char *message)
{
return pubSubClient->publish(topicName, message);
return pubSubClient->publish(topicName, message);
}

0 comments on commit d4a0d59

Please sign in to comment.