Hello, my name is Elisie, and today I'll be sharing my swift is the next-spicting
priority.
So in 2016, I got my degree in electrical engineering, and I started working full-time
in hardware design and embedded systems.
While this type of work was really rewarding, I was super interested in learning modern
software development, specifically iOS and Internet of Things.
So Internet of Things is taking everyday objects, connecting them to the Internet, and making
them smarter and more interactive.
Take a simple example, a smart-white that integrates seamlessly with the home app and can
turn on with a motion sensor.
So as I was working full-time as an electrical engineer, my days were spent coding and
see for embedded systems, but at night I was coding and swift, building my first iOS
app.
At the time, these two worlds, app development, and embedded systems felt miles apart.
I remember when I was coding and seeing, and I would worry that a single type of could
crash my program, or even worse, damage my hardware.
That constant learning made swips modern features like type safety, automatic memory management,
and interoperability with C, even more exciting.
Hensing out its potential to bring my two worlds together.
Traditionally, developers have had to rely on languages like COC++.
These precise languages give you full control of the hardware, but that precision comes
with challenges like manual memory management, error prone code, and a steep learning curve.
Now if we fast-load to 2024, embedding swip was announced, and suddenly my two worlds did
just overlap, being intersected.
Embedded swip is a subset of swip, not a variant or a dialect.
So all the code that I write in embedded swip also works in full swip.
Embedded swip brings modern, safe, and scalable code to hardware development.
And today, I'm going to show you how I used it to create a smart light with motion detection.
I started with the matter example in Swift.
This project was featured in the video Go Small within that is Swift.
Today, I'm only going to go over the additions that I made for my motion sensor, but if you're
interested in the rest of the implementation, go ahead and check out the repo.
So my motion sensor detects motion, and it sends a high signal to my ESP 3026.
Then this signal will update the state of the LED to either be on or off, and the change
of flags in the home app.
In this project, I'm using embedded swip, an ESP 3026 board, and motion sensor, and the
matter protocol.
Here's a quick close-up of the circuit for my motion sensor.
I have my smart light with me here today.
I have the ESP code put up, and my program's already running, and you can see the logs in
the terminal here.
So I'm going to do a quick walkthrough of my code.
So the first thing I did was add the driver for my GPIO pin, my inner app headers, and
a matter C++ call that I will need when I'm updating the state of the LED, so it will
sync with the home app.
All of this is in the bridging header in which you enable an operator ability with these
C and C++ libraries.
Now we can move on to the core functionality of my sensor, and motion sensor.swip, starting
in the initializer, I've added two functions, set up GPIO, and set up task.
Set up GPIO, configure my GPIO pin to interface with my sensor, ensuring that it can receive
the input signals when motion is detected.
The second function, set up task establishes a thread safe inner app handler for my GPIO
pin.
So when my inner app fires, my LED will update, ensuring that my sensor responds in real
time.
One thing that I really appreciate about Swift is it's structure to push to air handling.
So for example, if my GPIO pin doesn't configure correctly, or my inner app fails, it will
follow a very specific error, and here's why I set that up for my pin.
I created a custom extension in Swift that defines all of the specific cases that I could
possibly fail in my functions.
So these errors can be handled with Swift's du-catch or a flat blocks.
So scrolling back up to my initializer, if either of my functions have an error, I gracefully
catch the error and restart my board.
This adds to the reliability of my program because I'm effectively handling issues at one
time.
The next thing I need to do is make sure that the LED state is synced with the home app.
So let's go to matter.swift, where I've extended the pre-existing functionality to add
this function, report light state.
So in the state of the LED changes, I use this matter called to make sure that the home
app can stay in sync with the LED.
Wrapping anything up in main.swift, I initialize my motion sensor on GPIO pin 5.
I chose pin 5 because it was one of the available pins that could configure an interrupt.
I decided to use a closure in Swift because it makes my motion detection logic easy to read.
So in my inner of fires, I've set up a callback that will execute the code in this closure.
I have a variable called LED.enabled.
This was already in the implementation, but what it does is turn the LED on and off.
So that's the first thing that I do in my motion sensor detects motion is I toggle the state
of that variable.
And then to ensure the home app stays in sync, I use my report light state function to update
the state of the LED to the current state.
Lastly I built and flashed the program to my board and I have it running in my terminal.
You can see the logs every time the LED state changes.
And that is Swift has changed the way that I do IoT development.
The reduces risk of memory leaks with automatic memory management.
A voice airplane code with built-in safety features and it's easier for me to read.
It's small memory footprint makes it ideal for IoT devices.
But Swift's potential doesn't stop there.
It connects all parts of IoT, embedded, server, and application.
This simplifies development, removes barrier of entry for start-ups, and enables
the creation of efficient, scalable, and user-friendly products.
And that's why Swift is the next big thing for IoT.
Thank you.