Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ArduinoBearSSL library #465

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ jobs:
# Install samd platform via Boards Manager
- name: arduino:samd
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
- name: RTCZero
- name: WiFi101
Expand All @@ -116,6 +117,7 @@ jobs:
- name: arduino:samd
- name: arduino:mbed_nano
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
- name: RTCZero
- name: WiFiNINA
Expand Down Expand Up @@ -143,6 +145,7 @@ jobs:
# Install samd platform via Boards Manager
- name: arduino:samd
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
- name: RTCZero
- name: MKRGSM
Expand All @@ -156,6 +159,7 @@ jobs:
# Install samd platform via Boards Manager
- name: arduino:samd
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
- name: RTCZero
- name: MKRNB
Expand All @@ -169,6 +173,7 @@ jobs:
# Install mbed_portenta platform via Boards Manager
- name: arduino:mbed_portenta
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
- name: Arduino_Cellular
sketch-paths: |
Expand All @@ -191,6 +196,7 @@ jobs:
# Install mbed_opta platform via Boards Manager
- name: arduino:mbed_opta
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
sketch-paths: |
- examples/ArduinoIoTCloud-DeferredOTA
Expand All @@ -202,6 +208,7 @@ jobs:
# Install mbed_giga platform via Boards Manager
- name: arduino:mbed_giga
libraries: |
- name: ArduinoBearSSL
- name: ArduinoECCX08
sketch-paths: |
- examples/ArduinoIoTCloud-DeferredOTA
Expand All @@ -222,6 +229,8 @@ jobs:
platforms: |
# Install renesas_uno platform via Boards Manager
- name: arduino:renesas_uno
libraries: |
- name: ArduinoBearSSL
# Nano ESP32
- board:
type: arduino_esp32
Expand Down
151 changes: 151 additions & 0 deletions examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include "arduino_secrets.h"
/*
This sketch demonstrates how to connect to ArduinoIoTCloud and AWS IoT core.

The full list of compatible boards can be found here:
- https://github.com/arduino-libraries/ArduinoIoTCloud#what
*/

#include "thingProperties.h"
#include "aws_secrets.h"

Client& getDefaultClient() {
switch(ArduinoIoTPreferredConnection.getInterface()) {

#ifdef BOARD_HAS_WIFI
case NetworkAdapter::WIFI:
static WiFiClient wclient;
return wclient;
#endif

#ifdef BOARD_HAS_ETHERNET
case NetworkAdapter::ETHERNET:
static EthernetClient eclient;
return eclient;
#endif

default:
Serial.println("Error: could not create default AWS client");
break;
}
}

unsigned long publishMillis = 0;
unsigned long connectMillis = 0;

BearSSLClient sslClientAWS(getDefaultClient());
MqttClient mqttClientAWS(sslClientAWS);

void setup() {
/* Initialize serial and wait up to 5 seconds for port to open */
Serial.begin(9600);

/* Configure LED pin as an output */
pinMode(LED_BUILTIN, OUTPUT);

/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
initProperties();

/* Initialize Arduino IoT Cloud library */
ArduinoCloud.begin(ArduinoIoTPreferredConnection, true, "iot.arduino.cc");

setDebugMessageLevel(5);
ArduinoCloud.printDebugInfo();

/* Initialize AWS Client */
ArduinoBearSSL.onGetTime(getTime);
sslClientAWS.setEccSlot(AWS_SLOT, AWS_CERTIFICATE);

mqttClientAWS.setId("ArduinoAWSClient");
mqttClientAWS.onMessage(onMessageReceived);
mqttClientAWS.setConnectionTimeout(10 * 1000);
mqttClientAWS.setKeepAliveInterval(30 * 1000);
mqttClientAWS.setCleanSession(false);
}

void loop() {
ArduinoCloud.update();
potentiometer = analogRead(A0);
seconds = millis() / 1000;

if (!ArduinoCloud.connected()) {
return;
}

if (!mqttClientAWS.connected()) {
if (millis() - connectMillis > 5000) {
connectMillis = millis();
// MQTT client is disconnected, connect
if (!connectMQTT()) {
return;
}
} else {
return;
}
}

// poll for new MQTT messages and send keep alive
mqttClientAWS.poll();

// publish a message roughly every 5 seconds.
if (millis() - publishMillis > 5000) {
publishMillis = millis();

publishMessage();
}
}

