View All Posts
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Learn how to remotely monitor your Arduino devices by sending log messages to a central server using UDP and a paper trail. Get insights into how your hardware is performing, even when you can't physically access it.

Related Content

[0:00] well welcome back to another video in
[0:04] this video what we’re looking at the
[0:06] Arduino and particularly the ESP 32 but
[0:10] there should be general to any algebra
[0:13] you know that supports Wi-Fi or Ethernet
[0:16] what we’ll be doing is we’ll be doing
[0:19] the merge love it
[0:20] this is particularly useful if your
[0:23] device is embedded and you can’t easily
[0:26] connect a serial cable to it so the
[0:31] coding session we’re going to be sending
[0:35] messages to a remote modem service using
[0:38] UDP we’re going to be formatting the
[0:42] messages to indicate a longer level at
[0:45] base sauce you know I have support for
[0:49] printer to our logger so we can do a
[0:52] formatted thinking and we’re gonna
[0:55] change the color of the log messages to
[0:59] highlight if they are areas warnings or
[1:02] just general debug information it’s
[1:06] quite a lot of coding session so I hope
[1:09] you enjoy it and if you do please
[1:13] subscribe to the channel it’s a great
[1:15] way to get notified when we publish new
[1:18] videos so into the coding session so I
[1:23] started up the Arduino IDE and written a
[1:28] a very basic sketch that will print
[1:31] something to the serial monitor so let’s
[1:34] let’s run that and check that we
[1:38] actually get some output so they’re
[1:46] going we’re getting yourself output from
[1:49] the serial monitor every time our loop
[1:52] executes now what I want to do is have
[1:57] this login going out to a central server
[2:01] so when I deploy my hardware and I can’t
[2:05] really connect to it I can still easily
[2:08] see what is doing
[2:10] and how its performing now my go-to
[2:14] logging system is something called
[2:17] betrayal so this is what I use when I’m
[2:21] building production systems normally for
[2:25] server-side applications so let’s create
[2:30] an account on paper trail so I’ll just
[2:34] quickly create a temporary test account
[2:39] [Music]
[2:44] so that’s our paper trail account
[2:49] created let’s add a system to the paper
[2:55] trail so what you can see is a support
[2:59] various different types of logging so
[3:03] you can say I want to have a system log
[3:07] from anywhere and you can see you have
[3:13] all these integrations obviously there’s
[3:15] no integration with Arduino so what
[3:20] we’re going to do is integrated quite a
[3:24] very level and to do that all you have
[3:27] to do is send a UDP packet to this host
[3:32] and this port so the first thing we’ll
[3:36] do is get Wi-Fi up and running a larger
[3:41] ecosystem and we’ll just try sending a
[3:44] UDP message to this endpoint and see
[3:49] what happens
[3:49] so let’s get some get Wi-Fi up and light
[3:55] so obviously we need some includes sir
[4:04] [Music]
[4:08] Argentina
[4:12] and put them in the Wi-Fi header and
[4:18] what else they need Wi-Fi uttp so let’s
[4:26] do the usual down setup for getting
[4:29] Wi-Fi up that money say that should be
[4:53] our connection to Wi-Fi done man let’s
[4:56] try just sending an EDP message to do a
[5:01] paper trail system and see if it comes
[5:04] out so just enter UDP packet we need a
[5:12] Wi-Fi UDP class said let’s create all
[5:19] the players and now it’s intellectual
[5:26] message
[5:29] we just need to send a buffer to the
[5:36] remote service which is Wi-Fi you can
[5:41] begin thank you
[5:44] and for this for me to specify the coast
[5:48] and the pool number stop PIPA Trail host
[5:56] and portrayal port number never make
[6:03] this right maybe string and then we in
[6:11] the packet and that should send that
[6:16] message off to paper trail so let’s give
[6:21] that a quick go and see if we actually
[6:23] get any messages coming through
[6:26] so what uploaded and so have spelled a
[6:32] file room so let’s fix some of our type
[6:37] errors so I forgot I was created by
[6:43] Wi-Fi credentials so I’ll quickly put
[6:47] those into a separate file so I’ll
[6:55] create two hash turbines for their
[6:58] society and the password and now back in
[7:04] raincoat I’ll just replace things with
[7:07] those two your hashing files
[7:16] so Peter casting issues the Wi-Fi UDP
[7:21] right expects to have an earring to page
[7:25] stops and we’re just terrific cast to
[7:28] manage as Charles can the cast a vacuum
[7:31] Switzerland you in latest now a fingers
[7:38] crossed it should be all of our
[7:40] compilation maneras and hopefully this
[7:44] will now run so we’ve got our parried
[7:53] running and it’s not your message to the
[7:56] serial port the silicon paper trail and
[7:59] see if we’ve actually got anything
[8:02] coming through so you see on our paper
[8:06] trail a dashboard it’s found a system so
[8:09] if we click on this system that we can
[8:14] now see we’re getting some messages
[8:16] coming in now obviously this isn’t ideal
[8:22] what we have is an IP address and the
[8:25] thing that says logger so that’s not
[8:29] completely what we want we also can’t
[8:32] really see the severity of our messages
[8:35] so at the moment portrayal is defaulting
[8:39] to emergency for all of our log messages
[8:44] so ideally we’d want to show things like
[8:48] debug warning notice info so that we can
[8:53] categorize our messages depending on how
[8:55] version they are currently everything’s
[8:58] an emergency which is not really what we
[9:01] want so when it’s the next day let’s fix
[9:04] that by putting out this logging process
[9:10] into a separate locker class and then we
[9:13] can actually start making use of some of
[9:16] the more interesting features of paper
[9:18] trail so the way paper trail works is
[9:24] you can send it just a full message
[9:28] but the ideal thing to send paper trail
[9:31] is what’s called a syslog message so if
[9:38] we look at the Wikipedia page on syslog
[9:42] then you can see that it’s a standard
[9:45] for message logging and that’s quite a
[9:50] strange standard so you have what they
[9:56] call facility levels so if s so facility
[10:00] codes which kind of indicate what system
[10:04] is doing the logging and you have
[10:07] severity levels now certain levels of
[10:11] logging is probably more than we
[10:14] actually need so I’ll probably just do
[10:17] error warning notice info and debug as
[10:22] the most we actually need even that may
[10:28] be slightly over the top so let’s
[10:30] construct a syslog message T so not
[10:34] paper trail so if we look at the RFC for
[10:38] syslog then we can see what the message
[10:42] should look like and now this can be
[10:45] slightly hard to read so what it
[10:49] basically says is a syslog message
[10:52] consists of a header a space character
[10:56] some structured data can have a space
[10:59] character and a message and the Kadare
[11:03] consists of a priority a version a space
[11:07] a timestamp a space a cool name space
[11:11] pack name space book ID space and a
[11:15] message ID and our priority is made up
[11:19] there angle brackets a priority value
[11:22] and closing angle brackets and not most
[11:26] of these fields are actually optional so
[11:29] you can use what they describe as the
[11:33] middle value absurd look in here again
[11:39] you can see we have a simple corner by
[11:41] you and every scroll down we see that
[11:44] that is just a dash so let’s let’s build
[11:48] up one of these messages and see how it
[11:52] works now the primary is slightly weird
[11:59] as well research for this and see how
[12:03] we’re supposed to construct it then we
[12:06] can see that somewhere in the
[12:09] documentation there we go the priority
[12:12] value is calculated by multiplying the
[12:15] facility number by 8 and then adding the
[12:19] numerical value of the severity so
[12:23] basically to translate that into
[12:25] slightly more English we’ll take one of
[12:28] these facility curves so I’m just gonna
[12:31] use 16 which is for local use we’ll
[12:35] multiply that by 8 and then we add on a
[12:38] severity level so let’s let’s pull this
[12:43] out to a function for now so we’ll have
[12:50] our log function and that’ll just take a
[12:59] message for now and what we’ll do is
[13:04] we’ll send a packet off to you paper
[13:06] trail and we’ll construct one of these
[13:09] RFC syslog messages so what do you need
[13:15] them so according to you this so I’ll
[13:23] head up
[13:31] we’re going to need priority
[13:34] so that’s construct that they will have
[13:37] an open angle bracket then we need 16
[13:41] times 8 plus local level so actually
[13:44] that’s them Margaret here let’s create
[13:47] an enum for all look level so going back
[13:51] to the documentation our local levels
[13:54] are these ones so we’ll just do these
[13:58] last set of block messages so there’s
[14:08] three warning is for make it very
[14:20] slightly shorter so we’ll have to do so
[14:22] much piping
[14:26] [Music]
[14:36] that’s all night
[14:38] lovely level you know let’s add that as
[14:42] an argument
[14:43] nobody doesn’t matter which is good idea
[14:48] [Music]
[14:51] so now we can construct try a value
[14:55] which will be our facility code which
[14:58] I’m just going to use 16 let’s just fill
[15:02] that as well
[15:14] Plus local man changing angle bracket
[15:22] yesterday’s so that’s this done
[15:25] actually how versions just belongs there
[15:28] there’s a version one and then there’s a
[15:31] space timestamp we can submit that
[15:35] because will let paper trail deal with
[15:38] that and there’s the space and then it
[15:42] once what it calls the host name now I’m
[15:46] actually use this as our identifier so
[15:49] that’s another parameter and we’ll just
[15:52] call this system maybe have a another
[16:06] space now we have what they call a clean
[16:09] served you may not have one and for this
[16:15] pass one and so that’s just call this
[16:17] for this context so potentially we could
[16:19] have lock messages from different parts
[16:23] of the application so let’s pretend and
[16:31] then we needed another space and truck
[16:34] ID we don’t have it we’ll just do it -
[16:38] for that and then there’s a space and a
[16:39] message ID so we don’t have a message ID
[16:42] so let’s go back and check what else we
[16:46] should have we done the header now so
[16:53] that’s all header so after the header of
[16:55] the space and then there’s structured
[16:57] day - don’t really have any structured
[16:59] data there is a a format business by
[17:04] name value pairs in this structured data
[17:07] feel free to read the RFC
[17:09] if you’re interested in that and lost
[17:14] track of how many patches I need so
[17:16] we’ve got the smooth a service like it’s
[17:19] both so we have our priority version
[17:27] space time stamp space hostname space
[17:33] app name space I do space message
[17:39] ID space structured beta so then we just
[17:44] need our message on the end so that
[17:49] should be a syslog message that paper
[17:53] trail will understand
[17:55] so how that’s nasty string and now I’ll
[18:10] loop it should be able to log with
[18:14] different levels so let’s try that
[18:17] so we’re gonna log multi-level I have
[18:23] ever pulled our system please keep
[18:44] scrolling up and down sorry
[18:51] it’s delay a warning notice in ferry and
[19:01] a debug
[19:16] so that should hopefully log five
[19:21] different messages to pay the trail with
[19:24] the correct logging level let’s see what
[19:27] actually comes out so doesn’t like
[19:33] bytecode I think I’ve just forgotten how
[19:38] to do genomes one of the slightly
[19:45] frustrating things with the Arduino IDE
[19:48] is that it doesn’t do very good for to
[19:51] complete so when you’re building a large
[19:55] application it can be quite difficult so
[20:00] we have a problem with C so double lot
[20:09] of problems was the first one so it’s
[20:13] complaining in apologies of non-static
[20:16] remember function that needs to be
[20:21] length with brackets in fact X is that
[20:24] color getting closer said still seems a
[20:32] lot of errors
[20:42] I think it doesn’t like my constant
[20:46] strings so let’s just remember this and
[20:53] come back to that later okay so it looks
[21:03] like that was all of our errors let’s
[21:06] push this to the device and we’ll see
[21:08] what comes out
[21:14] okay so considering our serial monitor
[21:19] we ran up and running again let’s have a
[21:21] look on the trail and see you’re getting
[21:25] some messages through that without
[21:26] tagged correctly so go back to our
[21:29] dashboard can now see we have a new
[21:33] system or to paint the trail test so no
[21:37] longer getting an IP address we actually
[21:39] get the name of the system that’s doing
[21:41] the looking click through we can see we
[21:45] now have these nice colored labels to
[21:48] tell us what the actual message is a
[21:50] warning notice info debug and so that’s
[21:54] looking looking pretty cool we now have
[21:58] looking doing some our device to the
[22:01] Internet to this centralized lobbying
[22:04] system so like in the code quite nice if
[22:10] our learning system supported things
[22:12] like print line and printer then we
[22:16] could do similar logging into this but
[22:19] out to the Internet now what you can do
[22:23] is I kind of look at the implementation
[22:26] of serial so you bring in some code so
[22:33] what you can see is if we trace the
[22:36] definition of serial you can see that it
[22:40] implements this class called stream and
[22:43] if you dig into stream you can see it
[22:47] implements this class called print and
[22:50] then if you look on print we can
[22:53] actually see all the methods we actually
[22:55] need thin line and printf and the only
[22:59] method we actually need to implement is
[23:02] this right method that takes a single
[23:04] character so what we can do is we can
[23:08] also implement the print class override
[23:14] this method and then we have one of
[23:16] these nice print functions on our own
[23:18] class
[23:19] so going back to our code let’s next to
[23:23] what we’ve got here into a class that’s
[23:30] deranged logger class and let’s start
[23:39] putting this in a slightly more Oh way
[23:44] so actually it’s a paper trail logger to
[23:49] make sure we’re clear what we’re
[23:51] actually doing and while we’re here we
[23:55] over won’t implement the print last
[24:02] [Music]
[24:05] instructor so let’s take in let’s pull
[24:13] out these two parameters so we’ll have a
[24:17] host port copy across the user
[24:25] initialization parameters and I think
[24:30] mission all set probably you need to
[24:31] wrap up the Wi-Fi UDP into our class as
[24:35] well so let’s have that as a driver
[24:40] remember variable and well constructed
[24:43] as part of our login construction now I
[24:53] think I’m going to move the log level
[24:56] the context and the system name into
[25:00] this constructor as well so what I’m
[25:03] thinking is we could have a set flip the
[25:06] logo for each
[25:08] type of message we want to look so let’s
[25:11] do that as well so it will compete these
[25:15] things until our constructor paddle out
[25:17] I will make these private many variables
[25:26] so that’s copied across all of these
[25:29] values and now I just need to implement
[25:33] that like method from print and
[25:37] hopefully if we’ve done it correctly we
[25:41] should be able to log messages so it’s
[25:44] an interesting thing with this write
[25:46] method so it takes one character at a
[25:49] time so what I’m gonna do is just create
[25:54] a buffer to buffer up the characters as
[25:58] they come in and then we’ll write them
[26:01] out when there’s a new line so that’s
[26:04] great buffer I’m not really sure how big
[26:07] to make this that’s the best seeing
[26:10] we’re not cuz we’re not in huge messages
[26:15] yeah we are maybe that’s a sign we’re
[26:19] doing something wrong and we’ll just
[26:23] have to keep track so where we are in
[26:27] our buffer to check we don’t over love
[26:29] and when we visualize that you’re not a
[26:31] constructor to zero so that’s that’s
[26:36] what this vine method so the very
[26:40] signature there was sized he write that
[26:43] takes and you wind it and accepts a
[26:49] single character so what do we need to
[26:54] do I think we should probably detect the
[26:58] end of a line so if you get a line feed
[27:04] and I think we probably check if we run
[27:07] out of space or if we’re running out of
[27:09] space so I said we hit done with 99
[27:14] characters then we’re gonna let our
[27:17] specialist put that into where
[27:20] to accent as well so they’re motivated
[27:29] Buffy sighs - what
[27:32] so we’re gonna send the contents of our
[27:36] buffer off - paper trail and so let’s
[27:40] made sure buffer is no terminated so we
[27:48] don’t run off the end so just write a
[27:53] zero into the last proper position now
[27:56] we can construct past this lock message
[27:59] to send off to paper trails that’s a
[28:03] copy this and what do we want to do
[28:08] we’re gonna begin a packet through our
[28:12] hosts the actual syslog message facility
[28:21] we can just keep with our hard coded
[28:24] value that we will now be the level the
[28:26] looker was constructed with a system is
[28:30] now a system and context and this will
[28:36] be buffer remaini to back that in a
[28:40] string so the petition works so then to
[28:45] write a packet and we’re just offering
[28:51] up characters then we just need to store
[28:55] this new character you know a buffer and
[29:00] increment the position so that should be
[29:08] a paper trail log of down that’s just
[29:11] compiled check for any syntax errors so
[29:16] we have some problems doesn’t like this
[29:22] watered-down one you it
[29:26] right back home I think that might be
[29:30] the source of all of our fellas
[29:34] it’s our next problem there’s a
[29:42] constructor with married to it okay be
[29:47] mixing your minor JavaScript with my C++
[29:51] that should fix that problem
[29:54] hey what’s next
[29:58] it’s not Milan Paris mr. Carroll on
[30:06] Wi-Fi needy people is not very good
[30:09] that’s because I just called it okay and
[30:15] Wi-Fi UDP this less has changed four of
[30:18] these very correct
[30:21] I think say we have the problem with
[30:26] tracking castmate a normal string on to
[30:29] the end of this vest just left this in a
[30:35] string and then we’ll try and compile
[30:42] again okay almost there
[30:46] I’m just left in my older code so we can
[30:50] visit that and now let’s use let’s
[30:55] create on actually longest so we have
[30:58] our boolean class so let’s do over log
[31:06] just copy this like I just go up and
[31:12] down all right
[31:14] even further down we’ll do a warning
[31:27] notice and debug and then in our setup
[31:41] we’ll just create those so these new
[31:50] picture look at that would take the host
[31:54] and the port the block level the system
[31:57] another context so let’s construct these
[32:04] so our host
[32:05] it’s our paper trail host looks like
[32:10] deleted so let’s go back to you print
[32:12] the trail July dashboard let’s just
[32:16] check what our host and port are
[32:19] supposed to be so here’s our host port X
[32:32] is the overlock so it’s a lot of level
[32:36] ever and our system record it paper
[32:46] trail test so that’s not a locker that’s
[33:01] create the rest of them so warning
[33:09] notice debug info and now in our loop we
[33:19] should be able to use these individual
[33:21] loggers where the print line so let’s
[33:24] change this to
[33:26] [Music]
[33:29] - infos do this for different levels
[33:36] sighs x2a analog a warning look their
[33:49] test log debug log and then in fairy
[34:02] look we’re just like these messages
[34:11] different circuits so let’s see if this
[34:17] compiles and runs so have some areas
[34:25] similar software there are also various
[34:33] so it doesn’t like using and post for
[34:38] the port so that needs to be seen string
[34:46] so let’s try that see what areas we get
[34:52] okay so it doesn’t like using you
[34:56] intimated to you construct string so
[35:01] it’s nice quick cast just to make that
[35:05] work it will compile that again so it’s
[35:13] complaining about specifying and string
[35:17] for the port number which should be same
[35:19] number but I think we’re getting pretty
[35:26] close
[35:26] hopefully this should be the last couple
[35:29] of compilation Harris okay so let’s move
[35:33] up this and see if it works and sends
[35:37] messages to all trail
[35:42] okay so we’re up and bunny there’s no
[35:46] looking coming out for long Duke because
[35:48] the shoot now we’ll be going to paper
[35:51] trail so let’s check and see if I love
[35:54] messages about coming three so no sign
[36:01] of any messages yet which is because
[36:10] I’ve slightly mistyped the system name
[36:14] so in my first test I missed out of the
[36:18] bar so now we haven’t used system so
[36:21] let’s see the blocks are coming through
[36:23] and there we go so I can see one thing
[36:28] that’s missing is ICG missing a carriage
[36:32] return in my box so let’s see what we’ve
[36:36] done wrong yeah
[36:54] so I think what’s happening is we’re not
[36:58] picking up the cash return so we’re just
[37:01] hitting the end of our buffer and then
[37:03] vlogging so that’s kind of curious
[37:06] because we should be getting a carriage
[37:10] return so I’m going to try and debug
[37:13] this and see what’s going on I can see
[37:21] the problem immediately
[37:23] we’re not resetting a buffer position
[37:27] once we’ve actually a sent a message we
[37:30] should set our position back to zero so
[37:33] we start a new message so let’s fix that
[37:36] and upload it and see if it works okay
[37:42] so looking you want a paper trail you
[37:48] can see that our messages are now
[37:51] working properly
[37:53] so they should have rebooted man can see
[37:57] you’re leaping up and we have to click
[37:59] messages coming through and they’re
[38:01] tagged with the correct severity level
[38:05] the last thing I want to do is have it
[38:09] said the messages still come out in the
[38:11] serial monitor and also like to make it
[38:14] a bit clearer different levels of
[38:17] messaging so one thing you can do in
[38:20] paper trail is you can color messages
[38:22] using an see color codes so these are
[38:28] kind of escape codes then you can
[38:30] descend that will instruct some systems
[38:33] to change the colors of your text now
[38:37] it’s a bit of a shame the RGB no serial
[38:40] terminal doesn’t support these clothes
[38:43] so known again but I can’t wait to come
[38:44] out in here but if you connect using
[38:47] putty or some other terminal system then
[38:51] you will get colors in your locks but
[38:53] let’s quickly fix some of the issues
[38:58] also send messages to the serial port so
[39:02] just do a serial print line and set and
[39:06] buffered and then let’s have simple
[39:09] colors as well
[39:10] so people just pass those in to the
[39:13] constructor will have a color and then
[39:26] here we can just add our color escape
[39:31] code just before our message so most
[39:38] prints and colors into our constructors
[39:41] so I think we’ll have this red for ever
[39:50] so I put the color just out of the lock
[39:54] level so you can clearly escape sequence
[40:03] and then the color so let’s do that all
[40:09] three of our message types and then
[40:13] we’ll pick some nice colors so maybe for
[40:15] info we can have blue for debug we can
[40:23] have let’s say green from notices maybe
[40:31] you can have silent after warning
[40:36] because I think probably yellow I think
[40:41] I think that should be it just knock the
[40:44] formatting so let’s run this and we
[40:51] should have nicely colored messages
[40:55] coming up you know I’ll pick the trail
[40:56] log okay smear some errors so steel also
[41:05] doesn’t like converting from a
[41:09] you’re in place so that’s a quick cast
[41:15] there so our sketches compiled let’s
[41:22] check the serial monitor to see if BAM
[41:26] we’re now getting Lester’s coming
[41:28] through
[41:28] so yeah it’s looking into the sealer
[41:30] what if sir
[41:31] and I’ll take the trail let’s see if our
[41:35] colors work so there we go we have a
[41:38] multicolored logging say before you kind
[41:44] of use this code in production
[41:46] I’d recommend putting out the paper
[41:48] trail bloggers to a separate file so you
[41:52] can include it without get included all
[41:54] the code I hope this was a useful
[41:58] demonstration of how to log to a remote
[42:02] system paper trail is just one of many
[42:07] different remote logging systems so feel
[42:10] free to explore some of the alternatives
[42:13] and I use paper trail because it’s been
[42:16] simple to set up and it’s a simple way
[42:20] of sending messages using UDP so it was
[42:24] interesting and if it was you see the
[42:28] subscribe button and the revolver here
[42:31] is coming and subscribing is the best
[42:33] way to get notified when there’s new
[42:37] content to look at all this code is on
[42:40] github the repo is in the description
[42:43] thanks for watching and I’ll see you in
[42:46] the next video

HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening

> Image


A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts