├── .codespellrc ├── .github ├── dependabot.yml └── workflows │ ├── check-arduino.yml │ ├── compile-examples.yml │ ├── report-size-deltas.yml │ ├── spell-check.yml │ ├── sync-labels.yml │ └── unit-tests.yml ├── .gitignore ├── LICENSE ├── README.md ├── docs ├── api.md └── readme.md ├── examples ├── ArduinoIoTCloud-AWS-Basic │ ├── ArduinoIoTCloud-AWS-Basic.ino │ ├── arduino_secrets.h │ ├── aws_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-Advanced │ ├── ArduinoIoTCloud-Advanced.ino │ ├── arduino_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-Basic │ ├── ArduinoIoTCloud-Basic.ino │ ├── arduino_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-BlockForOTA │ ├── ArduinoIoTCloud-BlockForOTA.ino │ ├── arduino_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-Callbacks │ ├── ArduinoIoTCloud-Callbacks.ino │ ├── arduino_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-DeferredOTA │ ├── ArduinoIoTCloud-DeferredOTA.ino │ ├── arduino_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-NetConfig │ ├── ArduinoIoTCloud-NetConfig.ino │ └── thingProperties.h ├── ArduinoIoTCloud-Notecard │ ├── ArduinoIoTCloud-Notecard.ino │ ├── README.md │ ├── arduino_secrets.h │ └── thingProperties.h ├── ArduinoIoTCloud-Schedule │ ├── ArduinoIoTCloud-Schedule.ino │ ├── arduino_secrets.h │ └── thingProperties.h └── utility │ ├── ArduinoIoTCloud_Travis_CI │ ├── ArduinoIoTCloud_Travis_CI.ino │ ├── arduino_secrets.h │ └── thingProperties.h │ ├── Provisioning │ └── Provisioning.ino │ ├── Provisioning_2.0 │ ├── CSRHandler.cpp │ ├── CSRHandler.h │ ├── ClaimingHandler.cpp │ ├── ClaimingHandler.h │ ├── Provisioning_2.0.ino │ ├── SecretsHelper.h │ └── thingProperties.h │ └── SelfProvisioning │ ├── SelfProvisioning.ino │ └── arduino_secrets.h ├── extras ├── test │ ├── CMakeLists.txt │ ├── include │ │ ├── Arduino.h │ │ ├── Arduino_ConnectionHandler.h │ │ └── util │ │ │ ├── CBORTestUtil.h │ │ │ └── PropertyTestUtil.h │ └── src │ │ ├── Arduino.cpp │ │ ├── test_CloudColor.cpp │ │ ├── test_CloudFloat.cpp │ │ ├── test_CloudLocation.cpp │ │ ├── test_CloudSchedule.cpp │ │ ├── test_CloudWrapperFloat.cpp │ │ ├── test_addPropertyReal.cpp │ │ ├── test_callback.cpp │ │ ├── test_command_decode.cpp │ │ ├── test_command_encode.cpp │ │ ├── test_decode.cpp │ │ ├── test_encode.cpp │ │ ├── test_main.cpp │ │ ├── test_publishEvery.cpp │ │ ├── test_publishOnChange.cpp │ │ ├── test_publishOnChangeRateLimit.cpp │ │ ├── test_readOnly.cpp │ │ ├── test_writeOnChange.cpp │ │ ├── test_writeOnDemand.cpp │ │ ├── test_writeOnly.cpp │ │ └── util │ │ ├── CBORTestUtil.cpp │ │ └── PropertyTestUtil.cpp └── tools │ ├── .gitignore │ ├── README.md │ ├── bin2ota.py │ ├── lzss.c │ ├── lzss.dylib │ ├── lzss.py │ └── lzss.so ├── keywords.txt ├── library.properties └── src ├── AIoTC_Config.h ├── AIoTC_Const.h ├── ArduinoBearSSLConfig.h ├── ArduinoECCX08Config.h ├── ArduinoIoTCloud.cpp ├── ArduinoIoTCloud.h ├── ArduinoIoTCloudDevice.cpp ├── ArduinoIoTCloudDevice.h ├── ArduinoIoTCloudLPWAN.cpp ├── ArduinoIoTCloudLPWAN.h ├── ArduinoIoTCloudNotecard.cpp ├── ArduinoIoTCloudNotecard.h ├── ArduinoIoTCloudTCP.cpp ├── ArduinoIoTCloudTCP.h ├── ArduinoIoTCloudThing.cpp ├── ArduinoIoTCloudThing.h ├── cbor ├── CBOR.h ├── CBORDecoder.cpp ├── CBORDecoder.h ├── CBOREncoder.cpp ├── CBOREncoder.h ├── IoTCloudMessageDecoder.cpp ├── IoTCloudMessageDecoder.h ├── IoTCloudMessageEncoder.cpp └── IoTCloudMessageEncoder.h ├── interfaces ├── CloudProcess.h └── MessageStream.h ├── message └── Commands.h ├── ota ├── OTA.h ├── OTAConfig.h ├── OTATypes.h ├── implementation │ ├── OTAEsp32.cpp │ ├── OTAEsp32.h │ ├── OTANanoRP2040.cpp │ ├── OTANanoRP2040.h │ ├── OTASTM32H7.cpp │ ├── OTASTM32H7.h │ ├── OTASamd.cpp │ ├── OTASamd.h │ ├── OTAUnoR4.cpp │ └── OTAUnoR4.h └── interface │ ├── OTAInterface.cpp │ ├── OTAInterface.h │ ├── OTAInterfaceDefault.cpp │ └── OTAInterfaceDefault.h ├── property ├── Property.cpp ├── Property.h ├── PropertyContainer.cpp ├── PropertyContainer.h ├── math_utils.h └── types │ ├── CloudBool.h │ ├── CloudColor.h │ ├── CloudFloat.h │ ├── CloudInt.h │ ├── CloudLocation.h │ ├── CloudSchedule.h │ ├── CloudString.h │ ├── CloudUnsignedInt.h │ ├── CloudWrapperBase.h │ ├── CloudWrapperBool.h │ ├── CloudWrapperFloat.h │ ├── CloudWrapperInt.h │ ├── CloudWrapperString.h │ ├── CloudWrapperUnsignedInt.h │ └── automation │ ├── CloudColoredLight.h │ ├── CloudContactSensor.h │ ├── CloudDimmedLight.h │ ├── CloudLight.h │ ├── CloudMotionSensor.h │ ├── CloudSmartPlug.h │ ├── CloudSwitch.h │ ├── CloudTelevision.h │ └── CloudTemperatureSensor.h ├── tls ├── AIoTCSSCert.h ├── AIoTCUPCert.h ├── BearSSLClientProfile.c ├── BearSSLClientProfile.h ├── BearSSLTrustAnchors.h └── utility │ ├── TLSClientMqtt.cpp │ ├── TLSClientMqtt.h │ ├── TLSClientOta.cpp │ └── TLSClientOta.h └── utility ├── time ├── NTPUtils.cpp ├── NTPUtils.h ├── RTCMillis.cpp ├── RTCMillis.h ├── TimeService.cpp └── TimeService.h └── watchdog ├── Watchdog.cpp └── Watchdog.h /.codespellrc: -------------------------------------------------------------------------------- 1 | # See: https://github.com/codespell-project/codespell#using-a-config-file 2 | [codespell] 3 | # In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: 4 | ignore-words-list = alocation,bu,wan 5 | check-filenames = 6 | check-hidden = 7 | skip = ./.git,./extras/test/external,./src/cbor/lib/tinycbor,./src/tls/bearssl 8 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # See: https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#about-the-dependabotyml-file 2 | version: 2 3 | 4 | updates: 5 | # Configure check for outdated GitHub Actions actions in workflows. 6 | # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/dependabot/README.md 7 | # See: https://docs.github.com/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot 8 | - package-ecosystem: github-actions 9 | directory: /.github/workflows/ 10 | schedule: 11 | interval: daily 12 | labels: 13 | - "topic: infrastructure" 14 | -------------------------------------------------------------------------------- /.github/workflows/check-arduino.yml: -------------------------------------------------------------------------------- 1 | name: Check Arduino 2 | 3 | # See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | pull_request: 7 | schedule: 8 | # Run every Tuesday at 8 AM UTC to catch breakage caused by new rules added to Arduino Lint. 9 | - cron: "0 8 * * TUE" 10 | workflow_dispatch: 11 | repository_dispatch: 12 | 13 | jobs: 14 | lint: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v4 20 | 21 | - name: Arduino Lint 22 | uses: arduino/arduino-lint-action@v2 23 | with: 24 | compliance: specification 25 | library-manager: update 26 | # Always use this setting for official repositories. Remove for 3rd party projects. 27 | official: true 28 | -------------------------------------------------------------------------------- /.github/workflows/report-size-deltas.yml: -------------------------------------------------------------------------------- 1 | name: Report Size Deltas 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | paths: 7 | - ".github/workflows/report-size-deltas.yml" 8 | schedule: 9 | # Run at the minimum interval allowed by GitHub Actions. 10 | # Note: GitHub Actions periodically has outages which result in workflow failures. 11 | # In this event, the workflows will start passing again once the service recovers. 12 | - cron: "*/5 * * * *" 13 | workflow_dispatch: 14 | repository_dispatch: 15 | 16 | jobs: 17 | report: 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Comment size deltas reports to PRs 21 | uses: arduino/report-size-deltas@v1 22 | with: 23 | # Regex matching the names of the workflow artifacts created by the "Compile Examples" workflow 24 | sketches-reports-source: ^sketches-report-.+ 25 | -------------------------------------------------------------------------------- /.github/workflows/spell-check.yml: -------------------------------------------------------------------------------- 1 | name: Spell Check 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v4 14 | 15 | - name: Spell check 16 | uses: codespell-project/actions-codespell@master 17 | -------------------------------------------------------------------------------- /.github/workflows/unit-tests.yml: -------------------------------------------------------------------------------- 1 | name: Unit Tests 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - ".github/workflows/unit-tests.yml" 7 | - 'extras/test/**' 8 | - 'src/**' 9 | 10 | push: 11 | paths: 12 | - ".github/workflows/unit-tests.yml" 13 | - 'extras/test/**' 14 | - 'src/**' 15 | 16 | jobs: 17 | test: 18 | name: Run unit tests 19 | runs-on: ubuntu-latest 20 | 21 | env: 22 | COVERAGE_DATA_PATH: extras/coverage-data/coverage.info 23 | 24 | steps: 25 | - name: Checkout 26 | uses: actions/checkout@v4 27 | 28 | - uses: arduino/cpp-test-action@main 29 | with: 30 | runtime-paths: | 31 | - extras/test/build/bin/testArduinoIoTCloud 32 | coverage-exclude-paths: | 33 | - '*/extras/test/*' 34 | - '/usr/*' 35 | coverage-data-path: ${{ env.COVERAGE_DATA_PATH }} 36 | 37 | # A token is used to avoid intermittent spurious job failures caused by rate limiting. 38 | - name: Set up Codecov upload token 39 | run: | 40 | if [[ "${{ github.repository }}" == "arduino-libraries/ArduinoIoTCloud" ]]; then 41 | # In order to avoid uploads of data from forks, only use the token for runs in the parent repo. 42 | # Token is intentionally exposed. 43 | # See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 44 | CODECOV_TOKEN="47827969-3fda-4ba1-9506-e8d0834ed88c" 45 | else 46 | # codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input. 47 | CODECOV_TOKEN="" 48 | fi 49 | echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV" 50 | 51 | - name: Upload coverage report to Codecov 52 | uses: codecov/codecov-action@v3 53 | with: 54 | file: "${{ env.COVERAGE_DATA_PATH }}" 55 | fail_ci_if_error: true 56 | token: ${{ env.CODECOV_TOKEN }} 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bin 2 | *.elf 3 | *~ 4 | .vscode 5 | *.orig 6 | .vs 7 | build 8 | .idea/ 9 | ### CMake ### 10 | CMakeLists.txt.user 11 | CMakeCache.txt 12 | CMakeFiles 13 | CMakeScripts 14 | Testing 15 | Makefile 16 | cmake_install.cmake 17 | install_manifest.txt 18 | compile_commands.json 19 | CTestTestfile.cmake 20 | _deps 21 | 22 | ### CMake Patch ### 23 | CMakeUserPresets.json 24 | 25 | # External projects 26 | *-prefix/ 27 | -------------------------------------------------------------------------------- /docs/readme.md: -------------------------------------------------------------------------------- 1 | # ArduinoIoTCloud 2 | 3 | The ArduinoIoTCloud library is the central element of the firmware enabling certain Arduino boards to connect to the [Arduino IoT Cloud](https://create.arduino.cc/iot/). 4 | 5 | - To get started, check out the [official documentation](https://docs.arduino.cc/arduino-cloud/) 6 | - For source code, check out the [GitHub repository](https://github.com/arduino-libraries/ArduinoIoTCloud) 7 | 8 | ## thingProperties.h 9 | 10 | When you are create and configure a Thing in the [Arduino IoT Cloud interface](https://create.arduino.cc/iot/), the `thingProperties.h` file is generated automatically. 11 | 12 | This file adds all the variable/properties along with its specifications (e.g. update policy, read/write permissions), and the type of connection handler depending on what device you have attached. 13 | 14 | Methods such as `addProperty()` and `setThingId()` are implemented here, so there's no need for manually entering anything. 15 | 16 | We recommend that **you do not edit this file** as it is automatically updating whenever you make changes in the Arduino IoT Cloud. 17 | 18 | ### Offline Editing 19 | 20 | If you have set up your sketch in an offline environment, make sure that whatever changes you make are reflected in this file, as it will be updated in the online environment. 21 | 22 | ## TCP / LPWAN 23 | 24 | Depending on what type of connection is used (TCP/LPWAN), either the `ArduinoIoTCloudTCP` or `ArduinoIoTCloudLPWAN` base classes are initialized. This specification is done in the Arduino IoT Cloud's "Thing" interface, and is reflected in the automatically generated `thingProperties.h` file. 25 | 26 | - If a board is configured as a **TCP** device, it will automatically include the `ArduinoIoTCloudTCP.h` file, and methods of the `ArduinoIoTCloudTCP` class will be made available. This is the most common option. 27 | - If a board is configured as an **LPWAN** device, it will instead include the `ArduinoIoTCloudLPWAN.h` file, and methods of the `ArduinoIoTCloudLPWAN` class will be made available. 28 | 29 | As a result, functions such as `begin()`, `update()`, `connected()` and `printDebugInfo()` are documented under each corresponding class. 30 | 31 | ## Connection Handler Library 32 | 33 | When the library is initialized via the `begin()` function, it will choose the specified **connection handler**, which is automatically added to your `thingProperties.h` file when configuring a Thing. 34 | 35 | The connection handler is done via another library, [Arduino_ConnectionHandler](https://github.com/arduino-libraries/Arduino_ConnectionHandler), which supports Wi-Fi®, GSM, NB-IoT, LoRa® & Ethernet. 36 | 37 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #define SECRET_SSID "" 2 | #define SECRET_OPTIONAL_PASS "" 3 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-AWS-Basic/aws_secrets.h: -------------------------------------------------------------------------------- 1 | /* Fill in the hostname of your AWS IoT broker */ 2 | #define AWS_BROKER "" 3 | 4 | #define AWS_SLOT 4 5 | 6 | /* Fill in the boards public certificate */ 7 | const char AWS_CERTIFICATE[] = R"( 8 | -----BEGIN CERTIFICATE----- 9 | -----END CERTIFICATE----- 10 | )"; 11 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h: -------------------------------------------------------------------------------- 1 | // Code generated by Arduino IoT Cloud, DO NOT EDIT. 2 | 3 | #include 4 | #include 5 | 6 | const char SSID[] = SECRET_SSID; // Network SSID (name) 7 | const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP) 8 | 9 | void onLedChange(); 10 | 11 | bool led; 12 | int potentiometer; 13 | int seconds; 14 | 15 | void initProperties() { 16 | ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); 17 | ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); 18 | ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); 19 | } 20 | 21 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_OPTIONAL_PASS); 22 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to use more complex cloud data types such as a colour or coordinates. 3 | 4 | IMPORTANT: 5 | This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. 6 | On a LoRa board, if it is configured as a class A device (default and preferred option), 7 | values from Cloud dashboard are received only after a value is sent to Cloud. 8 | 9 | The full list of compatible boards can be found here: 10 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 11 | */ 12 | 13 | #include "thingProperties.h" 14 | 15 | void setup() { 16 | /* Initialize serial and wait up to 5 seconds for port to open */ 17 | Serial.begin(9600); 18 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 19 | 20 | /* Set the debug message level: 21 | * - DBG_ERROR: Only show error messages 22 | * - DBG_WARNING: Show warning and error messages 23 | * - DBG_INFO: Show info, warning, and error messages 24 | * - DBG_DEBUG: Show debug, info, warning, and error messages 25 | * - DBG_VERBOSE: Show all messages 26 | */ 27 | setDebugMessageLevel(DBG_INFO); 28 | 29 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 30 | initProperties(); 31 | 32 | /* Initialize Arduino IoT Cloud library */ 33 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 34 | 35 | ArduinoCloud.printDebugInfo(); 36 | } 37 | 38 | float latMov = 45.5058224, lonMov = 9.1628673; 39 | float latArd = 45.0502078, lonArd = 7.6674765; 40 | 41 | float hueRed = 0.0, satRed = 100.0, briRed = 100.0; 42 | float hueGreen = 80.0, satGreen = 100.0, briGreen = 100.0; 43 | 44 | void loop() { 45 | ArduinoCloud.update(); 46 | } 47 | 48 | void onSwitchButtonChange() { 49 | if (switchButton) 50 | { 51 | location = Location(latMov, lonMov); 52 | color = Color(hueRed, satRed, briRed); 53 | } 54 | else 55 | { 56 | location = Location(latArd, lonArd); 57 | color = Color(hueGreen, satGreen, briGreen); 58 | } 59 | } 60 | 61 | void onColorChange() { 62 | Serial.print("Hue = "); 63 | Serial.println(color.getValue().hue); 64 | Serial.print("Sat = "); 65 | Serial.println(color.getValue().sat); 66 | Serial.print("Bri = "); 67 | Serial.println(color.getValue().bri); 68 | } 69 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Advanced/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32 */ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */ 17 | /* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */ 18 | #if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \ 19 | defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 20 | #define SECRET_PIN "" 21 | #define SECRET_APN "" 22 | #define SECRET_LOGIN "" 23 | #define SECRET_PASS "" 24 | #endif 25 | 26 | /* MKR WAN 1300/1310 */ 27 | #if defined(BOARD_HAS_LORA) 28 | #define SECRET_APP_EUI "" 29 | #define SECRET_APP_KEY "" 30 | #endif 31 | 32 | /* Portenta H7 + Ethernet shield */ 33 | #if defined(BOARD_HAS_ETHERNET) 34 | #define SECRET_OPTIONAL_IP "" 35 | #define SECRET_OPTIONAL_DNS "" 36 | #define SECRET_OPTIONAL_GATEWAY "" 37 | #define SECRET_OPTIONAL_NETMASK "" 38 | #endif 39 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Advanced/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "arduino_secrets.h" 4 | 5 | #if !(defined(HAS_TCP) || defined(HAS_LORA)) 6 | #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" 7 | #endif 8 | 9 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 10 | #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11 | #endif 12 | 13 | #if defined(HAS_LORA) 14 | #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 15 | #endif 16 | 17 | void onSwitchButtonChange(); 18 | void onColorChange(); 19 | 20 | bool switchButton; 21 | CloudLocation location; 22 | CloudColor color; 23 | 24 | void initProperties() { 25 | #if defined(HAS_TCP) 26 | ArduinoCloud.addProperty(switchButton, Permission::Write).onUpdate(onSwitchButtonChange); 27 | ArduinoCloud.addProperty(location, Permission::Read).publishOnChange(0.0f); 28 | ArduinoCloud.addProperty(color, Permission::ReadWrite).onUpdate(onColorChange); 29 | 30 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 31 | ArduinoCloud.setBoardId(BOARD_ID); 32 | ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); 33 | #endif 34 | #elif defined(HAS_LORA) 35 | ArduinoCloud.addProperty(switchButton, 1, Permission::Write).onUpdate(onSwitchButtonChange); 36 | ArduinoCloud.addProperty(location, 2, Permission::Read).publishOnChange(0.0f); 37 | ArduinoCloud.addProperty(color, 3, Permission::ReadWrite).onUpdate(onColorChange); 38 | 39 | ArduinoCloud.setThingId(THING_ID); 40 | #endif 41 | } 42 | 43 | #if defined(BOARD_HAS_WIFI) 44 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 45 | #elif defined(BOARD_HAS_GSM) 46 | GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 47 | #elif defined(BOARD_HAS_LORA) 48 | LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A); 49 | #elif defined(BOARD_HAS_NB) 50 | NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 51 | #elif defined(BOARD_HAS_CATM1_NBIOT) 52 | CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 53 | #elif defined(BOARD_HAS_ETHERNET) 54 | /* DHCP mode */ 55 | //EthernetConnectionHandler ArduinoIoTPreferredConnection; 56 | /* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ 57 | EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); 58 | #elif defined(BOARD_HAS_CELLULAR) 59 | CellularConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 60 | #endif 61 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud. 3 | 4 | * Connect a potentiometer (or other analog sensor) to A0. 5 | * When the potentiometer (or sensor) value changes the data is sent to the Cloud. 6 | * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. 7 | 8 | IMPORTANT: 9 | This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. 10 | On a LoRa board, if it is configured as a class A device (default and preferred option), 11 | values from Cloud dashboard are received only after a value is sent to Cloud. 12 | 13 | The full list of compatible boards can be found here: 14 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 15 | */ 16 | 17 | #include "thingProperties.h" 18 | 19 | #if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) 20 | static int const LED_BUILTIN = 2; 21 | #endif 22 | 23 | void setup() { 24 | /* Initialize serial and wait up to 5 seconds for port to open */ 25 | Serial.begin(9600); 26 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 27 | 28 | /* Set the debug message level: 29 | * - DBG_ERROR: Only show error messages 30 | * - DBG_WARNING: Show warning and error messages 31 | * - DBG_INFO: Show info, warning, and error messages 32 | * - DBG_DEBUG: Show debug, info, warning, and error messages 33 | * - DBG_VERBOSE: Show all messages 34 | */ 35 | setDebugMessageLevel(DBG_INFO); 36 | 37 | /* Configure LED pin as an output */ 38 | pinMode(LED_BUILTIN, OUTPUT); 39 | 40 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 41 | initProperties(); 42 | 43 | /* Initialize Arduino IoT Cloud library */ 44 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 45 | 46 | ArduinoCloud.printDebugInfo(); 47 | } 48 | 49 | void loop() { 50 | ArduinoCloud.update(); 51 | potentiometer = analogRead(A0); 52 | seconds = millis() / 1000; 53 | } 54 | 55 | /* 56 | * 'onLedChange' is called when the "led" property of your Thing changes 57 | */ 58 | void onLedChange() { 59 | Serial.print("LED set to "); 60 | Serial.println(led); 61 | digitalWrite(LED_BUILTIN, led); 62 | } 63 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Basic/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32 */ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */ 17 | /* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */ 18 | #if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \ 19 | defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 20 | #define SECRET_PIN "" 21 | #define SECRET_APN "" 22 | #define SECRET_LOGIN "" 23 | #define SECRET_PASS "" 24 | #endif 25 | 26 | /* MKR WAN 1300/1310 */ 27 | #if defined(BOARD_HAS_LORA) 28 | #define SECRET_APP_EUI "" 29 | #define SECRET_APP_KEY "" 30 | #endif 31 | 32 | /* Portenta H7 + Ethernet shield */ 33 | #if defined(BOARD_HAS_ETHERNET) 34 | #define SECRET_OPTIONAL_IP "" 35 | #define SECRET_OPTIONAL_DNS "" 36 | #define SECRET_OPTIONAL_GATEWAY "" 37 | #define SECRET_OPTIONAL_NETMASK "" 38 | #endif 39 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Basic/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "arduino_secrets.h" 4 | 5 | #if !(defined(HAS_TCP) || defined(HAS_LORA)) 6 | #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" 7 | #endif 8 | 9 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 10 | #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11 | #endif 12 | 13 | #if defined(HAS_LORA) 14 | #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 15 | #endif 16 | 17 | void onLedChange(); 18 | 19 | bool led; 20 | int potentiometer; 21 | int seconds; 22 | 23 | void initProperties() { 24 | #if defined(HAS_TCP) 25 | ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); 26 | ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); 27 | ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); 28 | 29 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 30 | ArduinoCloud.setBoardId(BOARD_ID); 31 | ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); 32 | #endif 33 | #elif defined(HAS_LORA) 34 | ArduinoCloud.addProperty(led, 1, Permission::ReadWrite).onUpdate(onLedChange); 35 | ArduinoCloud.addProperty(potentiometer, 2, Permission::Read).publishOnChange(10); 36 | ArduinoCloud.addProperty(seconds, 3, Permission::Read).publishEvery(5 * MINUTES); 37 | 38 | ArduinoCloud.setThingId(THING_ID); 39 | #endif 40 | } 41 | 42 | #if defined(BOARD_HAS_ETHERNET) 43 | /* DHCP mode */ 44 | //EthernetConnectionHandler ArduinoIoTPreferredConnection; 45 | /* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ 46 | EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); 47 | #elif defined(BOARD_HAS_WIFI) 48 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 49 | #elif defined(BOARD_HAS_GSM) 50 | GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 51 | #elif defined(BOARD_HAS_LORA) 52 | LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A); 53 | #elif defined(BOARD_HAS_NB) 54 | NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 55 | #elif defined(BOARD_HAS_CATM1_NBIOT) 56 | CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 57 | #elif defined(BOARD_HAS_CELLULAR) 58 | CellularConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 59 | #endif 60 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-BlockForOTA/ArduinoIoTCloud-BlockForOTA.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to optimize OTA in case of complex loop(). 3 | 4 | * Connect a potentiometer (or other analog sensor) to A0. 5 | * When the potentiometer (or sensor) value changes the data is sent to the Cloud. 6 | * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. 7 | 8 | IMPORTANT: 9 | This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. 10 | On a LoRa board, if it is configured as a class A device (default and preferred option), 11 | values from Cloud dashboard are received only after a value is sent to Cloud. 12 | 13 | The full list of compatible boards can be found here: 14 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 15 | */ 16 | 17 | #include "thingProperties.h" 18 | 19 | #if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) 20 | static int const LED_BUILTIN = 2; 21 | #endif 22 | 23 | bool block_for_ota { false }; 24 | bool ota_started { false }; 25 | 26 | bool onOTARequestCallback() { 27 | block_for_ota = true; 28 | ota_started = true; 29 | return true; 30 | } 31 | 32 | constexpr unsigned long printInterval { 1000 }; 33 | unsigned long printNow { printInterval }; 34 | 35 | void setup() { 36 | /* Initialize serial and wait up to 5 seconds for port to open */ 37 | Serial.begin(9600); 38 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 39 | 40 | /* Set the debug message level: 41 | * - DBG_ERROR: Only show error messages 42 | * - DBG_WARNING: Show warning and error messages 43 | * - DBG_INFO: Show info, warning, and error messages 44 | * - DBG_DEBUG: Show debug, info, warning, and error messages 45 | * - DBG_VERBOSE: Show all messages 46 | */ 47 | setDebugMessageLevel(DBG_VERBOSE); 48 | 49 | /* Configure LED pin as an output */ 50 | pinMode(LED_BUILTIN, OUTPUT); 51 | 52 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 53 | initProperties(); 54 | 55 | /* Initialize Arduino IoT Cloud library */ 56 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 57 | 58 | /* Setup OTA callback */ 59 | ArduinoCloud.onOTARequestCb(onOTARequestCallback); 60 | 61 | ArduinoCloud.printDebugInfo(); 62 | } 63 | 64 | void loop() { 65 | // When OTA is available, stay there until it completes. 66 | // The rest of the loop() does not run and the sketch 67 | // restarts automatically at the end of the OTA process. 68 | while (block_for_ota) { 69 | ArduinoCloud.update(); 70 | if (ota_started) { 71 | Serial.print("Waiting for OTA to finish..."); 72 | ota_started = false; 73 | } 74 | if (millis() > printNow) { 75 | Serial.print("."); 76 | printNow = millis() + printInterval; 77 | } 78 | } 79 | 80 | ArduinoCloud.update(); 81 | potentiometer = analogRead(A0); 82 | seconds = millis() / 1000; 83 | 84 | if (millis() > printNow) { 85 | Serial.println(millis()); 86 | printNow = millis() + printInterval; 87 | } 88 | } 89 | 90 | /* 91 | * 'onLedChange' is called when the "led" property of your Thing changes 92 | */ 93 | void onLedChange() { 94 | Serial.print("LED set to "); 95 | Serial.println(led); 96 | digitalWrite(LED_BUILTIN, led); 97 | } 98 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-BlockForOTA/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32 */ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */ 17 | /* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */ 18 | #if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \ 19 | defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 20 | #define SECRET_PIN "" 21 | #define SECRET_APN "" 22 | #define SECRET_LOGIN "" 23 | #define SECRET_PASS "" 24 | #endif 25 | 26 | /* MKR WAN 1300/1310 */ 27 | #if defined(BOARD_HAS_LORA) 28 | #define SECRET_APP_EUI "" 29 | #define SECRET_APP_KEY "" 30 | #endif 31 | 32 | /* Portenta H7 + Ethernet shield */ 33 | #if defined(BOARD_HAS_ETHERNET) 34 | #define SECRET_OPTIONAL_IP "" 35 | #define SECRET_OPTIONAL_DNS "" 36 | #define SECRET_OPTIONAL_GATEWAY "" 37 | #define SECRET_OPTIONAL_NETMASK "" 38 | #endif 39 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-BlockForOTA/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "arduino_secrets.h" 4 | 5 | #if !(defined(HAS_TCP) || defined(HAS_LORA)) 6 | #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" 7 | #endif 8 | 9 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 10 | #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11 | #endif 12 | 13 | #if defined(HAS_LORA) 14 | #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 15 | #endif 16 | 17 | void onLedChange(); 18 | 19 | bool led; 20 | int potentiometer; 21 | int seconds; 22 | 23 | void initProperties() { 24 | #if defined(HAS_TCP) 25 | ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); 26 | ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); 27 | ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); 28 | 29 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 30 | ArduinoCloud.setBoardId(BOARD_ID); 31 | ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); 32 | #endif 33 | #elif defined(HAS_LORA) 34 | ArduinoCloud.addProperty(led, 1, Permission::ReadWrite).onUpdate(onLedChange); 35 | ArduinoCloud.addProperty(potentiometer, 2, Permission::Read).publishOnChange(10); 36 | ArduinoCloud.addProperty(seconds, 3, Permission::Read).publishEvery(5 * MINUTES); 37 | 38 | ArduinoCloud.setThingId(THING_ID); 39 | #endif 40 | } 41 | 42 | #if defined(BOARD_HAS_ETHERNET) 43 | /* DHCP mode */ 44 | //EthernetConnectionHandler ArduinoIoTPreferredConnection; 45 | /* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ 46 | EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); 47 | #elif defined(BOARD_HAS_WIFI) 48 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 49 | #elif defined(BOARD_HAS_GSM) 50 | GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 51 | #elif defined(BOARD_HAS_LORA) 52 | LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A); 53 | #elif defined(BOARD_HAS_NB) 54 | NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 55 | #elif defined(BOARD_HAS_CATM1_NBIOT) 56 | CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 57 | #elif defined(BOARD_HAS_CELLULAR) 58 | CellularConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 59 | #endif 60 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to subscribe to IoT Cloud events and perform actions 3 | The available events are 4 | 5 | CONNECT : Board successfully connects to IoT Cloud 6 | SYNC : Data is successfully synced between Board and IoT Cloud 7 | DISCONNECT : Board has lost connection to IoT Cloud 8 | 9 | You don't need any specific Properties to be created in order to demonstrate these functionalities. 10 | Simply create a new Thing and give it 1 arbitrary Property. 11 | Remember that the Thing ID needs to be configured in thingProperties.h 12 | These events can be very useful in particular cases, for instance to disable a peripheral 13 | or a connected sensor/actuator when no data connection is available, as well as to perform 14 | specific operations on connection or right after properties values are synchronised. 15 | 16 | To subscribe to an event you can use the `addCallback` method and specify 17 | which event will trigger which custom function. 18 | One function per event can be assigned. 19 | 20 | IMPORTANT: 21 | This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. 22 | On a LoRa board, if it is configured as a class A device (default and preferred option), 23 | values from Cloud dashboard are received only after a value is sent to Cloud. 24 | 25 | The full list of compatible boards can be found here: 26 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 27 | */ 28 | 29 | #include "thingProperties.h" 30 | 31 | void setup() { 32 | /* Initialize serial and wait up to 5 seconds for port to open */ 33 | Serial.begin(9600); 34 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 35 | 36 | /* Set the debug message level: 37 | * - DBG_ERROR: Only show error messages 38 | * - DBG_WARNING: Show warning and error messages 39 | * - DBG_INFO: Show info, warning, and error messages 40 | * - DBG_DEBUG: Show debug, info, warning, and error messages 41 | * - DBG_VERBOSE: Show all messages 42 | */ 43 | setDebugMessageLevel(DBG_INFO); 44 | 45 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 46 | initProperties(); 47 | 48 | /* Initialize Arduino IoT Cloud library */ 49 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 50 | 51 | /* 52 | Invoking `addCallback` on the ArduinoCloud object allows you to subscribe 53 | to any of the available events and decide which functions to call when they are fired. 54 | 55 | The functions `doThisOnConnect`, `doThisOnSync`, `doThisOnDisconnect` 56 | are custom functions and can be named to your likings and for this example 57 | they are defined/implemented at the bottom of the Sketch 58 | */ 59 | ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect); 60 | ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync); 61 | ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect); 62 | 63 | ArduinoCloud.printDebugInfo(); 64 | } 65 | 66 | void loop() { 67 | ArduinoCloud.update(); 68 | } 69 | 70 | void doThisOnConnect(){ 71 | /* add your custom code here */ 72 | Serial.println("Board successfully connected to Arduino IoT Cloud"); 73 | } 74 | void doThisOnSync(){ 75 | /* add your custom code here */ 76 | Serial.println("Thing Properties synchronised"); 77 | } 78 | void doThisOnDisconnect(){ 79 | /* add your custom code here */ 80 | Serial.println("Board disconnected from Arduino IoT Cloud"); 81 | } -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32 */ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */ 17 | /* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */ 18 | #if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \ 19 | defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 20 | #define SECRET_PIN "" 21 | #define SECRET_APN "" 22 | #define SECRET_LOGIN "" 23 | #define SECRET_PASS "" 24 | #endif 25 | 26 | /* MKR WAN 1300/1310 */ 27 | #if defined(BOARD_HAS_LORA) 28 | #define SECRET_APP_EUI "" 29 | #define SECRET_APP_KEY "" 30 | #endif 31 | 32 | /* Portenta H7 + Ethernet shield */ 33 | #if defined(BOARD_HAS_ETHERNET) 34 | #define SECRET_OPTIONAL_IP "" 35 | #define SECRET_OPTIONAL_DNS "" 36 | #define SECRET_OPTIONAL_GATEWAY "" 37 | #define SECRET_OPTIONAL_NETMASK "" 38 | #endif 39 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Callbacks/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "arduino_secrets.h" 4 | 5 | #if !(defined(HAS_TCP) || defined(HAS_LORA)) 6 | #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" 7 | #endif 8 | 9 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 10 | #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11 | #endif 12 | 13 | #if defined(BOARD_HAS_LORA) 14 | #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 15 | #endif 16 | 17 | void initProperties() { 18 | #if defined(HAS_TCP) 19 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 20 | ArduinoCloud.setBoardId(BOARD_ID); 21 | ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); 22 | #endif 23 | #elif defined(HAS_LORA) 24 | ArduinoCloud.setThingId(THING_ID); 25 | #endif 26 | } 27 | 28 | #if defined(BOARD_HAS_WIFI) 29 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 30 | #elif defined(BOARD_HAS_GSM) 31 | GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 32 | #elif defined(BOARD_HAS_LORA) 33 | LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A); 34 | #elif defined(BOARD_HAS_NB) 35 | NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 36 | #elif defined(BOARD_HAS_CATM1_NBIOT) 37 | CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 38 | #elif defined(BOARD_HAS_ETHERNET) 39 | /* DHCP mode */ 40 | //EthernetConnectionHandler ArduinoIoTPreferredConnection; 41 | /* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ 42 | EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); 43 | #elif defined(BOARD_HAS_CELLULAR) 44 | CellularConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 45 | #endif 46 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-DeferredOTA/ArduinoIoTCloud-DeferredOTA.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to handle deferred OTA from Arduino IoT Cloud. 3 | 4 | Deferred OTA can be triggered using the arduino-cloud-cli with the following command: 5 | ./arduino-cloud-cli ota upload --device-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --file filename.ino.bin --deferred 6 | The update file and the download link will be available to be used within one week. 7 | 8 | * always_deny callback will always postpone the OTA update 9 | * always_allow callback will immediately apply the OTA update 10 | * ask_user_via_serial callback will read user input from serial to apply or postpone OTA update 11 | 12 | IMPORTANT: 13 | This sketch works with WiFi and Ethernet enabled boards supported by Arduino IoT Cloud. 14 | 15 | The full list of compatible boards can be found here: 16 | - https://github.com/arduino-libraries/ArduinoIoTCloud/#ota 17 | */ 18 | 19 | #include "thingProperties.h" 20 | 21 | #if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) 22 | static int const LED_BUILTIN = 2; 23 | #endif 24 | 25 | bool always_deny() { 26 | return false; 27 | } 28 | 29 | bool always_allow() { 30 | return true; 31 | } 32 | 33 | static bool ask_user_via_serial_first_run = true; 34 | bool ask_user_via_serial() { 35 | if (ask_user_via_serial_first_run) { 36 | Serial.println("Apply OTA? y / [n]"); 37 | ask_user_via_serial_first_run = false; 38 | } 39 | if (Serial.available()) { 40 | char c = Serial.read(); 41 | if (c == 'y' || c == 'Y') { 42 | return true; 43 | } 44 | } 45 | return false; 46 | } 47 | 48 | bool onOTARequestCallback() 49 | { 50 | /* Select the preferred behaviour changing the called function */ 51 | //return always_deny(); 52 | //return always_allow(); 53 | return ask_user_via_serial(); 54 | } 55 | 56 | void setup() { 57 | /* Initialize serial and wait up to 5 seconds for port to open */ 58 | Serial.begin(9600); 59 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 60 | 61 | /* Set the debug message level: 62 | * - DBG_ERROR: Only show error messages 63 | * - DBG_WARNING: Show warning and error messages 64 | * - DBG_INFO: Show info, warning, and error messages 65 | * - DBG_DEBUG: Show debug, info, warning, and error messages 66 | * - DBG_VERBOSE: Show all messages 67 | */ 68 | setDebugMessageLevel(DBG_INFO); 69 | 70 | /* Configure LED pin as an output */ 71 | pinMode(LED_BUILTIN, OUTPUT); 72 | 73 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 74 | initProperties(); 75 | 76 | /* Initialize Arduino IoT Cloud library */ 77 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 78 | 79 | /* Setup OTA callback */ 80 | ArduinoCloud.onOTARequestCb(onOTARequestCallback); 81 | 82 | ArduinoCloud.printDebugInfo(); 83 | } 84 | 85 | void loop() { 86 | ArduinoCloud.update(); 87 | } 88 | 89 | /* 90 | * 'onLedChange' is called when the "led" property of your Thing changes 91 | */ 92 | void onLedChange() { 93 | Serial.print("LED set to "); 94 | Serial.println(led); 95 | digitalWrite(LED_BUILTIN, led); 96 | } 97 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-DeferredOTA/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32 */ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* Portenta H7 + Ethernet shield */ 17 | #if defined(BOARD_HAS_ETHERNET) 18 | #define SECRET_OPTIONAL_IP "" 19 | #define SECRET_OPTIONAL_DNS "" 20 | #define SECRET_OPTIONAL_GATEWAY "" 21 | #define SECRET_OPTIONAL_NETMASK "" 22 | #endif 23 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-DeferredOTA/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "arduino_secrets.h" 4 | 5 | #if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_ETHERNET)) 6 | #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" 7 | #endif 8 | 9 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 10 | #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11 | #endif 12 | 13 | void onLedChange(); 14 | 15 | bool led; 16 | 17 | void initProperties() { 18 | #if defined(HAS_TCP) 19 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 20 | ArduinoCloud.setBoardId(BOARD_ID); 21 | ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); 22 | #endif 23 | #endif 24 | ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); 25 | } 26 | 27 | #if defined(BOARD_HAS_WIFI) 28 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 29 | #elif defined(BOARD_HAS_ETHERNET) 30 | /* DHCP mode */ 31 | //EthernetConnectionHandler ArduinoIoTPreferredConnection; 32 | /* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ 33 | EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); 34 | #endif 35 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-NetConfig/ArduinoIoTCloud-NetConfig.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud. 3 | 4 | * Connect a potentiometer (or other analog sensor) to A0. 5 | * When the potentiometer (or sensor) value changes the data is sent to the Cloud. 6 | * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. 7 | 8 | IMPORTANT: 9 | This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. 10 | On a LoRa board, if it is configured as a class A device (default and preferred option), 11 | values from Cloud dashboard are received only after a value is sent to Cloud. 12 | 13 | The full list of compatible boards can be found here: 14 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 15 | */ 16 | 17 | #include "thingProperties.h" 18 | 19 | #if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) 20 | static int const LED_BUILTIN = 2; 21 | #endif 22 | 23 | void setup() { 24 | /* Initialize serial and wait up to 5 seconds for port to open */ 25 | Serial.begin(9600); 26 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 27 | 28 | /* Set the debug message level: 29 | * - DBG_ERROR: Only show error messages 30 | * - DBG_WARNING: Show warning and error messages 31 | * - DBG_INFO: Show info, warning, and error messages 32 | * - DBG_DEBUG: Show debug, info, warning, and error messages 33 | * - DBG_VERBOSE: Show all messages 34 | */ 35 | setDebugMessageLevel(DBG_INFO); 36 | 37 | /* Configure LED pin as an output */ 38 | pinMode(LED_BUILTIN, OUTPUT); 39 | 40 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 41 | initProperties(); 42 | 43 | /* Initialize Arduino IoT Cloud library */ 44 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 45 | 46 | ArduinoCloud.printDebugInfo(); 47 | } 48 | 49 | void loop() { 50 | ArduinoCloud.update(); 51 | potentiometer = analogRead(A0); 52 | seconds = millis() / 1000; 53 | } 54 | 55 | /* 56 | * 'onLedChange' is called when the "led" property of your Thing changes 57 | */ 58 | void onLedChange() { 59 | Serial.print("LED set to "); 60 | Serial.println(led); 61 | digitalWrite(LED_BUILTIN, led); 62 | } 63 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-NetConfig/thingProperties.h: -------------------------------------------------------------------------------- 1 | #if !defined(ARDUINO_SAMD_MKRWIFI1010) && !defined(ARDUINO_SAMD_NANO_33_IOT) && !defined(ARDUINO_NANO_RP2040_CONNECT) \ 2 | && !defined(ARDUINO_PORTENTA_H7_M7) && !defined(ARDUINO_NICLA_VISION) && !defined(ARDUINO_OPTA) && !defined(ARDUINO_GIGA) \ 3 | && !defined(ARDUINO_UNOR4_WIFI) && !defined(ARDUINO_PORTENTA_C33) 4 | #error "This example is not compatible with this board." 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | void onLedChange(); 14 | 15 | bool led; 16 | int potentiometer; 17 | int seconds; 18 | 19 | GenericConnectionHandler ArduinoIoTPreferredConnection; 20 | KVStore kvStore; 21 | NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection); 22 | BLEAgentClass BLEAgent; 23 | SerialAgentClass SerialAgent; 24 | 25 | void initProperties() { 26 | NetworkConfigurator.addAgent(BLEAgent); 27 | NetworkConfigurator.addAgent(SerialAgent); 28 | NetworkConfigurator.setStorage(kvStore); 29 | 30 | /* For changing the default reset pin uncomment and set your preferred pin. 31 | * Use DISABLE_PIN for disabling the reset procedure. 32 | * The pin must be in the list of digital pins usable for interrupts. 33 | * Please refer to the Arduino documentation for more details: 34 | * https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/ 35 | */ 36 | //NetworkConfigurator.setReconfigurePin(your_pin); 37 | 38 | /* Otherwise if you need to monitor the pin status changes 39 | * you can set a custom callback function that is fired on every change 40 | */ 41 | //NetworkConfigurator.setPinChangedCallback(your_callback); 42 | 43 | ArduinoCloud.setConfigurator(NetworkConfigurator); 44 | 45 | ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); 46 | ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); 47 | ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Notecard/ArduinoIoTCloud-Notecard.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch demonstrates how to exchange data between your board and the 3 | Arduino IoT Cloud, while using the Notecard for wireless communication. 4 | 5 | * Connect a potentiometer (or other analog sensor) to A0. 6 | * When the potentiometer (or sensor) value changes the data is sent to the Cloud. 7 | * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. 8 | 9 | IMPORTANT: 10 | This sketch works with any Wi-Fi, Cellular, LoRa or Satellite enabled Notecard. 11 | 12 | The full list of compatible boards can be found here: 13 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 14 | */ 15 | 16 | #include 17 | #include "thingProperties.h" 18 | 19 | #if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) 20 | static int const LED_BUILTIN = 2; 21 | #endif 22 | 23 | /* 24 | * Choose an interrupt capable pin to reduce polling and improve 25 | * the overall responsiveness of the ArduinoIoTCloud library 26 | */ 27 | // #define ATTN_PIN 9 28 | 29 | void setup() { 30 | /* Initialize serial and wait up to 5 seconds for port to open */ 31 | Serial.begin(9600); 32 | for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } 33 | 34 | /* Set the debug message level: 35 | * - DBG_ERROR: Only show error messages 36 | * - DBG_WARNING: Show warning and error messages 37 | * - DBG_INFO: Show info, warning, and error messages 38 | * - DBG_DEBUG: Show debug, info, warning, and error messages 39 | * - DBG_VERBOSE: Show all messages 40 | */ 41 | setDebugMessageLevel(DBG_INFO); 42 | 43 | /* Configure LED pin as an output */ 44 | pinMode(LED_BUILTIN, OUTPUT); 45 | 46 | /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 47 | initProperties(); 48 | 49 | /* Initialize Arduino IoT Cloud library */ 50 | #ifndef ATTN_PIN 51 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 52 | ArduinoCloud.setNotecardPollingInterval(3000); // default: 1000ms, min: 250ms 53 | #else 54 | ArduinoCloud.begin(ArduinoIoTPreferredConnection, ATTN_PIN); 55 | #endif 56 | 57 | ArduinoCloud.printDebugInfo(); 58 | } 59 | 60 | void loop() { 61 | ArduinoCloud.update(); 62 | potentiometer = analogRead(A0); 63 | seconds = millis() / 1000; 64 | } 65 | 66 | /* 67 | * 'onLedChange' is called when the "led" property of your Thing changes 68 | */ 69 | void onLedChange() { 70 | Serial.print("LED set to "); 71 | Serial.println(led); 72 | digitalWrite(LED_BUILTIN, led); 73 | } 74 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Notecard/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* If provided, the Wi-Fi Credentials will be passed along to the Notecard. If 4 | * the Notecard supports Wi-Fi, it will attempt to connect to the network using 5 | * these credentials, if not, the Notecard will safely ignore these values. */ 6 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 7 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 8 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Notecard/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include "arduino_secrets.h" 6 | 7 | /* The Notecard can provide connectivity to almost any board via ESLOV (I2C) 8 | * or UART. An empty string (or the default value provided below) will not 9 | * override the Notecard's existing configuration. 10 | * Learn more at: https://dev.blues.io */ 11 | #define NOTECARD_PRODUCT_UID "com.domain.you:product" 12 | 13 | /* Uncomment the following line to use the Notecard over UART */ 14 | // #define UART_INTERFACE Serial1 15 | 16 | void onLedChange(); 17 | 18 | bool led; 19 | int potentiometer; 20 | int seconds; 21 | 22 | #ifndef UART_INTERFACE 23 | NotecardConnectionHandler ArduinoIoTPreferredConnection(NOTECARD_PRODUCT_UID); 24 | #else 25 | NotecardConnectionHandler ArduinoIoTPreferredConnection(NOTECARD_PRODUCT_UID, UART_INTERFACE); 26 | #endif 27 | 28 | void initProperties() { 29 | ArduinoCloud.addProperty(led, Permission::ReadWrite).onUpdate(onLedChange); 30 | ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); 31 | ArduinoCloud.addProperty(seconds, Permission::Read).publishEvery(5 * MINUTES); 32 | 33 | if (::strncmp(SECRET_WIFI_SSID, "YOUR_WIFI_NETWORK_NAME", sizeof(SECRET_WIFI_SSID))) { 34 | ArduinoIoTPreferredConnection.setWiFiCredentials(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Schedule/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32 */ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */ 17 | /* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */ 18 | #if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \ 19 | defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 20 | #define SECRET_PIN "" 21 | #define SECRET_APN "" 22 | #define SECRET_LOGIN "" 23 | #define SECRET_PASS "" 24 | #endif 25 | 26 | /* Portenta H7 + Ethernet shield */ 27 | #if defined(BOARD_HAS_ETHERNET) 28 | #define SECRET_OPTIONAL_IP "" 29 | #define SECRET_OPTIONAL_DNS "" 30 | #define SECRET_OPTIONAL_GATEWAY "" 31 | #define SECRET_OPTIONAL_NETMASK "" 32 | #endif 33 | -------------------------------------------------------------------------------- /examples/ArduinoIoTCloud-Schedule/thingProperties.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "arduino_secrets.h" 4 | 5 | #if !defined(HAS_TCP) 6 | #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" 7 | #endif 8 | 9 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 10 | #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11 | #endif 12 | 13 | void onSwitchButtonChange(); 14 | 15 | bool switchButton; 16 | CloudSchedule oneShot; 17 | CloudSchedule minute; 18 | CloudSchedule hourly; 19 | CloudSchedule daily; 20 | CloudSchedule weekly; 21 | CloudSchedule monthly; 22 | CloudSchedule yearly; 23 | 24 | void initProperties() { 25 | #if defined(HAS_TCP) 26 | ArduinoCloud.addProperty(switchButton, Permission::Write); 27 | ArduinoCloud.addProperty(oneShot, Permission::ReadWrite); 28 | ArduinoCloud.addProperty(minute, Permission::ReadWrite); 29 | ArduinoCloud.addProperty(hourly, Permission::ReadWrite); 30 | ArduinoCloud.addProperty(daily, Permission::ReadWrite); 31 | ArduinoCloud.addProperty(weekly, Permission::ReadWrite); 32 | ArduinoCloud.addProperty(monthly, Permission::ReadWrite); 33 | ArduinoCloud.addProperty(yearly, Permission::ReadWrite); 34 | 35 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 36 | ArduinoCloud.setBoardId(BOARD_ID); 37 | ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); 38 | #endif 39 | #endif 40 | } 41 | 42 | #if defined(BOARD_HAS_WIFI) 43 | WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); 44 | #elif defined(BOARD_HAS_GSM) 45 | GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 46 | #elif defined(BOARD_HAS_NB) 47 | NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 48 | #elif defined(BOARD_HAS_CATM1_NBIOT) 49 | CatM1ConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 50 | #elif defined(BOARD_HAS_ETHERNET) 51 | /* DHCP mode */ 52 | //EthernetConnectionHandler ArduinoIoTPreferredConnection; 53 | /* Manual mode. It will fallback in DHCP mode if SECRET_OPTIONAL_IP is invalid or equal to "0.0.0.0" */ 54 | EthernetConnectionHandler ArduinoIoTPreferredConnection(SECRET_OPTIONAL_IP, SECRET_OPTIONAL_DNS, SECRET_OPTIONAL_GATEWAY, SECRET_OPTIONAL_NETMASK); 55 | #elif defined(BOARD_HAS_CELLULAR) 56 | CellularConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); 57 | #endif 58 | -------------------------------------------------------------------------------- /examples/utility/ArduinoIoTCloud_Travis_CI/ArduinoIoTCloud_Travis_CI.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This sketch is used in combination with Travis CI to check if 3 | unintentional breaking changes are made to the user facing 4 | Arduino IoT Cloud API. 5 | 6 | IMPORTANT: 7 | This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. 8 | On a LoRa board, if it is configured as a class A device (default and preferred option), 9 | values from Cloud dashboard are received only after a value is sent to Cloud. 10 | 11 | The full list of compatible boards can be found here: 12 | - https://github.com/arduino-libraries/ArduinoIoTCloud#what 13 | */ 14 | 15 | #include "thingProperties.h" 16 | 17 | void setup() { 18 | 19 | Serial.begin(9600); 20 | unsigned long serialBeginTime = millis(); 21 | while (!Serial && (millis() - serialBeginTime <= 5000)); 22 | 23 | Serial.println("Starting Arduino IoT Cloud Example"); 24 | 25 | initProperties(); 26 | ArduinoCloud.begin(ArduinoIoTPreferredConnection); 27 | 28 | ArduinoCloud.printDebugInfo(); 29 | } 30 | 31 | void loop() { 32 | ArduinoCloud.update(); 33 | 34 | } 35 | 36 | void onBoolPropertyChange() { 37 | Serial.println("'onBoolPropertyChange'"); 38 | } 39 | 40 | void onIntPropertyChange() { 41 | Serial.println("'onIntPropertyChange'"); 42 | } 43 | 44 | void onFloatPropertyChange() { 45 | Serial.println("'onFloatPropertyChange'"); 46 | } 47 | 48 | void onStringPropertyChange() { 49 | Serial.println("'onStringPropertyChange'"); 50 | } 51 | -------------------------------------------------------------------------------- /examples/utility/ArduinoIoTCloud_Travis_CI/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #include 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) 7 | #define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" 8 | #define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" 9 | #endif 10 | 11 | /* ESP8266 ESP32*/ 12 | #if !defined(BOARD_HAS_SECURE_ELEMENT) 13 | #define SECRET_DEVICE_KEY "my-device-password" 14 | #endif 15 | 16 | /* MKR GSM 1400 */ /* MKR NB 1500 */ /* Portenta CAT.M1/NB IoT GNSS Shield */ 17 | /* Portenta H7 and C33 + Portenta Mid Carrier + 4G Module */ 18 | #if defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || \ 19 | defined(BOARD_HAS_CATM1_NBIOT) || defined(BOARD_HAS_CELLULAR) 20 | #define SECRET_PIN "" 21 | #define SECRET_APN "" 22 | #define SECRET_LOGIN "" 23 | #define SECRET_PASS "" 24 | #endif 25 | 26 | /* MKR WAN 1300/1310 */ 27 | #if defined(BOARD_HAS_LORA) 28 | #define SECRET_APP_EUI "" 29 | #define SECRET_APP_KEY "" 30 | #endif 31 | 32 | /* Portenta H7 + Ethernet shield */ 33 | #if defined(BOARD_HAS_ETHERNET) 34 | #define SECRET_OPTIONAL_IP "" 35 | #define SECRET_OPTIONAL_DNS "" 36 | #define SECRET_OPTIONAL_GATEWAY "" 37 | #define SECRET_OPTIONAL_NETMASK "" 38 | #endif 39 | -------------------------------------------------------------------------------- /examples/utility/Provisioning_2.0/CSRHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2024 Arduino SA 3 | 4 | This Source Code Form is subject to the terms of the Mozilla Public 5 | License, v. 2.0. If a copy of the MPL was not distributed with this 6 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | #pragma once 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "utility/LEDFeedback.h" 16 | #define JITTER_BASE 0 17 | #define JITTER_MAX 1000 18 | 19 | class CSRHandlerClass { 20 | public: 21 | CSRHandlerClass(); 22 | ~CSRHandlerClass(); 23 | enum class CSRHandlerStates { 24 | BUILD_CSR, 25 | REQUEST_SIGNATURE, 26 | WAITING_RESPONSE, 27 | PARSE_RESPONSE, 28 | BUILD_CERTIFICATE, 29 | CERT_CREATED, 30 | WAITING_COMPLETE_RES, 31 | COMPLETED, 32 | ERROR, 33 | END 34 | }; 35 | bool begin(ConnectionHandler &connectionHandler, SecureElement &secureElement, String &uhwid); 36 | void end(); 37 | CSRHandlerStates poll(); 38 | private: 39 | CSRHandlerStates _state; 40 | unsigned long _nextRequestAt; 41 | uint32_t _requestAttempt; 42 | uint32_t _startWaitingResponse; 43 | String *_uhwid; 44 | String _fw_version; 45 | 46 | int _issueYear; 47 | uint8_t _issueMonth; 48 | uint8_t _issueDay; 49 | uint8_t _issueHour; 50 | byte _serialNumber[16]; 51 | byte _authorityKeyIdentifier[20]; 52 | byte _signature[64]; 53 | String _deviceId; 54 | 55 | ECP256Certificate *_certForCSR; 56 | ConnectionHandler *_connectionHandler; 57 | SecureElement *_secureElement; 58 | TLSClientMqtt *_tlsClient; 59 | HttpClient *_client; 60 | LEDFeedbackClass &_ledFeedback; 61 | void updateNextRequestAt(); 62 | void nextNetworkRetry(); 63 | uint32_t jitter(uint32_t base = JITTER_BASE, uint32_t max = JITTER_MAX); 64 | bool postRequest(const char *url, String &postData); 65 | uint32_t getTimestamp(); 66 | CSRHandlerStates handleBuildCSR(); 67 | CSRHandlerStates handleRequestSignature(); 68 | CSRHandlerStates handleWaitingResponse(); 69 | CSRHandlerStates handleParseResponse(); 70 | CSRHandlerStates handleBuildCertificate(); 71 | CSRHandlerStates handleCertCreated(); 72 | CSRHandlerStates handleWaitingCompleteRes(); 73 | void handleError(); 74 | }; 75 | -------------------------------------------------------------------------------- /examples/utility/Provisioning_2.0/ClaimingHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2024 Arduino SA 3 | 4 | This Source Code Form is subject to the terms of the Mozilla Public 5 | License, v. 2.0. If a copy of the MPL was not distributed with this 6 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | #pragma once 10 | #include "Arduino.h" 11 | #include "configuratorAgents/AgentsManager.h" 12 | #include 13 | #include "utility/LEDFeedback.h" 14 | 15 | typedef bool (*ClearStoredCredentialsHandler)(); 16 | class ClaimingHandlerClass { 17 | public: 18 | ClaimingHandlerClass(); 19 | bool begin(SecureElement &secureElement, String &uhwid, ClearStoredCredentialsHandler clearStoredCredentials); 20 | void end(); 21 | void poll(); 22 | private: 23 | String *_uhwid; 24 | enum class ClaimingHandlerStates { 25 | INIT, 26 | END 27 | }; 28 | enum class ClaimingReqEvents { NONE, 29 | GET_ID, 30 | RESET, 31 | GET_BLE_MAC_ADDRESS, 32 | GET_PROV_SKETCH_VERSION}; 33 | static inline ClaimingReqEvents _receivedEvent; 34 | ClaimingHandlerStates _state; 35 | AgentsManagerClass &_agentManager; 36 | LEDFeedbackClass &_ledFeedback; 37 | static inline uint64_t _ts; 38 | SecureElement *_secureElement; 39 | 40 | bool sendStatus(StatusMessage msg); 41 | /* Commands handlers */ 42 | void getIdReqHandler(); 43 | void resetStoredCredReqHandler(); 44 | void getBLEMacAddressReqHandler(); 45 | void getProvSketchVersionReqHandler(); 46 | ClearStoredCredentialsHandler _clearStoredCredentials; 47 | /* Callbacks for receiving commands */ 48 | static void getIdRequestCb(); 49 | static void setTimestamp(uint64_t ts); 50 | static void resetStoredCredRequestCb(); 51 | static void getBLEMacAddressRequestCb(); 52 | static void getProvSketchVersionRequestCb(); 53 | }; 54 | -------------------------------------------------------------------------------- /examples/utility/Provisioning_2.0/SecretsHelper.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2024 Arduino SA 3 | 4 | This Source Code Form is subject to the terms of the Mozilla Public 5 | License, v. 2.0. If a copy of the MPL was not distributed with this 6 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | 14 | inline String GetUHWID() { 15 | UniqueHWId Id; 16 | if (Id.begin()) { 17 | return Id.get(); 18 | } 19 | return ""; 20 | } 21 | -------------------------------------------------------------------------------- /examples/utility/Provisioning_2.0/thingProperties.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2024 Arduino SA 3 | 4 | This Source Code Form is subject to the terms of the Mozilla Public 5 | License, v. 2.0. If a copy of the MPL was not distributed with this 6 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 | */ 8 | #if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) 9 | #error "Board not supported for Provisioning 2.0" 10 | #endif 11 | 12 | #include 13 | #include 14 | #include 15 | #include "configuratorAgents/agents/BLEAgent.h" 16 | #include "configuratorAgents/agents/SerialAgent.h" 17 | 18 | GenericConnectionHandler ArduinoIoTPreferredConnection; 19 | KVStore kvStore; 20 | BLEAgentClass BLEAgent; 21 | SerialAgentClass SerialAgent; 22 | NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection); 23 | 24 | void initProperties() { 25 | 26 | NetworkConfigurator.addAgent(BLEAgent); 27 | NetworkConfigurator.addAgent(SerialAgent); 28 | NetworkConfigurator.setStorage(kvStore); 29 | ArduinoCloud.setConfigurator(NetworkConfigurator); 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /examples/utility/SelfProvisioning/arduino_secrets.h: -------------------------------------------------------------------------------- 1 | #define SECRET_SSID "" 2 | #define SECRET_PASS "" 3 | #define SECRET_CLIENT_ID "" 4 | #define SECRET_SECRET_ID "" 5 | -------------------------------------------------------------------------------- /extras/test/include/Arduino.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | #ifndef TEST_ARDUINO_H_ 6 | #define TEST_ARDUINO_H_ 7 | 8 | /****************************************************************************** 9 | INCLUDE 10 | ******************************************************************************/ 11 | 12 | #include 13 | 14 | /****************************************************************************** 15 | DEFINES 16 | ******************************************************************************/ 17 | #ifndef min 18 | #define min(a,b) ((a)<(b)?(a):(b)) 19 | #endif 20 | 21 | /****************************************************************************** 22 | TYPEDEF 23 | ******************************************************************************/ 24 | 25 | typedef std::string String; 26 | 27 | /****************************************************************************** 28 | FUNCTION PROTOTYPES 29 | ******************************************************************************/ 30 | 31 | void set_millis(unsigned long const millis); 32 | unsigned long millis(); 33 | 34 | #endif /* TEST_ARDUINO_H_ */ 35 | -------------------------------------------------------------------------------- /extras/test/include/Arduino_ConnectionHandler.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | #ifndef TEST_ARDUINO_CONNECTION_HANDLER_H_ 6 | #define TEST_ARDUINO_CONNECTION_HANDLER_H_ 7 | 8 | /****************************************************************************** 9 | TYPEDEF 10 | ******************************************************************************/ 11 | 12 | typedef void ConnectionHandler; 13 | 14 | #endif /* TEST_ARDUINO_CONNECTION_HANDLER_H_ */ 15 | -------------------------------------------------------------------------------- /extras/test/include/util/CBORTestUtil.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | #ifndef INCLUDE_CBOR_TESTUTIL_H_ 6 | #define INCLUDE_CBOR_TESTUTIL_H_ 7 | 8 | /************************************************************************************** 9 | INCLUDE 10 | **************************************************************************************/ 11 | 12 | #include 13 | 14 | #include 15 | 16 | /************************************************************************************** 17 | NAMESPACE 18 | **************************************************************************************/ 19 | 20 | namespace cbor 21 | { 22 | 23 | /************************************************************************************** 24 | PROTOTYPES 25 | **************************************************************************************/ 26 | 27 | std::vector encode(PropertyContainer & property_container, bool lightPayload = false); 28 | void print(std::vector const & vect); 29 | 30 | /************************************************************************************** 31 | NAMESPACE 32 | **************************************************************************************/ 33 | 34 | } /* cbor */ 35 | 36 | #endif /* INCLUDE_CBOR_TESTUTIL_H_ */ 37 | -------------------------------------------------------------------------------- /extras/test/include/util/PropertyTestUtil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | */ 4 | 5 | #ifndef PROPERTY_TEST_UTIL_H_ 6 | #define PROPERTY_TEST_UTIL_H_ 7 | 8 | /************************************************************************************** 9 | FUNCTION DECLARATION 10 | **************************************************************************************/ 11 | 12 | extern "C" unsigned long getTime(); 13 | 14 | #endif /* PROPERTY_TEST_UTIL_H_ */ 15 | -------------------------------------------------------------------------------- /extras/test/src/Arduino.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /****************************************************************************** 6 | INCLUDE 7 | ******************************************************************************/ 8 | 9 | #include 10 | 11 | /****************************************************************************** 12 | GLOBAL VARIABLES 13 | ******************************************************************************/ 14 | 15 | static unsigned long current_millis = 0; 16 | 17 | /****************************************************************************** 18 | PUBLIC FUNCTIONS 19 | ******************************************************************************/ 20 | 21 | void set_millis(unsigned long const millis) 22 | { 23 | current_millis = millis; 24 | } 25 | 26 | unsigned long millis() 27 | { 28 | return current_millis; 29 | } 30 | -------------------------------------------------------------------------------- /extras/test/src/test_CloudColor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | TEST CODE 17 | **************************************************************************************/ 18 | 19 | SCENARIO("Arduino Cloud Properties ", "[ArduinoCloudThing::CloudColor]") 20 | { 21 | WHEN("Set invalid color HSB") 22 | { 23 | CloudColor color_test = CloudColor(0.0, 0.0, 0.0); 24 | 25 | Color value_color_test = color_test.getValue(); 26 | REQUIRE(value_color_test.setColorHSB(500.0, 20.0, 30.0) == false); 27 | } 28 | 29 | WHEN("Set and Get different RGB colors") 30 | { 31 | uint8_t r, g, b; 32 | 33 | CloudColor color_test = CloudColor(0.0, 0.0, 0.0); 34 | 35 | Color value_color_test = color_test.getValue(); 36 | 37 | value_color_test.setColorRGB(128, 64, 64); 38 | value_color_test.getRGB(r, g, b); 39 | 40 | REQUIRE(r == 128); 41 | REQUIRE(g == 64); 42 | REQUIRE(b == 64); 43 | 44 | value_color_test.setColorRGB(126, 128, 64); 45 | value_color_test.getRGB(r, g, b); 46 | 47 | REQUIRE(r == 126); 48 | REQUIRE(g == 128); 49 | REQUIRE(b == 64); 50 | 51 | value_color_test.setColorRGB(64, 128, 64); 52 | value_color_test.getRGB(r, g, b); 53 | 54 | REQUIRE(r == 64); 55 | REQUIRE(g == 128); 56 | REQUIRE(b == 64); 57 | 58 | value_color_test.setColorRGB(64, 64, 128); 59 | value_color_test.getRGB(r, g, b); 60 | 61 | REQUIRE(r == 64); 62 | REQUIRE(g == 64); 63 | REQUIRE(b == 128); 64 | 65 | value_color_test.setColorRGB(255, 0, 255); 66 | value_color_test.getRGB(r, g, b); 67 | 68 | REQUIRE(r == 255); 69 | REQUIRE(g == 0); 70 | REQUIRE(b == 255); 71 | 72 | value_color_test.setColorRGB(0, 0, 0); 73 | value_color_test.getRGB(r, g, b); 74 | 75 | REQUIRE(r == 0); 76 | REQUIRE(g == 0); 77 | REQUIRE(b == 0); 78 | 79 | value_color_test.setColorRGB(50, 100, 20); 80 | value_color_test.getRGB(r, g, b); 81 | 82 | REQUIRE(r == 50); 83 | REQUIRE(g == 100); 84 | REQUIRE(b == 20); 85 | 86 | value_color_test.setColorRGB(20, 50, 70); 87 | value_color_test.getRGB(r, g, b); 88 | 89 | REQUIRE(r == 20); 90 | REQUIRE(g == 50); 91 | REQUIRE(b == 70); 92 | } 93 | 94 | WHEN("Set HSB colors and get RGB") 95 | { 96 | bool verify; 97 | uint8_t r, g, b; 98 | 99 | CloudColor color_test = CloudColor(0.0, 0.0, 0.0); 100 | 101 | Color value_color_test = color_test.getValue(); 102 | 103 | value_color_test.setColorHSB(240, 50, 50); 104 | value_color_test.getRGB(r, g, b); 105 | verify = r == 64 && g == 64 && b == 128; 106 | 107 | REQUIRE(verify); 108 | 109 | value_color_test.setColorHSB(120, 50, 50); 110 | value_color_test.getRGB(r, g, b); 111 | verify = r == 64 && g == 128 && b == 64; 112 | 113 | REQUIRE(verify); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /extras/test/src/test_addPropertyReal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | /************************************************************************************** 19 | TEST CODE 20 | **************************************************************************************/ 21 | 22 | SCENARIO("The same arduino cloud properties are added multiple times", "[ArduinoCloudThing::addPropertyToContainer]") 23 | { 24 | WHEN("The same bool property is added multiple times") 25 | { 26 | PropertyContainer property_container; 27 | 28 | CloudBool bool_property = false; 29 | 30 | Property * bool_property_ptr_1 = &addPropertyToContainer(property_container, bool_property, "bool_property", Permission::ReadWrite); 31 | Property * bool_property_ptr_2 = &addPropertyToContainer(property_container, bool_property, "bool_property", Permission::ReadWrite); 32 | THEN("No new property is added and the first added property is returned instead of a new one") { 33 | REQUIRE(bool_property_ptr_1 == bool_property_ptr_2); 34 | } 35 | } 36 | 37 | /**************************************************************************************/ 38 | 39 | WHEN("the same int property is added multiple times") 40 | { 41 | PropertyContainer property_container; 42 | 43 | CloudInt int_property = 1; 44 | 45 | Property * int_property_ptr_1 = &addPropertyToContainer(property_container, int_property, "int_property", Permission::ReadWrite); 46 | Property * int_property_ptr_2 = &addPropertyToContainer(property_container, int_property, "int_property", Permission::ReadWrite); 47 | 48 | THEN("No new property is added and the first added property is returned instead of a new one") { 49 | REQUIRE(int_property_ptr_1 == int_property_ptr_2); 50 | } 51 | } 52 | 53 | /**************************************************************************************/ 54 | 55 | WHEN("the same float property is added multiple times") 56 | { 57 | PropertyContainer property_container; 58 | 59 | CloudFloat float_property = 1.0f; 60 | 61 | Property * float_property_ptr_1 = &addPropertyToContainer(property_container, float_property, "float_property", Permission::ReadWrite); 62 | Property * float_property_ptr_2 = &addPropertyToContainer(property_container, float_property, "float_property", Permission::ReadWrite); 63 | 64 | THEN("No new property is added and the first added property is returned instead of a new one") { 65 | REQUIRE(float_property_ptr_1 == float_property_ptr_2); 66 | } 67 | } 68 | 69 | /**************************************************************************************/ 70 | 71 | WHEN("the same String property is added multiple times") 72 | { 73 | PropertyContainer property_container; 74 | 75 | CloudString str_property; 76 | 77 | Property * str_property_ptr_1 = &addPropertyToContainer(property_container, str_property, "str_property", Permission::ReadWrite); 78 | Property * str_property_ptr_2 = &addPropertyToContainer(property_container, str_property, "str_property", Permission::ReadWrite); 79 | 80 | THEN("No new property is added and the first added property is returned instead of a new one") { 81 | REQUIRE(str_property_ptr_1 == str_property_ptr_2); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /extras/test/src/test_main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE/MAIN 7 | **************************************************************************************/ 8 | 9 | #define CATCH_CONFIG_MAIN 10 | #include 11 | -------------------------------------------------------------------------------- /extras/test/src/test_publishEvery.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | /************************************************************************************** 15 | TEST CODE 16 | **************************************************************************************/ 17 | 18 | SCENARIO("A Arduino cloud property is published periodically", "[ArduinoCloudThing::publishEvery]") 19 | { 20 | PropertyContainer property_container; 21 | 22 | CloudBool test = true; 23 | unsigned long const PUBLISH_INTERVAL_SEC = 1 * SECONDS; 24 | 25 | addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).publishEvery(PUBLISH_INTERVAL_SEC); 26 | 27 | WHEN("t = 0 ms, publish interval = 1000 ms, 1st call to 'encode'") { 28 | set_millis(0); 29 | THEN("'encode' should encode the property") { 30 | REQUIRE(cbor::encode(property_container).size() != 0); 31 | WHEN("t = 999 ms") { 32 | set_millis(999); 33 | THEN("'encode' should not encode the property") { 34 | REQUIRE(cbor::encode(property_container).size() == 0); 35 | WHEN("t = 1000 ms") { 36 | set_millis(1000); 37 | THEN("'encode' should encode the property") { 38 | REQUIRE(cbor::encode(property_container).size() != 0); 39 | WHEN("t = 1999 ms") { 40 | set_millis(1999); 41 | THEN("'encode' should not encode the property") { 42 | REQUIRE(cbor::encode(property_container).size() == 0); 43 | WHEN("t = 2000 ms") { 44 | set_millis(2000); 45 | THEN("'encode' should encode the property") { 46 | REQUIRE(cbor::encode(property_container).size() != 0); 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /extras/test/src/test_publishOnChange.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | /************************************************************************************** 14 | TEST CODE 15 | **************************************************************************************/ 16 | 17 | SCENARIO("An Arduino cloud property is published on value change", "[ArduinoCloudThing::publishOnChange]") 18 | { 19 | PropertyContainer property_container; 20 | 21 | CloudInt test = 10; 22 | int const DELTA = 6; 23 | 24 | addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).publishOnChange(DELTA,0); 25 | 26 | WHEN("test = 10, delta = 6, the property is encoded for the 1st time") { 27 | THEN("The property should be encoded") { 28 | REQUIRE(cbor::encode(property_container).size() != 0); 29 | WHEN("test +=4 -> test = 14") { 30 | test += 4; 31 | THEN("Since the increment since the last update (4) is smaller than the delta of 6 the property should not be encoded") { 32 | REQUIRE(cbor::encode(property_container).size() == 0); 33 | WHEN("test +=4 -> test = 18") { 34 | test += 4; 35 | THEN("Since the increment since the last update (8) is greater than the delta of 6 the property should be encoded") { 36 | REQUIRE(cbor::encode(property_container).size() != 0); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /extras/test/src/test_publishOnChangeRateLimit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | /************************************************************************************** 14 | TEST CODE 15 | **************************************************************************************/ 16 | 17 | SCENARIO("An Arduino cloud property is published on value change but the update rate is limited", "[ArduinoCloudThing::publishOnChange]") 18 | { 19 | PropertyContainer property_container; 20 | 21 | CloudInt test = 0; 22 | int const MIN_DELTA = 0; 23 | unsigned long const MIN_TIME_BETWEEN_UPDATES_ms = 500; /* No updates faster than 500 ms */ 24 | 25 | addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).publishOnChange(MIN_DELTA, MIN_TIME_BETWEEN_UPDATES_ms); 26 | 27 | WHEN("t = 0 ms, min time between updates = 500 ms, property not modified, 1st call to 'encode'") { 28 | set_millis(0); 29 | THEN("'encode' should encode the property") { 30 | REQUIRE(cbor::encode(property_container).size() != 0); 31 | WHEN("t = 499 ms, property modified") { 32 | test++; 33 | set_millis(499); 34 | THEN("'encode' should not encode any property") { 35 | REQUIRE(cbor::encode(property_container).size() == 0); 36 | WHEN("t = 500 ms, property modified") { 37 | test++; 38 | set_millis(500); 39 | THEN("'encode' should encode the property") { 40 | REQUIRE(cbor::encode(property_container).size() != 0); 41 | WHEN("t = 999 ms, property modified") { 42 | test++; 43 | set_millis(999); 44 | THEN("'encode' should not encode any property") { 45 | REQUIRE(cbor::encode(property_container).size() == 0); 46 | WHEN("t = 1000 ms, property modified") { 47 | test++; 48 | set_millis(1000); 49 | THEN("'encode' should encode the property") { 50 | REQUIRE(cbor::encode(property_container).size() != 0); 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /extras/test/src/test_readOnly.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | TEST CODE 18 | **************************************************************************************/ 19 | 20 | SCENARIO("An Arduino cloud property is marked 'read only'", "[ArduinoCloudThing::decode]") 21 | { 22 | PropertyContainer property_container; 23 | 24 | CloudInt test = 0; 25 | addPropertyToContainer(property_container, test, "test", Permission::Read); 26 | 27 | /* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */ 28 | uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07}; 29 | int const payload_length = sizeof(payload) / sizeof(uint8_t); 30 | CBORDecoder::decode(property_container, payload, payload_length); 31 | 32 | REQUIRE(test == 0); 33 | } 34 | -------------------------------------------------------------------------------- /extras/test/src/test_writeOnChange.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | TEST CODE 18 | **************************************************************************************/ 19 | 20 | SCENARIO("An Arduino cloud property is marked 'write on change'", "[ArduinoCloudThing::decode]") 21 | { 22 | PropertyContainer property_container; 23 | 24 | CloudInt test = 0; 25 | addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).writeOnChange(); 26 | 27 | /* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */ 28 | uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07}; 29 | int const payload_length = sizeof(payload) / sizeof(uint8_t); 30 | CBORDecoder::decode(property_container, payload, payload_length); 31 | 32 | REQUIRE(test == 7); 33 | } 34 | -------------------------------------------------------------------------------- /extras/test/src/test_writeOnDemand.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | TEST CODE 18 | **************************************************************************************/ 19 | 20 | SCENARIO("An Arduino cloud property is marked 'write on demand'", "[ArduinoCloudThing::decode]") 21 | { 22 | PropertyContainer property_container; 23 | 24 | CloudInt test = 0; 25 | addPropertyToContainer(property_container, test, "test", Permission::ReadWrite).writeOnDemand(); 26 | 27 | /* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */ 28 | uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07}; 29 | int const payload_length = sizeof(payload) / sizeof(uint8_t); 30 | CBORDecoder::decode(property_container, payload, payload_length); 31 | 32 | REQUIRE(test == 0); 33 | 34 | Property* p = getProperty(property_container, "test"); 35 | p->fromCloudToLocal(); 36 | 37 | REQUIRE(test == 7); 38 | } 39 | -------------------------------------------------------------------------------- /extras/test/src/test_writeOnly.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | TEST CODE 18 | **************************************************************************************/ 19 | 20 | SCENARIO("An Arduino cloud property is marked 'write only'", "[ArduinoCloudThing::encode]") 21 | { 22 | PropertyContainer property_container; 23 | 24 | CloudInt test = 0; 25 | addPropertyToContainer(property_container, test, "test", Permission::Write); 26 | 27 | REQUIRE(cbor::encode(property_container).size() == 0); /* Since 'test' is 'write only' it should not be encoded */ 28 | } 29 | -------------------------------------------------------------------------------- /extras/test/src/util/CBORTestUtil.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2019 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | 16 | /************************************************************************************** 17 | NAMESPACE 18 | **************************************************************************************/ 19 | 20 | namespace cbor 21 | { 22 | 23 | /************************************************************************************** 24 | PUBLIC FUNCTIONS 25 | **************************************************************************************/ 26 | 27 | std::vector encode(PropertyContainer & property_container, bool lightPayload) 28 | { 29 | int bytes_encoded = 0; 30 | unsigned int starting_property_index = 0; 31 | uint8_t buf[256] = {0}; 32 | 33 | if (CBOREncoder::encode(property_container, buf, 256, bytes_encoded, starting_property_index, lightPayload) == CborNoError) 34 | return std::vector(buf, buf + bytes_encoded); 35 | else 36 | return std::vector(); 37 | } 38 | 39 | void print(std::vector const & vect) 40 | { 41 | for (auto i = vect.begin(); i != vect.end(); i++) { 42 | std::cout << std::uppercase 43 | << std::hex 44 | << std::setw(2) 45 | << std::setfill('0') 46 | << static_cast(*i) 47 | << std::dec 48 | << std::nouppercase 49 | << " "; 50 | } 51 | std::cout << std::endl; 52 | } 53 | 54 | /************************************************************************************** 55 | NAMESPACE 56 | **************************************************************************************/ 57 | 58 | } /* cbor */ 59 | -------------------------------------------------------------------------------- /extras/test/src/util/PropertyTestUtil.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | */ 4 | 5 | /************************************************************************************** 6 | INCLUDE 7 | **************************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | /************************************************************************************** 14 | FUNCTION DEFINITION 15 | **************************************************************************************/ 16 | 17 | unsigned long getTime() 18 | { 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /extras/tools/.gitignore: -------------------------------------------------------------------------------- 1 | *.bin 2 | *.ota 3 | *.json 4 | 5 | -------------------------------------------------------------------------------- /extras/tools/README.md: -------------------------------------------------------------------------------- 1 | Firmware Over-The-Air Tools 2 | =========================== 3 | 4 | ## How-to-OTA 5 | 6 | Arduino IDE: `Sketch` -> `Export compiled Binary` 7 | ```bash 8 | cp sketch.bin ~/Arduino/libraries/ArduinoIoTCloud/extras/tools/ 9 | cd ~/Arduino/libraries/ArduinoIoTCloud/extras/tools 10 | ./lzss.py --encode sketch.bin sketch.lzss 11 | ./bin2ota.py [MKR_WIFI_1010 | NANO_33_IOT] sketch.lzss sketch.ota 12 | ``` 13 | 14 | ## `lzss.py` 15 | This tool allows to compress a binary file using the LZSS algorithm. 16 | 17 | ### How-To-Use 18 | * Encoding (Compressing) 19 | ```bash 20 | ./lzss.py --encode sketch.bin sketch.lzss 21 | ``` 22 | * Decoding (Extracting) 23 | ```bash 24 | ./lzss.py --decode sketch.lzss sketch.bin 25 | ``` 26 | 27 | ## `bin2ota.py` 28 | This tool can be used to extend (actually prefix) a binary generated with e.g. the Arduino IDE with the required length and crc values required to perform an OTA (Over-The-Air) update of the firmware. 29 | 30 | ### How-To-Use 31 | ```bash 32 | ./bin2ota.py [MKR_WIFI_1010 | NANO_33_IOT] sketch.lzss sketch.ota 33 | ``` 34 | #### `sketch.lzss` 35 | ```bash 36 | 0 80602012 0a0cbe01 0094bfa2 bff7807c 37 | 16 bfdd00d9 655fd240 cfc11065 a071e0b2 38 | ... 39 | ``` 40 | #### `sketch.ota` 41 | ```bash 42 | 0 3bca0100 7e1c3a2b 54804123 00000000 43 | 16 00000040 80602012 0a0cbe01 0094bfa2 44 | ... 45 | ``` 46 | * `length(sketch.lzss) = 3bca0100 -> 0x0001'CA3B` 47 | * `CRC32(sketch.lzss + MAGIC NUMBER + VERSION) = 7e1c3a2b -> 0x2B3A'1C7E` 48 | * `MAGIC NUMBER(MKR WIFI 1010) = 54804123 -> 0x2341'8054` 49 | * `VERSION = 00000000 00000040 -> 0x40'00'00'00'00'00'00'00` 50 | -------------------------------------------------------------------------------- /extras/tools/bin2ota.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import sys 4 | import crccheck 5 | 6 | if len(sys.argv) != 4: 7 | print ("Usage: bin2ota.py BOARD sketch.bin sketch.ota") 8 | print (" BOARD = [ MKR_WIFI_1010 | NANO_33_IOT | PORTENTA_H7_M7 | NANO_RP2040_CONNECT | NICLA_VISION | OPTA | GIGA | NANO_ESP32 | ESP32 | UNOR4WIFI]") 9 | sys.exit() 10 | 11 | board = sys.argv[1] 12 | ifile = sys.argv[2] 13 | ofile = sys.argv[3] 14 | 15 | # Read the binary file 16 | in_file = open(ifile, "rb") 17 | bin_data = bytearray(in_file.read()) 18 | in_file.close() 19 | 20 | # Magic number (VID/PID) 21 | if board == "MKR_WIFI_1010": 22 | magic_number = 0x23418054.to_bytes(4,byteorder='little') 23 | elif board == "NANO_33_IOT": 24 | magic_number = 0x23418057.to_bytes(4,byteorder='little') 25 | elif board == "PORTENTA_H7_M7": 26 | magic_number = 0x2341025B.to_bytes(4,byteorder='little') 27 | elif board == "NANO_RP2040_CONNECT": 28 | magic_number = 0x2341005E.to_bytes(4,byteorder='little') 29 | elif board == "NICLA_VISION": 30 | magic_number = 0x2341025F.to_bytes(4,byteorder='little') 31 | elif board == "OPTA": 32 | magic_number = 0x23410064.to_bytes(4,byteorder='little') 33 | elif board == "GIGA": 34 | magic_number = 0x23410266.to_bytes(4,byteorder='little') 35 | elif board == "NANO_ESP32": 36 | magic_number = 0x23410070.to_bytes(4,byteorder='little') 37 | # Magic number for all ESP32 boards not related to (VID/PID) 38 | elif board == "ESP32": 39 | magic_number = 0x45535033.to_bytes(4,byteorder='little') 40 | elif board == "UNOR4WIFI": 41 | magic_number = 0x23411002.to_bytes(4,byteorder='little') 42 | elif board == "PORTENTA_C33": 43 | magic_number = 0x23410068.to_bytes(4,byteorder='little') 44 | else: 45 | print ("Error,", board, "is not a supported board type") 46 | sys.exit() 47 | 48 | # Version field (byte array of size 8) - all 0 except the compression flag set. 49 | version = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40]) 50 | 51 | # Prepend magic number and version field to payload 52 | bin_data_complete = magic_number + version + bin_data 53 | 54 | # Calculate length and CRC32 55 | bin_data_len = len(bin_data_complete) 56 | bin_data_crc = crccheck.crc.Crc32.calc(bin_data_complete) 57 | 58 | # Write to outfile 59 | out_file = open(ofile, "wb") 60 | out_file.write((bin_data_len).to_bytes(4,byteorder='little')) 61 | out_file.write((bin_data_crc).to_bytes(4,byteorder='little')) 62 | out_file.write(bin_data_complete) 63 | out_file.close() 64 | -------------------------------------------------------------------------------- /extras/tools/lzss.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arduino-libraries/ArduinoIoTCloud/e649174c96b58e66cf83311ce0a0a9f02e5a9452/extras/tools/lzss.dylib -------------------------------------------------------------------------------- /extras/tools/lzss.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import platform 4 | import sys 5 | import ctypes 6 | 7 | LZSS_SO_EXT = "so" if platform.uname()[0] != "Darwin" else "dylib" 8 | 9 | LZSS_SO_FILE = f"./lzss.{LZSS_SO_EXT}" 10 | 11 | if len(sys.argv) != 4: 12 | print ("Usage: lzss.py --[encode|decode] infile outfile") 13 | sys.exit() 14 | 15 | lzss_functions = ctypes.CDLL(LZSS_SO_FILE) 16 | 17 | mode = sys.argv[1] 18 | ifile = sys.argv[2] 19 | ofile = sys.argv[3] 20 | 21 | b_ifile = ifile.encode('utf-8') 22 | b_ofile = ofile.encode('utf-8') 23 | 24 | if mode == "--encode": 25 | lzss_functions.encode_file.argtypes = [ctypes.c_char_p, ctypes.c_char_p] 26 | lzss_functions.encode_file(b_ifile, b_ofile) 27 | elif mode == "--decode": 28 | lzss_functions.decode_file.argtypes = [ctypes.c_char_p, ctypes.c_char_p] 29 | lzss_functions.decode_file(b_ifile, b_ofile) 30 | else: 31 | print ("Error, invalid mode parameter, use --encode or --decode") 32 | -------------------------------------------------------------------------------- /extras/tools/lzss.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arduino-libraries/ArduinoIoTCloud/e649174c96b58e66cf83311ce0a0a9f02e5a9452/extras/tools/lzss.so -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For ArduinoIoTCloud 3 | ####################################### 4 | 5 | ####################################### 6 | # Class (KEYWORD1) 7 | ####################################### 8 | 9 | ArduinoCloud KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | initProperties KEYWORD2 16 | 17 | # ArduinoIoTCloud.h 18 | update KEYWORD2 19 | connected KEYWORD2 20 | printDebugInfo KEYWORD2 21 | push KEYWORD2 22 | setTimestamp KEYWORD2 23 | setThingId KEYWORD2 24 | getThingId KEYWORD2 25 | setDeviceId KEYWORD2 26 | getDeviceId KEYWORD2 27 | getConnection KEYWORD2 28 | getInternalTime KEYWORD2 29 | getLocalTime KEYWORD2 30 | addCallback KEYWORD2 31 | addProperty KEYWORD2 32 | 33 | # ArduinoIoTCloudLPWAN.h 34 | begin KEYWORD2 35 | isRetryEnabled KEYWORD2 36 | getMaxRetry KEYWORD2 37 | getIntervalRetry KEYWORD2 38 | enableRetry KEYWORD2 39 | setMaxRetry KEYWORD2 40 | setIntervalRetry KEYWORD2 41 | 42 | # ArduinoIoTCloudTCP.h 43 | begin KEYWORD2 44 | setBoardId KEYWORD2 45 | setSecretDeviceKey KEYWORD2 46 | getBrokerAddress KEYWORD2 47 | getBrokerPort KEYWORD2 48 | setOTAStorage KEYWORD2 49 | reconnect KEYWORD2 50 | 51 | # ArduinoIoTCloudNotecard.h 52 | begin KEYWORD2 53 | setNotecardPollingInterval KEYWORD2 54 | 55 | # OTA 56 | onOTARequestCallbackFunc KEYWORD2 57 | onOTARequestCb KEYWORD2 58 | 59 | ####################################### 60 | # Constants (LITERAL1) 61 | ####################################### 62 | 63 | ON LITERAL1 64 | OFF LITERAL1 65 | ON_CHANGE LITERAL1 66 | SECONDS LITERAL1 67 | MINUTES LITERAL1 68 | HOURS LITERAL1 69 | DAYS LITERAL1 70 | 71 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ArduinoIoTCloud 2 | version=2.6.1 3 | author=Arduino 4 | maintainer=Arduino 5 | sentence=This library allows connecting to the Arduino IoT Cloud service. 6 | paragraph=It provides a ConnectionManager to handle connection/disconnection, property-change updates and events callbacks. The supported boards are MKR GSM, MKR1000 and WiFi101. 7 | category=Communication 8 | url=https://github.com/arduino-libraries/ArduinoIoTCloud 9 | architectures=mbed,samd,esp8266,mbed_nano,mbed_portenta,mbed_nicla,esp32,mbed_opta,mbed_giga,renesas_portenta,renesas_uno,mbed_edge,stm32 10 | includes=ArduinoIoTCloud.h 11 | depends=Arduino_ConnectionHandler,Arduino_DebugUtils,Arduino_SecureElement,ArduinoMqttClient,ArduinoECCX08,RTCZero,Adafruit SleepyDog Library,ArduinoHttpClient,Arduino_CloudUtils,ArduinoBearSSL,Arduino_NetworkConfigurator 12 | -------------------------------------------------------------------------------- /src/AIoTC_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_AIOTC_CONST_H_ 19 | #define ARDUINO_AIOTC_CONST_H_ 20 | 21 | /****************************************************************************** 22 | CONSTANTS 23 | ******************************************************************************/ 24 | 25 | static bool const ON = true; 26 | static bool const OFF = false; 27 | 28 | static long const ON_CHANGE = -1; 29 | static long const SECONDS = 1; 30 | static long const MINUTES = 60; 31 | static long const HOURS = 3600; 32 | static long const DAYS = 86400; 33 | 34 | #endif /* ARDUINO_AIOTC_CONST_H_ */ 35 | -------------------------------------------------------------------------------- /src/ArduinoBearSSLConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2024 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_BEARSSL_CONFIG_H_ 19 | #define ARDUINO_BEARSSL_CONFIG_H_ 20 | 21 | /* Enabling this define allows the usage of ArduinoBearSSL without crypto chip. */ 22 | //#define ARDUINO_DISABLE_ECCX08 23 | 24 | /* Enable/Disable global instances*/ 25 | #define ARDUINO_BEARSSL_DISABLE_AES128 26 | #define ARDUINO_BEARSSL_DISABLE_DES 27 | #define ARDUINO_BEARSSL_DISABLE_MD5 28 | #define ARDUINO_BEARSSL_DISABLE_SHA1 29 | #define ARDUINO_BEARSSL_DISABLE_SHA256 30 | 31 | #define ARDUINO_BEARSSL_DISABLE_KEY_DECODER 32 | 33 | /* If uncommented profile should be configured using client.setProfile(...) */ 34 | //#define ARDUINO_BEARSSL_DISABLE_FULL_CLIENT_PROFILE 35 | 36 | /* If uncommented TA should be configured via constructor */ 37 | //#define ARDUINO_BEARSSL_DISABLE_BUILTIN_TRUST_ANCHORS 38 | 39 | /* If uncommented disables br_sslio_close call.From BearSSL docs: 40 | * 41 | * br_sslio_close(): perform the SSL closure protocol. This entails sending a 42 | * close_notify alert, and receiving a close_notify response. 43 | * 44 | * Note that a number of deployed SSL implementations do not follow the protocol 45 | * for closure, and may drop the underlying socket abruptly. As such, errors are 46 | * often reported by br_sslio_close(). 47 | * 48 | * In case of mbed-os + ArduinoIoTCloud br_sslio_close is endless looping 49 | * blocking sketch execution. 50 | */ 51 | #define ARDUINO_BEARSSL_DISABLE_TLS_CLOSE 52 | 53 | #define BEAR_SSL_CLIENT_CHAIN_SIZE 1 54 | 55 | #if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) ||\ 56 | defined(ARDUINO_GIGA) || defined(ARDUINO_NANO_RP2040_CONNECT) 57 | /* Allows download from OTA storage API */ 58 | #define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325) 59 | #endif 60 | 61 | #endif /* ARDUINO_BEARSSL_CONFIG_H_ */ 62 | -------------------------------------------------------------------------------- /src/ArduinoECCX08Config.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_ECCX08_CONFIG_H 19 | #define ARDUINO_ECCX08_CONFIG_H 20 | 21 | #define ECCX08_DISABLE_ASN1 22 | #define ECCX08_DISABLE_CSR 23 | #define ECCX08_DISABLE_JWS 24 | #define ECCX08_DISABLE_SSC 25 | #define ECCX08_DISABLE_PEM 26 | 27 | #endif /* ARDUINO_ECCX08_CONFIG_H */ 28 | -------------------------------------------------------------------------------- /src/ArduinoIoTCloudDevice.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_SecureElement library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #ifndef ARDUINO_IOT_CLOUD_DEVICE_H 12 | #define ARDUINO_IOT_CLOUD_DEVICE_H 13 | 14 | /****************************************************************************** 15 | * INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include 19 | #include "interfaces/CloudProcess.h" 20 | #include "property/PropertyContainer.h" 21 | 22 | /****************************************************************************** 23 | * CLASS DECLARATION 24 | ******************************************************************************/ 25 | 26 | class ArduinoCloudDevice : public CloudProcess { 27 | public: 28 | 29 | ArduinoCloudDevice(MessageStream* stream); 30 | virtual void update() override; 31 | virtual void handleMessage(Message* m) override; 32 | 33 | virtual void begin(); 34 | virtual int connected(); 35 | 36 | inline PropertyContainer &getPropertyContainer() { 37 | return _propertyContainer; 38 | }; 39 | inline unsigned int &getPropertyContainerIndex() { 40 | return _propertyContainerIndex; 41 | } 42 | inline bool isAttached() { 43 | return _attached; 44 | }; 45 | 46 | 47 | private: 48 | 49 | enum class State { 50 | Disconnected, 51 | Init, 52 | SendCapabilities, 53 | Connected, 54 | }; 55 | 56 | State _state; 57 | CommandId _command; 58 | TimedAttempt _attachAttempt; 59 | PropertyContainer _propertyContainer; 60 | unsigned int _propertyContainerIndex; 61 | bool _attached; 62 | bool _registered; 63 | 64 | State handleInit(); 65 | State handleSendCapabilities(); 66 | State handleConnected(); 67 | State handleDisconnected(); 68 | }; 69 | 70 | #endif /* ARDUINO_IOT_CLOUD_DEVICE_H */ 71 | -------------------------------------------------------------------------------- /src/ArduinoIoTCloudLPWAN.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_IOT_CLOUD_LPWAN_H 19 | #define ARDUINO_IOT_CLOUD_LPWAN_H 20 | 21 | /****************************************************************************** 22 | * INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | 27 | /****************************************************************************** 28 | * CLASS DECLARATION 29 | ******************************************************************************/ 30 | 31 | class ArduinoIoTCloudLPWAN : public ArduinoIoTCloudClass 32 | { 33 | public: 34 | 35 | ArduinoIoTCloudLPWAN(); 36 | virtual ~ArduinoIoTCloudLPWAN() { } 37 | 38 | virtual void update () override; 39 | virtual int connected () override; 40 | virtual void printDebugInfo() override; 41 | 42 | int begin(ConnectionHandler& connection, bool retry = false); 43 | 44 | inline bool isRetryEnabled () const { return _retryEnable; } 45 | inline int getMaxRetry () const { return _maxNumRetry; } 46 | inline long getIntervalRetry() const { return _intervalRetry; } 47 | 48 | inline void enableRetry (bool val) { _retryEnable = val; } 49 | inline void setMaxRetry (int val) { _maxNumRetry = val; } 50 | inline void setIntervalRetry(long val) { _intervalRetry = val; } 51 | 52 | inline PropertyContainer &getThingPropertyContainer() { return _thing_property_container; } 53 | 54 | 55 | private: 56 | 57 | enum class State 58 | { 59 | ConnectPhy, 60 | SyncTime, 61 | Connected, 62 | }; 63 | 64 | State _state; 65 | bool _retryEnable; 66 | int _maxNumRetry; 67 | long _intervalRetry; 68 | 69 | PropertyContainer _thing_property_container; 70 | unsigned int _last_checked_property_index; 71 | 72 | State handle_ConnectPhy(); 73 | State handle_SyncTime(); 74 | State handle_Connected(); 75 | 76 | void decodePropertiesFromCloud(); 77 | void sendPropertiesToCloud(); 78 | int writeProperties(const byte data[], int length); 79 | }; 80 | 81 | /****************************************************************************** 82 | * EXTERN DECLARATION 83 | ******************************************************************************/ 84 | 85 | extern ArduinoIoTCloudLPWAN ArduinoCloud; 86 | 87 | #endif -------------------------------------------------------------------------------- /src/ArduinoIoTCloudThing.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Arduino_SecureElement library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | 12 | #ifndef ARDUINO_IOT_CLOUD_THING_H 13 | #define ARDUINO_IOT_CLOUD_THING_H 14 | 15 | /****************************************************************************** 16 | * INCLUDE 17 | ******************************************************************************/ 18 | 19 | #include 20 | #include "interfaces/CloudProcess.h" 21 | #include "property/PropertyContainer.h" 22 | 23 | /****************************************************************************** 24 | * CLASS DECLARATION 25 | ******************************************************************************/ 26 | 27 | class ArduinoCloudThing : public CloudProcess { 28 | public: 29 | 30 | ArduinoCloudThing(MessageStream *stream); 31 | virtual void update() override; 32 | virtual void handleMessage(Message *m) override; 33 | 34 | virtual void begin(); 35 | virtual int connected(); 36 | 37 | inline PropertyContainer &getPropertyContainer() { 38 | return _propertyContainer; 39 | }; 40 | inline unsigned int &getPropertyContainerIndex() { 41 | return _propertyContainerIndex; 42 | } 43 | 44 | private: 45 | 46 | enum class State { 47 | Disconnect, 48 | Init, 49 | RequestLastValues, 50 | Connected, 51 | }; 52 | 53 | State _state; 54 | CommandId _command; 55 | TimedAttempt _syncAttempt; 56 | PropertyContainer _propertyContainer; 57 | unsigned int _propertyContainerIndex; 58 | int _utcOffset; 59 | Property *_utcOffsetProperty; 60 | unsigned int _utcOffsetExpireTime; 61 | Property *_utcOffsetExpireTimeProperty; 62 | 63 | State handleInit(); 64 | State handleRequestLastValues(); 65 | State handleConnected(); 66 | State handleDisconnect(); 67 | }; 68 | 69 | #endif /* ARDUINO_IOT_CLOUD_THING_H */ 70 | -------------------------------------------------------------------------------- /src/cbor/CBOR.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | /****************************************************************************** 14 | * INCLUDE 15 | ******************************************************************************/ 16 | #include 17 | #include 18 | 19 | /****************************************************************************** 20 | TYPEDEF 21 | ******************************************************************************/ 22 | 23 | enum CBORCommandTag: CBORTag { 24 | // Commands UP 25 | CBOROtaBeginUp = 0x010000, 26 | CBORThingBeginCmd = 0x010300, 27 | CBORLastValuesBeginCmd = 0x010500, 28 | CBORDeviceBeginCmd = 0x010700, 29 | CBOROtaProgressCmdUp = 0x010200, 30 | CBORTimezoneCommandUp = 0x010800, 31 | 32 | // Commands DOWN 33 | CBOROtaUpdateCmdDown = 0x010100, 34 | CBORThingUpdateCmd = 0x010400, 35 | CBORThingDetachCmd = 0x011000, 36 | CBORLastValuesUpdate = 0x010600, 37 | CBORTimezoneCommandDown = 0x010900, 38 | }; 39 | -------------------------------------------------------------------------------- /src/cbor/CBORDecoder.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef ARDUINO_CBOR_CBOR_DECODER_H_ 19 | #define ARDUINO_CBOR_CBOR_DECODER_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | 27 | #undef max 28 | #undef min 29 | #include 30 | 31 | #include "../property/PropertyContainer.h" 32 | 33 | /****************************************************************************** 34 | CLASS DECLARATION 35 | ******************************************************************************/ 36 | 37 | class CBORDecoder 38 | { 39 | 40 | public: 41 | 42 | /* decode a CBOR payload received from the cloud */ 43 | static void decode(PropertyContainer & property_container, uint8_t const * const payload, size_t const length, bool isSyncMessage = false); 44 | 45 | 46 | private: 47 | 48 | CBORDecoder() { } 49 | CBORDecoder(CBORDecoder const &) { } 50 | 51 | enum class MapParserState { 52 | EnterMap, 53 | MapKey, 54 | UndefinedKey, 55 | BaseVersion, 56 | BaseName, 57 | BaseTime, 58 | Name, 59 | Value, 60 | StringValue, 61 | BooleanValue, 62 | Time, 63 | LeaveMap, 64 | Complete, 65 | Error 66 | }; 67 | 68 | static MapParserState handle_EnterMap(CborValue * map_iter, CborValue * value_iter); 69 | static MapParserState handle_MapKey(CborValue * value_iter); 70 | static MapParserState handle_UndefinedKey(CborValue * value_iter); 71 | static MapParserState handle_BaseVersion(CborValue * value_iter, CborMapData & map_data); 72 | static MapParserState handle_BaseName(CborValue * value_iter, CborMapData & map_data); 73 | static MapParserState handle_BaseTime(CborValue * value_iter, CborMapData & map_data); 74 | static MapParserState handle_Name(CborValue * value_iter, CborMapData & map_data, PropertyContainer & property_container); 75 | static MapParserState handle_Value(CborValue * value_iter, CborMapData & map_data); 76 | static MapParserState handle_StringValue(CborValue * value_iter, CborMapData & map_data); 77 | static MapParserState handle_BooleanValue(CborValue * value_iter, CborMapData & map_data); 78 | static MapParserState handle_Time(CborValue * value_iter, CborMapData & map_data); 79 | static MapParserState handle_LeaveMap(CborValue * map_iter, CborValue * value_iter, CborMapData & map_data, PropertyContainer & property_container, String & current_property_name, unsigned long & current_property_base_time, unsigned long & current_property_time, bool const is_sync_message, std::list & map_data_list); 80 | 81 | static bool ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val); 82 | static double convertCborHalfFloatToDouble(uint16_t const half_val); 83 | 84 | }; 85 | 86 | #endif /* ARDUINO_CBOR_CBOR_DECODER_H_ */ 87 | -------------------------------------------------------------------------------- /src/cbor/IoTCloudMessageDecoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #ifndef ARDUINO_CBOR_MESSAGE_DECODER_H_ 12 | #define ARDUINO_CBOR_MESSAGE_DECODER_H_ 13 | 14 | /****************************************************************************** 15 | INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "./CBOR.h" 19 | #include 20 | #include "message/Commands.h" 21 | 22 | /****************************************************************************** 23 | CLASS DECLARATION 24 | ******************************************************************************/ 25 | 26 | class OtaUpdateCommandDecoder: public CBORMessageDecoderInterface { 27 | public: 28 | OtaUpdateCommandDecoder() 29 | : CBORMessageDecoderInterface(CBOROtaUpdateCmdDown, OtaUpdateCmdDownId) {} 30 | protected: 31 | MessageDecoder::Status decode(CborValue* iter, Message *msg) override; 32 | }; 33 | 34 | class ThingUpdateCommandDecoder: public CBORMessageDecoderInterface { 35 | public: 36 | ThingUpdateCommandDecoder() 37 | : CBORMessageDecoderInterface(CBORThingUpdateCmd, ThingUpdateCmdId) {} 38 | protected: 39 | MessageDecoder::Status decode(CborValue* iter, Message *msg) override; 40 | }; 41 | 42 | class ThingDetachCommandDecoder: public CBORMessageDecoderInterface { 43 | public: 44 | ThingDetachCommandDecoder() 45 | : CBORMessageDecoderInterface(CBORThingDetachCmd, ThingDetachCmdId) {} 46 | protected: 47 | MessageDecoder::Status decode(CborValue* iter, Message *msg) override; 48 | }; 49 | 50 | class LastValuesUpdateCommandDecoder: public CBORMessageDecoderInterface { 51 | public: 52 | LastValuesUpdateCommandDecoder() 53 | : CBORMessageDecoderInterface(CBORLastValuesUpdate, LastValuesUpdateCmdId) {} 54 | protected: 55 | MessageDecoder::Status decode(CborValue* iter, Message *msg) override; 56 | }; 57 | 58 | class TimezoneCommandDownDecoder: public CBORMessageDecoderInterface { 59 | public: 60 | TimezoneCommandDownDecoder() 61 | : CBORMessageDecoderInterface(CBORTimezoneCommandDown, TimezoneCommandDownId) {} 62 | protected: 63 | MessageDecoder::Status decode(CborValue* iter, Message *msg) override; 64 | }; 65 | 66 | namespace cbor { namespace decoder { namespace iotcloud { 67 | /** 68 | * Some link time optimization may exclude these classes to be instantiated 69 | * thus it may be required to reference them from outside of this file 70 | */ 71 | void commandDecoders(); 72 | }}} 73 | 74 | #endif /* ARDUINO_CBOR_MESSAGE_DECODER_H_ */ 75 | -------------------------------------------------------------------------------- /src/cbor/IoTCloudMessageEncoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #ifndef ARDUINO_CBOR_MESSAGE_ENCODER_H_ 12 | #define ARDUINO_CBOR_MESSAGE_ENCODER_H_ 13 | 14 | /****************************************************************************** 15 | * INCLUDE 16 | ******************************************************************************/ 17 | 18 | #include "./CBOR.h" 19 | #include 20 | #include "message/Commands.h" 21 | 22 | /****************************************************************************** 23 | * CLASS DECLARATION 24 | ******************************************************************************/ 25 | 26 | class OtaBeginCommandEncoder: public CBORMessageEncoderInterface { 27 | public: 28 | OtaBeginCommandEncoder() 29 | : CBORMessageEncoderInterface(CBOROtaBeginUp, OtaBeginUpId) {} 30 | protected: 31 | MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; 32 | }; 33 | 34 | class ThingBeginCommandEncoder: public CBORMessageEncoderInterface { 35 | public: 36 | ThingBeginCommandEncoder() 37 | : CBORMessageEncoderInterface(CBORThingBeginCmd, ThingBeginCmdId) {} 38 | protected: 39 | MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; 40 | }; 41 | 42 | class LastValuesBeginCommandEncoder: public CBORMessageEncoderInterface { 43 | public: 44 | LastValuesBeginCommandEncoder() 45 | : CBORMessageEncoderInterface(CBORLastValuesBeginCmd, LastValuesBeginCmdId) {} 46 | protected: 47 | MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; 48 | }; 49 | 50 | class DeviceBeginCommandEncoder: public CBORMessageEncoderInterface { 51 | public: 52 | DeviceBeginCommandEncoder() 53 | : CBORMessageEncoderInterface(CBORDeviceBeginCmd, DeviceBeginCmdId) {} 54 | protected: 55 | MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; 56 | }; 57 | 58 | class OtaProgressCommandUpEncoder: public CBORMessageEncoderInterface { 59 | public: 60 | OtaProgressCommandUpEncoder() 61 | : CBORMessageEncoderInterface(CBOROtaProgressCmdUp, OtaProgressCmdUpId) {} 62 | protected: 63 | MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; 64 | }; 65 | 66 | class TimezoneCommandUpEncoder: public CBORMessageEncoderInterface { 67 | public: 68 | TimezoneCommandUpEncoder() 69 | : CBORMessageEncoderInterface(CBORTimezoneCommandUp, TimezoneCommandUpId) {} 70 | protected: 71 | MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; 72 | }; 73 | 74 | namespace cbor { namespace encoder { namespace iotcloud { 75 | /** 76 | * Some link time optimization may exclude these classes to be instantiated 77 | * thus it may be required to reference them from outside of this file 78 | */ 79 | void commandEncoders(); 80 | }}} 81 | 82 | #endif /* ARDUINO_CBOR_MESSAGE_ENCODER_H_ */ 83 | -------------------------------------------------------------------------------- /src/interfaces/CloudProcess.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #ifndef ARDUINO_IOT_CLOUD_PROCESS 12 | #define ARDUINO_IOT_CLOUD_PROCESS 13 | 14 | /****************************************************************************** 15 | * INCLUDES 16 | ******************************************************************************/ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | /****************************************************************************** 24 | * CLASS DECLARATION 25 | ******************************************************************************/ 26 | 27 | class CloudProcess { 28 | public: 29 | CloudProcess(MessageStream* stream): stream(stream) {} 30 | 31 | /** 32 | * Abstract method that is called whenever a message comes from Message stream 33 | * @param m: the incoming message 34 | */ 35 | virtual void handleMessage(Message* m) = 0; 36 | 37 | /** 38 | * Abstract method that is called to update the FSM of the CloudProcess 39 | */ 40 | virtual void update() = 0; 41 | 42 | protected: 43 | /** 44 | * Used by a derived class to send a message to the underlying messageStream 45 | * @param msg: the message to send 46 | */ 47 | void deliver(Message* msg) { 48 | assert(stream != nullptr); 49 | stream->sendUpstream(msg); 50 | } 51 | 52 | private: 53 | MessageStream* stream; 54 | }; 55 | 56 | #endif /* ARDUINO_IOT_CLOUD_PROCESS */ 57 | -------------------------------------------------------------------------------- /src/interfaces/MessageStream.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | /****************************************************************************** 14 | * INCLUDE 15 | ******************************************************************************/ 16 | 17 | #include 18 | #include 19 | 20 | using upstreamFunction = std::function; 21 | 22 | /****************************************************************************** 23 | * CLASS DECLARATION 24 | ******************************************************************************/ 25 | 26 | class MessageStream { 27 | public: 28 | MessageStream(upstreamFunction upstream): upstream(upstream) {} 29 | 30 | /** 31 | * Send message upstream 32 | * @param m: message to send 33 | */ 34 | virtual inline void sendUpstream(Message* m) { 35 | upstream(m); 36 | } 37 | 38 | private: 39 | upstreamFunction upstream; 40 | }; 41 | -------------------------------------------------------------------------------- /src/ota/OTA.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | #include "AIoTC_Config.h" 13 | #if OTA_ENABLED 14 | 15 | #include "OTAConfig.h" 16 | #ifdef ARDUINO_ARCH_SAMD 17 | 18 | #include "implementation/OTASamd.h" 19 | using ArduinoCloudOTA = SAMDOTACloudProcess; 20 | 21 | // TODO Check if a macro already exist 22 | // constexpr uint32_t OtaMagicNumber = 0x23418054; // MKR_WIFI_1010 23 | constexpr uint32_t OtaMagicNumber = 0x23418057; // NANO_33_IOT 24 | 25 | #elif defined(ARDUINO_NANO_RP2040_CONNECT) 26 | #include "implementation/OTANanoRP2040.h" 27 | using ArduinoCloudOTA = NANO_RP2040OTACloudProcess; 28 | 29 | // TODO Check if a macro already exist 30 | constexpr uint32_t OtaMagicNumber = 0x2341005E; // TODO check this value is correct 31 | 32 | #elif defined(BOARD_STM32H7) 33 | #include "implementation/OTASTM32H7.h" 34 | using ArduinoCloudOTA = STM32H7OTACloudProcess; 35 | 36 | constexpr uint32_t OtaMagicNumber = ARDUINO_PORTENTA_OTA_MAGIC; 37 | 38 | #elif defined(ARDUINO_ARCH_ESP32) 39 | #include "implementation/OTAEsp32.h" 40 | using ArduinoCloudOTA = ESP32OTACloudProcess; 41 | 42 | #if defined (ARDUINO_NANO_ESP32) 43 | constexpr uint32_t OtaMagicNumber = 0x23410070; 44 | #else 45 | constexpr uint32_t OtaMagicNumber = 0x45535033; 46 | #endif 47 | 48 | #elif defined(ARDUINO_UNOR4_WIFI) 49 | 50 | #include "implementation/OTAUnoR4.h" 51 | using ArduinoCloudOTA = UNOR4OTACloudProcess; 52 | 53 | constexpr uint32_t OtaMagicNumber = 0x23411002; 54 | 55 | #else 56 | #error "This Board doesn't support OTA" 57 | #endif 58 | 59 | #endif // OTA_ENABLED 60 | -------------------------------------------------------------------------------- /src/ota/OTAConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #ifdef ARDUINO_ARCH_SAMD 14 | #define OFFLOADED_DOWNLOAD 15 | 16 | #elif defined(ARDUINO_NANO_RP2040_CONNECT) 17 | 18 | #elif defined(BOARD_STM32H7) 19 | 20 | #if defined(ARDUINO_PORTENTA_H7_M7) 21 | #define ARDUINO_PORTENTA_OTA_MAGIC 0x2341025b 22 | #define ARDUINO_PORTENTA_OTA_SDMMC_SUPPORT 23 | #define ARDUINO_PORTENTA_OTA_QSPI_SUPPORT 24 | #endif 25 | 26 | #if defined(ARDUINO_NICLA_VISION) 27 | #define ARDUINO_PORTENTA_OTA_MAGIC 0x2341025f 28 | #define ARDUINO_PORTENTA_OTA_QSPI_SUPPORT 29 | #endif 30 | 31 | #if defined(ARDUINO_OPTA) 32 | #define ARDUINO_PORTENTA_OTA_MAGIC 0x23410064 33 | #define ARDUINO_PORTENTA_OTA_QSPI_SUPPORT 34 | #endif 35 | 36 | #if defined(ARDUINO_GIGA) 37 | #define ARDUINO_PORTENTA_OTA_MAGIC 0x23410266 38 | #define ARDUINO_PORTENTA_OTA_QSPI_SUPPORT 39 | #endif 40 | 41 | 42 | #elif defined(ARDUINO_ARCH_ESP32) 43 | #define OTA_BASIC_AUTH 44 | 45 | #elif defined(ARDUINO_UNOR4_WIFI) 46 | #define OFFLOADED_DOWNLOAD 47 | #endif 48 | -------------------------------------------------------------------------------- /src/ota/OTATypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "AIoTC_Config.h" 14 | 15 | #if OTA_ENABLED 16 | #include 17 | 18 | namespace ota { 19 | enum class OTAError : int16_t { 20 | None = 0, 21 | NoCapableBootloader = -1, 22 | NoOtaStorage = -2, 23 | OtaStorageInit = -3, 24 | OtaStorageOpen = -4, 25 | OtaHeaderLength = -5, 26 | OtaHeaderCrc = -6, 27 | OtaHeaterMagicNumber = -7, 28 | ParseHttpHeader = -8, 29 | UrlParseError = -9, 30 | ServerConnectError = -10, 31 | HttpHeaderError = -11, 32 | OtaDownload = -12, 33 | OtaHeaderTimeout = -13, 34 | HttpResponse = -14, 35 | OtaStorageEnd = -15, 36 | StorageConfig = -16, 37 | Library = -17, 38 | Modem = -18, 39 | ErrorOpenUpdateFile = -19, 40 | ErrorWriteUpdateFile = -20, 41 | ErrorReformat = -21, 42 | ErrorUnmount = -22, 43 | ErrorRename = -23, 44 | CaStorageInit = -24, 45 | CaStorageOpen = -25, 46 | }; 47 | 48 | #ifndef OFFLOADED_DOWNLOAD 49 | union HeaderVersion { 50 | struct __attribute__((packed)) { 51 | uint32_t header_version : 6; 52 | uint32_t compression : 1; 53 | uint32_t signature : 1; 54 | uint32_t spare : 4; 55 | uint32_t payload_target : 4; 56 | uint32_t payload_major : 8; 57 | uint32_t payload_minor : 8; 58 | uint32_t payload_patch : 8; 59 | uint32_t payload_build_num : 24; 60 | } field; 61 | uint8_t buf[sizeof(field)]; 62 | static_assert(sizeof(buf) == 8, "Error: sizeof(HEADER.VERSION) != 8"); 63 | }; 64 | 65 | union OTAHeader { 66 | struct __attribute__((packed)) { 67 | uint32_t len; 68 | uint32_t crc32; 69 | uint32_t magic_number; 70 | HeaderVersion hdr_version; 71 | } header; 72 | uint8_t buf[sizeof(header)]; 73 | static_assert(sizeof(buf) == 20, "Error: sizeof(HEADER) != 20"); 74 | }; 75 | #endif // OFFLOADED_DOWNLOAD 76 | } 77 | 78 | #endif // OTA_ENABLED 79 | -------------------------------------------------------------------------------- /src/ota/implementation/OTAEsp32.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #include "AIoTC_Config.h" 12 | #if defined(ARDUINO_ARCH_ESP32) && OTA_ENABLED 13 | #include "OTAEsp32.h" 14 | #include 15 | #if defined __has_include 16 | #if __has_include() 17 | #include 18 | #endif 19 | #endif 20 | #include 21 | 22 | ESP32OTACloudProcess::ESP32OTACloudProcess(MessageStream *ms, Client* client) 23 | : OTADefaultCloudProcessInterface(ms), rom_partition(nullptr) { 24 | 25 | } 26 | 27 | 28 | OTACloudProcessInterface::State ESP32OTACloudProcess::resume(Message* msg) { 29 | return OtaBegin; 30 | } 31 | 32 | OTACloudProcessInterface::State ESP32OTACloudProcess::startOTA() { 33 | if(Update.isRunning()) { 34 | Update.abort(); 35 | DEBUG_VERBOSE("%s: Aborting running update", __FUNCTION__); 36 | } 37 | 38 | if(!Update.begin(UPDATE_SIZE_UNKNOWN)) { 39 | DEBUG_VERBOSE("%s: failed to initialize flash update", __FUNCTION__); 40 | return OtaStorageInitFail; 41 | } 42 | 43 | return OTADefaultCloudProcessInterface::startOTA(); 44 | } 45 | 46 | OTACloudProcessInterface::State ESP32OTACloudProcess::flashOTA() { 47 | 48 | if (!Update.end(true)) { 49 | DEBUG_VERBOSE("%s: Failure to apply OTA update", __FUNCTION__); 50 | return OtaStorageEndFail; 51 | } 52 | 53 | return Reboot; 54 | } 55 | 56 | OTACloudProcessInterface::State ESP32OTACloudProcess::reboot() { 57 | ESP.restart(); 58 | 59 | return Idle; // we won't reach this 60 | } 61 | 62 | int ESP32OTACloudProcess::writeFlash(uint8_t* const buffer, size_t len) { 63 | return Update.write(buffer, len); 64 | } 65 | 66 | bool ESP32OTACloudProcess::isOtaCapable() { 67 | const esp_partition_t * ota_0 = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL); 68 | const esp_partition_t * ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL); 69 | return ((ota_0 != nullptr) && (ota_1 != nullptr)); 70 | } 71 | 72 | void* ESP32OTACloudProcess::appStartAddress() { 73 | return nullptr; 74 | } 75 | uint32_t ESP32OTACloudProcess::appSize() { 76 | return ESP.getSketchSize(); 77 | } 78 | 79 | bool ESP32OTACloudProcess::appFlashOpen() { 80 | rom_partition = esp_ota_get_running_partition(); 81 | 82 | if(rom_partition == nullptr) { 83 | return false; 84 | } 85 | 86 | return true; 87 | } 88 | 89 | void ESP32OTACloudProcess::calculateSHA256(SHA256& sha256_calc) { 90 | if(!appFlashOpen()) { 91 | return; // TODO error reporting 92 | } 93 | 94 | sha256_calc.begin(); 95 | 96 | uint8_t b[SPI_FLASH_SEC_SIZE]; 97 | 98 | uint32_t read_bytes = 0; 99 | uint32_t const app_size = ESP.getSketchSize(); 100 | for(uint32_t a = rom_partition->address; read_bytes < app_size; ) { 101 | /* Check if we are reading last sector and compute used size */ 102 | uint32_t const read_size = read_bytes + SPI_FLASH_SEC_SIZE < app_size ? 103 | SPI_FLASH_SEC_SIZE : app_size - read_bytes; 104 | 105 | /* Use always 4 bytes aligned reads */ 106 | if (!ESP.flashRead(a, reinterpret_cast(b), (read_size + 3) & ~3)) { 107 | DEBUG_VERBOSE("ESP32::SHA256 Could not read data from flash"); 108 | return; 109 | } 110 | sha256_calc.update(b, read_size); 111 | a += read_size; 112 | read_bytes += read_size; 113 | } 114 | 115 | appFlashClose(); 116 | } 117 | 118 | #endif // defined(ARDUINO_ARCH_ESP32) && OTA_ENABLED -------------------------------------------------------------------------------- /src/ota/implementation/OTAEsp32.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "ota/interface/OTAInterfaceDefault.h" 14 | 15 | class ESP32OTACloudProcess: public OTADefaultCloudProcessInterface { 16 | public: 17 | ESP32OTACloudProcess(MessageStream *ms, Client* client=nullptr); 18 | 19 | virtual bool isOtaCapable() override; 20 | protected: 21 | virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override; 22 | 23 | // we are overriding the method of startOTA in order to download ota file on ESP32 24 | virtual OTACloudProcessInterface::State startOTA() override; 25 | 26 | // whene the download is correctly finished we set the mcu to use the newly downloaded binary 27 | virtual State flashOTA() override; 28 | 29 | // we reboot the device 30 | virtual State reboot() override; 31 | 32 | // write the decompressed char buffer of the incoming ota 33 | virtual int writeFlash(uint8_t* const buffer, size_t len) override; 34 | 35 | void* appStartAddress(); 36 | uint32_t appSize(); 37 | bool appFlashOpen(); 38 | bool appFlashClose() { return true; }; 39 | 40 | void calculateSHA256(SHA256&) override; 41 | private: 42 | const esp_partition_t *rom_partition; 43 | }; 44 | -------------------------------------------------------------------------------- /src/ota/implementation/OTANanoRP2040.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "ota/interface/OTAInterfaceDefault.h" 14 | 15 | #include "FATFileSystem.h" 16 | #include "FlashIAPBlockDevice.h" 17 | 18 | class NANO_RP2040OTACloudProcess: public OTADefaultCloudProcessInterface { 19 | public: 20 | NANO_RP2040OTACloudProcess(MessageStream *ms, Client* client=nullptr); 21 | ~NANO_RP2040OTACloudProcess(); 22 | 23 | virtual bool isOtaCapable() override; 24 | protected: 25 | virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override; 26 | 27 | virtual OTACloudProcessInterface::State startOTA() override; 28 | 29 | // whene the download is correctly finished we set the mcu to use the newly downloaded binary 30 | virtual OTACloudProcessInterface::State flashOTA() override; 31 | 32 | // we reboot the device 33 | virtual OTACloudProcessInterface::State reboot() override; 34 | 35 | // write the decompressed char buffer of the incoming ota 36 | virtual int writeFlash(uint8_t* const buffer, size_t len) override; 37 | 38 | virtual void reset() override; 39 | 40 | void* appStartAddress(); 41 | uint32_t appSize(); 42 | bool appFlashOpen() { return true; }; 43 | bool appFlashClose() { return true; }; 44 | private: 45 | FlashIAPBlockDevice flash; 46 | FILE* decompressed; 47 | mbed::FATFileSystem* fs; 48 | static const char UPDATE_FILE_NAME[]; 49 | 50 | int close_fs(); 51 | }; 52 | -------------------------------------------------------------------------------- /src/ota/implementation/OTASTM32H7.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | #include "ota/interface/OTAInterfaceDefault.h" 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "WiFi.h" /* WiFi from ArduinoCore-mbed */ 22 | #include 23 | 24 | namespace STM32H747OTA { 25 | /* External QSPI flash + MBR + FatFs */ 26 | static const uint32_t constexpr STORAGE_TYPE = ((1 << 2) | (1 << 5) | (1 << 7)); 27 | /* Default OTA partition */ 28 | static const uint32_t constexpr PARTITION = 2; 29 | /* OTA Magic number */ 30 | static const uint32_t constexpr MAGIC = 0x07AA; 31 | /* OTA download folder name */ 32 | static const char constexpr FOLDER[] = "ota"; 33 | /* OTA update filename */ 34 | static const char constexpr NAME[] = "UPDATE.BIN"; 35 | } 36 | 37 | class STM32H7OTACloudProcess: public OTADefaultCloudProcessInterface { 38 | public: 39 | STM32H7OTACloudProcess(MessageStream *ms, Client* client=nullptr); 40 | ~STM32H7OTACloudProcess(); 41 | void update() override; 42 | 43 | virtual bool isOtaCapable() override; 44 | protected: 45 | virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override; 46 | 47 | // we are overriding the method of startOTA in order to open the destination file for the ota download 48 | virtual OTACloudProcessInterface::State startOTA() override; 49 | 50 | // whene the download is correctly finished we set the mcu to use the newly downloaded binary 51 | virtual OTACloudProcessInterface::State flashOTA() override; 52 | 53 | // we reboot the device 54 | virtual OTACloudProcessInterface::State reboot() override; 55 | 56 | // write the decompressed char buffer of the incoming ota 57 | virtual int writeFlash(uint8_t* const buffer, size_t len) override; 58 | 59 | virtual void reset() override; 60 | 61 | void* appStartAddress(); 62 | uint32_t appSize(); 63 | bool appFlashOpen() { return true; }; 64 | bool appFlashClose() { return true; }; 65 | private: 66 | bool storageInit(); 67 | bool findProgramLength(uint32_t & program_length); 68 | void storageClean(); 69 | 70 | FILE* decompressed; 71 | mbed::BlockDevice* _bd_raw_qspi; 72 | 73 | mbed::BlockDevice* _bd; 74 | mbed::FATFileSystem* _fs; 75 | 76 | String _filename; 77 | }; 78 | -------------------------------------------------------------------------------- /src/ota/implementation/OTASamd.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #include 12 | 13 | #if defined(ARDUINO_ARCH_SAMD) && OTA_ENABLED 14 | #include "OTASamd.h" 15 | 16 | #include 17 | #if OTA_STORAGE_SNU 18 | # include 19 | # include /* WiFiStorage */ 20 | #endif 21 | 22 | SAMDOTACloudProcess::SAMDOTACloudProcess(MessageStream *ms) 23 | : OTACloudProcessInterface(ms){ 24 | 25 | } 26 | 27 | OTACloudProcessInterface::State SAMDOTACloudProcess::resume(Message* msg) { 28 | return OtaBegin; 29 | } 30 | 31 | OTACloudProcessInterface::State SAMDOTACloudProcess::startOTA() { 32 | reset(); 33 | return Fetch; 34 | } 35 | 36 | OTACloudProcessInterface::State SAMDOTACloudProcess::fetch() { 37 | #if OTA_STORAGE_SNU 38 | uint8_t nina_ota_err_code = 0; 39 | if (!WiFiStorage.downloadOTA(this->context->url, &nina_ota_err_code)) { 40 | DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code); 41 | switch(static_cast(nina_ota_err_code)) { 42 | case ninaOTAError::Open: 43 | return ErrorOpenUpdateFileFail; 44 | case ninaOTAError::Length: 45 | return OtaDownloadFail; 46 | case ninaOTAError::CRC: 47 | return OtaHeaderCrcFail; 48 | case ninaOTAError::Rename: 49 | return ErrorRenameFail; 50 | default: 51 | return OtaDownloadFail; 52 | } 53 | } 54 | #endif // OTA_STORAGE_SNU 55 | 56 | return FlashOTA; 57 | } 58 | 59 | OTACloudProcessInterface::State SAMDOTACloudProcess::flashOTA() { 60 | return Reboot; 61 | } 62 | 63 | OTACloudProcessInterface::State SAMDOTACloudProcess::reboot() { 64 | NVIC_SystemReset(); 65 | } 66 | 67 | void SAMDOTACloudProcess::reset() { 68 | #if OTA_STORAGE_SNU 69 | WiFiStorage.remove("/fs/UPDATE.BIN.LZSS"); 70 | WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP"); 71 | #endif // OTA_STORAGE_SNU 72 | } 73 | 74 | bool SAMDOTACloudProcess::isOtaCapable() { 75 | #if OTA_STORAGE_SNU 76 | if (strcmp(WiFi.firmwareVersion(), "1.4.1") < 0) { 77 | DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion()); 78 | return false; 79 | } else { 80 | return true; 81 | } 82 | #endif 83 | return false; 84 | } 85 | 86 | extern void* __text_start__; 87 | extern void* __etext; 88 | extern void* __data_end__; 89 | extern void* __data_start__; 90 | 91 | void* SAMDOTACloudProcess::appStartAddress() { return &__text_start__; } 92 | 93 | uint32_t SAMDOTACloudProcess::appSize() { 94 | return ((&__etext - &__text_start__) + (&__data_end__ - &__data_start__))*sizeof(void*); 95 | } 96 | 97 | #endif // defined(ARDUINO_ARCH_SAMD) && OTA_ENABLED -------------------------------------------------------------------------------- /src/ota/implementation/OTASamd.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "ota/interface/OTAInterface.h" 14 | #include 15 | 16 | class SAMDOTACloudProcess: public OTACloudProcessInterface { 17 | public: 18 | SAMDOTACloudProcess(MessageStream *ms); 19 | 20 | virtual bool isOtaCapable() override; 21 | protected: 22 | virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override; 23 | 24 | // we are overriding the method of startOTA in order to download ota file on ESP32 25 | virtual OTACloudProcessInterface::State startOTA() override; 26 | 27 | // we start the download and decompress process 28 | virtual OTACloudProcessInterface::State fetch() override; 29 | 30 | // whene the download is correctly finished we set the mcu to use the newly downloaded binary 31 | virtual OTACloudProcessInterface::State flashOTA(); 32 | 33 | // we reboot the device 34 | virtual OTACloudProcessInterface::State reboot(); 35 | 36 | virtual void reset() override; 37 | 38 | void* appStartAddress(); 39 | uint32_t appSize(); 40 | 41 | bool appFlashOpen() { return true; } 42 | bool appFlashClose() { return true; } 43 | 44 | private: 45 | enum class ninaOTAError : int { 46 | None = 0, 47 | Open = 1, 48 | Length = 2, 49 | CRC = 3, 50 | Rename = 4, 51 | }; 52 | }; 53 | -------------------------------------------------------------------------------- /src/ota/implementation/OTAUnoR4.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "ota/interface/OTAInterface.h" 14 | #include "OTAUpdate.h" 15 | #include "r_flash_lp.h" 16 | 17 | class UNOR4OTACloudProcess: public OTACloudProcessInterface { 18 | public: 19 | UNOR4OTACloudProcess(MessageStream *ms); 20 | 21 | bool isOtaCapable() override; 22 | protected: 23 | virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override; 24 | 25 | // we are overriding the method of startOTA in order to download ota file on ESP32 26 | virtual OTACloudProcessInterface::State startOTA() override; 27 | 28 | // we start the download and decompress process 29 | virtual OTACloudProcessInterface::State fetch() override; 30 | 31 | // whene the download is correctly finished we set the mcu to use the newly downloaded binary 32 | virtual OTACloudProcessInterface::State flashOTA() override; 33 | 34 | // we reboot the device 35 | virtual OTACloudProcessInterface::State reboot() override; 36 | 37 | virtual void reset() override; 38 | 39 | constexpr void* appStartAddress(); 40 | uint32_t appSize(); 41 | 42 | bool appFlashOpen(); 43 | bool appFlashClose(); 44 | 45 | public: 46 | // used to access to flash memory for sha256 calculation 47 | flash_lp_instance_ctrl_t ctrl; 48 | flash_cfg_t cfg; 49 | 50 | OTAUpdate ota; 51 | static const char UPDATE_FILE_NAME[]; 52 | 53 | struct Context { 54 | uint32_t downloadSize; 55 | uint32_t lastReportTime; 56 | } *context; 57 | }; 58 | -------------------------------------------------------------------------------- /src/ota/interface/OTAInterfaceDefault.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | #pragma once 11 | 12 | #include 13 | 14 | #if OTA_ENABLED && ! defined(OFFLOADED_DOWNLOAD) 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include "OTAInterface.h" 21 | 22 | /** 23 | * This class is the extension of the abstract class for OTA, with the addition that 24 | * the download is performed by the mcu itself and not offloaded to the network peripheral 25 | */ 26 | class OTADefaultCloudProcessInterface: public OTACloudProcessInterface { 27 | public: 28 | OTADefaultCloudProcessInterface(MessageStream *ms, Client* client=nullptr); 29 | ~OTADefaultCloudProcessInterface(); 30 | 31 | inline virtual void setClient(Client* c) { client = c; } 32 | 33 | void setAuthentication(const char* const username, const char* const password) { 34 | this->username = username; 35 | this->password = password; 36 | } 37 | 38 | protected: 39 | State startOTA(); 40 | State fetch(); 41 | void reset(); 42 | virtual int writeFlash(uint8_t* const buffer, size_t len) = 0; 43 | 44 | private: 45 | void parseOta(uint8_t* buffer, size_t bufLen); 46 | State requestOta(OtaFlags mode = None); 47 | bool fetchMore(); 48 | 49 | Client* client; 50 | HttpClient* http_client; 51 | 52 | const char *username, *password; 53 | 54 | // The amount of time that each iteration of Fetch has to take at least 55 | // This mitigate the issues arising from tasks run in main loop that are using all the computing time 56 | static constexpr uint32_t downloadTime = 2000; 57 | 58 | // The amount of data that each iteration of Fetch has to take at least 59 | // This should be enabled setting ChunkDownload OtaFlag to 1 and mitigate some Ota corner cases 60 | static constexpr size_t maxChunkSize = 1024 * 10; 61 | 62 | enum OTADownloadState: uint8_t { 63 | OtaDownloadHeader, 64 | OtaDownloadFile, 65 | OtaDownloadCompleted, 66 | OtaDownloadMagicNumberMismatch, 67 | OtaDownloadError 68 | }; 69 | 70 | protected: 71 | struct Context { 72 | Context( 73 | const char* url, 74 | std::function putc); 75 | 76 | ParsedUrl parsed_url; 77 | ota::OTAHeader header; 78 | OTADownloadState downloadState; 79 | uint32_t calculatedCrc32; 80 | uint32_t headerCopiedBytes; 81 | uint32_t downloadedSize; 82 | uint32_t lastReportTime; 83 | uint32_t contentLength; 84 | bool writeError; 85 | 86 | uint32_t downloadedChunkStartTime; 87 | uint32_t downloadedChunkSize; 88 | 89 | // LZSS decoder 90 | arduino::lzss::Decoder decoder; 91 | 92 | static constexpr size_t bufLen = 64; 93 | uint8_t buffer[bufLen]; 94 | } *context; 95 | }; 96 | 97 | #endif /* OTA_ENABLED && ! defined(OFFLOADED_DOWNLOAD) */ 98 | -------------------------------------------------------------------------------- /src/property/math_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | #pragma once 11 | #include 12 | 13 | namespace arduino { namespace math { 14 | 15 | template 16 | inline bool ieee754_different(const T a, const T b, const T delta = 0.0) { 17 | /* The following comparison is introduced to take into account the very peculiar 18 | * way of handling NaN values by the standard IEEE754. 19 | * We consider two floating point number different if their binary representation is different. 20 | * or if those two IEEE754 values are numbers or zero(which is handled on its own) exceed 21 | * a certain threshold 22 | */ 23 | return memcmp((uint8_t*)&a, (uint8_t*)&b, sizeof(b)) != 0 && ( 24 | (std::fpclassify(a) != FP_NORMAL && std::fpclassify(a) != FP_ZERO) || 25 | (std::fpclassify(b) != FP_NORMAL && std::fpclassify(b) != FP_ZERO) || 26 | fabs(a - b) >= delta 27 | ); 28 | } 29 | }} 30 | -------------------------------------------------------------------------------- /src/property/types/CloudBool.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDBOOL_H_ 19 | #define CLOUDBOOL_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../Property.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudBool : public Property { 35 | protected: 36 | bool _value, 37 | _cloud_value; 38 | public: 39 | CloudBool() { 40 | CloudBool(false); 41 | } 42 | CloudBool(bool v) : _value(v), _cloud_value(v) {} 43 | operator bool() const { 44 | return _value; 45 | } 46 | virtual bool isDifferentFromCloud() { 47 | return _value != _cloud_value; 48 | } 49 | virtual void fromCloudToLocal() { 50 | _value = _cloud_value; 51 | } 52 | virtual void fromLocalToCloud() { 53 | _cloud_value = _value; 54 | } 55 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 56 | return appendAttribute(_value, "", encoder); 57 | } 58 | virtual void setAttributesFromCloud() { 59 | setAttribute(_cloud_value, ""); 60 | } 61 | //modifiers 62 | CloudBool& operator=(bool v) { 63 | _value = v; 64 | updateLocalTimestamp(); 65 | return *this; 66 | } 67 | CloudBool& operator=(CloudBool v) { 68 | return operator=((bool)v); 69 | } 70 | //accessors 71 | CloudBool operator!() const { 72 | return CloudBool(!_value); 73 | } 74 | //friends 75 | }; 76 | 77 | 78 | #endif /* CLOUDBOOL_H_ */ 79 | -------------------------------------------------------------------------------- /src/property/types/CloudLocation.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDLOCATION_H_ 19 | #define CLOUDLOCATION_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include "../Property.h" 28 | 29 | /****************************************************************************** 30 | CLASS DECLARATION 31 | ******************************************************************************/ 32 | 33 | 34 | 35 | class Location { 36 | public: 37 | float lat, 38 | lon; 39 | Location(float lat, float lon) : lat(lat), lon(lon) {} 40 | Location& operator=(Location& aLocation) { 41 | lat = aLocation.lat; 42 | lon = aLocation.lon; 43 | return *this; 44 | } 45 | Location operator-(Location& aLocation) { 46 | return Location(lat - aLocation.lat, lon - aLocation.lon); 47 | } 48 | bool operator==(Location& aLocation) { 49 | return lat == aLocation.lat && lon == aLocation.lon; 50 | } 51 | bool operator!=(Location& aLocation) { 52 | return !(operator==(aLocation)); 53 | } 54 | static float distance(Location& loc1, Location& loc2) { 55 | return sqrt(pow(loc1.lat - loc2.lat, 2) + pow(loc1.lon - loc2.lon, 2)); 56 | } 57 | }; 58 | 59 | class CloudLocation : public Property { 60 | private: 61 | Location _value, 62 | _cloud_value; 63 | public: 64 | CloudLocation() : _value(0, 0), _cloud_value(0, 0) {} 65 | CloudLocation(float lat, float lon) : _value(lat, lon), _cloud_value(lat, lon) {} 66 | virtual bool isDifferentFromCloud() { 67 | float const distance = Location::distance(_value, _cloud_value); 68 | return _value != _cloud_value && (abs(distance) >= Property::_min_delta_property); 69 | } 70 | 71 | CloudLocation& operator=(Location aLocation) { 72 | _value.lat = aLocation.lat; 73 | _value.lon = aLocation.lon; 74 | updateLocalTimestamp(); 75 | return *this; 76 | } 77 | 78 | Location getCloudValue() { 79 | return _cloud_value; 80 | } 81 | 82 | Location getValue() { 83 | return _value; 84 | } 85 | 86 | virtual void fromCloudToLocal() { 87 | _value = _cloud_value; 88 | } 89 | virtual void fromLocalToCloud() { 90 | _cloud_value = _value; 91 | } 92 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 93 | CHECK_CBOR_MULTI(appendAttribute(_value.lat, "lat", encoder)); 94 | CHECK_CBOR_MULTI(appendAttribute(_value.lon, "lon", encoder)); 95 | return CborNoError; 96 | } 97 | virtual void setAttributesFromCloud() { 98 | setAttribute(_cloud_value.lat, "lat"); 99 | setAttribute(_cloud_value.lon, "lon"); 100 | } 101 | }; 102 | 103 | #endif /* CLOUDLOCATION_H_ */ 104 | -------------------------------------------------------------------------------- /src/property/types/CloudString.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDSTRING_H_ 19 | #define CLOUDSTRING_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../Property.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudString : public Property { 35 | private: 36 | String _value, 37 | _cloud_value; 38 | public: 39 | CloudString() { 40 | CloudString(""); 41 | } 42 | CloudString(const char *v) { 43 | CloudString(String(v)); 44 | } 45 | CloudString(String v) : _value(v), _cloud_value(v) {} 46 | operator String() const { 47 | return _value; 48 | } 49 | void clear() { 50 | _value = PropertyActions::CLEAR; 51 | } 52 | virtual bool isDifferentFromCloud() { 53 | return _value != _cloud_value; 54 | } 55 | virtual void fromCloudToLocal() { 56 | _value = _cloud_value; 57 | } 58 | virtual void fromLocalToCloud() { 59 | _cloud_value = _value; 60 | } 61 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 62 | return appendAttribute(_value, "", encoder); 63 | } 64 | virtual void setAttributesFromCloud() { 65 | setAttribute(_cloud_value, ""); 66 | } 67 | //modifiers 68 | CloudString& operator=(String v) { 69 | _value = v; 70 | updateLocalTimestamp(); 71 | return *this; 72 | } 73 | CloudString& operator=(const char *v) { 74 | return operator=(String(v)); 75 | } 76 | CloudString& operator+=(String v) { 77 | return operator=(_value += v); 78 | } 79 | bool operator==(const char *c) const { 80 | return operator==(String(c)); 81 | } 82 | bool operator==(String c) const { 83 | return _value == c; 84 | } 85 | //friends 86 | friend CloudString operator+(CloudString cs, String v) { 87 | return cs += v; 88 | } 89 | }; 90 | 91 | 92 | #endif /* CLOUDSTRING_H_ */ 93 | -------------------------------------------------------------------------------- /src/property/types/CloudWrapperBase.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDWRAPPERBASE_H_ 19 | #define CLOUDWRAPPERBASE_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../Property.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class CloudWrapperBase : public Property { 33 | public: 34 | virtual bool isChangedLocally() = 0; 35 | }; 36 | 37 | 38 | #endif /* CLOUDWRAPPERBASE_H_ */ 39 | -------------------------------------------------------------------------------- /src/property/types/CloudWrapperBool.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDWRAPPERBOOL_H_ 19 | #define CLOUDWRAPPERBOOL_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "CloudWrapperBase.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class CloudWrapperBool : public CloudWrapperBase { 33 | private: 34 | bool &_primitive_value, 35 | _cloud_value, 36 | _local_value; 37 | public: 38 | CloudWrapperBool(bool& v) : _primitive_value(v), _cloud_value(v), _local_value(v) {} 39 | virtual bool isDifferentFromCloud() { 40 | return _primitive_value != _cloud_value; 41 | } 42 | virtual void fromCloudToLocal() { 43 | _primitive_value = _cloud_value; 44 | } 45 | virtual void fromLocalToCloud() { 46 | _cloud_value = _primitive_value; 47 | } 48 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 49 | return appendAttribute(_primitive_value, "", encoder); 50 | } 51 | virtual void setAttributesFromCloud() { 52 | setAttribute(_cloud_value, ""); 53 | } 54 | virtual bool isPrimitive() { 55 | return true; 56 | } 57 | virtual bool isChangedLocally() { 58 | return _primitive_value != _local_value; 59 | } 60 | }; 61 | 62 | 63 | #endif /* CLOUDWRAPPERBOOL_H_ */ 64 | -------------------------------------------------------------------------------- /src/property/types/CloudWrapperFloat.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDWRAPPERFLOAT_H_ 19 | #define CLOUDWRAPPERFLOAT_H_ 20 | 21 | #include 22 | #include "../math_utils.h" 23 | 24 | /****************************************************************************** 25 | INCLUDE 26 | ******************************************************************************/ 27 | 28 | #include 29 | #include "CloudWrapperBase.h" 30 | 31 | /****************************************************************************** 32 | CLASS DECLARATION 33 | ******************************************************************************/ 34 | 35 | class CloudWrapperFloat : public CloudWrapperBase { 36 | private: 37 | float &_primitive_value, 38 | _cloud_value, 39 | _local_value; 40 | public: 41 | CloudWrapperFloat(float& v) : _primitive_value(v), _cloud_value(v), _local_value(v) {} 42 | virtual bool isDifferentFromCloud() { 43 | return arduino::math::ieee754_different(_primitive_value, _cloud_value, Property::_min_delta_property); 44 | } 45 | virtual void fromCloudToLocal() { 46 | _primitive_value = _cloud_value; 47 | } 48 | virtual void fromLocalToCloud() { 49 | _cloud_value = _primitive_value; 50 | } 51 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 52 | return appendAttribute(_primitive_value, "", encoder); 53 | } 54 | virtual void setAttributesFromCloud() { 55 | setAttribute(_cloud_value, ""); 56 | } 57 | virtual bool isPrimitive() { 58 | return true; 59 | } 60 | virtual bool isChangedLocally() { 61 | return _primitive_value != _local_value; 62 | } 63 | }; 64 | 65 | 66 | #endif /* CLOUWRAPPERFLOAT_H_ */ 67 | -------------------------------------------------------------------------------- /src/property/types/CloudWrapperInt.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDWRAPPERINT_H_ 19 | #define CLOUDWRAPPERINT_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "CloudWrapperBase.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class CloudWrapperInt : public CloudWrapperBase { 33 | private: 34 | int &_primitive_value, 35 | _cloud_value, 36 | _local_value; 37 | public: 38 | CloudWrapperInt(int& v) : _primitive_value(v), _cloud_value(v), _local_value(v) {} 39 | virtual bool isDifferentFromCloud() { 40 | return _primitive_value != _cloud_value && (abs(_primitive_value - _cloud_value) >= Property::_min_delta_property); 41 | } 42 | virtual void fromCloudToLocal() { 43 | _primitive_value = _cloud_value; 44 | } 45 | virtual void fromLocalToCloud() { 46 | _cloud_value = _primitive_value; 47 | } 48 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 49 | return appendAttribute(_primitive_value, "", encoder); 50 | } 51 | virtual void setAttributesFromCloud() { 52 | setAttribute(_cloud_value, ""); 53 | } 54 | virtual bool isPrimitive() { 55 | return true; 56 | } 57 | virtual bool isChangedLocally() { 58 | return _primitive_value != _local_value; 59 | } 60 | }; 61 | 62 | 63 | #endif /* CLOUDWRAPPERINT_H_ */ 64 | -------------------------------------------------------------------------------- /src/property/types/CloudWrapperString.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDWRAPPERSTRING_H_ 19 | #define CLOUDWRAPPERSTRING_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "CloudWrapperBase.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class CloudWrapperString : public CloudWrapperBase { 33 | private: 34 | String &_primitive_value, 35 | _cloud_value, 36 | _local_value; 37 | public: 38 | CloudWrapperString(String& v) : 39 | _primitive_value(v), 40 | _cloud_value(v), 41 | _local_value(v) { 42 | } 43 | virtual bool isDifferentFromCloud() { 44 | return _primitive_value != _cloud_value; 45 | } 46 | virtual void fromCloudToLocal() { 47 | _primitive_value = _cloud_value; 48 | } 49 | virtual void fromLocalToCloud() { 50 | _cloud_value = _primitive_value; 51 | } 52 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 53 | return appendAttribute(_primitive_value, "", encoder); 54 | } 55 | virtual void setAttributesFromCloud() { 56 | setAttribute(_cloud_value, ""); 57 | } 58 | virtual bool isPrimitive() { 59 | return true; 60 | } 61 | virtual bool isChangedLocally() { 62 | return _primitive_value != _local_value; 63 | } 64 | }; 65 | 66 | 67 | #endif /* CLOUDWRAPPERSTRING_H_ */ 68 | -------------------------------------------------------------------------------- /src/property/types/CloudWrapperUnsignedInt.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDWRAPPERUINT_H_ 19 | #define CLOUDWRAPPERUINT_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "CloudWrapperBase.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | class CloudWrapperUnsignedInt : public CloudWrapperBase { 33 | private: 34 | unsigned int &_primitive_value, 35 | _cloud_value, 36 | _local_value; 37 | public: 38 | CloudWrapperUnsignedInt(unsigned int& v) : _primitive_value(v), _cloud_value(v), _local_value(v) {} 39 | virtual bool isDifferentFromCloud() { 40 | return _primitive_value != _cloud_value && ((std::max(_primitive_value , _cloud_value) - std::min(_primitive_value , _cloud_value)) >= Property::_min_delta_property); 41 | } 42 | virtual void fromCloudToLocal() { 43 | _primitive_value = _cloud_value; 44 | } 45 | virtual void fromLocalToCloud() { 46 | _cloud_value = _primitive_value; 47 | } 48 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 49 | return appendAttribute(_primitive_value, "", encoder); 50 | } 51 | virtual void setAttributesFromCloud() { 52 | setAttribute(_cloud_value, ""); 53 | } 54 | virtual bool isPrimitive() { 55 | return true; 56 | } 57 | virtual bool isChangedLocally() { 58 | return _primitive_value != _local_value; 59 | } 60 | }; 61 | 62 | 63 | #endif /* CLOUDWRAPPERINT_H_ */ 64 | -------------------------------------------------------------------------------- /src/property/types/automation/CloudContactSensor.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDCONTACTSENSOR_H_ 19 | #define CLOUDCONTACTSENSOR_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../CloudBool.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudContactSensor : public CloudBool { 35 | private: 36 | public: 37 | operator bool() const { 38 | return _value; 39 | } 40 | CloudContactSensor& operator=(bool v) { 41 | CloudBool::operator=(v); 42 | return *this; 43 | } 44 | }; 45 | 46 | 47 | #endif /* CLOUDCONTACTSENSOR_H_ */ 48 | -------------------------------------------------------------------------------- /src/property/types/automation/CloudDimmedLight.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDDIMMEDLIGHT_H_ 19 | #define CLOUDDIMMEDLIGHT_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include "../../Property.h" 28 | 29 | /****************************************************************************** 30 | CLASS DECLARATION 31 | ******************************************************************************/ 32 | class DimmedLight { 33 | public: 34 | bool swi; 35 | float bri; 36 | DimmedLight(bool swi, float bri): swi(swi), bri(bri) { 37 | } 38 | 39 | bool operator==(DimmedLight & aLight) { 40 | return aLight.swi == swi && aLight.bri == bri; 41 | } 42 | 43 | bool operator!=(DimmedLight & aLight) { 44 | return !(operator==(aLight)); 45 | } 46 | 47 | }; 48 | 49 | class CloudDimmedLight : public Property { 50 | private: 51 | DimmedLight _value, 52 | _cloud_value; 53 | 54 | public: 55 | CloudDimmedLight() : _value(false, 0), _cloud_value(false, 0) {} 56 | CloudDimmedLight(bool swi, float brightness) : _value(swi, brightness), _cloud_value(swi, brightness) {} 57 | 58 | virtual bool isDifferentFromCloud() { 59 | 60 | return _value != _cloud_value; 61 | } 62 | 63 | CloudDimmedLight& operator=(DimmedLight aLight) { 64 | _value.swi = aLight.swi; 65 | _value.bri = aLight.bri; 66 | updateLocalTimestamp(); 67 | return *this; 68 | } 69 | 70 | DimmedLight getCloudValue() { 71 | return _cloud_value; 72 | } 73 | 74 | DimmedLight getValue() { 75 | return _value; 76 | } 77 | 78 | float getBrightness() { 79 | return _value.bri; 80 | } 81 | 82 | void setBrightness(float const bri) { 83 | _value.bri = bri; 84 | } 85 | 86 | bool getSwitch() { 87 | return _value.swi; 88 | } 89 | 90 | void setSwitch(bool const swi) { 91 | _value.swi = swi; 92 | } 93 | 94 | virtual void fromCloudToLocal() { 95 | _value = _cloud_value; 96 | } 97 | virtual void fromLocalToCloud() { 98 | _cloud_value = _value; 99 | } 100 | 101 | virtual CborError appendAttributesToCloud(CborEncoder *encoder) { 102 | CHECK_CBOR_MULTI(appendAttribute(_value.swi, "swi", encoder)); 103 | CHECK_CBOR_MULTI(appendAttribute(_value.bri, "bri", encoder)); 104 | return CborNoError; 105 | } 106 | 107 | virtual void setAttributesFromCloud() { 108 | setAttribute(_cloud_value.swi, "swi"); 109 | setAttribute(_cloud_value.bri, "bri"); 110 | } 111 | }; 112 | 113 | #endif /* CLOUDDIMMEDLIGHT_H_ */ -------------------------------------------------------------------------------- /src/property/types/automation/CloudLight.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDLIGHT_H_ 19 | #define CLOUDLIGHT_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../CloudBool.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudLight : public CloudBool { 35 | private: 36 | public: 37 | operator bool() const { 38 | return _value; 39 | } 40 | CloudLight& operator=(bool v) { 41 | CloudBool::operator=(v); 42 | return *this; 43 | } 44 | }; 45 | 46 | 47 | #endif /* CLOUDLIGHT_H_ */ 48 | -------------------------------------------------------------------------------- /src/property/types/automation/CloudMotionSensor.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDMOTIONSENSOR_H_ 19 | #define CLOUDMOTIONSENSOR_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../CloudBool.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudMotionSensor : public CloudBool { 35 | private: 36 | public: 37 | operator bool() const { 38 | return _value; 39 | } 40 | CloudMotionSensor& operator=(bool v) { 41 | CloudBool::operator=(v); 42 | return *this; 43 | } 44 | }; 45 | 46 | 47 | #endif /* CLOUDMOTIONSENSOR_H_ */ 48 | -------------------------------------------------------------------------------- /src/property/types/automation/CloudSmartPlug.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDSMARTPLUG_H_ 19 | #define CLOUDSMARTPLUG_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../CloudBool.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudSmartPlug : public CloudBool { 35 | private: 36 | public: 37 | operator bool() const { 38 | return _value; 39 | } 40 | CloudSmartPlug& operator=(bool v) { 41 | CloudBool::operator=(v); 42 | return *this; 43 | } 44 | }; 45 | 46 | 47 | #endif /* CLOUDSMARTPLUG_H_ */ 48 | -------------------------------------------------------------------------------- /src/property/types/automation/CloudSwitch.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUDSWITCH_H_ 19 | #define CLOUDSWITCH_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | #include "../CloudBool.h" 27 | 28 | /****************************************************************************** 29 | CLASS DECLARATION 30 | ******************************************************************************/ 31 | 32 | 33 | 34 | class CloudSwitch : public CloudBool { 35 | private: 36 | public: 37 | operator bool() const { 38 | return _value; 39 | } 40 | CloudSwitch& operator=(bool v) { 41 | CloudBool::operator=(v); 42 | return *this; 43 | } 44 | }; 45 | 46 | 47 | #endif /* CLOUDSWITCH_H_ */ 48 | -------------------------------------------------------------------------------- /src/property/types/automation/CloudTemperatureSensor.h: -------------------------------------------------------------------------------- 1 | // 2 | // This file is part of ArduinoCloudThing 3 | // 4 | // Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | // 6 | // This software is released under the GNU General Public License version 3, 7 | // which covers the main part of ArduinoCloudThing. 8 | // The terms of this license can be found at: 9 | // https://www.gnu.org/licenses/gpl-3.0.en.html 10 | // 11 | // You can be released from the requirements of the above licenses by purchasing 12 | // a commercial license. Buying such a license is mandatory if you want to modify or 13 | // otherwise use the software for commercial activities involving the Arduino 14 | // software without disclosing the source code of your own applications. To purchase 15 | // a commercial license, send an email to license@arduino.cc. 16 | // 17 | 18 | #ifndef CLOUD_TEMPERATURE_SENSOR_H_ 19 | #define CLOUD_TEMPERATURE_SENSOR_H_ 20 | 21 | /****************************************************************************** 22 | INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include "../CloudFloat.h" 26 | 27 | /****************************************************************************** 28 | CLASS DECLARATION 29 | ******************************************************************************/ 30 | 31 | class CloudTemperatureSensor : public CloudFloat 32 | { 33 | public: 34 | 35 | CloudTemperatureSensor & operator = (float v) 36 | { 37 | CloudFloat::operator=(v); 38 | return *this; 39 | } 40 | }; 41 | 42 | #endif /* CLOUD_TEMPERATURE_SENSOR_H_ */ 43 | -------------------------------------------------------------------------------- /src/tls/AIoTCSSCert.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTBearSSL. 3 | 4 | Copyright 2019 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of ArduinoIoTBearSSL. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | 17 | */ 18 | 19 | #ifndef _AIOTC_SS_CERT_H_ 20 | #define _AIOTC_SS_CERT_H_ 21 | 22 | /****************************************************************************** 23 | * INCLUDE 24 | ******************************************************************************/ 25 | 26 | #include 27 | #if defined(BOARD_HAS_SE050) || defined(BOARD_HAS_SOFTSE) 28 | 29 | /****************************************************************************** 30 | * CONSTANTS 31 | ******************************************************************************/ 32 | static const char AIoTSSCert[] = 33 | /* https://iot.arduino.cc:8885 */ 34 | "-----BEGIN CERTIFICATE-----\n" 35 | "MIIB0DCCAXagAwIBAgIUb62eK/Vv1baaPAaY5DADBUbxB1owCgYIKoZIzj0EAwIw\n" 36 | "RTELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDkFyZHVpbm8gTExDIFVTMQswCQYDVQQL\n" 37 | "EwJJVDEQMA4GA1UEAxMHQXJkdWlubzAgFw0yNTAxMTAxMDUzMjJaGA8yMDU1MDEw\n" 38 | "MzEwNTMyMlowRTELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDkFyZHVpbm8gTExDIFVT\n" 39 | "MQswCQYDVQQLEwJJVDEQMA4GA1UEAxMHQXJkdWlubzBZMBMGByqGSM49AgEGCCqG\n" 40 | "SM49AwEHA0IABKHhU2w1UhozDegrrFsSwY9QN7M+ZJug7icCNceNWhBF0Mr1UuyX\n" 41 | "8pr/gcbieZc/0znG16HMa2GFcPY7rmIdccijQjBAMA8GA1UdEwEB/wQFMAMBAf8w\n" 42 | "DgYDVR0PAQH/BAQDAgEGMB0GA1UdDgQWBBRCZSmE0ASI0cYD9AmzeOM7EijgPjAK\n" 43 | "BggqhkjOPQQDAgNIADBFAiEAz6TLYP9eiVOr/cVU/11zwGofe/FoNe4p1BlzMl7G\n" 44 | "VVACIG8tL3Ta2WbIOaUVpBL2gfLuI9WSW1sR++zXP+zFhmen\n" 45 | "-----END CERTIFICATE-----\n" 46 | /* https://iot.oniudra.cc:8885 */ 47 | "-----BEGIN CERTIFICATE-----\n" 48 | "MIIBzzCCAXagAwIBAgIUI5fEitwlnwujc/mU0d8LnDiDXBIwCgYIKoZIzj0EAwIw\n" 49 | "RTELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDkFyZHVpbm8gTExDIFVTMQswCQYDVQQL\n" 50 | "EwJJVDEQMA4GA1UEAxMHQXJkdWlubzAgFw0yNTAxMDgxMTA4MzdaGA8yMDU1MDEw\n" 51 | "MTExMDgzN1owRTELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDkFyZHVpbm8gTExDIFVT\n" 52 | "MQswCQYDVQQLEwJJVDEQMA4GA1UEAxMHQXJkdWlubzBZMBMGByqGSM49AgEGCCqG\n" 53 | "SM49AwEHA0IABBFwNODDPgC9C1kDmKBbawtQ31FmTudAXVpGSOUwcDX582z820cD\n" 54 | "eIaCwOxghmI+p/CpOH63f5F6h23ErqZMBkijQjBAMA8GA1UdEwEB/wQFMAMBAf8w\n" 55 | "DgYDVR0PAQH/BAQDAgEGMB0GA1UdDgQWBBQdnBmQGLB7ls/r1Tetdp+MVMqxfTAK\n" 56 | "BggqhkjOPQQDAgNHADBEAiBPSZ9HpF7MuFoK4Jsz//PHILQuHM4WmRopQR9ysSs0\n" 57 | "HAIgNadMPgxv01dy59kCgzehgKzmKdTF0rG1SniYqnkLqPA=\n" 58 | "-----END CERTIFICATE-----\n"; 59 | 60 | #endif /* #if defined(BOARD_HAS_SE050) || defined(BOARD_HAS_SOFTSE) */ 61 | 62 | #endif /* _AIOTC_SS_CERT_H_ */ 63 | -------------------------------------------------------------------------------- /src/tls/BearSSLClientProfile.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #ifndef _BEAR_SSL_CLIENT_PROFILE_H_ 26 | #define _BEAR_SSL_CLIENT_PROFILE_H_ 27 | 28 | /****************************************************************************** 29 | * INCLUDE 30 | ******************************************************************************/ 31 | 32 | #include 33 | #ifdef BOARD_HAS_ECCX08 34 | 35 | extern "C" void aiotc_client_profile_init(br_ssl_client_context *cc, br_x509_minimal_context *xc, const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num); 36 | 37 | #endif /* #ifdef BOARD_HAS_ECCX08 */ 38 | 39 | #endif /* _BEAR_SSL_CLIENT_PROFILE_H_ */ 40 | 41 | -------------------------------------------------------------------------------- /src/tls/utility/TLSClientMqtt.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #include 12 | 13 | #ifdef HAS_TCP 14 | 15 | #include "TLSClientMqtt.h" 16 | 17 | #if defined(BOARD_HAS_SECRET_KEY) 18 | #include "tls/AIoTCUPCert.h" 19 | #endif 20 | 21 | #if defined(BOARD_HAS_SE050) || defined(BOARD_HAS_SOFTSE) 22 | #include "tls/AIoTCSSCert.h" 23 | #endif 24 | 25 | #ifdef BOARD_HAS_ECCX08 26 | #include "tls/BearSSLTrustAnchors.h" 27 | #include "tls/BearSSLClientProfile.h" 28 | extern "C" { 29 | unsigned long getTime(); 30 | } 31 | #endif 32 | 33 | 34 | void TLSClientMqtt::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) { 35 | 36 | #if defined(BOARD_HAS_OFFLOADED_ECCX08) 37 | /* Arduino Root CA is configured in nina-fw 38 | * https://github.com/arduino/nina-fw/blob/master/arduino/libraries/ArduinoBearSSL/src/BearSSLTrustAnchors.h 39 | */ 40 | (void)authMode; 41 | #elif defined(BOARD_HAS_ECCX08) 42 | (void)authMode; 43 | setClient(connection.getClient()); 44 | setProfile(aiotc_client_profile_init); 45 | setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM); 46 | ArduinoBearSSL.onGetTime(getTime); 47 | #elif defined(ARDUINO_PORTENTA_C33) 48 | (void)authMode; 49 | setClient(connection.getClient()); 50 | setCACert(AIoTSSCert); 51 | #elif defined(ARDUINO_NICLA_VISION) 52 | (void)authMode; 53 | appendCustomCACert(AIoTSSCert); 54 | #elif defined(ARDUINO_EDGE_CONTROL) 55 | (void)authMode; 56 | appendCustomCACert(AIoTUPCert); 57 | #elif defined(ARDUINO_UNOR4_WIFI) 58 | /* Arduino Root CA is configured in uno-r4-wifi-usb-bridge fw >= 0.4.1 59 | * https://github.com/arduino/uno-r4-wifi-usb-bridge/blob/main/certificates/cacrt_all.pem 60 | * Boards using username/password authentication relies on Starfield Class 2 CA 61 | * also present in older firmware revisions 62 | * https://github.com/arduino/uno-r4-wifi-usb-bridge/blob/f09ca94fdcab845b8368d4435fdac9f6999d21d2/certificates/certificates.pem#L852 63 | */ 64 | (void)connection; 65 | /* Temporary force CACert to add new CA without rebuilding firmware */ 66 | if (authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) { 67 | setCACert(AIoTSSCert); 68 | } 69 | #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) 70 | setCACert(AIoTUPCert); 71 | #elif defined(ARDUINO_ARCH_ESP32) 72 | (void)authMode; 73 | setCACert(AIoTUPCert); 74 | #elif defined(ARDUINO_ARCH_ESP8266) 75 | (void)authMode; 76 | setInsecure(); 77 | #endif 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /src/tls/utility/TLSClientMqtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | enum class ArduinoIoTAuthenticationMode 17 | { 18 | PASSWORD, 19 | CERTIFICATE 20 | }; 21 | 22 | #if defined(BOARD_HAS_OFFLOADED_ECCX08) 23 | /* 24 | * Arduino MKR WiFi1010 - WiFi 25 | * Arduino NANO 33 IoT - WiFi 26 | */ 27 | #include "WiFiSSLClient.h" 28 | class TLSClientMqtt : public WiFiBearSSLClient { 29 | #elif defined(BOARD_HAS_ECCX08) 30 | /* 31 | * Arduino MKR GSM 1400 32 | * Arduino MKR NB 1500 33 | * Arduino NANO RP 2040 34 | * Arduino Portenta H7 35 | * Arduino Giga R1 36 | * OPTA 37 | */ 38 | #include 39 | #include 40 | class TLSClientMqtt : public BearSSLClient { 41 | #elif defined(ARDUINO_PORTENTA_C33) 42 | /* 43 | * Arduino Portenta C33 44 | */ 45 | #include 46 | class TLSClientMqtt : public SSLClient { 47 | #elif defined(ARDUINO_NICLA_VISION) 48 | /* 49 | * Arduino Nicla Vision 50 | */ 51 | #include 52 | class TLSClientMqtt : public WiFiSSLSE050Client { 53 | #elif defined(ARDUINO_EDGE_CONTROL) 54 | /* 55 | * Arduino Edge Control 56 | */ 57 | #include 58 | class TLSClientMqtt : public GSMSSLClient { 59 | #elif defined(ARDUINO_UNOR4_WIFI) 60 | /* 61 | * Arduino UNO R4 WiFi 62 | */ 63 | #include 64 | class TLSClientMqtt : public WiFiSSLClient { 65 | #elif defined(BOARD_ESP) || defined(ARDUINO_RASPBERRY_PI_PICO_W) 66 | /* 67 | * ESP32* 68 | * ESP82* 69 | * PICOW 70 | */ 71 | #include 72 | class TLSClientMqtt : public WiFiClientSecure { 73 | #endif 74 | 75 | public: 76 | void begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE); 77 | 78 | }; 79 | -------------------------------------------------------------------------------- /src/tls/utility/TLSClientOta.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #include 12 | 13 | #if defined(HAS_TCP) && OTA_ENABLED 14 | 15 | #include "TLSClientOta.h" 16 | 17 | #if defined(BOARD_HAS_SECRET_KEY) 18 | #include "tls/AIoTCUPCert.h" 19 | #endif 20 | 21 | #if defined(BOARD_HAS_SE050) || defined(BOARD_HAS_SOFTSE) 22 | #include "tls/AIoTCSSCert.h" 23 | #endif 24 | 25 | #ifdef BOARD_HAS_ECCX08 26 | #include "tls/BearSSLTrustAnchors.h" 27 | #include "tls/BearSSLClientProfile.h" 28 | extern "C" { 29 | unsigned long getTime(); 30 | } 31 | #endif 32 | 33 | void TLSClientOta::begin(ConnectionHandler &connection) { 34 | #if defined(BOARD_HAS_OFFLOADED_ECCX08) 35 | /* AWS Root CAs are configured in nina-fw 36 | * https://github.com/arduino/nina-fw/blob/master/data/roots.pem 37 | */ 38 | #elif defined(BOARD_HAS_ECCX08) 39 | setClient(*getNewClient(connection.getInterface())); 40 | setProfile(aiotc_client_profile_init); 41 | setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM); 42 | ArduinoBearSSL.onGetTime(getTime); 43 | #elif defined(ARDUINO_PORTENTA_C33) 44 | setClient(*getNewClient(connection.getInterface())); 45 | setCACert(AIoTSSCert); 46 | #elif defined(ARDUINO_NICLA_VISION) 47 | appendCustomCACert(AIoTSSCert); 48 | #elif defined(ARDUINO_EDGE_CONTROL) 49 | appendCustomCACert(AIoTUPCert); 50 | #elif defined(ARDUINO_UNOR4_WIFI) 51 | /* AWS Root CAs are configured in uno-r4-wifi-usb-bridge/libraries/Arduino_ESP32_OTA 52 | * https://github.com/arduino-libraries/Arduino_ESP32_OTA/blob/fc755e7d1d3946232107e2590662ee08d6ccdec4/src/tls/amazon_root_ca.h 53 | */ 54 | (void)connection; 55 | #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) 56 | setCACert(AIoTUPCert); 57 | #elif defined(ARDUINO_ARCH_ESP32) 58 | setCACert(AIoTUPCert); 59 | #elif defined(ARDUINO_ARCH_ESP8266) 60 | setInsecure(); 61 | #endif 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/tls/utility/TLSClientOta.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the ArduinoIoTCloud library. 3 | 4 | Copyright (c) 2024 Arduino SA 5 | 6 | This Source Code Form is subject to the terms of the Mozilla Public 7 | License, v. 2.0. If a copy of the MPL was not distributed with this 8 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | 11 | #pragma once 12 | 13 | #include 14 | #include 15 | 16 | #if defined(BOARD_HAS_OFFLOADED_ECCX08) 17 | /* 18 | * Arduino MKR WiFi1010 - WiFi 19 | * Arduino NANO 33 IoT - WiFi 20 | */ 21 | #include "WiFiSSLClient.h" 22 | class TLSClientOta : public WiFiBearSSLClient { 23 | #elif defined(BOARD_HAS_ECCX08) 24 | /* 25 | * Arduino MKR GSM 1400 26 | * Arduino MKR NB 1500 27 | * Arduino NANO RP 2040 28 | * Arduino Portenta H7 29 | * Arduino Giga R1 30 | * OPTA 31 | */ 32 | #include 33 | #include 34 | class TLSClientOta : public BearSSLClient { 35 | #elif defined(ARDUINO_PORTENTA_C33) 36 | /* 37 | * Arduino Portenta C33 38 | */ 39 | #include 40 | class TLSClientOta : public SSLClient { 41 | #elif defined(ARDUINO_NICLA_VISION) 42 | /* 43 | * Arduino Nicla Vision 44 | */ 45 | #include 46 | class TLSClientOta : public WiFiSSLSE050Client { 47 | #elif defined(ARDUINO_EDGE_CONTROL) 48 | /* 49 | * Arduino Edge Control 50 | */ 51 | #include 52 | class TLSClientOta : public GSMSSLClient { 53 | #elif defined(ARDUINO_UNOR4_WIFI) 54 | /* 55 | * Arduino UNO R4 WiFi 56 | */ 57 | #include 58 | class TLSClientOta : public WiFiSSLClient { 59 | #elif defined(BOARD_ESP) || defined(ARDUINO_RASPBERRY_PI_PICO_W) 60 | /* 61 | * ESP32* 62 | * ESP82* 63 | * PICOW 64 | */ 65 | #include 66 | class TLSClientOta : public WiFiClientSecure { 67 | #endif 68 | 69 | public: 70 | void begin(ConnectionHandler & connection); 71 | 72 | private: 73 | inline Client* getNewClient(NetworkAdapter net) { 74 | switch(net) { 75 | #ifdef BOARD_HAS_WIFI 76 | case NetworkAdapter::WIFI: 77 | return new WiFiClient(); 78 | #endif // BOARD_HAS_WIFI 79 | #ifdef BOARD_HAS_ETHERNET 80 | case NetworkAdapter::ETHERNET: 81 | return new EthernetClient(); 82 | #endif // BOARD_HAS_ETHERNET 83 | #ifdef BOARD_HAS_NB 84 | case NetworkAdapter::NB: 85 | return new NBClient(); 86 | #endif // BOARD_HAS_NB 87 | #ifdef BOARD_HAS_GSM 88 | case NetworkAdapter::GSM: 89 | return new GSMClient(); 90 | #endif // BOARD_HAS_GSM 91 | #ifdef BOARD_HAS_CATM1_NBIOT 92 | case NetworkAdapter::CATM1: 93 | return new GSMClient(); 94 | #endif // BOARD_HAS_CATM1_NBIOT 95 | #ifdef BOARD_HAS_CELLULAR 96 | case NetworkAdapter::CELL: 97 | return new TinyGsmClient(modem, 1); 98 | #endif // BOARD_HAS_CELLULAR 99 | default: 100 | return nullptr; 101 | } 102 | } 103 | }; 104 | -------------------------------------------------------------------------------- /src/utility/time/NTPUtils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | /************************************************************************************** 19 | * INCLUDE 20 | **************************************************************************************/ 21 | 22 | #include "../../AIoTC_Config.h" 23 | #ifndef HAS_LORA 24 | 25 | #include "NTPUtils.h" 26 | 27 | #include 28 | #ifdef BOARD_HAS_ECCX08 29 | #include 30 | #endif 31 | 32 | /************************************************************************************** 33 | * PUBLIC MEMBER FUNCTIONS 34 | **************************************************************************************/ 35 | 36 | unsigned long NTPUtils::getTime(UDP & udp) 37 | { 38 | #ifdef NTP_USE_RANDOM_PORT 39 | udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT)); 40 | #else 41 | udp.begin(NTP_LOCAL_PORT); 42 | #endif 43 | 44 | sendNTPpacket(udp); 45 | 46 | bool is_timeout = false; 47 | unsigned long const start = millis(); 48 | do 49 | { 50 | is_timeout = (millis() - start) >= NTP_TIMEOUT_MS; 51 | } while(!is_timeout && !udp.parsePacket()); 52 | 53 | if(is_timeout) { 54 | udp.stop(); 55 | return 0; 56 | } 57 | 58 | uint8_t ntp_packet_buf[NTP_PACKET_SIZE]; 59 | udp.read(ntp_packet_buf, NTP_PACKET_SIZE); 60 | udp.stop(); 61 | 62 | unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]); 63 | unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]); 64 | unsigned long const secsSince1900 = highWord << 16 | lowWord; 65 | 66 | /* Check for corrupted NTP response */ 67 | if(secsSince1900 == 0) { 68 | return 0; 69 | } 70 | 71 | unsigned long const seventyYears = 2208988800UL; 72 | unsigned long const epoch = secsSince1900 - seventyYears; 73 | 74 | return epoch; 75 | } 76 | 77 | /************************************************************************************** 78 | * PRIVATE MEMBER FUNCTIONS 79 | **************************************************************************************/ 80 | 81 | void NTPUtils::sendNTPpacket(UDP & udp) 82 | { 83 | uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0}; 84 | 85 | ntp_packet_buf[0] = 0b11100011; 86 | ntp_packet_buf[1] = 0; 87 | ntp_packet_buf[2] = 6; 88 | ntp_packet_buf[3] = 0xEC; 89 | ntp_packet_buf[12] = 49; 90 | ntp_packet_buf[13] = 0x4E; 91 | ntp_packet_buf[14] = 49; 92 | ntp_packet_buf[15] = 52; 93 | 94 | udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT); 95 | udp.write(ntp_packet_buf, NTP_PACKET_SIZE); 96 | udp.endPacket(); 97 | } 98 | 99 | int NTPUtils::getRandomPort(int const min_port, int const max_port) 100 | { 101 | #if defined (BOARD_HAS_ECCX08) 102 | return ECCX08.random(min_port, max_port); 103 | #elif defined (ARDUINO_ARCH_ESP8266) || (ARDUINO_ARCH_ESP32) 104 | /* Uses HW Random Number Generator */ 105 | return random(min_port, max_port); 106 | #else 107 | randomSeed(analogRead(0)); 108 | return random(min_port, max_port); 109 | #endif 110 | } 111 | 112 | #endif /* #ifndef HAS_LORA */ 113 | -------------------------------------------------------------------------------- /src/utility/time/NTPUtils.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef __NTP_UTILS__ 19 | #define __NTP_UTILS__ 20 | 21 | #include "../../AIoTC_Config.h" 22 | #ifndef HAS_LORA 23 | 24 | /* 25 | This Utility Class is derived from the example code found here https://www.arduino.cc/en/Tutorial/UdpNTPClient 26 | For more information on NTP (Network Time Protocol) you can refer to this Wikipedia article https://en.wikipedia.org/wiki/Network_Time_Protocol 27 | */ 28 | 29 | /************************************************************************************** 30 | * INCLUDE 31 | **************************************************************************************/ 32 | 33 | #include 34 | #include 35 | 36 | /************************************************************************************** 37 | * CLASS DECLARATION 38 | **************************************************************************************/ 39 | 40 | class NTPUtils 41 | { 42 | public: 43 | 44 | static unsigned long getTime(UDP & udp); 45 | static int getRandomPort(int const min_port, int const max_port); 46 | 47 | private: 48 | 49 | static size_t const NTP_PACKET_SIZE = 48; 50 | static int const NTP_TIME_SERVER_PORT = 123; 51 | static int const NTP_LOCAL_PORT = 8888; 52 | #if NTP_USE_RANDOM_PORT 53 | static int const MIN_NTP_PORT = 49152; 54 | static int const MAX_NTP_PORT = 65535; 55 | #endif 56 | static unsigned long const NTP_TIMEOUT_MS = 1000; 57 | static constexpr const char * NTP_TIME_SERVER = "time.arduino.cc"; 58 | 59 | static void sendNTPpacket(UDP & udp); 60 | }; 61 | 62 | #endif /* #ifndef HAS_LORA */ 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/utility/time/RTCMillis.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | /************************************************************************************** 19 | * INCLUDE 20 | **************************************************************************************/ 21 | 22 | #include "AIoTC_Config.h" 23 | 24 | #if defined(HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined (ARDUINO_RASPBERRY_PI_PICO_W) 25 | 26 | #include 27 | #include "RTCMillis.h" 28 | 29 | /************************************************************************************** 30 | * CTOR/DTOR 31 | **************************************************************************************/ 32 | 33 | RTCMillis::RTCMillis() 34 | : _last_rtc_update_tick(0) 35 | , _last_rtc_update_value(0) 36 | { 37 | 38 | } 39 | 40 | /************************************************************************************** 41 | * PUBLIC MEMBER FUNCTIONS 42 | **************************************************************************************/ 43 | 44 | void RTCMillis::begin() 45 | { 46 | 47 | } 48 | 49 | void RTCMillis::set(unsigned long time) 50 | { 51 | _last_rtc_update_tick = millis(); 52 | _last_rtc_update_value = time; 53 | } 54 | 55 | unsigned long RTCMillis::get() 56 | { 57 | unsigned long current_tick = millis(); 58 | unsigned long const elapsed_s = (current_tick - _last_rtc_update_tick) / 1000; 59 | _last_rtc_update_value += elapsed_s; 60 | _last_rtc_update_tick += elapsed_s * 1000; 61 | return _last_rtc_update_value; 62 | } 63 | 64 | #endif /* HAS_NOTECARD || ARDUINO_ARCH_ESP8266 || ARDUINO_RASPBERRY_PI_PICO_W */ 65 | -------------------------------------------------------------------------------- /src/utility/time/RTCMillis.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ 19 | #define ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ 20 | 21 | #if defined(HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined (ARDUINO_RASPBERRY_PI_PICO_W) 22 | 23 | /************************************************************************************** 24 | * INCLUDE 25 | **************************************************************************************/ 26 | 27 | /************************************************************************************** 28 | * CLASS DECLARATION 29 | **************************************************************************************/ 30 | 31 | class RTCMillis 32 | { 33 | 34 | public: 35 | 36 | RTCMillis(); 37 | 38 | void begin(); 39 | void set(unsigned long time); 40 | unsigned long get(); 41 | 42 | private: 43 | unsigned long _last_rtc_update_tick; 44 | unsigned long _last_rtc_update_value; 45 | 46 | }; 47 | 48 | #endif /* HAS_NOTECARD || ARDUINO_ARCH_ESP8266 || ARDUINO_RASPBERRY_PI_PICO_W */ 49 | 50 | #endif /* ARDUINO_IOT_CLOUD_RTC_MILLIS_H_ */ 51 | -------------------------------------------------------------------------------- /src/utility/time/TimeService.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ 19 | #define ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ 20 | 21 | /************************************************************************************** 22 | * INCLUDE 23 | **************************************************************************************/ 24 | 25 | #include 26 | #include 27 | 28 | /****************************************************************************** 29 | * TYPEDEF 30 | ******************************************************************************/ 31 | 32 | typedef unsigned long(*syncTimeFunctionPtr)(void); 33 | 34 | /************************************************************************************** 35 | * CLASS DECLARATION 36 | **************************************************************************************/ 37 | 38 | class TimeServiceClass 39 | { 40 | 41 | public: 42 | 43 | TimeServiceClass(); 44 | 45 | void begin (ConnectionHandler * con_hdl); 46 | unsigned long getTime(); 47 | void setTime(unsigned long time); 48 | unsigned long getLocalTime(); 49 | void setTimeZoneData(long offset, unsigned long valid_until); 50 | bool sync(); 51 | void setSyncInterval(unsigned long seconds); 52 | void setSyncFunction(syncTimeFunctionPtr sync_func); 53 | 54 | /* Helper function to convert an input String into a UNIX timestamp. 55 | * The input String format must be as follow "2021 Nov 01 17:00:00" 56 | */ 57 | static unsigned long getTimeFromString(const String& input); 58 | 59 | static bool isTimeValid(unsigned long const time); 60 | 61 | private: 62 | 63 | ConnectionHandler * _con_hdl; 64 | bool _is_rtc_configured; 65 | bool _is_tz_configured; 66 | long _timezone_offset; 67 | unsigned long _timezone_dst_until; 68 | unsigned long _last_sync_tick; 69 | unsigned long _sync_interval_ms; 70 | syncTimeFunctionPtr _sync_func; 71 | 72 | #if defined(HAS_NOTECARD) || defined(HAS_TCP) 73 | unsigned long getRemoteTime(); 74 | bool connected(); 75 | #endif 76 | void initRTC(); 77 | void setRTC(unsigned long time); 78 | unsigned long getRTC(); 79 | static bool isTimeZoneOffsetValid(long const offset); 80 | 81 | }; 82 | 83 | /****************************************************************************** 84 | * EXTERN DECLARATION 85 | ******************************************************************************/ 86 | 87 | extern TimeServiceClass TimeService; 88 | 89 | #endif /* ARDUINO_IOT_CLOUD_TIME_SERVICE_H_ */ 90 | -------------------------------------------------------------------------------- /src/utility/watchdog/Watchdog.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of ArduinoIoTCloud. 3 | 4 | Copyright 2020 ARDUINO SA (http://www.arduino.cc/) 5 | 6 | This software is released under the GNU General Public License version 3, 7 | which covers the main part of arduino-cli. 8 | The terms of this license can be found at: 9 | https://www.gnu.org/licenses/gpl-3.0.en.html 10 | 11 | You can be released from the requirements of the above licenses by purchasing 12 | a commercial license. Buying such a license is mandatory if you want to modify or 13 | otherwise use the software for commercial activities involving the Arduino 14 | software without disclosing the source code of your own applications. To purchase 15 | a commercial license, send an email to license@arduino.cc. 16 | */ 17 | 18 | #ifndef ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ 19 | #define ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ 20 | 21 | /****************************************************************************** 22 | * INCLUDE 23 | ******************************************************************************/ 24 | 25 | #include 26 | 27 | /****************************************************************************** 28 | * FUNCTION DECLARATION 29 | ******************************************************************************/ 30 | 31 | #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) 32 | void watchdog_enable(); 33 | void watchdog_reset(); 34 | void watchdog_enable_network_feed(NetworkAdapter ni); 35 | #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ 36 | 37 | #endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */ 38 | --------------------------------------------------------------------------------