ci: Switch to GitHub Actions for CI (#28)

This commit is contained in:
Noah Koontz 2020-11-28 21:33:53 -08:00 committed by GitHub
parent f6aa0fd5ad
commit 1f1f81fde5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 550 additions and 112 deletions

281
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,281 @@
name: CI
on:
push:
tags:
- v*
branches-ignore:
- gh-pages
pull_request:
env:
ARDUINO_BOARD_MANAGER_ADDITIONAL_URLS: "https://adafruit.github.io/arduino-board-index/package_adafruit_index.json https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"
jobs:
build-examples-arduino:
name: Arduino ${{ matrix.example }} for ${{ matrix.board.fqbn }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
example:
- EthernetHTTPS
- EthernetMultiHTTPS
- EthernetMQTT
- EthernetAWSIoT
board:
# Arduino Zero
- arduino-platform: arduino:samd
fqbn: arduino:samd:mzero_bl
# Adafruit Feather M0
- arduino-platform: arduino:samd adafruit:samd
fqbn: adafruit:samd:adafruit_feather_m0
# Arduino Due
- arduino-platform: arduino:sam
fqbn: arduino:sam:arduino_due_x
# ESP32
- arduino-platform: esp32:esp32
fqbn: esp32:esp32:d32
include:
# STM32 Nucleo 144
- board:
arduino-platform: STM32:stm32
fqbn: STM32:stm32:Nucleo_144:pnum=NUCLEO_F767ZI
pio-platform: nucleo_f767zi
example: stm32/EthernetHTTPSstm32
steps:
# Setup pyserial for esptool.py
- name: Setup Python
if: matrix.board.arduino-platform == 'esp32:esp32'
uses: actions/setup-python@v2
with:
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
- name: Install Pyserial
if: matrix.board.arduino-platform == 'esp32:esp32'
run: |
python -m pip install --upgrade pip
pip install pyserial
# Setup Arduino-CLI
- name: Install Arduino CLI
uses: arduino/setup-arduino-cli@v1.1.1
# Install Dependencies
- name: Install Core(s)
run: arduino-cli core install ${{ matrix.board.arduino-platform }} -v
- name: Install EthernetLarge
run: git clone https://github.com/OPEnSLab-OSU/EthernetLarge.git ~/Arduino/libraries/EthernetLarge
- name: Install Other Libraries
run: arduino-cli lib install "STM32duino STM32Ethernet" PubSubClient -v
# Checkout
- name: Checkout
uses: actions/checkout@v2
with:
path: SSLClient
# Compile
- name: Compile Sketch
run: arduino-cli compile -v --libraries . --warnings all --fqbn ${{ matrix.board.fqbn }} SSLClient/examples/${{ matrix.example }}
build-examples-platformio:
name: PIO ${{ matrix.example }} for ${{ matrix.board.pio-platform }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# Copy of the above matrix (no anchors :( )
example:
- EthernetHTTPS
- EthernetMultiHTTPS
- EthernetMQTT
- EthernetAWSIoT
board:
# Arduino Zero
- pio-platform: zeroUSB
# Adafruit Feather M0
- pio-platform: adafruit_feather_m0
# Arduino Due
- pio-platform: dueUSB
# ESP32
- pio-platform: lolin32
# Teensy 4.0
- pio-platform: teensy40
# Teensy 3.6
- pio-platform: teensy36
# Teensy 3.5
- pio-platform: teensy35
# Teensy 3.1/3.2
- pio-platform: teensy31
# Teensy 3.0
- pio-platform: teensy30
include:
# STM32 Nucleo 144
- board:
pio-platform: nucleo_f767zi
example: stm32/EthernetHTTPSstm32
# TIVA-C
- board:
pio-platform: lptm4c1294ncpdt
extra-flags: -O "lib_deps=SPI"
example: tivac/EthernetHTTPStivac
steps:
# Setup python for platformio
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
# Setup PlatformIO
- name: Install Python Dependencies
run: |
python -m pip install --upgrade pip
pip install pyserial platformio
# Install Dependencies
- name: Install Libraries
run: |
pio lib -g install "stm32duino/STM32duino LwIP"
pio lib -g install stm32duino/STM32Ethernet
pio lib -g install PubSubClient
pio lib -g install https://github.com/OPEnSLab-OSU/EthernetLarge.git
# Checkout
- name: Checkout
uses: actions/checkout@v2
# Compile
- name: Compile Sketch
run: pio ci -l . -b ${{ matrix.board.pio-platform }} ${{ matrix.board.extra-flags }} ${{ github.workspace }}/examples/${{ matrix.example }}
compile-archives:
name: Compile Archives
runs-on: ubuntu-latest
needs: [build-examples-arduino, build-examples-platformio]
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
strategy:
fail-fast: true
matrix:
include:
# Adafruit Feather M0
- board:
arduino-platform: arduino:samd adafruit:samd
fqbn: adafruit:samd:adafruit_feather_m0
arch: cortex-m0plus
example: EthernetHTTPS
# Arduino Due
- board:
arduino-platform: arduino:sam
fqbn: arduino:sam:arduino_due_x
arch: cortex-m3
example: EthernetHTTPS
# ESP32
- board:
arduino-platform: esp32:esp32
fqbn: esp32:esp32:d32
arch: esp32
example: EthernetHTTPS
# STM32
- board:
arduino-platform: STM32:stm32
fqbn: STM32:stm32:Nucleo_144:pnum=NUCLEO_F767ZI
arch: cortex-m7
example: stm32/EthernetHTTPSstm32
steps:
# Setup pyserial for esptool.py
- name: Setup Python
if: matrix.board.arduino-platform == 'esp32:esp32'
uses: actions/setup-python@v2
with:
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax
- name: Install Pyserial
if: matrix.board.arduino-platform == 'esp32:esp32'
run: |
python -m pip install --upgrade pip
pip install pyserial
# Setup Arduino-CLI
- name: Install Arduino CLI
uses: arduino/setup-arduino-cli@v1.1.1
# Install Dependencies
- name: Install Core(s)
run: arduino-cli core install ${{ matrix.board.arduino-platform }} -v
- name: Install EthernetLarge
run: git clone https://github.com/OPEnSLab-OSU/EthernetLarge.git ~/Arduino/libraries/EthernetLarge
- name: Install Other Libraries
run: arduino-cli lib install "STM32duino STM32Ethernet" PubSubClient -v
# Checkout
- name: Checkout
uses: actions/checkout@v2
with:
path: SSLClient
# Compile with dot-a-linkage
- name: Compile with Archive
run: |
echo "dot_a_linkage=true" >> SSLClient/library.properties
arduino-cli compile -v --build-path ${{ github.workspace }}/build --libraries . --warnings all --fqbn ${{ matrix.board.fqbn }} SSLClient/examples/${{ matrix.example }}
# Upload as an artifact
- name: Emit Compiled SSLClient
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.board.arch }}
path: build/libraries/SSLClient/SSLClient.a
generate-release:
name: Generate Release
runs-on: ubuntu-latest
needs: compile-archives
steps:
# Checkout
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
# Build the release changelog
- name: Build Changelog
id: build_changelog
uses: heineiuo/create-changelogs@master
# Create a release
- name: Publish Release
id: publish_release
uses: actions/create-release@v1
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: ${{ steps.build_changelog.outputs.changelogs }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Create SSLClient-precompiled
- name: Convert Library to Precompiled Format
run: |
echo "precompiled=true" >> library.properties
rm -rf .git
find src/ -iname "*.c" -delete
find src/ -iname "*.cpp" -delete
# Download all the artifacts
- name: Add Precompiled Artifacts
uses: actions/download-artifact@v2
with:
path: src
# Zip the result
- name: Generate SSLClient-precompiled
run: zip -r SSLClient-precompiled.zip .
# Upload SSLClient-precompiled.zip to the release created
- name: Upload SSLClient-precompiled
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.publish_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ./SSLClient-precompiled.zip
asset_name: SSLClient-precompiled.zip
asset_content_type: application/zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

26
.github/workflows/docs.yml vendored Normal file
View file

@ -0,0 +1,26 @@
name: Documentation
on:
push:
branches:
- master
jobs:
documentation:
name: Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false
- name: Build Documentation
uses: mattnotmitt/doxygen-action@v1
- name: Deploy to GitHub Pages
uses: JamesIves/github-pages-deploy-action@3.7.1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages # The branch the action should deploy to.
FOLDER: docs # The folder the action should deploy.
CLEAN: true # Automatically remove deleted files from the deploy branch

View file

@ -1,112 +0,0 @@
language: c
env:
global:
# You can uncomment this to explicitly choose an (old) version of the Arduino IDE
#- ARDUINO_IDE_VERSION="1.8.7"
- ADDITIONAL_URLS="https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json,https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json"
- DOXYFILE=$TRAVIS_BUILD_DIR/.travis/Doxyfile
cache:
directories:
- ~/arduino_ide
- ~/.arduino15/packages/
# Blacklist
branches:
except:
- gh-pages
# Install dependencies
addons:
apt:
packages:
- doxygen
jobs:
include:
- stage: "Build"
name: "Feather M0"
env: CORE="adafruit:samd" BOARD="adafruit:samd:adafruit_feather_m0"
- name: "Arduino Zero"
env: CORE="arduino:samd" BOARD="arduino:samd:mzero_bl"
- name: "Arduino Due"
env: CORE="arduino:sam" BOARD="arduino:sam:arduino_due_x"
- name: "ESP32"
env: CORE="esp32:esp32" BOARD="esp32:esp32:d32"
- name: "STM32"
env: CORE="STM32:stm32" BOARD="STM32:stm32:Nucleo_144:pnum=NUCLEO_F767ZI"
script:
- arduino-cli compile --warnings all --fqbn $BOARD $PWD/examples/stm32/EthernetHTTPSstm32
- stage: "Documentation"
if: branch = master
install: skip
script:
- doxygen $DOXYFILE
deploy:
provider: pages
skip_cleanup: true
local_dir: docs
github_token: $GITHUB_TOKEN
- stage: "Deploy"
if: tag is present
env: CORE="arduino:samd arduino:sam esp32:esp32 STM32:stm32"
script: skip
before_deploy:
- mkdir tmp-bin
- cp library.properties library.properties.old
- echo "dot_a_linkage=true" >> library.properties
- rm -rf /tmp/arduino-sketch*
# cortex-m0plus
- arduino-cli compile --fqbn arduino:samd:mzero_bl $PWD/examples/EthernetHTTPS
- mkdir tmp-bin/cortex-m0plus
- cp "$(find /tmp/ -maxdepth 1 -type d -name "arduino-sketch*" -print | head -n 1)/libraries/SSLClient/SSLClient.a" tmp-bin/cortex-m0plus/SSLClient.a
- rm -rf /tmp/arduino-sketch*
# cortex-m3
- arduino-cli compile --fqbn arduino:sam:arduino_due_x $PWD/examples/EthernetHTTPS
- mkdir tmp-bin/cortex-m3
- cp "$(find /tmp/ -maxdepth 1 -type d -name "arduino-sketch*" -print | head -n 1)/libraries/SSLClient/SSLClient.a" tmp-bin/cortex-m3/SSLClient.a
- rm -rf /tmp/arduino-sketch*
# cortex-m7
- arduino-cli compile --fqbn STM32:stm32:Nucleo_144:pnum=NUCLEO_F767ZI $PWD/examples/stm32/EthernetHTTPSstm32
- mkdir tmp-bin/cortex-m7
- cp "$(find /tmp/ -maxdepth 1 -type d -name "arduino-sketch*" -print | head -n 1)/libraries/SSLClient/SSLClient.a" tmp-bin/cortex-m7/SSLClient.a
- rm -rf /tmp/arduino-sketch*
# esp32
- arduino-cli compile --fqbn esp32:esp32:d32 $PWD/examples/EthernetHTTPS
- mkdir tmp-bin/esp32
- cp "$(find /tmp/ -maxdepth 1 -type d -name "arduino-sketch*" -print | head -n 1)/libraries/SSLClient/SSLClient.a" tmp-bin/esp32/SSLClient.a
# bundle it up!
- mv library.properties.old library.properties
- echo "precompiled=true" >> library.properties
- mv tmp-bin/* src/
- rm -rf tmp-bin
- rm -rf .git
- find src/ -iname "*.c" -delete
- find src/ -iname "*.cpp" -delete
- zip -r SSLClient-precompiled.zip .
deploy:
provider: releases
api_key: $GITHUB_TOKEN
skip_cleanup: true
file: "SSLClient-precompiled.zip"
on:
tags: true
install:
- curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/usr/bin sudo sh
- arduino-cli core update-index --additional-urls $ADDITIONAL_URLS
- arduino-cli core install arduino:samd -v
- arduino-cli core install $CORE -v --additional-urls $ADDITIONAL_URLS
- mkdir -p $HOME/Arduino/libraries
- rm -rf $HOME/Arduino/libraries/EthernetLarge
- git clone https://github.com/OPEnSLab-OSU/EthernetLarge.git $HOME/Arduino/libraries/EthernetLarge
- arduino-cli lib install "STM32duino STM32Ethernet"
- arduino-cli lib install "PubSubClient"
- ln -s $PWD $HOME/Arduino/libraries/.
script:
- arduino-cli compile --warnings all --fqbn $BOARD $PWD/examples/EthernetHTTPS
- arduino-cli compile --warnings all --fqbn $BOARD $PWD/examples/EthernetMultiHTTPS
- arduino-cli compile --warnings all --fqbn $BOARD $PWD/examples/EthernetMQTT

View file

@ -0,0 +1,74 @@
#ifndef _CERTIFICATES_H_
#define _CERTIFICATES_H_
#ifdef __cplusplus
extern "C"
{
#endif
/* This file is auto-generated by the pycert_bearssl tool. Do not change it manually.
* Certificates are BearSSL br_x509_trust_anchor format. Included certs:
*
* Index: 0
* Label: Amazon Root CA 1
* Subject: CN=Amazon Root CA 1,O=Amazon,C=US
*/
#define TAs_NUM 1
static const unsigned char TA_DN0[] = {
0x30, 0x39, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
0x02, 0x55, 0x53, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x0a,
0x13, 0x06, 0x41, 0x6d, 0x61, 0x7a, 0x6f, 0x6e, 0x31, 0x19, 0x30, 0x17,
0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x10, 0x41, 0x6d, 0x61, 0x7a, 0x6f,
0x6e, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x20, 0x31,
};
static const unsigned char TA_RSA_N0[] = {
0xb2, 0x78, 0x80, 0x71, 0xca, 0x78, 0xd5, 0xe3, 0x71, 0xaf, 0x47, 0x80,
0x50, 0x74, 0x7d, 0x6e, 0xd8, 0xd7, 0x88, 0x76, 0xf4, 0x99, 0x68, 0xf7,
0x58, 0x21, 0x60, 0xf9, 0x74, 0x84, 0x01, 0x2f, 0xac, 0x02, 0x2d, 0x86,
0xd3, 0xa0, 0x43, 0x7a, 0x4e, 0xb2, 0xa4, 0xd0, 0x36, 0xba, 0x01, 0xbe,
0x8d, 0xdb, 0x48, 0xc8, 0x07, 0x17, 0x36, 0x4c, 0xf4, 0xee, 0x88, 0x23,
0xc7, 0x3e, 0xeb, 0x37, 0xf5, 0xb5, 0x19, 0xf8, 0x49, 0x68, 0xb0, 0xde,
0xd7, 0xb9, 0x76, 0x38, 0x1d, 0x61, 0x9e, 0xa4, 0xfe, 0x82, 0x36, 0xa5,
0xe5, 0x4a, 0x56, 0xe4, 0x45, 0xe1, 0xf9, 0xfd, 0xb4, 0x16, 0xfa, 0x74,
0xda, 0x9c, 0x9b, 0x35, 0x39, 0x2f, 0xfa, 0xb0, 0x20, 0x50, 0x06, 0x6c,
0x7a, 0xd0, 0x80, 0xb2, 0xa6, 0xf9, 0xaf, 0xec, 0x47, 0x19, 0x8f, 0x50,
0x38, 0x07, 0xdc, 0xa2, 0x87, 0x39, 0x58, 0xf8, 0xba, 0xd5, 0xa9, 0xf9,
0x48, 0x67, 0x30, 0x96, 0xee, 0x94, 0x78, 0x5e, 0x6f, 0x89, 0xa3, 0x51,
0xc0, 0x30, 0x86, 0x66, 0xa1, 0x45, 0x66, 0xba, 0x54, 0xeb, 0xa3, 0xc3,
0x91, 0xf9, 0x48, 0xdc, 0xff, 0xd1, 0xe8, 0x30, 0x2d, 0x7d, 0x2d, 0x74,
0x70, 0x35, 0xd7, 0x88, 0x24, 0xf7, 0x9e, 0xc4, 0x59, 0x6e, 0xbb, 0x73,
0x87, 0x17, 0xf2, 0x32, 0x46, 0x28, 0xb8, 0x43, 0xfa, 0xb7, 0x1d, 0xaa,
0xca, 0xb4, 0xf2, 0x9f, 0x24, 0x0e, 0x2d, 0x4b, 0xf7, 0x71, 0x5c, 0x5e,
0x69, 0xff, 0xea, 0x95, 0x02, 0xcb, 0x38, 0x8a, 0xae, 0x50, 0x38, 0x6f,
0xdb, 0xfb, 0x2d, 0x62, 0x1b, 0xc5, 0xc7, 0x1e, 0x54, 0xe1, 0x77, 0xe0,
0x67, 0xc8, 0x0f, 0x9c, 0x87, 0x23, 0xd6, 0x3f, 0x40, 0x20, 0x7f, 0x20,
0x80, 0xc4, 0x80, 0x4c, 0x3e, 0x3b, 0x24, 0x26, 0x8e, 0x04, 0xae, 0x6c,
0x9a, 0xc8, 0xaa, 0x0d,
};
static const unsigned char TA_RSA_E0[] = {
0x01, 0x00, 0x01,
};
static const br_x509_trust_anchor TAs[] = {
{
{ (unsigned char *)TA_DN0, sizeof TA_DN0 },
BR_X509_TA_CA,
{
BR_KEYTYPE_RSA,
{ .rsa = {
(unsigned char *)TA_RSA_N0, sizeof TA_RSA_N0,
(unsigned char *)TA_RSA_E0, sizeof TA_RSA_E0,
} }
}
},
};
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* ifndef _CERTIFICATES_H_ */

View file

@ -0,0 +1,169 @@
/*
Connect to AWS IOT using SSLClient and Wiz850io Ethernet Mdoule
AWS_Root_CA.h is the trust anchor created using the Root CA from:
https://www.amazontrust.com/repository/AmazonRootCA1.pem
You can re-create it again using the python file present
in SSLClient/tools/pycert_bearssl/pycert_bearssl.py
python pycert_bearssl.py convert --no-search <certificate PEM file>
refer: https://github.com/OPEnSLab-OSU/SSLClient/issues/17#issuecomment-700143405
Circuit:
Ethernet shield WIZ850io:
CS 10
MOSI 11
MISO 12
SCK 13
created 10 October 2020
by Ram Rohit Gannavarapu
*/
#include <SPI.h>
#include <EthernetLarge.h>
#include <SSLClient.h>
#include <PubSubClient.h>
#include "AWS_Root_CA.h" // This file is created using AmazonRootCA1.pem from https://www.amazontrust.com/repository/AmazonRootCA1.pem
#define THING_NAME "<Thing_Name>"
#define MQTT_PACKET_SIZE 1024
void MQTTPublish(const char *topic, char *payload);
void updateThing();
const char my_cert[] = \
"-----BEGIN CERTIFICATE-----\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"-----END CERTIFICATE-----\n";
const char my_key[] = \
"-----BEGIN RSA PRIVATE KEY-----\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" \
"-----END RSA PRIVATE KEY-----\n";
SSLClientParameters mTLS = SSLClientParameters::fromPEM(my_cert, sizeof my_cert, my_key, sizeof my_key);
const char* mqttServer = "xxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com";
const char publishShadowUpdate[] = "$aws/things/" THING_NAME "/shadow/update";
char publishPayload[MQTT_PACKET_SIZE];
char *subscribeTopic[5] =
{
"$aws/things/" THING_NAME "/shadow/update/accepted",
"$aws/things/" THING_NAME "/shadow/update/rejected",
"$aws/things/" THING_NAME "/shadow/update/delta",
"$aws/things/" THING_NAME "/shadow/get/accepted",
"$aws/things/" THING_NAME "/shadow/get/rejected"
};
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
SSLClient ethClientSSL(ethClient, TAs, (size_t)TAs_NUM, A5);
PubSubClient mqtt(mqttServer, 8883, callback, ethClientSSL);
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
void reconnect()
{
while (!mqtt.connected())
{
Serial.print("Attempting MQTT connection...");
if (mqtt.connect("arduinoClient"))
{
Serial.println("connected");
for (int i = 0; i < 5; i++)
{
// Serial.println(subscribeTopic[i]);
mqtt.subscribe(subscribeTopic[i]);
ethClientSSL.flush();
}
Serial.println("Started updateThing ");
updateThing();
Serial.println("Done updateThing ");
}
else
{
Serial.print("failed, rc=");
Serial.print(mqtt.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
ethClientSSL.setMutualAuthParams(mTLS);
mqtt.setBufferSize(MQTT_PACKET_SIZE);
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// no point in carrying on, so do nothing forevermore:
while (true) {
delay(1);
}
}
// print your local IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
if (!mqtt.connected())
{
reconnect();
}
mqtt.loop();
}
void updateThing()
{
strcpy(publishPayload, "{\"state\": {\"reported\": {\"powerState\":\"ON\"}}}");
MQTTPublish(publishShadowUpdate, publishPayload);
}
void MQTTPublish(const char *topic, char *payload)
{
mqtt.publish(topic, payload);
ethClientSSL.flush();
Serial.print("Published [");
Serial.print(topic);
Serial.print("] ");
Serial.println(payload);
}