GET Weather data with your ESP32 for FREE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you want to learn how to measure and plot  temperatures build wireless radio controllers   use servos to drive tools or build weird things  like I don't know a thermal camera here you'll   learn mechatronics in minutes if you've ever  wanted to use your esp32 to get information   from a website and display it I'm going to show  you how the example that we're going to use is   a simple weather station I'm using data right  from the National Weather Service and it gives   me things like the day the temperature and the  weather conditions I'm also getting the time and   date directly from the web as well I'm able to  get up to seven days but because my display is   a little short I'm having to scroll through each  of the individual days if you're wondering why   the temperatures are in a different color that's  because they're a different temperature I wrote   a small function to map color to temperature so  if it's cold you'll see blue if it's hot you'll   see red I wasn't sure how to get web data  so I did a quick Google search just typed   in National Weather Service API and there you  go there is an API for this particular company   if I took a look at the pricing free to use  for any purpose and is a public service of   the United States government so my tax dollars  hard at work if you're in another country you   might have a similar service Google that and find  out also I see the API is based on Json which is   a pretty familiar term to me it's basically data  organized in a very specific format I'm going to   go to the general use questions page and learn  a little bit more they have a general FAQ and   a grid point this particular web page how they  operate is you specify a latitude and a longitude   and it will spit back a a series of data for you  I've already created this URL so I'm going to go   ahead and paste it into my web browser and within  a couple of milliseconds I get a Json string back   you can tell by the brace and some keyword terms  here down underneath the properties you will find   the grid ID the grid X and the grid y that is the  information that you're going to need to know to   build your next string to get actual information  there's not a whole lot of data in this entire   Json string it does tell you city and state but  not a lot of useful information I'm going to use   that information to build my next string they've  also done that for you under the forecast notice   how they've given you the exact format of what the  grid ID the grid X and the grid y must look like   so we'll copy that to the clipboard we'll create  a new page and we'll just simply paste it right in   and within a split second you get a whole  series of data for this particular URL   under the properties we get periods which there  is an array of them and I believe there is 14.   elements seven days a week and you get this  afternoon tonight tomorrow and Friday night and   you're going to get things like the temperature  the unit of measure the wind direction the short   forecast and a few other interesting things if we  go back to that previous page there's another URL   for hourly forecasts so if we copy that to the  clipboard paste this in and you know the drill   by now we're going to get within a split second a  very very large Json string of tons of information   this is hourly so this is from three to four  four to five five to six same information the   temperature the degree of the unit of measure wind  speed and a short forecast in so on and so forth   unfortunately I was not able to use this  particular Json string it's just too big   the person that wrote the Wi-Fi Library I wrote  it string based so it is character limited to 65   000 characters thanks for all your hard work  but I wish it were charged we could make it a   little bit bigger but don't fret this particular  site here has got plenty of information on it if   we take a look at our display screen and notice  how I have the um the day the weather condition   and the temperature all listed I could also put  wooden speed on there if I wanted to but this is   something you can play around with all right now  let's take a look at the code like I mentioned   earlier I'm using esp32 and I'm using the Arduino  IDE to program bmcu my display is connected right   to my esp32 and there's the pin the information  that I'm using if you're not familiar with how to   get a display working there are plenty of YouTube  videos to get an esp32 working with the display   I'm using the Adafruit gfx and ili 9341 libraries  I know how to get them to work and that's the ones   that I use I'm using the Wi-Fi web server and  HTTP client libraries and if you're if you want   to get the ones that I'm using just copy that Json  string you know here we go again Json strings to   your clipboard and paste it right into your file  preferences under the additional boards manager   and you'll be able to download the exact library  that I'm using I'm using a time.h for helping me   get time from the internet in an elapsed to  milliseconds which is a great tool for doing   automatic sort of uh every five seconds update  this or every two hours update that it does the   counting for you automatically I don't know how  to rip apart a Json string but this Library does   and if you know me I've got to have pretty fonts  and all of my displays and projects with displays   I'm using a flicker free print library to keep  the text from flickering every time I update it   and the location I am in is six hours  of GMT hence the offset that I'm using   you're going to have to key in your home SSID  and password and your latitude and longitude   wherever you happen to be in the United States  here are the base URLs that I'm using for the   site points the day forecast and the ntp time  server I went a little over the top and if you   look at the display you see the temperatures in  different colors hot is red and blue is cold and   I've got a neat little function that Maps  temperature to color pin defines as you can   imagine and ways that I manage my fonts I have a  Define for each of the font sizes and I use that   a little later on the code bunch of defines for  green blue light pink and on so on and so forth   and some variables for the temperature forecast  time and a few other things all this is pretty   standard stuff for creating objects to  connect to a web server Json documents   objects as well and a few other things and of  course my elapsed counters connect to the internet   as you would normally do with the begin method and  if you want you can print the status of your Wi-Fi   to get the actual IP address use the config time  with the ntp server so you can actually get that   all working and then I get the internet time  then optionally you can print the local time   First Step that you've got to do is you've got  to get your grid you know I don't know what my   grid is and you won't either but you can easily  find your latitude and longitude you key that   in and this get grid will get the grid X and Y  for you get the day forecast then display the   header and display the data pretty simple stuff  then in a loop every second I update the time   every day I go back to the internet and  double check the time to make sure I have   the correct time every five seconds or so  I update my scrolling capability so you can   see each of these seven days display when  I only have enough room for uh three rows   every hour I'm going to check the forecast again  to make sure things haven't changed or just get   the updated data if they have changed I'm going to  just blank the screen out and redisplay everything   here's where I'm getting my internet time  there's a function called get local time   you remember we set the configuration earlier  so all I have to do is pass in a struct and   it's going to give me things like the uh the  hour the minute and a few other things back   here's the value of uh the elapped second  counter that I'm using I'm actually   um the current time is actually an elapsed counter  so I'm going to uh uh tear apart the hour and the   minute and build a uh unsigned long and I'm  going to assign that right to the current time   and that Library will automatically increment  that for me so I don't need to worry about   getting the milliseconds getting the rollover  it just does it then every uh 24 hours I go   back I get the internet time and I simply build  that current time again then I let the esp32   manage the time with this internal  counter and that elapsed time Library   display a header pretty basic stuff here just get  the second minute hour build a buffer using the   S printf and display it using my time or Flickr  free print Library so you can see a nice uh nice   smooth update then draw the date and anything else  that that you might want then display the data you   know this is where you can get real creative  and do whatever you want to do do you want to   show icons do you want to show text you want to  show colors or or whatever I made it real simple   I just draw five rows and populate all of that  information in there every iteration I bump the   row up by one so you can see how this thing kind  of Scrolls through and all I'm printing is the   um the time the forecast which is actually the  uh Monday Monday night or whatever and then the   actual temp itself and here's something a little  bit over the top here I'm using the get color to   convert the uh the degree F temperature to degrees  C and it's going to give me the actual five six   five color back and I can use that in the set text  color method to go to display the correct color   print local time and Wi-Fi status I'm going to  skip those two things he can read over that get   day forecast and the get grid are the two real  heavy um lifting sort of functions in here the   get grid will start there first do we have a Wi-Fi  connection if we do then we're going to create a   HTTP client I'll set timeout to six seconds and  you don't need anything that long and I just do   that that is a safeguard start you with your  weather server path and build in your complete   path with the latitude and longitude and then  simply pass that right into the begin statement   it's going to give you a response code your  typical 404 if the web page wasn't found and uh so   on and so forth I've found this site to be pretty  reliable I usually get a 200 back I am not error   checking that but I should be that's something  you could you could do and then you get your   Json string and I'm putting that into a very large  string here and I'm using a deserialized function   on the uh the Json string object and here's where  all the fun can begin this magical function right   here takes that wild looking string that we looked  at earlier and It prepares it so you can ask it   simple question like go to the properties and  get the grid ID and we'll take you right to the   properties the grid ID and give you that lot or  whatever that data happens to be same thing for   properties and grid X it's going to return 75  properties for a good grid y it will give you   72. then we're going to build a day forecast path  so we can feed that up into another call where I   create an HTTP client we set the timeout and I I  zero everything out just in case I couldn't get to   the web page because sometimes it's down check for  web connection pass in the day forecast get the   begin get your response code and here I do some  very very basic checking that if it's above 200   the service is unavailable just return and because  I've zero everything out up here I don't have to   worry about a bunch of null data or bad data or  old data then we move on to the next thing where   we get the string for the Json and here I'm just  printing it out so you can see it and I'll let   you run the code to see what that actually looks  like which will be just like this over here by   the way then we're going to deserialize that  so I can ask it questions like the properties   uh the period the iteration the name which will  be Saturday in this particular Row the temperature   which will be right down here 40 and the short  forecast will be mostly cloudy so the library does   a really good job of uh of taking this Json string  and sucking all the relative relevant information   that you need out of it last but not least I  close the HTTP object so I can open it up for   um running something else uh get grid we just  talked about that get color a real neat little   function that I wrote to uh you send in the temp  c and it will give you the RGB value back for   your color 565 monitor well that's it hopefully  you learned something and uh I had a good time   working on this project I learned a lot about how  to get data out of web pages or web servers and   uh use Json decomposition to get information out  of it maybe I'll work on another one like how do   you get stock quotes well that covers this week's  lesson if you learned something give me a thumbs   up if you have a tip to share leave a comment  so my Subs can learn more thanks for watching
Info
Channel: Kris Kasprzak
Views: 5,806
Rating: undefined out of 5
Keywords: ESP32, Arduino, IOT, web site hacking, web server, get internet data, JSON, hack, GET, parse, get web data, weather data, national weather service
Id: xUrhLaviBrI
Channel Id: undefined
Length: 13min 32sec (812 seconds)
Published: Fri Dec 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.