/*
* 'onLedChange' is called when the "led" property of your Thing changes
*/
void onLedChange() {
Serial.print("LED set to ");
Serial.println(led);
digitalWrite(LED_BUILTIN, led);
}

void onMessageReceived(int messageSize)
{
// we received a message, print out the topic and contents
Serial.print("Received a message with topic '");
Serial.print(mqttClientAWS.messageTopic());
Serial.print("', length ");
Serial.print(messageSize);
Serial.println(" bytes:");

for (int i = 0; i < messageSize; i++) {
const char c = mqttClientAWS.read();
Serial.print(c);
}
Serial.println();
}

int connectMQTT() {
Serial.print("Attempting to connect to MQTT broker: ");
Serial.print(AWS_BROKER);
Serial.println(" ");

if (!mqttClientAWS.connect(AWS_BROKER, 8883)) {
// failed, retry
Serial.print(".");
return 0;
}
Serial.println();

Serial.println("You're connected to the MQTT broker");
Serial.println();

// subscribe to a topic
mqttClientAWS.subscribe("arduino/incoming");
return 1;
}

void publishMessage() {
Serial.println("Publishing message");

// send message, the Print interface can be used to set the message contents
mqttClientAWS.beginMessage("arduino/outgoing");
mqttClientAWS.print("hello ");
mqttClientAWS.print(millis());
mqttClientAWS.endMessage();
}
2 changes: 2 additions & 0 deletions examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_OPTIONAL_PASS ""
10 changes: 10 additions & 0 deletions examples/ArduinoIoTCloud-AWS-Basic/aws_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Fill in the hostname of your AWS IoT broker */
#define AWS_BROKER ""

#define AWS_SLOT 4

/* Fill in the boards public certificate */
const char AWS_CERTIFICATE[] = R"(
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
)";
21 changes: 21 additions & 0 deletions examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)

void onLedChange();

bool led;
int potentiometer;
int seconds;

void initProperties() {
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_OPTIONAL_PASS);
2 changes: 2 additions & 0 deletions src/AIoTC_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@

#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325) // Allows download from storage API
#define BEAR_SSL_CLIENT_OBUF_SIZE (512 + 85)
#define BOARD_STM32H7
#endif

#if defined(ARDUINO_NANO_RP2040_CONNECT)
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325) // Allows download from storage API
#define BEAR_SSL_CLIENT_OBUF_SIZE (512 + 85)
#endif

#if defined(ARDUINO_EDGE_CONTROL)
Expand Down
43 changes: 43 additions & 0 deletions src/ArduinoBearSSLConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
This file is part of ArduinoIoTCloud.

Copyright 2024 ARDUINO SA (http://www.arduino.cc/)

This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html

You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/

#ifndef ARDUINO_BEARSSL_CONFIG_H_
#define ARDUINO_BEARSSL_CONFIG_H_

/* Enabling this define allows the usage of ArduinoBearSSL without crypto chip. */
#if defined(ARDUINO_UNOR4_WIFI)
#define ARDUINO_DISABLE_ECCX08
#endif

/* Enable/Disable global instances*/
#define ARDUINO_BEARSSL_DISABLE_AES128
#define ARDUINO_BEARSSL_DISABLE_DES
#define ARDUINO_BEARSSL_DISABLE_MD5
#define ARDUINO_BEARSSL_DISABLE_SHA1
#define ARDUINO_BEARSSL_DISABLE_SHA256

#define ARDUINO_BEARSSL_DISABLE_KEY_DECODER

/* If uncommented profile should be configured using client.setProfile(...) */
//#define ARDUINO_BEARSSL_DISABLE_FULL_CLIENT_PROFILE

/* If uncommented TA should be configured via constructor */
//#define ARDUINO_BEARSSL_DISABLE_BUILTIN_TRUST_ANCHORS

#define BEAR_SSL_CLIENT_CHAIN_SIZE 1

#endif /* ARDUINO_BEARSSL_CONFIG_H_ */
Loading
Loading