Step 2 - Configuring an edge device channel

The second step is to create a new device channel in the IoT cloud. A device channel is a defined endpoint (specific to a particular edge device) in the IoT cloud where we can transmit data by simply invoking URLs via HTTP. So, if we have 10 edge devices, then we will have to create 10 separate device channels in the IoT cloud.

In this section, we will learn how to create a ThingSpeak channel. Start by clicking on the New Channel button as shown in the following screenshot:

Figure 4: Creating a new channel

After clicking on the New Channel button, a new data entry screen will appear. In the new channel creation screen, fill out the details of the device from which the data will be received. For the Arduino sketch example provided in this chapter, you will just have to fill out the textboxes Name, Description, and Field 1, as shown in the following screenshot:

Figure 5: Setting up a new device channel

In the new channel creation screen, scroll down further and make this channel public (as shown next) by checking the Make Public checkbox, so that you can post data to it from the edge device:

Figure 6: Making the device channel publicly accessible

After filling the details as mentioned, check on the Save Channel button to create the new device channel. After creating the channel, the channel details screen will get displayed (as shown next). Notice that there will be five tabs: Private View, Public View, Channel Settings, API Keys, and Data Import/Export. That is all. You are now all set to start using the edge device and post data to the ThingSpeak IoT cloud. Browse through the five tabs to get a feeling of the contents under each of these tabs. Make note of the Channel ID, as you will need it for posting and reading data to ThingSpeak:

Figure 7: Channel details dashboard

Now we will see how to log data and read data to and from this channel. Browse to the API Keys tab for your channel:

Figure 8: Channel API keys

Note down the Read API Keys and Write API Key (as shown earlier), as you will need it for reading and posting data from and to the ThingSpeak IoT cloud respectively. The API keys for the channel shown in the preceding figure have been obscured intentionally.

Never share your channel API keys in order to secure your device channel from unauthorized or inadvertent spamming.

Now, let's understand how to send data to the newly created device channel. First we will learn how to simply use an URL and hit the URL in a web browser, and send the desired data to the ThingSpeak IoT cloud platform. Once we have understood how the URL was used, then we will attempt the same thing from an Arduino sketch. Thus, we will effectively learn how to send data from an edge device to an IoT cloud.

You can use the following URL format to log data to ThingSpeak: https://api.thingspeak.com/update.json?api_key=<your-write-api-key>&field1=58.

In the preceding URL format, replace <your-write-api-key> with the API Write Key for your device channel. The field1=58 refers to the field1 that was set up while creating the device channel. You should replace the value 58 by the actual value that has to be sent to the cloud:

Figure 9: Recording data into ThingSpeak via the URL in the browser

Upon hitting the URL in a web browser, the data '58' will get logged in field1 for the configured device channel in the ThingSpeak IoT cloud. Immediately after the URL is hit, you will see the preceding response in the browser. The response text is the JSON (JavaScript Object Notation) format response sent from the ThingSpeak device channel endpoint. If you inspect the response displayed in the browser, then it can be seen that it represents a data row from a table that got populated with some meaningful data when we hit the URL in the browser. In other words, when the URL was hit in the browser, the data was sent to the device channel endpoint and got stored in an internal database table corresponding to the device channel. So every time we want to send a new data point to the cloud, the same URL format can be used. The only change would be the value to be posted.

Similarly, we can utilize the same URL format to send an HTTP get request from an Arduino sketch. However, the Arduino board will require an additional piece of hardware for connecting to the internet. As already explained at the beginning of this chapter, there are various hardware options available for connecting to the internet. In this chapter, we will learn how to use the ESP8266-01 Wi-Fi chip with Arduino Uno.

After sending data to the ThingSpeak IoT platform, the next step in understanding the process is to browse to the web dashboard from where the newly logged data can be viewed in a graphical format. In case of the ThingSpeak cloud, you can also see the logged data in the ThingSpeak channel's Private View and Public View tabs, as shown in the following screenshot:

Figure 10: Viewing logged data

Now that we have understood how to send data to an IoT cloud by using very simple URL formats, let's learn the technique to fetch data. Once again, we will use very simple URL formats for fetching data from the ThingSpeak IoT cloud.

The important point to note here is the structure and/or format of the URL and the protocols used that vary across various IoT cloud platforms. Depending on the platform being used, you should refer to the platform-specific documentation.

Use the following URL format to read the logged data from ThingSpeak: http://api.thingspeak.com/channels/<your-channel-id>/feeds.json?key=<your-read-api-key>&results=1.

On hitting the preceding URL in the browser, you will receive the last posted data row from ThingSpeak in the following screenshot:

Figure 11: Fetching the logged data from ThingSpeak in the browser

Using JSON-parsing techniques, we can extract the value for field1. Once the value has been extracted, it is up to the consuming application (either in the cloud or on a device) to decide what needs to be done next. JSON (JavaScript Object Notation) is a web development technology used to represent JavaScript objects in plain text format. Since JSON is a separate topic and is beyond the scope of this book, you can use the following online resource for learning the basics of JSON:

https://www.w3schools.com/js/js_json_intro.asp

Basically, you are free to use any technology of your choice that you are comfortable with, to parse the received data from the cloud. However, JSON has been mentioned here, because the data returned by the cloud is readily available in JSON format, as shown in Figure 11.

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

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