HTTP client

Since Raspberry Pi acts as our client, we will write our client code in Raspberry Pi itself. Let's install Node.js in our Raspberry Pi. At the end, we will have Node.js up and running. Here, we will SSH into Pi using PuTTY:

  1. Open the PuTTY terminal on laptop/desktop.
  2. Include the IP address, which your router has provided to your Pi, as the Host Name (IP address) and click on Open, as shown in Figure 3.4:

Figure 3.4
  1. If you are connecting for the first time, a security warning will appear. Just click Yes as shown in Figure 3.5, as you are connecting to your own Pi and not a public computer that may harm your system:
Figure 3.5
  1. Once you have connected to Pi successfully, you will see the login screen shown in Figure 3.6. Log in using the username pi and password raspberry (if you provided a different username/password while setting up, then use those):
Figure 3.6
  1. Since we have successfully logged in through SSH, let's install Node.js now. Create a folder, nodeDownload, using the following command:
mkdir nodeDownload
  1. Now move into the newly created folder, using the cd command:
cd nodeDownload/
  1. Run the command in the same order:
curl -sL http://deb.nodesource.com/setup_8.x | sudo -E bash
sudo apt-get -y install nodejs
  1. To check that Node.js has successfully installed, use the version command. It will give you the version of node installed:
node -v
  1. Open the terminal window and create a folder under the /home/pi directory with the name piclient, or you can give it any name of your choice. Use this command to create the folder:
mkdir piclient
  1. Before we get into writing our client code, we will repeat step 6 of the previous topic, HTTP server, to make sure we have the node module installed and the package.json file at the local folder level.
  2. In the terminal window, execute the following command, which will create a file with the name client.js and open it for editing. Here we use the nano editor for creating and editing our file:
sudo nano client.js
  1. Let's start with our client code now:
const Client = require('node-rest-client').Client;
var client = new Client();
setInterval(function () {
var sendReq= client.get("http://IP_ADDRESS_OF_HTTP_SERVER : 8080/get/information", function (data) {
console.log("response from server"+data);
});
sendReq.on('error', function (err) {
console.log('request error', err);
});
},5000);
  1. In the client code, we import the node-rest-client module, which will provide the functionality to send HTTP requests to our HTTP server using methods such as GET and POST.
  2. Then, we create a new object for the client to send the request.
  1. Use the setInterval function of Node.js, which is used to send the request every five seconds.
  2. Using a callback function, we send the request to the server by providing the complete URL, which includes the public IP address of the server and the path/endpoint. This callback function returns the response from the server as the data variable, which we print on the console.
  3. In case of an error, such as the server not responding and other such issues, we have done error handling on the response from the server.
  4. Now, before we run our code, we execute this command in the terminal window to make sure we have the node-rest-client module installed locally:
npm install node-rest-client -save
  1. Once done, let's run the client code in the terminal by executing the following command:
node client.js
  1. On successful execution, you will see the output shown in Figure 3.7. The client is sending a request to the server every five seconds and getting a successful response:
Figure 3.7

In this topic, we went through an introduction to the HTTP protocol, did some hands-on implementation, connected Raspberry Pi as a client to the server, and exchanged data between them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.79.59