Controlling IO Lines of a NetPing Monitoring Unit Using Telegram

  • Published In: Tutorial
  • Created Date: 2018-06-01
  • Hits: 45

We continue publishing a series of articles on implementing the integration of NetPing server room environmental monitoring solutions and power distribution units with «Telegram» messenger. Earlier in our blog, we have specifically examined the process of a bot creation for «Telegram» messenger, controlling IP PDU NetPing sockets using Telegram and controlling an air conditioner in a server room using Telegram. In this article, we will regard a process of a bot configuration for controlling discrete input-output lines of a NetPing IO v2 server room monitoring unit. A similar integration can be practically used when:

  • Service staff can send a command to open and close a barrier using «Telegram» messenger. In this case, the use of a bot allows to refuse using a regular mechanic fixed button in favor of a variant of a remote control using a mobile phone and/or a tablet. A barrier is connected to BM8070D power relay 16A/250V for DIN rack, that is in turn connected to an IO line of a NetPing IO v2 server room monitoring unit;
  • Technical staff can control load using «Telegram» messenger, including a server, a switch, a router, light, ventilation, etc. that is connected to a controlled socket NetPing AC/DIN, regardless a geographical location;
  • Technical stuff can open or close electric lock on the entrance door to a server room using «Telegram» messenger, which also provides additional protection from unauthorized access of the third parties on the object.

Required equipment, software, and preparing for a development

To implement a functionality of remote controlling discrete input-output lines in a NetPing server room monitoring unit using «Telegram», there is a need to have the next set of equipment:

  • NetPing IO v2 server room monitoring unit – 1 pc.;
  • BM8070D power relay 16A/250V for a DIN rack – 1 pc.;
  • barrier – 1 pc.;
  • controllable socket NetPing AC/DIN – 1 pc.;
  • electric lock for an entrance door in a server room – 1 pc.
  • PC or server with a preinstalled operating system Linux or Windows, and an Internet access – 1 pc.;
  • local network to connect a monitoring device and a PC, where a bot script is executed – 1 pc.;
  • «Telegram» messenger on a PC or a smartphone with an Internet access – 1 pc.

Let's consider a NetPing IO v2 server room monitoring unit configured for the operation on your local network. BM8070D power relay 6A/250V for DIN rack, controllable socket NetPing AC/DIN and electric lock are connected to corresponding input-output lines of a NetPing IO v2 device and are configured. More detailed information on the settings of NetPing IO v2 can be found in the documentation.

Below, there is an approximate equipment wiring diagram for a NetPing IO v2 (more detailed information about connecting external devices can be browsed in a user guide on the page «Plugging sensors» or contact a technical support on the address support@netpingdevice.com):

NetPing IO v2 server room monitoring unit equipment wiring diagram

It is suggested that your server with OS Ubuntu is configured, and bots are created according to the recommendations of this article. In this example, we create a bot named @NetPing_IO_bot.

Programming controlling functions for discrete input-output lines for a bot @NetPing_IO_bot

Let's program an extended functionality for our bot @NetPing_IO_bot. A bot controls discrete input-output lines of a NetPing IO v2 server room monitoring unit using URL-encoded commands that are specified in the bot script code and are assigned to buttons. Let's edit a primary script file that controls a bot – «bot.py»:

import telebot
import requests
from telebot import types
import conf_bot

auth = conf_bot.auth
url = conf_bot.url
bot = telebot.TeleBot(conf_bot.TOKEN)

# Processing commands "/start" and "/help"
@bot.message_handler(commands=['start', 'help'])
def start(message):
markup = types.ReplyKeyboardMarkup()
markup.row('/relay1', '/relay2')
markup.row('/relay3', '/relay4')
markup.row('/npstatus', '/help')
bot.send_message(message.chat.id, ''' 
***Test bot to control the outlets of NetPing 4/PWR-220 v3/SMS ***

To query the current status of the relay, use the command /npstatus

To control relay #1, use the /relay1 command

To control relay #2, use the /relay2 command

To control relay #3, use the /relay3 command

To control relay #4, use the /relay4 command

To access this help, use the /help command

''',
reply_markup=markup)

