Jump to content

Use ISY to trigger bash script on Raspberry Pi?


blueman2

Recommended Posts

Posted

I would like to be able to have an ISY program trigger a bash script or python program to run on a raspberry pi.  I have Polyglot running if that helps.  Any ideas?  

Posted
  • Using Network Resources
    • Webserver and exec the scripts as cgi scripts
    • Mini webserver (python, node.js, perl) to run your desired code from request end point can provide optional parameters
    • Node-RED workflow probably the most customizable and perhaps easiest
  • Custom polyglot that would exec scripts in a known directory is also feasible.
  • Like 1
  • Thanks 1
Posted (edited)
14 hours ago, blueman2 said:

I would like to be able to have an ISY program trigger a bash script or python program to run on a raspberry pi.  I have Polyglot running if that helps.  Any ideas?  

It's easy to do if you run NodeLink. You'll need a script on your RPi, a Network Resource on your ISY and NodeLink configured properly.  I use this to have my ISY bounce Polyglot on occasion (both NodeLink and Polyglot run on the same RPi), amongst other things.  I'd post an example but I'm away from home this week.  

EDIT: In NodeLink, you need to turn on the "Relay Server".

Edited by Bumbershoot
  • Thanks 1
Posted
17 hours ago, blueman2 said:

I would like to be able to have an ISY program trigger a bash script or python program to run on a raspberry pi.  I have Polyglot running if that helps.  Any ideas?  

If you are familiar with python3 I posted script to receive Get" calls about a year back on this forum.

  • Thanks 1
Posted

 

11 hours ago, Bumbershoot said:

It's easy to do if you run NodeLink. You'll need a script on your RPi, a Network Resource on your ISY and NodeLink configured properly.  I use this to have my ISY bounce Polyglot on occasion (both NodeLink and Polyglot run on the same RPi), amongst other things.  I'd post an example but I'm away from home this week.  

EDIT: In NodeLink, you need to turn on the "Relay Server".

Yup, I am running Nodelink.  I will give that a try.

 

7 hours ago, larryllix said:

If you are familiar with python3 I posted script to receive Get" calls about a year back on this forum.

Yup, it is actually a python3 program I wrote that I want to trigger.  The program talks to my Solaredge inverter using Modbus over TCP to update power usage info.  

Thanks, guys!

Posted
14 minutes ago, blueman2 said:

 

Yup, I am running Nodelink.  I will give that a try.

 

Yup, it is actually a python3 program I wrote that I want to trigger.  The program talks to my Solaredge inverter using Modbus over TCP to update power usage info.  

Thanks, guys!

Here is some of the code I wrote to receive NR commands.

You will have to dig through it to make it work from yourself.

I left the ISY heartbeat code in in case you want to know it it out there in your LAN cloud working from ISY

 

#!/usr/bin/Python-3.5.1 NRbridge.py
#Program to pass ISY Network Resource calls to MagicHome LED controller drivers
#by larryllix (UDI forum), June 29, 2019

# Format Network resource as 'ledenet'/command/bulb/colour/RGBlevel/Wlevel/speed/effect
# Device type ='LEDenet' or 'MiLight'
# command = 'on', 'off', 'set', 'effect', 'keepalive'
# bulb = 0, 1 - n. 0 = all bulbs on controller
# colour = 0-255, 0=Red, 85=Green, 170=Blue
# RGBlevel = 0-100% brightness of the RGB LEDs
# Wlevel = 0-100% brightness of the white LEDs
# speed = used for multi-bulb effects
# effect = 0-? native bulb effects or 23-37 for multi-bulb animations

from http.server  import BaseHTTPRequestHandler, HTTPServer
import urllib.request
import base64
import time
from  magichome import magichome

# Server (this CPU) IP address
server_IP = '192.168.0.175' 
server_port = 8000

