-
Notifications
You must be signed in to change notification settings - Fork 81
Use ArduinoBearSSL library #465
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
Changes from all commits
5f10263
c16a39c
6222f1b
95e3cff
5ebefcd
c7980ff
6188ed5
97f42eb
6b4dda9
9340617
8e2e915
243681a
9734bb1
a93867d
f0be6b2
f494e2c
2966cf7
bf25de6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define SECRET_SSID "" | ||
#define SECRET_OPTIONAL_PASS "" |
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----- | ||
)"; |
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
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. */ | ||
//#define ARDUINO_DISABLE_ECCX08 | ||
|
||
/* 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 | ||
|
||
/* If uncommented disables br_sslio_close call.From BearSSL docs: | ||
* | ||
* br_sslio_close(): perform the SSL closure protocol. This entails sending a | ||
* close_notify alert, and receiving a close_notify response. | ||
* | ||
* Note that a number of deployed SSL implementations do not follow the protocol | ||
* for closure, and may drop the underlying socket abruptly. As such, errors are | ||
* often reported by br_sslio_close(). | ||
* | ||
* In case of mbed-os + ArduinoIoTCloud br_sslio_close is endless looping | ||
* blocking sketch execution. | ||
*/ | ||
#define ARDUINO_BEARSSL_DISABLE_TLS_CLOSE | ||
|
||
#define BEAR_SSL_CLIENT_CHAIN_SIZE 1 | ||
|
||
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) ||\ | ||
defined(ARDUINO_GIGA) || defined(ARDUINO_NANO_RP2040_CONNECT) | ||
/* Allows download from OTA storage API */ | ||
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325) | ||
#endif | ||
|
||
#endif /* ARDUINO_BEARSSL_CONFIG_H_ */ |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this marked as moved? they seem a complete different file to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it is a complete different file 🤔