Connect Arduino to internet over USB (with Processing) – Simple Space Weather forecast meter example
Okay, i guess ill need some type of wi fi shield or a wi fi enabled board in order to connect to the internet. But what? If youre, building something thats? More of a desktop project – that is, you fully anticipate that its going to be sitting right next to your computer, and if you want this thing to have internet access like does it really have to connect over wi fi? So then your next thought might be well. If its not going to be wi fi, i probably need to get an ethernet shield, or something like that. So i can connect my arduino directly into the ethernet, but there is another option that doesnt require wi, fi or ethernet its just connecting your arduino via usb. To your computer, assuming that your computers connected to the internet, what youre able to do is use the arduino serial library to communicate with an application on your computer that manages your internet requests. So if you go this route, all you need is an arduino board and a usb cable and you can connect to the internet. No other hardware needed all you have to do is install some open source software called processing and write a small program to grab the information that you want from the internet. Now, what were going to do in this lesson is walk through that process. From start to finish, so by the time youre done here, you will be able to get information from the internet down to your arduino board over a usb connection to your computer now to demonstrate getting some information off the internet, we need some type of example.
So what ive set up and what well demonstrate in this lesson is grabbing some space weather forecast information from the noaa website, thats the national oceanic and atmospheric administration website. So our program is going to grab some information, its going to be called the planetary k index. Its a measure of the magnitude of geomagnetic storms, its just a number, its a number from 0 to 9., were going to grab the current forecasted number from the internet and then were going to pass that number down to our arduino board. And then our arduino is gon na light up a number of leds based on that number. So if the forecast, if the kp forecast is five, then were gon na light up five leds, if its eight well light up eight leds, i think you get the idea. So by the time were done here were going to have a super simple space weather forecast, widget sitting next to your computer. All right lets get started Music subscribe to our youtube channel to get more videos like this. Before we start just a big shout out to ultim for sponsoring this video huge thanks, you can check the description to get a free trial of the ultim software all right. So this is what were going to do. We want a computer program on our computer to grab specific data from the internet and then pass that data over the usb cable to our attached arduino board.
And then we want our arduino board to be running a program that keeps an eye out for incoming serial data and when it gets that data were gon na, have it do something with it and in this case, were gon na light up some leds. So that sounds like a lot, but thanks to open source libraries, its not gon na, be all that crazy. Now, when im working on a project like this theres two principles, i like to keep close to my heart, the first one is, i always like to start with just a simple plan. The second thing is, i always like to look for code examples to kind of get me started so first lets go ahead and write out a simple plan of what we need to do. I want to connect to the internet with some program on my computer. I want to send data from that program to my arduino. I want to have my arduino read in data from that program, and then i want to light up some leds on my arduino. Based on that data. The first thing im going to try and figure out, is how to write a program that sits on my computer and sends data to my arduino. So what im going to do is use a programming language called processing to do this now you can download processing for free its open source software, just like the arduino, so im going to go ahead and download the processing.
What were downloading here is an ide, so an integrated development environment. So when you download arduino and you install it same idea, you know youre, downloading and installing an ide and what im doing is downloading and installing the processing ide. So its really simple to download and install do you need a printed circuit board design software to move your prototype to the next level? All team designer is a great choice for designing pcbs, sharing your design with team members and even getting your design manufactured. What really kind of blows me away about this software is that, even though its a super powerful tool at the same time, its really intuitive to use theyve got helpful. Video tutorials built right into the software, so you can kick start your learning process and actually get something made right now you can get a free trial to ultim designer, with our link in the description, thats right, you can test drive this super powerful software with the Free trial just check out the link in the description. Now, when you open up processing, you are going to notice that it looks a lot like the arduino, ide and thats, because the arduino ide was actually based on the processing ide all right. So here we are in the processing ide and what i want to do is open up an example sketch. So if you go up to file – and you go to examples – theyve got a bunch of different examples in here, and the one were going to use is from their serial library here.
It is right here and you can play around with any of these, but were going to play around with the simple right. I just want to write a program in here. I want to use a program in here thats going to send a piece of data to my arduino. So let me click on this, and here is this sketch right here so whats cool about this sketch? Is that not only does it have the processing code in here thats going to run on the computer, but it also has the code that runs on the arduino. So what im going to do is im going to copy this code right here and im going to paste it in my arduino ide ill. Get rid of these comments here. Do some formatting now on my arduino im, going to have an led attached to pin 6 so im just going to change that, but otherwise it should be the same ill. Make sure to save this and then im going to make sure my arduino board is connected. So arduino uno yep there it is the port all right, com3 arduino uno. I just want to take note of that all right, com3 arduino uno, and let me just upload the sketch to my arduino now im using the kit on a shield on top of my arduino. So i dont actually have the like. I dont have a breadboard wiring up the led ive just got the kit on a shield there, but its just going to have an led of pin six and all this sketch is doing.
Is it sets the mode of the pin as an output so pin? Six is going to be an output start serial communication using a baud rate of 9600 and then down in the loop, its got a while loop inside the loop, and it says hey if anythings available in the serial buffer, then we want to read that value. So serial.available is going to return either true or false, based on whether or not theres information in the buffer. So if its true read the value from the serial buffer and store it in a variable called val, and that value is either going to be an h or it wont be anything if its an h, if you get a value, then the led is going to Turn on well do digital right high, so the well turn the led on otherwise were just going to turn the led off and then theres going to be a delay of 100 milliseconds thats it so right here. This line 14 is whats. Looking for data coming into the serial buffer, this is whats allowing the arduino to sit there and wait for information to come in and respond when information does come in all right. So lets take note of that, so it was com3. Is the port were using and weve already got the arduino? They gave us the sketch to set it up to look for information coming to the serial port. Now were going to come over to this processing sketch now im not trying to you know this isnt going to be a lesson on teaching you all the ins and outs of processing, because one i couldnt do it.
I dont know processing all that well, but i know it enough to kind of, like you know, be dangerous, so lets just start at the top here. Basically, what the sketch is doing is its importing the serial library from processing and then it creates a serial object. This serial object is what allows processing to talk to the arduino okay. Now, in this case, we are calling the serial object, my port, so thats, what theyre, using here my port. Now i like to call it like arduino, but you can call it my port doesnt really matter now. Theyve also got another integer called val here. Im not sure were actually going to use that in here, are we using val? No, i dont even think they use val in this program, but i think thats a carryover from another example. They have all right so processing, just like arduino, has two very important functions. So one is called setup just like arduino setup. It runs once and then the other one is called draw so draw is to processing what loop is to arduino, so uh processing is a visual arts. Software, like the idea, is that you use this software to make like these visual, like graphics, i guess is a way to put it uh or interactions. I dont know its really pretty cool ive played around with it a bit its been a while, but its super fun to work with. Anyhow, what happens is every time you hit this play button.
A little app opens up on the computer and like theres a little window that opens up here ill just hit play here. Real, quick here and youll see this little window pop up youll see this little window. This is like, where stuff happens right inside your inside. Your app so like visual stuff would happen here, but the size of that window is determined by the size function, so thats what this size is doing now. This is where were interested down here, line 21 and 22. all right, so this is probably going to be the toughest part of this entire thing right, because its just a little bit confusing. So we use this function called serial.list. All right. Serial list is going to return an array, so an array is just a list of things in this case its going to turn return an array of all the serial ports on your computer, so theres going to be a list starting at 1 or starting at 0. Going down to whatever of all the serial ports now right now we have 0 selected okay. So this is saying whatever the first element in this serial list array is, we are going to set it equal to port name, okay, so basically the port. We want to associate with this serial object up here. We are saying its the first element in this array, but we dont know that yet right like this is just kind of a default setting.
So what we need to do, though, to figure out which serial port our arduino is attached to on this list? Is we actually have to print this out to kind of get an idea of where it is so so im going to use the print array function to print off serial dot, the list of serial ports? So let me do that Music all right. So now, when i hit play down here, i should see a list of all the serial ports attached to my computer. So here there is only one serial port, its com3, so thats the arduino have attached. Now it might be, you might be like wow. Why did you go through the trouble of printing it off on most computers, or at least on other computers? Ive used theres a huge list of different ports available right. This one happens to be at zero. Okay. So this is gon na work ill just set this to zero and uh. That should work fine. But if its not lets say com lets see, there was like a com, one, a com2, a com 20, whatever wherever com3 was whatever value in that associated array. Thats. What i would want to set line 22 equal to, if that makes sense, all right, so this line 21 weve got a string, were calling it port name and were setting it equal to the zeroth element in this list of serial ports right now, theres only one Serial port in the list so its going to be set at zero.
All right, then, what we do is we initialize that serial object all right and it takes three parameters, this port name, which is the name of the port right. So you got to put the port that were going to have it look at or send to right there and then the baud rate, so thats got to match up whatever youre using in your arduino ide. You need to use here so were going to be using 9600 as the baud. So i know this syntax is a little bit different were using a its like the serial constructor to create a new serial object, so thats a little weird. But you know if you just kind of use the example code, it kind of draws it out there. So what this is going to do, and this this isnt too important – i think the most important thing i want to focus on here down here is: what is this line of code right here, where it says my port dot right? If we want to send something? Serially over the arduino to the serial port that the arduino is monitoring. We use the dot write function, so my port dot. Right again, my port is a serial object that we created up here right. So my port is a serial object, and so when we say myport.right right is a function of that serial object, and so whatever we write here, thats whats going to get sent over now.
In this case, what happens is, let me go ahead and hit play here. Is this opens up a little square so with this program, when my mouse is over the square, the square changes color and it sends an h to the arduino, and so when the arduino gets the h, it turns on the led and then when im off the Square, it sends an l and then that tells the arduino hey dont, write anything, so you can turn the led on and off. You know like this, which is kind of fun to do really simple. You know what i mean, but again the basic concept here is: we need to use this myport.right okay. So again, this is just waiting here. We are in the arduino. Ide were waiting for some information once it comes in, we check hey. What is the information based on the information we do something in this case you know, were turning an led on or off. Okay, so now were armed with this basic information, but now what we want to do, instead of sending like an h or an l based on like hey what you know, some square mouse over square. What we want to do is grab information from the internet and send that to the arduino okay. So how are we going to do that all right? Well, what were going to do is were going to reach out and connect to an api, so that is an application programming interface, youve, probably heard of apis before at least in passing, but basically its a way for organizations or companies or whatever to expose information in A database so that you can use it, you know publicly, so you might think like google maps right, google maps has an api so that other applications can basically point to that api or grab from that api and then implement those google maps inside their app or Think of weather right, like theres all different types of applications out there that show you weather but theyre, all probably getting their information from you know: different weather apis out there theyre actually pulling down the actual forecast from some api like noaa, for example, so thats.