Configure Periodic Data Collection and Looging From Remote Sensors

Published May 10, 2020  ( 5 min read )
automation

Many a times need arises to monitor sensors located at remote locations and/or collect data from them periodically for records as well as analysis. Sensors such a temperature, pressure, humidity, level sensors etc may be located far at remote places. All of them may not be located at the same remote location. This can be achieved by using our smart devices and IOT systems.

In this blog, we will learn how we can use wifisecureaccess to collect temperature from a remote sensor periodically. In this example, we will use a DS18B20 temperature sensor. We will learn how to display the temperature on our local machine and/or store them in a file for future data analysis. Once we learn this, you can configure to collect and log data periodically from any number of sensors of any type. So, lets get started :

Things we need


  1. WifiSecure Access Smart device
  2. DS18B20 Temperature Sensor
WiFiSecureAccess smart device can be purchased from Amazon.

Fig 1: WiFi Secure Access Smart Device



DS18B20 Temperature sensor is a low cost sensor that can read temperature relatively accurately.

Fig 2: DS18B20 Temperature Sensor

DS18B20 Temperature sensor can be purchased from anywhere. Given below is a link from Amazon. If this link is not available, search for DS18B20 Temperature/Humidity in ebay, amazon or google


Step 1

Connect DS18B20 temperature sensor to the smart devices as shown in Fig 3 below.

Fig 3: Cabling DS18B20 Temperature Sensor to Smart Device

Connect GND, DATA and VCC of the temperature sensor to GND, D7 and 3V3 of the connector PJ4 of the smart device. You can use J6 connector as well and you can use D6 instead of D7, if you prefer that way. For our example here, we will use D7. Apply either 12V power at connector J1 or 240VAC at connector J3 and J4 of the device depending on what power source is available at this location. Make sure that you place jumper on the correct pin of PJ1 connector of the device depending on what power source you are using. Tis is explained in Section 2.0 'Installation' of the smart device.

Step 2

Once you have completed Step 1 successfully, put on the power to the smart devices. If this devices is brand new and you have not yet assigned the ownership to yourself and not yet set its WiFi, go through the steps given in section 2.0 'Installation' of the documentation. If you had done those steps earlier for this two device, you will hear a beep sound when the power is put on and the LED will turn red.

Next, we need to configure this device. Click on 'Configure Device' under 'Device Admin' in the dashboard. This can be done either from a smartphone or using a browser of a computer. If using smartphone, open the app and press 'Admin Dashboard'. Then, expand the menu at the top right and click on click on 'Configure Device' under 'Device admin'. If you are using a computer, do the same on a dashboard. You will see a page similar to Fig 4 when you click 'Configure Device'. On this form, select Use Type as 'CONTROL/SENSOR' and select 'DS18B20 Temperature' for D7. Then press Submit button.

Fig 4: Configuration of Device for DS18B20

Next, check if this smart device is reading the temperature correctly. Click on Dashboad and click on 'Home/Industrial Automation' and then on 'Devices in Automation' and then click on thename of this device. Alternatively, click on 'Show Device Details' under 'Device Admin' in Dashoard. You will be taken to the details of this device. On that page click on Sensor Values. You should be seeing the temperature read by the DS18B20 sensor. You will see the display in degree celcius as well as Fareinheiht similar to Fig 5.
style="width: 800px;"
Fig 5: Display DS18B20 Temperature Sensor

On this page make sure that the it is reading the temperature correctly. If not reading correctly, there may be some problem with the cabling or the sensor.

Step 3

Now that the system is reading temperature correctly, we need to learn how to read this data periodically and send this to our local computer. IOT system provides a json url that can be used to get this data any time to your local machine. Provided below is a sample program in python to make a json call to this url and display the data on the terminal of your computer. This python program can be run in a terminal session of your computer. When run, it will prompt for your user name and password for wifisecureaccess. It will also prompt you to enter sampling period. The sampling period cannot be too low. Keep in mind that this data is provided through internet and, therefore, using too fast sampling rates may not work because of the latency of internet. We do not recommend less than 10 seconds between two succesive reads.


#!/usr/bin/env python
import time
import requests
import json
import getpass
from threading import Timer
import os
import signal

#
def keyboardInterruptHandler(signal, frame):
    print("KeyboardInterrupt (ID: {}) has been caught. Cleaning up...".format(signal))
    ti.cancel()
    f.close()
    exit(0)


def main_loop():
   while True:
      email = input("Enter email : ")
      if email and email != "":
         break
   email = email.upper()
   while True:
      password = getpass.getpass()
      if password and password != "":
         break
   while True:
      device_name = input("Device Name: ")
      if device_name and device_name != '':
         break
   if device_name.isspace():
      print("device name cannot be empty spaces")
      exit(1)
   Error_code = 1
   while Error_code == 1:
      try:
         time_period = int(input("Time Period for data collection in seconds: "))
         Error_code = 0
      except:
         Error_code = 1
   
   if time_period < 10:
      print(" Time period is too short for data collection through internet")
      exit(1)
   while True:
      file_name = input("File Name: ")
      if file_name and file_name != '':
         break
   if file_name.isspace():
      print("Error.  File name cannot be blank spaces")
      exit(1)

   s = requests.session()
   url = "https://wifisecureaccess.com/access/loginJson/"
   data = {'email': email, 'password': password,}
   headers = {'Content-Type': 'application/json', 'Accept': 'application/json',}
   r = s.post(url, data=json.dumps(data), headers=headers)
   if r.status_code == requests.codes['ok']:
      global f
      try:
         f = open(file_name, "a+")
      except:
         print("Unable to open the file %s" % (file_name))
         exit(1)

      signal.signal(signal.SIGINT, keyboardInterruptHandler)
      res = r.json()
      print(res)
      np = [s, device_name, time_period]
      global ti
      ti = Timer(time_period, get_data, (np,))
      ti.start()
   else:
      print("Fail")


