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

Allow usage without ConnectionHandler #405

Closed
wants to merge 5 commits into from
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/ArduinoIoTCloud-Client/ArduinoIoTCloud-Client.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud.

* Connect a potentiometer (or other analog sensor) to A0.
* When the potentiometer (or sensor) value changes the data is sent to the Cloud.
* When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF.

IMPORTANT:
This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud.
On a LoRa board, if it is configured as a class A device (default and preferred option), values from Cloud dashboard are received
only after a value is sent to Cloud.

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

#include "arduino_secrets.h"
#include "thingProperties.h"

#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32)
static int const LED_BUILTIN = 2;
#endif

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

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

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

Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
unsigned long start = millis();
WiFi.begin(SECRET_SSID, SECRET_PASS);
while ((WiFi.status() != WL_CONNECTED) && ((millis() - start) < 3000)) {
delay(100);
}
Serial.println("WiFi connected");

/* Initialize Arduino IoT Cloud library */
ArduinoCloud.begin(client, udp);

setDebugMessageLevel(DBG_VERBOSE);
ArduinoCloud.printDebugInfo();
}

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

/*
* '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);
}
45 changes: 45 additions & 0 deletions examples/ArduinoIoTCloud-Client/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

/* A complete list of supported boards with WiFi is available here:
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
*/
#if defined(BOARD_HAS_WIFI)
#define SECRET_SSID ""
#define SECRET_PASS ""
#endif

/* ESP8266 ESP32*/
#if defined(BOARD_HAS_SECRET_KEY)
#define SECRET_DEVICE_KEY ""
#endif

/* MKR GSM 1400 */
#if defined(BOARD_HAS_GSM)
#define SECRET_PIN ""
#define SECRET_APN ""
#define SECRET_LOGIN ""
#define SECRET_PASS ""
#endif

/* MKR WAN 1300/1310 */
#if defined(BOARD_HAS_LORA)
#define SECRET_APP_EUI ""
#define SECRET_APP_KEY ""
#endif

/* MKR NB 1500 */
#if defined(BOARD_HAS_NB)
#define SECRET_PIN ""
#define SECRET_APN ""
#define SECRET_LOGIN ""
#define SECRET_PASS ""
#endif

/* Portenta H7 + Ethernet shield */
#if defined(BOARD_HAS_ETHERNET)
#define SECRET_OPTIONAL_IP ""
#define SECRET_OPTIONAL_DNS ""
#define SECRET_OPTIONAL_GATEWAY ""
#define SECRET_OPTIONAL_NETMASK ""
#endif
36 changes: 36 additions & 0 deletions examples/ArduinoIoTCloud-Client/thingProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#if defined(BOARD_HAS_WIFI)
WiFiClient client;
WiFiUDP udp;
#elif defined(BOARD_HAS_GSM)
#elif defined(BOARD_HAS_LORA)
#elif defined(BOARD_HAS_NB)
#elif defined(BOARD_HAS_ETHERNET)
#else
#error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what"
#endif

#if defined(BOARD_HAS_SECRET_KEY)
#define BOARD_ID ""
#endif

void onLedChange();

bool led;
int potentiometer;
int seconds;

void initProperties() {
#if defined(BOARD_HAS_SECRET_KEY)
ArduinoCloud.setBoardId(BOARD_ID);
ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY);
#endif
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET)
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
#elif defined(BOARD_HAS_LORA)
ArduinoCloud.addProperty(led, 1, READWRITE, ON_CHANGE, onLedChange);
ArduinoCloud.addProperty(potentiometer, 2, READ, ON_CHANGE);
ArduinoCloud.addProperty(seconds, 3, READ, 5 * MINUTES);
#endif
}
4 changes: 4 additions & 0 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

ArduinoIoTCloudClass::ArduinoIoTCloudClass()
: _connection{nullptr}
#ifdef HAS_TCP
, _client{nullptr}
#endif
, _adapter{NetworkAdapter::WIFI}
, _last_checked_property_index{0}
, _time_service(TimeService)
, _tz_offset{0}
Expand Down
8 changes: 8 additions & 0 deletions src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class ArduinoIoTCloudClass
inline bool deviceNotAttached() { return _thing_id == ""; }

inline ConnectionHandler * getConnection() { return _connection; }
#ifdef HAS_TCP
inline Client * getClient() { return _client; }
#endif


inline unsigned long getInternalTime() { return _time_service.getTime(); }
inline unsigned long getLocalTime() { return _time_service.getLocalTime(); }
Expand Down Expand Up @@ -153,6 +157,10 @@ class ArduinoIoTCloudClass
protected:

ConnectionHandler * _connection;
#ifdef HAS_TCP
Client * _client;
#endif
NetworkAdapter _adapter;
PropertyContainer _device_property_container;
PropertyContainer _thing_property_container;
unsigned int _last_checked_property_index;
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoIoTCloudLPWAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry)
{
_connection = &connection;
_retryEnable = retry;
_time_service.begin(nullptr);
_time_service.begin();
return 1;
}

Expand Down
48 changes: 33 additions & 15 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,22 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
{
_connection = &connection;
_brokerAddress = brokerAddress;
_brokerPort = brokerPort;
_time_service.begin(&connection);
return begin(enable_watchdog, _brokerAddress, _brokerPort);

return begin(_connection->getClient(), _connection->getUDP(), _connection->getInterface(), enable_watchdog, brokerAddress, brokerPort);
}

