We have learnt to send data from Arduino to the Internet, but if we think about the task which we had set ourselves for the realization of the WaterMe program, te also need to run the poliv command from the Internet. We don’t know how to do this yet, but we are going to learn it now. I am going to change the sample sketch a bit by adding several lines to it. Firstly, we are going to have a test LED, which will be turned on following the command from the network. Secondly, I am going to create an object of the String type. It’ll be a line, i.e. a set of symbols, with which we will be able to perform various actions. You are going to find out now why we might need it. The rest will be carried out in the same manner. I have set the LED pin as our output, [PAUSE] and then, after we start receiving the request from the client, I will add the received symbol to the object of the String type, which I’m going to call “request”. We are going to enlarge it in order to find the section which is responsible for the command issued from the website. I’ve also transformed this line a bit, as it was outputting a slightly incorrect heading which made the browser send a new request every second and refresh. I have added the address which needs to be requested, this is the same address that we used before. Why it’s important, you are going to find out a bit later. I have left everything unchanged here, and then, after the data has been output from the analog input, I have added these four lines. One of them is the transmitted tag of the line break. The next transmitted tag is the one which creates a hyperlink, i.e. a link that redirects us to the Internet. That’s what it looks like. Tag A is responsible for the address, which we are going to be using later. In our case, I’ve just added symbols ?on to our address. Between the opening and the closing tags, it’s written “on”. This is the link text which we can see and which we
need to click on. Next we insert a space, and after the space, there will be one more link where it’s written “off”. It’ll redirect us to the /?off address. These slashes have been flickering in your eyes too much recently,
I know. Let me explain to you why we need this slash in particular or, for instance,
that one. Take a look at the line which we are sending to the client - it is written in quotation marks. Whatever we find inside these quotation marks will be the line itself. That’s how we do it in coding. If we need to transit the quotation marks too as part of the line, we need to inform the compiler somehow that this is not the end. That this quotation mark is now the end of the line, but it’s part of the line. That’s why we put a “/” before it. In other words, this is also a sequence, just like the line break and the tabs, but it also contains a quotation mark. We need this to make quotation marks be considered not only as the opening and
the closing of the line, but also as the content of this line. This section is added on the page. After we’ve terminated the connection with the client, we shall see what information has been saved in the “request” line, where we were adding symbol by symbol while we were receiving the client’s
request. Let’s look for the “?on” substring in the “request” What is this “?on” exactly? We’ve just added a link to the web page markup, which redirects us to the ?on address. Therefore, when the user clicks on this link, as it has been done in our request received by the server, they will see this ?on substring. If we find it, we will perform this action. However, if we find a different substring, the “?off” substring, then we are going
to perform a different action. As you can see, this line is responsible for turning the
LED on and off. Now we can check how this works in practice. I also promised to tell you why we needed to refresh the page, but I’m going to mention this in the next fragment, really. For now, we are going to check the command sent from the Internet. I have connected an LED to our test device, which will be turned on following the Internet command. I have uploaded
a remastered sketch, and now we can see that our web page looks a bit different. In fact, we have two new links now: On and Off, so the page is refreshed
more often now, precisely every second. This is because we have changed this mysterious heading. Let’s see what happens when I press On. We can see that the LED is now on. When I press Off, it turns off. I’m going to repeat this several times. I can still rotate the potentiometer and change our value. Look, I’m going to highlight the section in the location bar, which you need to pay attention to. I am clicking on the On link, and for one single second, /?on has been added
to the address. Same thing will happen if I press Off. We need this mysterious heading, refresh1url=http 192.168.0.150, to be redirected by clicking just on the address of the device after sending the command, without all these slashes, or on and off. We have now learnt to both transmit data from Arduino to the Internet and send commands from the Internet to Arduino. In reality, this trick with “refresh” is not the best way to achieve the desired results. However, if you are interested in this subject and would like to work on it, you’ll definitely find a better way to execute this. Let’s now return to the design of our WaterMe program. And look again at our diagram, where everything has already been made clear for us. We know how to send data from Arduino to the Internet and receive commands from the Internet, so we can add the depiction of the router to the diagram and the Internet, from where we get our requests and where we send our replies. Now we are almost ready for the realization of our project, but before that, let’s discuss how to organize the code of our device.