def get_data(np):
      s = np[0]
      device_name = np[1]
      time_period = np[2]
      ti = Timer(time_period, get_data, (np,))
      ti.start()
      url = "https://wifisecureaccess.com/access/devicePortsJson/"
      # The labels should be the changed names.  That means, if the user has changed the labels, use the changed labels.
      # Returned data will have the status of all the input ports of this device
      data = {'device_name': device_name,}
      headers = {'Content-Type': 'application/json', 'Accept': 'application/json',}
      r = s.post(url, data=json.dumps(data), headers=headers)
      if r.status_code == requests.codes['ok']:
         res = r.json()
         #print(res)
         if res['result'] and res['result'] == 'SUCCESS':
            if 'dev_time' in res:
               print('Device_time = %s' % res['dev_time'], end=', ')
               f.write('Device_time = %s, ' % res['dev_time'])
            if 'Vin' in res:
               print('Vin = %s' % res['Vin'], end=', ')
               f.write('Vin = %s, ' % res['Vin'])
            if 'U_SOUND' in res:
               print('Ultra_sound = %s' % res['U_SOUND'], end=", ")
               f.write('Ultra_sound = %s, ' % res['U_SOUND'])
            if 'D6' in res:
               print('D6 = %s' % res['D6'], end=', ')
               f.write('D6 = %s, ' % res['D6'])
            if 'D7' in res:
               print('D7 = %s' % res['D7'], end=', ')
               f.write('D7 = %s, ' % res['D7'])
            if 'D6_temp_c' in res:
               print('D6_tem_c = %s' % res['D6_temp_c'], end=', ')
               f.write('D6_tem_c = %s, ' % res['D6_temp_c'])
            if 'D6_temp_f' in res:
               print('D6_temp_f = %s' % res['D6_temp_f'], end=', ')
               f.write('D6_temp_f = %s, ' % res['D6_temp_f'])
            if 'D6_humidity' in res:
               print('D6_humidity = %s' % res['D6_humidity'], end=', ')
               f.write('D6_humidity = %s, ' % res['D6_humidity'])
            if 'D7_temp_c' in res:
               print('D7_tem_c = %s' % res['D7_temp_c'], end=', ')
               f.write('D7_tem_c = %s, ' % res['D7_temp_c'])
            if 'D7_temp_f' in res:
               print('D7_temp_f = %s' % res['D7_temp_f'], end=', ')
               f.write('D7_temp_f = %s, ' % res['D7_temp_f'])
            if 'D7_humidity' in res:
               print('D7_humidity = %s' % res['D7_humidity'], end=', ')
               f.write('D7_humidity = %s, ' % res['D7_humidity'])
            print()
            f.write("\r\n")
         else:
            if 'msg' in res:
               msg = res['msg']
            else:
               msg = ""
            print("FAIL: ", msg)
            ti.cancel()
            f.close()
      else:
         print("FAIL.  Device not communicating")
         ti.cancel()
         f.close()

if __name__ == '__main__':
   main_loop()
The program collects data periodically. Then it displays it on line. It also writes the data to the file you have specified. And, here is the snapshot of the screen running this program on a local computer:

$ python test_periodic_data_collection.py
Enter email : test53680@gmail.com
Password: 
Device Name: New_Device
Time Period for data collection in seconds: 10
{'result': 'SUCCESS', 'msg': 'Login Success'}
Device_time = 2020-04-09 22:23:23, Vin = 0.01, D7_tem_c = 22.0, D7_temp_f = 71.6, 
Device_time = 2020-04-09 22:23:33, Vin = 0.01, D7_tem_c = 22.0, D7_temp_f = 71.6, 
Device_time = 2020-04-09 22:23:43, Vin = 0.01, D7_tem_c = 22.0, D7_temp_f = 71.6, 
Device_time = 2020-04-09 22:23:53, Vin = 0.01, D7_tem_c = 22.0, D7_temp_f = 71.6, 
Device_time = 2020-04-09 22:24:03, Vin = 0.01, D7_tem_c = 22.0, D7_temp_f = 71.6, 
Device_time = 2020-04-09 22:24:13, Vin = 0.01, D7_tem_c = 22.06, D7_temp_f = 71.6, 
Device_time = 2020-04-09 22:24:23, Vin = 0.01, D7_tem_c = 22.06, D7_temp_f = 71.71, 
Device_time = 2020-04-09 22:24:33, Vin = 0.01, D7_tem_c = 22.06, D7_temp_f = 71.71, 
Device_time = 2020-04-09 22:24:43, Vin = 0.01, D7_tem_c = 22.06, D7_temp_f = 71.71, 
Device_time = 2020-04-09 22:24:53, Vin = 0.01, D7_tem_c = 22.06, D7_temp_f = 71.71, 
The above program makes a call to the server at every specified time period. And, it prints out the return values in a single string. You may modify this program to to suit your any special requirements. You can use the data in the file for doing any data analyis.