Skip to content

Commit 95fc47f

Browse files
committed
Improve example
1 parent e8db397 commit 95fc47f

File tree

4 files changed

+39
-57
lines changed

4 files changed

+39
-57
lines changed

examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,37 @@
1515
*/
1616

1717
#include "thingProperties.h"
18+
#include "aws_secrets.h"
19+
20+
Client& getDefaultClient() {
21+
switch(ArduinoIoTPreferredConnection.getInterface()) {
22+
23+
#ifdef BOARD_HAS_WIFI
24+
case NetworkAdapter::WIFI:
25+
static WiFiClient client;
26+
return client;
27+
#endif
28+
29+
#ifdef BOARD_HAS_ETHERNET
30+
case NetworkAdapter::ETHERNET:
31+
static EthernetClient client;
32+
return client;
33+
#endif
34+
35+
default:
36+
Serial.println("Error: could not create default AWS client");
37+
break;
38+
}
39+
40+
41+
}
1842

1943
unsigned long publishMillis = 0;
2044
unsigned long connectMillis = 0;
2145

46+
BearSSLClient sslClientAWS(getDefaultClient());
47+
MqttClient mqttClientAWS(sslClientAWS);
48+
2249
void setup() {
2350
/* Initialize serial and wait up to 5 seconds for port to open */
2451
Serial.begin(9600);
@@ -55,9 +82,9 @@ void loop() {
5582
return;
5683
}
5784

58-
if (AWSIoTPreferredConnection.check() != NetworkConnectionState::CONNECTED) {
59-
return;
60-
}
85+
//if (AWSIoTPreferredConnection.check() != NetworkConnectionState::CONNECTED) {
86+
// return;
87+
//}
6188

6289
if (!mqttClientAWS.connected()) {
6390
if (millis() - connectMillis > 5000) {
Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,2 @@
1-
#include <Arduino_ConnectionHandler.h>
2-
3-
/* A complete list of supported boards with WiFi is available here:
4-
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
5-
*/
6-
#if defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION)
7-
#define SECRET_WIFI_SSID ""
8-
#define SECRET_WIFI_PASS ""
9-
#endif
10-
11-
/* Portenta H7 + Ethernet shield */
12-
#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION)
13-
#define SECRET_ETH_OPTIONAL_IP ""
14-
#define SECRET_ETH_OPTIONAL_DNS ""
15-
#define SECRET_ETH_OPTIONAL_GATEWAY ""
16-
#define SECRET_ETH_OPTIONAL_NETMASK ""
17-
#endif
18-
19-
/* Portenta CAT.M1/NB IoT GNSS Shield */
20-
#if defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION)
21-
#define SECRET_CATM_PIN ""
22-
#define SECRET_CATM_APN ""
23-
#define SECRET_CATM_LOGIN ""
24-
#define SECRET_CATM_PASS ""
25-
#endif
1+
#define SECRET_SSID ""
2+
#define SECRET_OPTIONAL_PASS ""
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
2+
13
#include <ArduinoIoTCloud.h>
24
#include <Arduino_ConnectionHandler.h>
3-
#include "aws_config.h"
5+
#include "arduino_secrets.h"
46

5-
#if !(defined(BOARD_STM32H7))
6-
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what"
7-
#endif
7+
const char SSID[] = SECRET_SSID; // Network SSID (name)
8+
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
89

910
void onLedChange();
1011

@@ -18,28 +19,5 @@ void initProperties() {
1819
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
1920
}
2021

21-
//#define USE_ETHERNET_DHCP_CONNECTION
22-
//#define USE_ETHERNET_MANUAL_CONNECTION
23-
#define USE_WIFI_CONNECTION
24-
//#define USE_CATM1_NBIOT_CONNECTION
25-
26-
#include "arduino_secrets.h"
27-
28-
#if defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_CONNECTION)
29-
/* DHCP mode */
30-
EthernetConnectionHandler ArduinoIoTPreferredConnection;
31-
EthernetConnectionHandler AWSIoTPreferredConnection;
32-
#elif defined(BOARD_HAS_ETHERNET) && defined(USE_ETHERNET_MANUAL_CONNECTION)
33-
/* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */
34-
EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_ETH_OPTIONAL_IP, SECRET_ETH_OPTIONAL_DNS, SECRET_ETH_OPTIONAL_GATEWAY, SECRET_ETH_OPTIONAL_NETMASK);
35-
EthernetConnectionHandler AWSIoTPreferredConnection(SECRET_ETH_OPTIONAL_IP, SECRET_ETH_OPTIONAL_DNS, SECRET_ETH_OPTIONAL_GATEWAY, SECRET_ETH_OPTIONAL_NETMASK);
36-
#elif defined(BOARD_HAS_WIFI) && defined(USE_WIFI_CONNECTION)
37-
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
38-
WiFiConnectionHandler AWSIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
39-
#elif defined(BOARD_HAS_CATM1_NBIOT) && defined(USE_CATM1_NBIOT_CONNECTION)
40-
CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_CATM_PIN, SECRET_CATM_APN, SECRET_CATM_LOGIN, SECRET_CATM_PASS);
41-
CatM1ConnectionHandler AWSIoTPreferredConnection(SECRET_CATM_PIN, SECRET_CATM_APN, SECRET_CATM_LOGIN, SECRET_CATM_PASS);
42-
#endif
43-
44-
BearSSLClient sslClientAWS(AWSIoTPreferredConnection.getClient());
45-
MqttClient mqttClientAWS(sslClientAWS);
22+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_OPTIONAL_PASS);
23+

0 commit comments

Comments
 (0)