# Processing the "/npstatus" command, getting information about the relay status
@bot.message_handler(commands=['npstatus'])
def status(m):
i=1
while i <=4:
def relay ():
r = requests.get(url+'relay.cgi?r'+str(i), auth=auth)
io = r.text[22:23] 
if io == '0':
io = ('Current state of the relay #' 
+ str(i) + ': ' + io + ' Off')
return io
else:
io= ('Current state of the relay #' 
+ str(i) + ': ' + io + ' On')
return io 
io_stat = relay() 
sent = bot.send_message(m.chat.id, io_stat ) 
i=i+1
# Processing the "/relay1" command, creating a menu.
@bot.message_handler(commands=['relay1'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 1', 
'Disable relay 1', 'Reset relay 1' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 1.

Select the required action:''',
reply_markup=keyboard)

# Processing the "/relay2" command, creating a menu
@bot.message_handler(commands=['relay2'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 2', 
'Disable relay 2', 'Reset relay 2' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 2.

Select the required action:''',
reply_markup=keyboard)

# Processing the "/relay3" command, creating a menu
@bot.message_handler(commands=['relay3'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 3', 
'Disable relay 3', 'Reset relay 3' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 3.

Select the required action:''',
reply_markup=keyboard)

# Processing the "/relay4" command, creating a menu
@bot.message_handler(commands=['relay4'])
def status(m):
keyboard = types.InlineKeyboardMarkup() 
keyboard.add(*[types.InlineKeyboardButton(text=name, 
callback_data=name) for name in ['Enable relay 4', 
'Disable relay 4', 'Reset relay 4' ]])
msg = bot.send_message(m.chat.id, '''
This command allows you to control the relay 4.

Select the required action:''',
reply_markup=keyboard)

# Processing of pressing the menu button "Enable relay 1"
@bot.callback_query_handler(func=lambda c: True)
def inline(c):
if c.data =='Enable relay 1':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 1 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r1=1', auth=auth)
#Processing of pressing the menu button "Disable relay 1"
elif c.data =='Disable relay 1':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 1 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r1=0', auth=auth)
#Processing of pressing the menu button "Reset relay 1"
elif c.data =='Reset relay 1':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 1 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r1=f,10', auth=auth)

# Processing of pressing the menu button "Enable relay 2"
elif c.data =='Enable relay 2':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 2 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r2=1', auth=auth)
#Processing of pressing the menu button "Disable relay 2"
elif c.data =='Disable relay 2':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 2 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r2=0', auth=auth)
#Processing of pressing the menu button "Reset relay 2"
elif c.data =='Reset relay 2':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 2 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r2=f,10', auth=auth)

# Processing of pressing the menu button "Enable relay 3"
elif c.data =='Enable relay 3':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 3 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r3=1', auth=auth)
#Processing of pressing the menu button "Disable relay 3"
elif c.data =='Disable relay 3':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 3 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r3=0', auth=auth)
#Processing of pressing the menu button "Reset relay 3"
elif c.data =='Reset relay 3':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 3 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r3=f,10', auth=auth)

# Processing of pressing the menu button "Enable relay 4"
elif c.data =='Enable relay 4':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 4 is on',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r4=1', auth=auth)
#Processing of pressing the menu button "Disable relay 4"
elif c.data =='Disable relay 4':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 4 is off',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r4=0', auth=auth)
#Processing of pressing the menu button "Reset relay 4"
elif c.data =='Reset relay 4':
bot.edit_message_text(
chat_id=c.message.chat.id,
message_id=c.message.message_id, 
text='Relay 4 switched to inverse state for 10 seconds.',
parse_mode='Markdown')
r = requests.get(url+'relay.cgi?r4=f,10', auth=auth) 
bot.polling()

Source files of a bot can be downloaded from here.

Working with a bot for remote controlling IO lines of a NetPing IOv2 device

Enable a dialogue with a bot @NetPing_IO_bot, where we added new functionality to the controlling script. After we send a command «/start» (1) to a bot, a dialogue window will display a memo (2), and below the field for entering a message, buttons (3) will appear that duplicate commands for controlling a bot:

Telegram starting a dialogue with a bot to control IO lines of a NetPing IO v2 server room monitoring unit

Now, let's learn commands one by one. The first command is «/barrier» and is designed to cotnrol a barrier, and mimics pushing a physical button. When selecting a command in a dialogue window, there is а notification about a selected command (1), and buttons for selecting possible actions (2). The actions «Open the barrier» and «Close the barrier» are available:

Telegram command for controlling a barrier connected to NetPing IO v2 server room monitoring unit

During a command execution, a notification about a current status of a barrier will be displayed in a dialogue window. On a screenshot below, there are the results of the consequent clicking buttons for controlling a barrier:

Telegram result of controlling a barrier connected to NetPing IO v2 server room monitoring unit

The next command is «/socket», and it allows to control a power supply of a controllable socket NetPing AC/DIN, and, accordingly, a power supply of the load that is connected to it (in out example, it is a router). When selecting a command in a dialogue window, there is a notification about a selected command (1) and buttons for choosing possible actions (2):

Telegram command for controlling a socket connected to NetPing IO v2 server room monitoring unit

To control a power supply of a controllable socket NetPing AC/DIN, the commands «On», «Off» and «Reboot» are available. A command «Reboot» performs a short-term (10 seconds) switching off an input-output line in an inverted status (issuing a reset pulse), therefore a connected equipment in the socket is also rebooted. When executing a command in a dialogue window there is a notification about a current status of an input-output line. On a screenshot below, there are the results of consequent clicking the button of controlling a power supply socket:

Telegram results of controlling a socket connected to NetPing IO v2 server room monitoring unit

A command «/door» works the same as the similar command «/barrier», but it controls electric load that is connected to the IO 3 line of a NetPing IO v2. When selecting a command in a dialogue window, a notification about a chosen command (1) and buttons for selecting possible actions (2) are displayed in a dialogue window. The actions «Open the electric lock» and «Close the electric lock» are available:

Telegram command for controlling an electric lock connected to NetPing IO v2 server room monitoring unit

When executing a command, a notification about a current status of a door to a server room is displayed in a dialogue window. On a screenshot below, there are the results of consequent clicking the button for controlling the electric lock:

Telegram result of controlling an electric lock connected to NetPing IO v2 server room monitoring unit

Hereby, discrete input-output lines of a monitoring device NetPing IO v2 are remotely controlled using «Telegram» messenger.


Tags: All devices
comments powered by Disqus