PIC and AVR IoT Transceiving Data With AWS User Guide
PIC and AVR IoT Transceiving Data With AWS User Guide
PIC and AVR IoT Transceiving Data With AWS User Guide
Introduction
Author: Lars Olav Skrebergene, Microchip Technology Inc.
Important: This user guide is a part of a series of tutorials originally published in the Microchip IoT
Developer Guides repository on GitHub. The repository has more tutorials and information about using a
PIC® or AVR® IoT Board with AWS.
In this tutorial, it will be showcased how the AVR-IoT and PIC-IoT Development Boards can be configured to
communicate with the cloud using Amazon Web Services® (AWS) and the MQTT messaging protocol.
It will be demonstrated how a an example application can be developed where a network of IoT devices are
configured to blink their LEDs whenever a button is pressed on any of them. Detailed step-by-step instructions will be
provided and relevant concepts will also be covered when needed.
The primary goal of this tutorial is for the reader to experience how to develop their own applications with the AVR-
IoT and PIC-IoT Development Boards.
Prerequisites
It is assumed that the reader has already provisioned their PIC-IoT and/or AVR-IoT Development Board(s) to
communicate with their own AWS account, as described in the previous tutorial: Connect the Board to your AWS
Account.
Before starting this tutorial, make sure that IoT device(s) are successfully sending sensor data to AWS IoT Core. It is
also assumed that the reader has installed the MPLAB® X IDE and the XC8 (AVR-IoT) or XC16 (PIC-IoT) compiler.
Links to software and other useful tools and guides are provided in the Resources section at the end of this tutorial.
Table of Contents
Introduction.....................................................................................................................................................1
3. Resources............................................................................................................................................... 8
4. Revision History...................................................................................................................................... 9
Customer Support........................................................................................................................................ 10
Legal Notice..................................................................................................................................................11
Trademarks...................................................................................................................................................11
SW0_EnableInterruptForFallingEdge();
PORTF_SW0_SetInterruptHandler(sendButtonPressToCloud);
The first line enables falling edge interrupt detection for the SW0 button on the AVR-IoT boards, and a function
handler for this interrupt is then assigned on the second line. The interested reader is encouraged to take a look at
the pin_manager.c file to see how these functions are implemented.
Skip ahead to Procedures for both AVR-IoT and PIC-IoT boards to complete this step.
1. In the PIN_MANAGER_initialize function, enable interrupts for the SW0 button (which is connected to RA7)
and clear its interrupt flag by including these two lines:
2. Add another variable below the INT_InterruptHandler variable to store the interrupt handler for the SW0
hardware button:
void (*SW0_InterruptHandler)(void) = NULL;
3. Add a function that sets the variable we just created (place it just after the INT_SetInterruptHandler
function):
void SW0_SetInterruptHandler(void (* InterruptHandler)(void))
{
IEC1bits.IOCIE = 0; //Disable IOCI interrupt
SW0_InterruptHandler = InterruptHandler;
IEC1bits.IOCIE = 1; //Enable IOCI interrupt
}
4. Modify the _IOCInterrupt interrupt service routine to also handle the SW0 button presses (the interrupt
service routine is located near line 155 in pin_manager.c). The SW0 button is connected to the RA7 pin. The
fully modified interrupt service routine is provided below. Either copy and replace _IOCInterrupt in its
entirety or add the second nested if statement to your project.
void __attribute__ (( interrupt, no_auto_psv )) _IOCInterrupt ( void )
{
if(IFS1bits.IOCIF == 1)
{
// Clear the flag
IFS1bits.IOCIF = 0;
if(IOCFAbits.IOCFA12 == 1)
{
IOCFAbits.IOCFA12 = 0; //Clear flag for Pin - RA12
if(INT_InterruptHandler)
{
INT_InterruptHandler();
}
}
In pin_manager.h, add a declaration of the SW0_SetInterruptHandler function that was just added to make it
available in other files, for example after the declaration of the INT_SetInterruptHandler function:
In application_manager.c, set the SW0 interrupt handler just after the call to the SYSTEM_Initialize() in the
application_init function:
char tutorialMqttSubscribeTopic[SUBSCRIBE_TOPIC_SIZE];
Implement the aforementioned function handler by adding the following code to application_manager.c:
// Construct payload
tutorialLen = sprintf(tutorialPayload,"{\"thing_name\":\"%s\"}", cid);
This function closely resembles the sendToCloud function that was mentioned earlier and will publish an MQTT
message to the buttonPresses topic. The content of the message will be a JSON object that contains the name of
the thing/device that sent the message.
Build the modified project and program it onto the device using MPLAB X. This is done by clicking on the Make and
Program Device Main Project button on the MPLAB X toolbar (see the image below).
Figure 2-2. How to make and program device in MPLAB X
If you are unfamiliar with the MPLAB X integrated developer environment (IDE), check out the following guide: Get
Started with MPLAB® X IDE and Microchip Tools
#defineNUM_TOPICS_SUBSCRIBE2
staticvoidsubscribeToCloud(void)
{
sprintf(mqttSubscribeTopic, "$aws/things/%s/shadow/update/delta", cid);
CLOUD_registerSubscription((uint8_t*)mqttSubscribeTopic,receivedFromCloud);
sprintf(tutorialMqttSubscribeTopic, "buttonPresses");
CLOUD_registerSubscription((uint8_t*)tutorialMqttSubscribeTopic,receiveButtonPressFromClo
ud);
}
– The second parameter of the CLOUD_registerSubscription function is a handler that dictates which
function will be run when a message is received to the specified topic. The
receiveButtonPressFromCloud function will therefore have to be implemented to handle any
received messages.
3. Add the following function definition to application_manager.c (somewhere above the
subscribeToCloud function) to make the device's LEDs blink twice when a message is received:
4. Build the project and program the device in MPLAB X. If multiple AVR-IoT or PIC-IoT devices are available, try
programming several of them using the same project.
Remember that all of the devices first will have to be provisioned for use with an AWS account. Note also that
even though AVR-IoT and PIC-IoT devices can be connected to AWS simultaneously and communicate with
each other over MQTT, the GitHub project used in this tutorial is only compatible with either AVR-IoT devices
or PIC-IoT devices. To use devices from two different device families together, it is necessary to complete this
tutorial individually for the AVR-IoT and PIC-IoT repositories on GitHub and program the devices with the
compatible firmware.
The device(s) should now be configured correctly. If the SW0 button is pressed on any of the configured IoT kits, the
LEDs on all configured IoT kits should blink twice. If this is not the case, make sure that the tutorial has been followed
correctly and that the devices are properly conditioned.
3. Resources
• PIC-IoT WA Development Board Product Page
• AVR-IoT WA Development Board Product Page
• Get Started with MPLAB® X IDE and Microchip Tools
• AWS IoT Developer Guide
• IoT Provisioning Tool
• GitHub repositories with the IoT Boards' pre-loaded firmware:
– For the PIC-IoT Development Boards
– For the AVR-IoT Development Boards
4. Revision History
Revision Date Description
A 08/2020 Initial document release
Microchip provides online support via our website at www.microchip.com/. This website is used to make files and
information easily available to customers. Some of the content available includes:
• Product Support – Data sheets and errata, application notes and sample programs, design resources, user’s
guides and hardware support documents, latest software releases and archived software
• General Technical Support – Frequently Asked Questions (FAQs), technical support requests, online
discussion groups, Microchip design partner program member listing
• Business of Microchip – Product selector and ordering guides, latest Microchip press releases, listing of
seminars and events, listings of Microchip sales offices, distributors and factory representatives
Microchip’s product change notification service helps keep customers current on Microchip products. Subscribers will
receive email notification whenever there are changes, updates, revisions or errata related to a specified product
family or development tool of interest.
To register, go to www.microchip.com/pcn and follow the registration instructions.
Customer Support
Note the following details of the code protection feature on Microchip devices:
• Microchip products meet the specifications contained in their particular Microchip Data Sheet.
• Microchip believes that its family of products is secure when used in the intended manner and under normal
conditions.
• There are dishonest and possibly illegal methods being used in attempts to breach the code protection features
of the Microchip devices. We believe that these methods require using the Microchip products in a manner
outside the operating specifications contained in Microchip’s Data Sheets. Attempts to breach these code
protection features, most likely, cannot be accomplished without violating Microchip’s intellectual property rights.
• Microchip is willing to work with any customer who is concerned about the integrity of its code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of its code. Code
protection does not mean that we are guaranteeing the product is “unbreakable.” Code protection is constantly
evolving. We at Microchip are committed to continuously improving the code protection features of our products.
Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act.
If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue
for relief under that Act.
Legal Notice
Information contained in this publication is provided for the sole purpose of designing with and using Microchip
products. Information regarding device applications and the like is provided only for your convenience and may be
superseded by updates. It is your responsibility to ensure that your application meets with your specifications.
THIS INFORMATION IS PROVIDED BY MICROCHIP “AS IS”. MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR
OTHERWISE, RELATED TO THE INFORMATION INCLUDING BUT NOT LIMITED TO ANY IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
OR WARRANTIES RELATED TO ITS CONDITION, QUALITY, OR PERFORMANCE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR
CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
INFORMATION OR ITS USE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW,
MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THE INFORMATION OR ITS USE
WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR
THE INFORMATION. Use of Microchip devices in life support and/or safety applications is entirely at the buyer’s risk,
and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or
expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual
property rights unless otherwise stated.
Trademarks
The Microchip name and logo, the Microchip logo, Adaptec, AnyRate, AVR, AVR logo, AVR Freaks, BesTime,
BitCloud, chipKIT, chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, HELDO, IGLOO, JukeBlox,
KeeLoq, Kleer, LANCheck, LinkMD, maXStylus, maXTouch, MediaLB, megaAVR, Microsemi, Microsemi logo, MOST,
MOST logo, MPLAB, OptoLyzer, PackeTime, PIC, picoPower, PICSTART, PIC32 logo, PolarFire, Prochip Designer,
QTouch, SAM-BA, SenGenuity, SpyNIC, SST, SST Logo, SuperFlash, Symmetricom, SyncServer, Tachyon,
TempTrackr, TimeSource, tinyAVR, UNI/O, Vectron, and XMEGA are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
APT, ClockWorks, The Embedded Control Solutions Company, EtherSynch, FlashTec, Hyper Speed Control,
HyperLight Load, IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision Edge, ProASIC, ProASIC Plus,
ProASIC Plus logo, Quiet-Wire, SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub, TimePictra, TimeProvider,
Vite, WinPath, and ZL are registered trademarks of Microchip Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BlueSky, BodyCom,
CodeGuard, CryptoAuthentication, CryptoAutomotive, CryptoCompanion, CryptoController, dsPICDEM,
dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP,
INICnet, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, memBrain, Mindi, MiWi, MPASM, MPF,
MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation, PICDEM,
PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE, Ripple Blocker, SAM-ICE, Serial Quad
I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense,
ViewSpan, WiperLock, Wireless DNA, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A.
and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
The Adaptec logo, Frequency on Demand, Silicon Storage Technology, and Symmcom are registered trademarks of
Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip
Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2020, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-6655-0