# ISY  parameters to be defined by user
ISY_username = 'admin'
ISY_password = 'password'
ISY_IP = '192.168.0.161'
# Heartbeat into ISY variable
ISY_HBpage = 2  # 1=Integer, 2=State
ISY_HBaddr = 83 # ISY heartbeat variable
ISY_HBperiod = 90 # In seconds. toggles ISY flag variable
ISY_HBlast_sent = 0.0

# Refresh bulb statuses and socket keepAlive
lastCheckRefeshTime = 0.0 # time of last refresh
refreshCheckPeriod = 10.0 # seconds between scans


verbose = True # Set verbose = False  to shut off reporting of  details of ISY sends

def report(result):
    if verbose:
        print(time.strftime("%H:%M:%S", time.localtime(time.time())), result)
    return

# HTTPRequestHandler class
class myHTTPServer_RequestHandler(BaseHTTPRequestHandler):
  def do_GET(self):
        global last_time
        global ledenet_passed, ledenet_passed_valid
        global bulbStatusList
        words = self.requestline.split()
        passed = words[1].split("/",20)
        if len(passed) > 3:
            self.send_response_only(200,'NRbridge received OK')   # send  ACK response
            self.end_headers()
            time.sleep(0.001) # allow I/O time slice
            type = str(passed[1]).strip().lower()
            ####### ### --------------------code to run bash command here---------------------- #############################

        else:   # wasn't a GET
            self.send_error(400, 'NRbridge: bad Get format')  # send NAK response
            self.end_headers()
            time.sleep(0.001) # allow I/O time slice
            report("NRbridge: ISY GET:format error! ***")
        return

# Write ISY handshake variable via REST i/f
def write_ISY(data):
    ISY_rest = "/rest/vars/set/%s/%s/%s" % (ISY_HBpage, ISY_HBaddr, data)
    get_url = urllib.request.Request('http://' + ISY_IP + ISY_rest) #format the URL
    ISY_authorise = base64.b64encode((ISY_username+":"+ISY_password).encode('utf-8')) #encode it
    get_url.add_header("Authorization", 'Basic %s' % ISY_authorise.decode('utf-8'))
    try:
        r = urllib.request.urlopen(get_url)
        time.sleep(0.001) # allow I/O time slice
        returned = r.read()
        r.close()
        time.sleep(0.001) # allow I/O time slice
    except:
        report("NRBridge: ISY heartbeat send failed!***")
    return

#-------------------------------------

# Get server setup
try:
    httpd = HTTPServer((server_IP, server_port), myHTTPServer_RequestHandler)
    httpd.socket.settimeout(0.8)
except:
    print('')
    report("NRbridge: Not started! May already be running * * * * * *")
    exit()

print('')
report('NRbridge: started. . .')
heartbeat = 0

# Get list of sockets and status of all bulbs
bulbStatusList = [] # bulb data
message, bulbStatusList = magichome(['refresh'], bulbStatusList)
report(message)

# Main loop
while(True):
    # Check for incoming command
    httpd.handle_request()

    # ISY heartbeat signal
    if time.time() >= ISY_HBlast_sent + ISY_HBperiod:
        if heartbeat != 1:
            heartbeat = 1
        else:
            heartbeat = -1
        write_ISY(heartbeat)
        ISY_HBlast_sent = time.time()

    # Update bulb statuses and refresh sockets
    if time.time() >= lastCheckRefeshTime + refreshCheckPeriod:
        lastCheckRefeshTime = time.time()
        message, bulbStatusList = magichome(['refresh'], bulbStatusList)
        #report(message)

 

  • Like 1
  • Thanks 1
Posted

Well, I was able to get this working really quickly using Nodelink with Relay Server turned on.  Embarrassingly easy.  

I plan to also play with larryllix's code as well.  Thanks again both of you for the help and guidance.  

  • Like 2
Guest
This topic is now closed to further replies.

  • Recently Browsing

    • No registered users viewing this page.
  • Forum Statistics

    • Total Topics
      37k
    • Total Posts
      371.4k
×
×
  • Create New...