We took a Linklt Smart 7688 device and configured it to make it a Restful client via a simple application written in Python. Of course then we will use this device later as an iOT device that transmits data on an Ethereum blockchain.
Linklt Smart 7688
The chosen device equips a Linux distribution defined as OpenWrt and is ideal for doing enough interesting tests for iOT applications. The system already starts with many useful features for the development of dedicated applications. There are already many modules that can be used by those who develop complex appications, besides the possibility of plugging sensors of all kinds. We already find on the card for example packages such as python, node.js, git, curl and so on, and you can easily install any other packages that may be needed
Let’s start to using it
First the device must be powered so take your mini-usb charger and connect it to the port with (very small) PWR written. Immediately the device turns on and the boot starts. You can see that the WiFi LEDs are turned on and as soon as it turns green it means that the device is able to provide a WiFi network to which you can connect via our PC (the device is in AP mode). Look for it among the available networks. The default name is Linlt_Smart_7688_xxxxx as you can see looking at the picture below
At this point, connect to the device’s network and get an IP address with which you can communicate later. The subnet used by the LinkLt is 192.168.100.x
Open a browser (NOT Edge because the execution of the script for administrative management crashes, we have used chrome) and connect to the address 192.168.100.1 which will open the login page to access the administration site. The first time you connect, it will ask you to set a password that will be persisted on the device until a possible hard-reset
Once inside we move on to the network tab where it will be possible to switch from the AP mode to the Station Mode in which it is possible to connect the device to the local WiFi network we want, by entering the appropriate network password. At this point also the device can access to internet and we too can switch to the local WiFi and share the same network of our device. After restarting, LinkLt is ready.
To detect the IP received by our device use a Network Scanner to discover all assigned IP Address. We’ve used IPScan a free tool that you can download from internet. Our result are shown below:
Connect to Linklt
To connect to the device we will use an SSH connection using the classic SSH client called PuTTY downloadable from the network as open source software.
Once the PuTTY console is open, we insert the IP address of our device and get a root console (make login with credential ‘root’ and the password you’ve choosen before) from which we can start to make the device operational. From the command prompt, download the python responses module using the language package manager called ‘pip’ and give the command as shown in the figure
Write the Client
At this point, all we have to do is start writing some code. We should not expect to have Visual Studio installed and for this we will have to be content with a historical editor for those who have been using Linux for a long time called Vi, obviously already present in the device as part of the openWRT system. Type vi restcli.py from the command line and open our editor. We enter editing mode (one of the two modes of vi: ‘editing’ and ‘command’) and insert the following lines of code:
Enter ‘Esc’ and ‘colon’ and wq to save and quit from the editor. Here the complete code:
import requests import time import random import json count = 0 lats = ["44.534041", "44.535264", "44.536947", "44.539325","44.540732","44.542139","44.543852","44.545542","44.546689","44.548188"] lngs = ["11.124427", "11.121359", "11.117056","11.110705","11.107121","11.103366","11.098850","11.094579","11.091575","11.087616"] while (count < 10): rnd = random.uniform(11, 30) truncatedrnd = round(rnd, 2) iotData = {'DeviceID':'Link Smart 7688', 'Temperature': str(truncatedrnd), 'Humidity': '89.2', 'Lat':lats[count], 'Lng':lngs[couunt]} h = {'Content-Type': 'application/json'} rp = requests.post(""http://localhost/iotapi/api/temphum", data=json.dumps(iotData), headers=h) print(rp.text) time.sleep(1) count = count + 1 print ('end loop')
The code is very simple. We define two arrays of strings containing the latitude and longitude of ten GPS positions. We begin a cycle (only of ten iterations) in which a random float is chosen between the values 11 and 30, the decimal digits are truncated after the second and the payload of the call Rest is constructed. After declaring a header containing the Content-Type the http request is executed indicating the URL, the Payload and the header just built. We wait synchronously for the answer which will then be printed on the screen. At each iteration, you wait for a second. If you use the https protocol add the verify = False parameter to the call so as to avoid verification problems of a non-secure certificate (such as for example a self-signed certificate).
Launch the client by the command prompt line python restcli.py. The server listening for this call is an API server that inserts this data into a Blockchain creating a smart contract instance for each call. The result of the call is the address of the newly created smart contract
Our client rest has been programmed to simulate sending the temperature and GPS position and can be used in many areas where a cold chain must be certified with absolute certainty (transport of medicines, transport of frozen food, … ). The use of this device, however, can be absolutely extended to domotics (we think of the detection of images in a security system) or to new applications for the automotive sector (let us think, for example, of the registration of the paths of a car in the insurance sector). You will simply need to insert the detector to obtain the necessary physical data.
Conclusion
In the following posts we will see the part related to the Server API that receives the data from our device and inserts it in the bloackchain, while in the final post we will see a web application that reads the data in real time from the blockchain as soon as inserted by the API server part.