welcome back as you know I am Eli the
computer guy and in today's class we are
going to be creating an
iot messaging device using a Raspberry
Pi and a blues iot modem well you might
be sitting there going wow that's a lot
of words that I don't understand I
wonder what the hell Eli the computer
guy is talking about well for the past
few classes I've been creating projects
with the blues iot Moto so basically
what this is is this is a modem that
connects to the LTE network so
essentially the cell network and you're
able to send and receive text very
specifically formatted in Json so
anyways we did a couple of classes where
I showed you how to use this with their
dev kit which is basically an Arduino
code a clone uh in order to do things
such as send uh GPS locations back so
you know uh where the item is I think
think about using that for something
like field technicians so that you can
track field technicians when they're
going about their jobs uh and then I
also showed you uh how you could send uh
commands to that iot modem uh to turn on
a power relay so basically we uh we used
a system so that we were able to turn a
light on or off and that could simulate
uh any number of things that you could
turn off uh turn on or off essentially
with a switch uh so think of air
conditioning units dehumidifiers water
pumps any of that type of stuff so we
were doing that with arduinos the thing
is with arduinos they are
microcontrollers they're not computers
and basically you give them commands and
they just loop loop loop loopop loop
loop with very basic commands and so for
the the following projects basically
what I decided to do is connect our
Blues iot modem to a Raspberry Pi so we
get the full power of a computer a
Raspberry Pi is an arm-based computer
I'm using a Rasberry Pi 4 for this
particular project uh and it's a quad
core processor with four gigs of RAM a
full Wi-Fi stack and all that kind of
thing you actually install VSS code onto
it uh and you get all of the Power of
python so instead of dealing with the uh
the Arduino IDE or the platform IO
system you're actually able to do with
deal with python which is absolutely
lovely so anyways what I have done here
is I've created a system where on my
main computer I am able to send a
message through an HTML for that goes up
to Blue's API a plat form and then this
device will then download that message
so imagine if you wanted a very
inexpensive messaging system again for
your field technicians so you know uh
smartphones cost a lot of money data
plans for smartphones cost a lot of
money so the cool thing with Blues is
you buy the modem for like $60 plus a
couple of other fees uh but you buy it
and with that you get 500 Megs of data
that you can use for up to 10 years so
you only pay for the data that you need
once you burn through 500 Megs of data
then you simply pay another $10 for an
additional 500 Megs of data so instead
of paying $5 a month or $10 a month or
$50 a month basically for the
connectivity you just pay $10 for every
500 Megs and again since you're only
dealing with text formatted in Json 500
Megs actually goes a long way so imagine
uh again uh for your field technicians
if you had a computer device in their
vehicles and you were simply able to
send messages to them uh for whatever is
going on so basically uh if uh you need
to basically send a text message you
know essentially say hey make sure to
pick up the invoice while you're at the
client's house the system would be able
to do that again imagine being able to
do things programmatically so I talked
about that before where you could create
a server system that's constantly
looking for things like weather reports
uh disaster declarations that type of
thing and if it saw that your people
were in the area of a storm a tornado
that type of deal it could automatically
alert them with the fact uh one of the
things I think about having dealt with
construction people I had a lot of
construction done on my last house which
was miserable absolutely miserable but
anyways one of the interesting things I
found with that is that many times they
would be working all the way up until it
started raining and then my house would
get wet because it got rained on so
imagine if you had a little system like
this just to be able to beep your field
your your field people your construction
people to say hey a weather event is
moving through in 30 minutes so seal up
the house now before we have to pay for
damages when the house literally gets
rained on right that's the kind of thing
that's very interesting about this kind
of system here so basically with this
right this is simply communicating with
the LTE network this is does not use
tcpip this is communicating with the LTE
network uh using Python and the bottle
web app framework uh to be able to grab
the messages that are sent to it and
present it onto the the nice little
screen like this uh so this is what
we're going to be showing you how to
build today danger Will Robinson danger
I'm going to I'm going to have to have a
massive trigger warning for this
particular class because um uh the blues
modem actually works really well on the
Rasberry pie uh so one of the things
when you come here for to me for
education is do realize I don't get
sponsors Blues is in no way shape or
form paying for any of this I've
actually had to pay for all of this
including with the brain cells that I've
lost one of the things I was talking
about in the previous class is using the
Arduino clone dev kit that you're
supposed to start with is that sucked
they de kits I hate their de kit oh it
was pain you you can watch your videos I
literally I literally sped sped up the
uh the video for how long it would take
uh for for that little dev kit thing to
actually be able to get messages and
that type of deal um it's it's it's
interesting um devkit is definitely
curious one of the things that I'll say
though is I uh you know I am excited I'm
interested in what Blues is doing so I
decided to purchase the uh hat basically
the the the connector for the blues
modem to a Raspberry Pi 4 because my
thought was is again sometimes overall
systems are pretty good but individual
products suck you've probably seen that
before and so I was like well I believe
in what what Blues is trying to do I do
not believe in this crappy ass dev kit
let let me try a Raspberry Pi hat and I
have to say using a Raspberry Pi hat
then being able to use Python to be able
to interact with a Raspberry Pi hat uh
this works like a dream this is
wonderful again it's still quirky
there's still really too focused on Json
uh but that is very manageable uh
basically working with the hat from
almost day one uh everything works
pretty well not too many quirks there uh
so what I would say is again if you're
interested in this whole Blues World
definitely think about getting a
Raspberry Pi and a Raspberry Pi hat in
order to learn this stuff because I find
uh it works actually great uh now one of
the quirks uh is with this whole system
uh you will notice I'm literally
powering this off of a little USB
battery pack uh this is actually a
touchcreen this is a touchcreen uh the
the Raspberry Pi is connected to the
back again with the blues modem I will
say if you plan to do this exact setup
there are some we weird voltage issues
because surprise surprise surprise the
voltage that comes out of a USB battery
pack might not be the best so having
this system work this system here works
perfectly and as I do the demo today
this is simply what I'm going to be
showing you I will plug in a keyboard or
Mouse just so I can interact with it a
little bit but this is what I'll be
showing you I will warn you though is
that if you do try to go to like normal
websites like CNN.com or YouTube because
of the voltage issues like going going
to those websites I guess require more
voltage and then this whole thing just
starts getting quirky we'll call it
quirky uh really quickly uh so do do
just be careful that I bought this again
the screen for about $50 off of Amazon
is pretty generic uh basically a touch
screen that you can buy uh for the
Raspberry Pi generally speaking if
you're running command line tasks if
you're running your own little web app
running on it that type of thing uh the
power works fine again when you start to
do things like YouTube it just starts
blinking and having a little hissy fit
uh and when I say that that's actually
not just simply off of this little USB
battery pack even when I connect a a
Raspberry Pi official power supply to
this thing it acts a little quirky so
again this is one of the things I talk
about in the world of it where sometimes
or for certain situations products can
be amazing and for other situations they
can suck this particular uh product is
amazing for when you want to use it as a
Blues iot messaging system uh but if you
want to do you know watch YouTube
videos well that'll be quirky that'll be
quirky so anyways with that let's dive
in let me give you a demo of what we're
doing here because I think this is
really really cool okay are you ready
for coolness are you ready for Geek sexy
let's do this demo so this is actually
running on my local computer I'm using
the bottle web app framework with python
to run this web app on my local computer
at 127.0.0.1 P 80 and this is basically
just an html text form here I can go to
this html text form and I can put in a
message hello world from the class I am
doing uh for my students right there you
go nice little message there I can now
hit
submit that has now gone off this is the
note Hub uh API here uh we can see the
message went to the note Hub API and we
can see already I don't know if you can
see that already by the time I've said
that that message has then come to our
little iot messaging device and again
what I'm going to be showing you with
the code today is how this kind of thing
could be done programmatically again you
could have a server that's constantly
API calls to weather natural disasters
can be doing all kinds of different
things and basically when it sees
something of interest uh to your people
out in the field it could then very
easily send a message to them and this
is a way to to have communication that
will be more
reliable than uh than normal like LTE
communication like with uh with
smartphones and things a lot of times if
the bandwidth isn't good enough right
you can't even open up a website the
next nice thing here is you're just
sending Json so it'll probably be more
reliable um and again as I said before
it'll be a hell of a lot less expensive
if you simply need basic communication
Services now let's take a look at this
hardware for just a second there's not a
whole hell of a lot to this so again I
went on to Amazon and I bought myself a
very generic $50 touchcreen uh that
allows you to connect a Raspberry Pi
directly to it this is kind of cool
again with these iot projects is go out
and actually do a search for things like
screens specifically for your raspberry
Pi 4 or Raspberry Pi 5 or Raspberry Pi 3
find the one specifically for your
Raspberry Pi because many times it'll
actually have screw holes so you can
actually screw it all together so like
with this with this screen I could
connect a normal computer to this screen
I could connect a Raspberry Pi 5 to the
screen the whole nine yards but
obviously I can't necessarily screw it
directly to the back so the cool thing
here is we actually have the Raspberry
Pi 4 screwed directly to the back of the
screen uh from there basically there's a
couple of connectors that go uh from the
HDMI uh output on the Raspberry Pi into
the HDMI input for the screen and the
USB output into USB input uh for the
screen uh to give you that touch
capability uh past that uh there's the
uh note carrier so it's a
$115 a hat from blues uh that you plug
into your Raspberry Pi 4 or five or
whatever else and then on that you screw
them the modem so again the modem is its
own thing costs like $60 for the modem
self plus the raspberry the the uh the
Raspberry Pi hat so this thing called
the note carrier which is an additional
$15 uh and then you have the Raspberry
Pi and then you have this screen and
with that you get this PR pretty nifty
neat device right here which again I
just think for for any number of things
could be very useful again like right
now we're doing a messaging app here so
again imagine Fiel technicians out on
the field being able to send them
essentially text messages but you could
use this for digital displays uh imagine
if you have digital displays in a campus
where again you have a large facility uh
that's geographically spread out let's
say you have digital displays throughout
that facility and you're thinking how
can I have messages show up on those
displays so you might put a solar panel
on it put a little battery pack on it
again because this doesn't need very
much power uh but then how are you going
to deal with the connectivity do you
want to pay $10 or $20 a month for real
LTE connectivity do you want to make
sure the Wi-Fi can actually get out
there what's cool with this is you could
have digital displays that you can
update from anywhere uh Simply Connected
again using this blues uh iot modem so
anyways and this is the hardware and
basically how it's built out uh again
it's it's pretty good except for that
weird power problem and again that might
be like with with the screen that you
buy I just found a random screen and
bought the screen so depending on what
screen you buy may may or may not have
the power issues that I'm running into
so now let's talk about architecture how
does this overall system work so I Tippy
tap type at my computer and that message
shows up on my iot messaging device but
but how exactly does that happen well
the first thing to realize with this
system is that we are not actually using
a server up on the internet again when
you create something like a web app you
don't necessarily have to put that web
app up on the Internet depending on how
it's doing communication so our system
when I when I when I go to that little
uh text area box and I I hit the submit
on the form right that is simply sending
data out to the API that Blues has it's
not receiving any communication back so
the cool part about that is is we can
actually just have that on our computer
or server internal to our Network
because we don't have to have uh
basically back and forth uh
communication connectivity uh basically
Blues right Blues has their API up on
the Internet uh they use a system called
notubes their platform notehub.org
an little text box and we got a little
submit and I'm going to put information
in right so I say put in the message so
basically from here what's going to
happen is first we have to get something
called an ooth token ooth so whenever
you're dealing with
apis basically they they decide how the
authentication works again a lot of a
lot of times they get with like new
coders and they're like why do you use
ooth versus something else and it's like
because they require you you to use ooth
that's how it works right so anyways
ooth is a token based system uh for
allowing people access to resources on
apis and essentially what happens here
is your computer goes up to note hub's
ooth server and it gets something called
an ooth token that's the first part of
the communication process once it gets
the ooth token it then takes that ooth
token numbers as a token number it
combines the O token with the message
and then sends all all of that up to the
API and that is when the API will accept
that basically accepts the credentials
and the message and so it gets stored
into the API then over here we have our
device right so we have our screen we
have our PI and then we have our Blues
modem um connected to all this now
important thing to understand with this
is that the blues modem is its own
subsystem H so many times when you're
doing coding or you're building projects
your code will be be able to interact
directly with the resources uh so think
about this like with a Wi-Fi card or a
network card right in Python you
basically simply do something and you're
able to do whatever it is that you want
the interesting thing here is your
python script right actually has to
communicate with the blues modem and it
sends a command to the blues modem and
it says to the blues modem hey get a new
note so in blues World these are called
notes basically a message get a new new
synchronize with the API and get us the
new note once that new note comes in
it's actually stored in the Blues modem
initially which is kind of an
interesting thing again we think about
things like security policies and where
data gets stored in cash and if somebody
snagged something when they weren't
supposed to anyways it actually gets
stored within the blues modem once it's
in the Blues modem then your python
script can then ask for that message to
be pulled out of the Blues modem so that
it's able to do something with it right
uh and then it actually has to tell the
Blues modem to delete the message again
one of the interesting things from a
cyber security world is think about this
the blues modem does not delete messages
until you tell it to so if you had lots
and lots and lots of messages coming in
again for for your System Authentication
messages whatever you're doing if
somebody literally stole that modem and
you hadn't sent sent the command to
delete the messages they might be able
to get whatever messages that have have
been sent between the devices so this is
again one of those things just to
consider so anyway you got the python
script uh my python script basically
Loops every 5 seconds so just to make
the system work you know pretty quickly
unlike the last example I just have a
loop uh basically every 5 seconds I
don't know if that's a great idea in the
field again what you do as a
demonstration versus what you do in the
field might be entirely different if I
was in the field I probably have a loop
every minute or something like that but
we're just doing it for 5 seconds to
make it quick and so basically every 5
seconds it's saying that the bles modem
please synchronize and once you've
synchronized give me what messages uh
that you've gotten right so basically
every 5 Seconds it goes out into the LTE
network connects with uh with the blue
servers and then it pulls down whatever
it has once it pulls down the data
basically once it's pulled down the
message one of the things you have to
think about is the data store is it
simply good enough to have the current
message show up on the screen so one of
the things you'll notice here is if you
take a look at my device I can scroll up
and I have all of the junk
messages that I've I've previously done
over the last little while right it's
got a nice little time code and ID and
all that kind of thing right because
again I don't simply want the message
the old message to be deleted and the
new message to replace it again if we're
having like a little conversation or
something I want to be able to see that
history when order to have a history you
need to store to a data store you need
to store somewhere so basically what
happens here is once python uh gets
access to the message it then sends that
message to SQL light so again my new fa
my my second new favorite toy sqlite is
a very easy to use uh relational
database system uh that basically as as
easy to manage as a text file uh with
all the power of a relational database
you get SQL structured query language
right so basically what happens here is
a script grabs a message stores in the
sqlite database once it's stored in the
sqlite database it then does a select
all so basically selects uh all the uh
all the records in that database takes
all of those records and then prints
that out onto the screen as all the
messages that you have previously seen
it it writes that as an HTML table so
again that's why uh there's you know
adequate formatting uh when you actually
look at the screen and that's what
presents uh the message app to you so
this is the basic basic architecture of
how this works we have uh bottle and
python on my local computer again it's
just a MacBook Pro computer is a normal
computer but because we use bottle we're
able to have that web app framework uh
we put in the message we get the OA
token we send it the message in a token
up to the API our python script is
constantly looping every 5 Seconds
making a request of the Blues modem to
be able to pull down any messages when a
new message comes down that then gets
stored into a SQL database once it's
stored into the sqlite database we do a
select all basically we select all the
records and then we write all of the
records onto that HTML document so that
we get a nice little message screen like
this and so you know this is the
architecture of this particular system
so now let's take a look at the code the
first thing we're going to look at is
our quote unquote server again my
MacBook Pro so this is the code that
gives us this nice little HTML form here
uh so when you take a look at at this uh
we've got a couple of modules that we
have to import in the beginning so we
have from bottle so again bottle is that
web app framework you do have to use pip
3 install uh bottle in order to make
sure that works on your system we're
going to import route run request post
and redirect uh we're then also going to
import the Json module so we're going to
need this actually to be able to pull
out the access token got to go over that
in a couple minutes and we're going to
import OS so that we can interact with
the operating system uh if we come down
here there's not a lot to this we have a
modem uh function here that I'll talk
about in a second we have a route uh to
the the root directory here uh which is
the message form and then we have the
post for the message send so we have the
form here this is going to be the
function to send the message and then
this is going to call the modem function
uh to actually do a little bit more of
the process um at the end here this is
basically just simply the line that you
need to put in to actually be able to
run the script as a web app so run host
at 0.0.0.0 so this means whatever your
current IP address is it will run at
that Port 0 again since I'm actually
administrator on my system I can run
this at Port 80 so we don't have to do
colon 8080 or anything like that and
debug equals true because if there's a
problem I do want to see what the
problem is uh the first thing that we're
going to take a look at is the route so
at Route uh so this is called a
decorator and this is a route decorator
so basically this is the page that
you're going to be going to page uh
equals so that we can put this on
multiple lines and be able to format
this decently uh single quotation single
quotation single quotation H1 message
app form action equals message send
method post so we're going to go to the
message send page or script right and
that is down here uh with whatever we
put in we're going to create a text area
named message four columns 40 rows going
to close that and then we're going to
have the submit form so you submit the
text and then that is going to be sent
to the uh the the message send script
that we've created now you'll notice
here The Decorator says at post so this
is so that you can grab post values that
are coming in um and so the post value
is when you have an HTML form the data
is sent as a post uh message equals
request. forms. getet the message right
so that text area we call message before
basically this is how we get the value
for message print so print out on the
screen message from or message uh from
form whatever the message is if message
does not equal none send the message to
the modem function so again one of the
quirks is if your uh if your text area
is blank let's say you're doing
troubleshooting or whatever else you
don't want to be sending blank messages
that's just bad for everybody so if it's
none if it's if it's if it's not
none do this so if it is none don't do
it if it's not none right right and then
uh redirect back to um our home uh
basically the the rout uh with this
we're then going to go to the modem
function and if we come up to the modem
function this is where we get a little
bit more complicated uh yes there are
Keys here that will all be deleted
before you watch this video one of the
problems when I do this especially when
I'm doing live demonstrations is
figuring out how to show you folks all
the stuff while also keeping my keys
hidden and so basically what I just
decided to do is delete my Keys as soon
as I'm done doing these videos anyways
we have a device ID uh you get this from
not Hub you have a project ID you get
this from not Hub you have the ID uh for
the client and the ID for the secret so
the device ID and the project ID this is
what you're going to be sending to and
the identifier for you this is what
we're down here this is what we're going
to be using uh for that ooff token here
we're going to get the ooff token uh B
using an F string three quotation marks
uh just to make this easier curl post
we're going to be going to to
notehub.org
Grant type equals client credentials the
ID for the client so that's here the ID
for the client client secret the secret
for the client the response is going to
equal
ospen query. read so what this means is
we're using the OS module we're going to
be basically running this query Comm
this Curl we're going to be running this
uh but we want to give the results back
so you can you can send a command
without getting results back we want the
results back and we want those results
back as text not simply an object so we
can read it so basically we're going to
send this curl query we're going to get
a response back from the uh the note Hub
servers and then we're going to save
that text as a response that text is
just text though we can't really access
it the way that we want so we want a
Json value so then we're going to do is
json. loads response so that response
value that came back as text we're going
to turn that into Json token is then
going to equal response access token so
the value of access token is now going
to be token and then we're going to
print out the token on the screen now
we're going to do the message so we're
going to print the message so we know
what's going on uh the first thing that
we're going to do here is we're going to
get rid of single quotation marks and
double quotation marks so how you handle
this is your own thing because the issue
is right we're sending this we're
sending this message through curl right
again we got to think about systems and
how all this different stuff is working
so we're sending this essentially
through command line a command line
command using curl if you put in single
quotation marks or double quotation
marks in that again when we're doing
these F strings uh things will fail out
very quickly so what I'm just doing here
to be very simple is I'm simply
eliminating the single quotation marks
and double quotation marks so we don't
actually ex we don't accidentally exit
out of things before we're supposed to
again you might want to go into and look
at how curl does escapes and that type
of thing if you care about it then we
have query so query equals uh F string
we're using double quotation marks here
because in the curl we use single
quotation marks Curl we're now going uh
to notef file.net projects and we're
sending to the project ID devices the ID
of the device notes data. Qi so there's
different files up a
notubes so that the O off token that we
got we're going to be using here and
then we're going to send body message
message so again remember everything has
to be in Json so basically the body of
what we're sending is going to have a is
going to have a key of message and the
value of message is going to be message
uh we have the double squiggles here uh
because basically when you're using F
strings another squiggle escapes out of
the first
squiggle anyways it's escape the reason
we have double here is because since
we're doing Json we have to send
squiggles but squiggles actually mean
something to FST strings so to escape
out of the squiggles in Python we use an
additional squiggle bracket when you're
doing an F string anyways uh then here
we're going to do os. system query so
this is where you simply can send the
command without getting response right
up here up here we needed the response
so that we can get the access token so
that's why we did OSP open here I don't
really care I just want to send I don't
want the value back so I'm just going to
send that off then I'm going to print
message was sent and whatever the value
uh for for message is uh and so
basically that is how the code for
sending the message from uh my local uh
web app works to be able to send it up
to note hub now in order to make this
function uh basically all you do is you
hit the Run button right so you hit the
Run button here and uh you know you can
now see that this is running uh if I go
here and I can say new
message I can hit submit then we come
back here and this is where we can see
all of that printout uh message uh from
before so we get uh token information so
the token information is here that's the
token for this uh we have new message
message was sent what the new message is
and so basically that information is
being printed on the screen as a
troubleshooting procedure so now let's
take a look at the code for the client
system so our little iot messaging app
right here this is the python the bottle
code that's running in order to get us
this web page here uh there are a couple
more modules that you have to deal with
because you are dealing with that note
card and so that note card requires a
couple of things uh so from bottle we're
going to import route and run that's all
we need we're then going to import SQL
light three so you're probably going to
have to install SQL light 3 onto your
system and that is just the database
that we're going to be using uh we're
going to import OS module and then we're
going to import the note card module so
the note card module is a specific
module that was created by Blues for
interacting with the note card and so
this is where you're going to import it
uh from uh periphery we then need to
import I i2c so periphery is also
another module that you're probably
going to have to install and what i2c is
is it's an Electronics uh communication
protocol It's actually an addressable
protocol and the idea is if you want to
plug in a whole bunch of different
electronic devices into an Electronics
project this is a way that you can do
that without having to run too much wire
so basically everything generally has a
hardcoded address and then you're able
to communicate so you can have multiple
screens you can have a lot lot of
different sensors all of those things
and your electronics device is able to
communicate with all of those those
different widgets uh with this again if
you're dealing with Arduino you will
deal with i2c a lot with Raspberry Pi
you don't have to deal with it so much
but probably if you're going to be doing
doing Electronics projects you will have
to deal with i2c a little bit and so for
this basically we're just simply
importing uh what you're going to be
able to need for communicating with i2c
now one of the warnings here is make
sure that you actually turn on i2c on
your Raspberry Pi so one of the weird
quirks with Raspberry Pi is it allows
you to do a lot of things but sometimes
you actually have to turn on whatever it
is within the configurations so the
webcam right you connect up your webcam
and for some reason it doesn't work it's
because you haven't turned on the webcam
within the configurations there's a
whole bunch of stuff like that with
Raspberry Pi one of the things that you
run
into is that if you do not turn on i2c
i2c doesn't work so go into your
configurations and turn on i2c uh then
from date time we're going to import
date time uh just for a little format
and we're going to deal with later uh so
uh what's going to happen here is when
the script initially runs uh we're going
to set up everything that is required
for our note card to work the first
thing is we actually have to do the i2c
connection so as long as i2c is turned
on in Raspberry Pi you should have Dev
i2c hyphen 1 so the port is going to
equal this i2c hyen 1 this is the
address for it and then incard is going
to equal note card open i2c at the port
with a couple of values I'm not sure
where those values are copy and paste
those values anyways this is how you
connect to the note card uh product ID
uh so basically uh this is your user
account plus the project that you're
doing so I have an Eli the computer guy
uh and a project of Pi because that's
what I'm doing and so that's the product
ID we're then going to be setting up the
uh the the note card and again
everything actually has to be in Json
they use Json to a hell of a degree uh
so uh the request is going to equal a
Json value of request Hub set so we're
going to be setting the modem here when
we turn on this device request product
so the key in request is product we're
going to put in the product uid so
that's going to connect to our system
and to our project the mode is going to
be continuous so theoretically speaking
theoretically the idea is that the modem
is going to be continuously talking uh
with the uh with the network so that it
can bring down any new information you
know that's what they say that's what
they say and then we have the request
interval equals two so if we don't get
new messages automatically down from
there from their API at least every 2
minutes it should automatically try to
sync with their API to bring the message
down again you know whether or not that
works I don't know I have some code to
deal with that later uh we have response
so RSP equals incard transaction the
request so we're going to send this
request up to their servers and we're
going to print out what the response is
here and so this is all just the basic
uh configurations then the next thing
that we're going to have is creating the
database uh if it does not exist so
print create DB current directory equals
os. paath directory name blah blah so
basically this is so uh the uh the
database is stored in the same location
that this python script is in name
equals message DB file path equals os.
path. jooin current directory DB name
connection equals sqlite 3 connect to
the file path cursor equals connect con.
cursor we're going create a SQL
statement for create table create table
if not exists so this is one of the
things I like with SQL light is
basically you can run this command
automatically on any new system if the
table doesn't exist it autoc creates a
table if it already does exist it
doesn't worry about it we're going to
have a table named message it's going to
have an ID uh uh column with an integer
value that's a primary key we're going
to have a timestamp from the note card
column that's text and we're going to
have the message that's text execute
create table so it's going to run the
SQL statement if the table doesn't exist
it's going to create it we're then going
to commit do remember with SQL life you
have to commit if you don't commit you
didn't do anything and then we're going
to close basically I set this all up as
its own function and what you'll notice
all the way down here is essentially
when the script runs for the first time
um these other things like with routes
this kind of stuff doesn't get called
but but this will automatically get
called as soon as the script runs this
create DB function will fire off to
create that table for you if need be and
again you have the Run host down here
0.0.0.0 that's for this system just with
the Raspberry Pi I have it at port
8080 and debug equals true so from there
so we've created the database and we've
got the system running so we we've we've
connected to the note card we set up the
note card we have the database than is
running so now let's take a look at the
route for the root right so we have this
to the root directory so 127.0.0.1 this
will show us our message right so I
don't have to use anything additional in
the URL just 127.0.0.1
uh so here we're going to get timestamp
_ refresh equals message uncore getet uh
so I want a timestamp value uh so
there's a time stamp value up here that
says last refreshed again just for
trouble shooting purposes for people out
the field they know the last time that
the screen was refreshed if they're
looking to wonder why they haven't got
in a message and basically we're going
to have a return from the message unorg
function let's go up here and take a
look at that message unorg function this
is we're going to be gting the values
from the note card this right here is
why this system works so much quicker
than my Arduino system so message doget
fires off whenever basically that that
the the index file gets gets pulled up
so it's going to get refreshed every 5
seconds so every time it gets refreshed
then the uh this is going to run this
message. get function is going to run
and so the first thing that we're going
to do is we're going to do a request of
Hub sync so Hub sync this is where your
Blues device communicates with their
system and basically uh Records any
changes so one of the reasons this
system works so much better than the
last last one is it's not waiting on the
continuous communication it's not
waiting on the interval communication
it's forcing this hubs sync literally
every five seconds which may or may not
be a good idea right uh so this is the
request command Hub sync to do the
synchronization incard command uh
request so basically transaction is
where you want to get a response if you
do transaction you want to get a
response back command basically just
says send the command I don't want to
respond
then we're going to create a request
here
request uh wreck note changes so I want
the note changes to file data. Qi delete
so basically if a new note comes in I
want the old the notes to be deleted off
of the card because remember it does not
automatically delete notes off the card
unless you tell it to so I do want it to
be delete print request request response
incard transaction and then print the
response then BAS basically what we're
going to be getting back is we're going
to be getting back uh these Json values
and so we're going to be looping through
this basically as as multi multi-level
dictionary so four outer key uh
interdict in response. items so we get
this response back so we want to get
response. items all the values if
message is in string of interdict so
basically if there is a message there
because that's all we want to care about
for key value in interdict items so
interdict is the values and then we want
the items of the interdict print time
value time message value body message so
that's all one thing insert so DB insert
so this is going to be where we're going
to be inserting the values into the
database that's a different function
value time value body and message so we
want the the time that comes in from
this and we want the message that comes
in from this and we're going to be
sending this to the DB insert function
we scroll up here to the DB insert
function we're on a print insert DB time
and message so time and message are
coming in current directory we could
probably clean all this out but it's
here so the current directory OS path
directory name database name file path
name connection sqlite 3 connect to the
file path cursor is the same s is insert
into the message table that we created
timestamp note card comma message values
question mark question mark then we're
going to execute the SQL statement we're
going to use the time value we're going
to use the message value so that's going
to be the SQL statement for the insert
again we do have to commit if we do not
commit it it doesn't actually stick and
then we're going to close that
particular connection so this is where
we insert the values uh into uh the
database so we're going to come down
here
right so that's where we insert the
value then timestamp refresh equals date
time now so the date time module that we
had before basically whenever this this
runs I just want to get this value for
the the time stamp refresh and then
we're going to return that timestamp
refresh we're going to go here we're
going to go back to the index so again
so now we have the timestamp so we
receive the message we then insert the
message into the database we got our
little time stamp
then we're going to do record equals DB
select so we need all the values from
our
database so we're going to go up to this
function DB select is here right anyways
current directory again I could probably
clean this up current directory database
name OS path connect to SQL light and
the cursor SQL statement here is select
all from message order by T timestamp
note card uh descending so basically I
want all all the values from the message
uh database ordered by the timestamp
from the not the note card going from
the the closest to me back so so the
newest to oldest one of the interesting
things with the timestamps is I've
notice sometimes in the communication uh
the order is different so things will
actually be in a different order the a
message will be received but it'll be at
a different ID than you think it would
be so anyways the Tim stamp I think
seems to be the best way for being able
to sort the
messages cursor execute SQL so we're
executing the SQL statement record
equals cursor. fetchall so basically
record is now going to be all the
records that came in from the database
we're going to commit we're going to
commit we're going to close and then
we're going to return the value for
record so basically all the records are
now going to be returned as the value
for record uh we're going to have record
page so record page table uh so we're
going to Open Table uh table row table
header ID table header timestamp table
header message close a table row
oops close table row uh then forx in
record timestamp equals uh date time uh
from timestamp
X1 uh string blah blah so basically what
this is going to be is this timestamp
here is oh I'm
sor oh too many computers this timestamp
here is going to be this right see see
how it says like Friday at
104305 so that's the time stamp that
we're dealing with right there so Tim
stamp equals that basically modified for
day hour minute seconds so we're going
to do record page record page uh equals
F string uh previously whatever has been
in record page TR x0 so this is the ID
uh then we're going to do that timestamp
that we created then we're going to do
X2 which is going to be the message
itself so for X so for every basically
record in that record that comes back
we're going to do X at index zero will
be the ID of the record we're then going
to print out that human readable
timestamp that we created using X1 and
then X2 is going to be the message so at
the Third third index that's where your
message is uh then you're going to close
out uh the TR there record page equals
uh record page so basically what we
created here and then we're going to
close the table so again whenever you're
dealing uh with HTML remember you open
and close things so we open the table
here we create the table headers we then
print out all the records within the
table and then we close uh the table
there we're then going to create a
header So Meta http equiv refresh
content equals 5 so what this is is with
an HTML 5 you can Auto refresh a page so
this is just within HTML 5 it's not
JavaScript or anything like that again a
very simple way of doing things so
basically what it's going to do is it's
going to Auto refresh every 5 seconds so
all those function I showed you every 5
seconds is going to Auto refresh and
it's going to fire off all those
functions to to to get the message to to
insert the message and then to select
the
messages uh then we're going to have a
message app at the top uh so that's
where oh if you take a look at this
again this is the header part uh you
know message app here so that's message
app there and then last refresh we have
the timestamp refresh so this is the
time stamp refresh so the time stamp
refresh is the last time this entire
page was updated whereas the note card
timestamp that is the timestamp value
for for whenever the the message came in
uh from the note card then page is going
to equal basically header the header
value with everything that came in from
the record page and then we're going to
return everything for that and that is
what gives us this little page that
shows up when we go to
127.0.0.1
and that my friends that is the code for
this particular project so there you go
that is how you two can build your own
iot messaging device and I will say with
the quirks as long as you're only
printing h ml text out on the screen
this whole thing works like a champ
again you try to go watch a YouTube
video it goes to hell very quickly but
again that's one of the big things when
you're thinking about systems and when
you're thinking about actually you know
Building Solutions you know there's all
of these different things that come
together to build a solution right
there's the code the python uh there's
the product that you're buying there's
the API calls there's the hardware
itself if any one of these things fails
or is quirky
oh the entire thing is a disaster but
again for just this this actually works
well and again this could be a messaging
device uh again and um you know things
like uh fuel technicians vehicles uh
this could be um you know digital
display so you could have um CSS in
order to make this prettier different
colors and that kind of thing you could
store images locally so you cannot send
images or movie files or anything like
that over Blues that's just not going to
work uh but you could store them locally
and then basically trigger them or point
to them based off of current conditions
uh that's where like let's say you had
like a little weather app here like if
you have an i if you have an iPhone and
the weather is going to be rainy or the
weather is going to be sunny you know
how the the image in the background
changes well what you could do is you
could have the same type of thing on
your device so you could have a rainy
image and a sunny image and a cloudy
image and then if the weather is going
to be sunny you simply show The Sunny
image that's already on the device you
don't have to send the image to the
device it's already there and you simply
reference it right that's some of the
interesting stuff that you can kind of
do with this and again like it's like I
say if you're do dealing with digital
Billboards and campus facilities or that
type of thing some place where you can
have access to the local storage so it's
like I can gain access to the local
storage to put in a new USB stick I just
don't want to do it every day or every
week or every month right so I could be
like an IT person basically clone you
know 30 different USB drives and then
once every month or two simply go to all
my different digital displays plug in
the USB drives and then when I send new
messages to all those displays it will
be able to pull uh you know from the the
local uh Graphics files that are on the
device again this is the type of thing
that you need to be thinking about with
this whole concept of architecture and
again in this iot world what happens
when tcpip 4 uh you know high bandwidth
connections simply don't exist can you
more or less get the same results by
designing a different architecture a
different way of doing the communication
maybe it's a little iot modem maybe it's
a little bit of sneaker net maybe it's a
little bit of some some other things
this is the kind of stuff that I want
you to be thinking about as modern uh
you know computer people modern
technology professionals uh so anyways
uh yeah um again with this I'm very
happy with
blues if you watch my videos you will
know I was very frustrated with Blues
before using the blues dev kit using the
blues dev kit yep devkit definitely
exists oh poises anyways being able to
use Python and using this on a Raspberry
Pi I have to say this whole system works
so much better and again one thing to be
thinking about when you're designing
these types of systems is what do you
really want like a lot of people think
well you know a microcontroller is going
to be a lot less expensive than a
Raspberry Pi so I'd want to go with an
Arduino or microcontroller but remember
you know Raspberry Pi has those the the
Raspberry Pi zeros the Raspberry Pi
zeros are down to like $10 a piece so
you could actually get a full-fledged
computer that can run python that you
can plug this hat into that might be a
much better development environment for
you again for 10 bucks um versus you
know going down the Arduino route so
that's going to be the other thing that
you need to be thinking about again as a
professional when you start deploying
this type of thing is like what is your
knowledge what is your comfort are you
more comfortable with python or with
Arduino that type of world and then also
institutional knowledge do remember when
you start designing this type of thing
at some point you're going to leave the
company I don't know whether it's going
to be a heart attack or you're going to
get fired or you're going to get a
better job right and some some point
you're no longer going to be working for
that particular company so one of the
things to be thinking about is the
institutional knowledge of your company
what other people in your company know
so again if you're in an environment
where you're dealing with
microcontrollers all the time so it's
not just you but your peers are also
dealing with microcontrollers then it
might be much better again to be in that
Arduino World on the other hand if
you're in an environment where people
are more again in the python world doing
backend infrastructure building web apps
that type of thing it might make a lot
more sense to design your solution for
python so that when you leave the
company for whatever reason somebody can
actually pick up your code and have a
chance in hell of knowing what's going
on again having done projects in Arduino
versus python I know personally for me I
feel a lot more comfortable in Python
and I feel a lot more confident with
what I'm doing in Python even though I'm
pretty sure what I did in Arduino was
solid I'm pretty sure
Prett sure again I code like an mcse
let's be clear let's be completely clear
I code like an mcse but I'm pretty sure
what I did was solid but I don't have
the confidence that I do that that I
have with python so again this is
another thing that you need need to be
thinking about when you start deploying
these types of projects so anyways with
that I think that's about all I've got
to say as always I enjoy teaching this
class and I hope to see you at the next
one