int ArduinoIoTCloudTCP::begin(Client & client, UDP & udp, NetworkAdapter adapter, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
{
_adapter = adapter;
_time_service.begin(udp);

return begin(client, enable_watchdog, brokerAddress, brokerPort);
}

int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
int ArduinoIoTCloudTCP::begin(Client & client, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
{
_client = &client;

_brokerAddress = brokerAddress;
_brokerPort = brokerPort;

Expand Down Expand Up @@ -160,9 +168,9 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
#endif

#if defined(BOARD_HAS_ECCX08)
_sslClient.setClient(_connection->getClient());
_sslClient.setClient(client);
#elif defined(ARDUINO_PORTENTA_C33)
_sslClient.setClient(_connection->getClient());
_sslClient.setClient(client);
_sslClient.setCACert(AIoTSSCert);
#elif defined(BOARD_HAS_SE050)
_sslClient.appendCustomCACert(AIoTSSCert);
Expand Down Expand Up @@ -234,7 +242,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
if (enable_watchdog) {
watchdog_enable();
bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false;
bool const use_ethernet = _adapter == NetworkAdapter::ETHERNET ? true : false;
watchdog_enable_network_feed(use_ethernet);
}
#endif
Expand Down Expand Up @@ -301,6 +309,11 @@ void ArduinoIoTCloudTCP::printDebugInfo()

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
{
if (_connection == nullptr)
{
return State::SyncTime;
}

if (_connection->check() == NetworkConnectionState::CONNECTED)
{
bool const is_retry_attempt = (_last_connection_attempt_cnt > 0);
Expand All @@ -313,12 +326,17 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
unsigned long const internal_posix_time = _time_service.getTime();
#pragma GCC diagnostic pop
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time);
return State::ConnectMqttBroker;
_time_service.sync();
Serial.println("_time_service.sync()");

if (_time_service.isTimeValid())
{
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, _time_service.getTime());
return State::ConnectMqttBroker;
}

// TODO: handle retry delay
return State::SyncTime;
}

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
Expand Down Expand Up @@ -585,7 +603,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
/* Transmit the cleared request flags to the cloud. */
sendDevicePropertyToCloud("OTA_REQ");
/* Call member function to handle OTA request. */
_ota_error = OTA::onRequest(_ota_url, _connection->getInterface());
_ota_error = OTA::onRequest(_ota_url, _adapter);
/* If something fails send the OTA error to the cloud */
sendDevicePropertyToCloud("OTA_ERROR");
}
Expand Down
6 changes: 5 additions & 1 deletion src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass

#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
int begin(Client & client, UDP & udp, NetworkAdapter adapter = NetworkAdapter::WIFI, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
#else
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
int begin(Client & client, UDP & udp, NetworkAdapter adapter = NetworkAdapter::WIFI, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
#endif
int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);


#ifdef BOARD_HAS_SECRET_KEY
inline void setBoardId (String const device_id) { setDeviceId(device_id); }
Expand Down Expand Up @@ -189,6 +191,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
onOTARequestCallbackFunc _get_ota_confirmation;
#endif /* OTA_ENABLED */

int begin(Client & client, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);

inline String getTopic_deviceout() { return String("/a/d/" + getDeviceId() + "/e/o");}
inline String getTopic_devicein () { return String("/a/d/" + getDeviceId() + "/e/i");}
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
Expand Down
22 changes: 11 additions & 11 deletions src/utility/time/NTPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

unsigned long NTPUtils::getTime(UDP & udp)
unsigned long NTPUtils::getTime(UDP * udp)
{
#ifdef NTP_USE_RANDOM_PORT
udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT));
udp->begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT));
#else
udp.begin(NTP_LOCAL_PORT);
udp->begin(NTP_LOCAL_PORT);
#endif

sendNTPpacket(udp);
Expand All @@ -48,16 +48,16 @@ unsigned long NTPUtils::getTime(UDP & udp)
do
{
is_timeout = (millis() - start) >= NTP_TIMEOUT_MS;
} while(!is_timeout && !udp.parsePacket());
} while(!is_timeout && !udp->parsePacket());

if(is_timeout) {
udp.stop();
udp->stop();
return 0;
}

uint8_t ntp_packet_buf[NTP_PACKET_SIZE];
udp.read(ntp_packet_buf, NTP_PACKET_SIZE);
udp.stop();
udp->read(ntp_packet_buf, NTP_PACKET_SIZE);
udp->stop();

unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]);
unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]);
Expand All @@ -72,7 +72,7 @@ unsigned long NTPUtils::getTime(UDP & udp)
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/

void NTPUtils::sendNTPpacket(UDP & udp)
void NTPUtils::sendNTPpacket(UDP * udp)
{
uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0};

Expand All @@ -85,9 +85,9 @@ void NTPUtils::sendNTPpacket(UDP & udp)
ntp_packet_buf[14] = 49;
ntp_packet_buf[15] = 52;

udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT);
udp.write(ntp_packet_buf, NTP_PACKET_SIZE);
udp.endPacket();
udp->beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT);
udp->write(ntp_packet_buf, NTP_PACKET_SIZE);
udp->endPacket();
}

int NTPUtils::getRandomPort(int const min_port, int const max_port)
Expand Down
4 changes: 2 additions & 2 deletions src/utility/time/NTPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NTPUtils
{
public:

static unsigned long getTime(UDP & udp);
static unsigned long getTime(UDP * udp);
static int getRandomPort(int const min_port, int const max_port);

private:
Expand All @@ -56,7 +56,7 @@ class NTPUtils
static unsigned long const NTP_TIMEOUT_MS = 1000;
static constexpr const char * NTP_TIME_SERVER = "time.arduino.cc";

static void sendNTPpacket(UDP & udp);
static void sendNTPpacket(UDP * udp);
};

#endif /* #ifndef HAS_LORA */
Expand Down
Loading
Loading