Jump to content

Push ISY variable to RPI Python3 Program


Recommended Posts

Posted

Hoping someone can point me in the right direction

i have a RPI sending a numeric value to an Arduino. I would like that variable to come from the ISY and be read by a Python3 program. I am pretty much a novice when it comes to getting devices to work together so not sure where to start.

i have an ISY 994i Pro with the network module running 5.0.16 firmware

thanks in advance for any help

Posted (edited)
#!python3 NRbridge.py
#Program to pass ISY Network Resource calls to MagicHome LED controller drivers
#by larryllix (UDI forum), March 3, 2021

# Format Network resource as 'ledenet'/command/bulb/colour/RGBlevel/Wlevel/speed/effect
# Device type ='magichome' 
# command = 'on', 'off', 'set', 'effect'
# 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.179' 
server_port = 8000

# ISY  parameters to be defined by user
ISY_username = 'xxxxxx'
ISY_password = 'xxxxxxxx'
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

# HTTPRequestHandler class
class myHTTPServer_RequestHandler(BaseHTTPRequestHandler):
  def do_GET(self):
        global last_time
        global ledenet_passed, ledenet_passed_valid
        global bulbList
        words = self.requestline.split()
        passed = words[1].split("/",20)
        if len(passed) > 3:
            type = str(passed[1]).strip().lower()
            if type == 'magichome':
                self.send_response_only(200)   # send  ACK response
                self.end_headers()
                time.sleep(0.001) # allow I/O time slice
                message, bulbList = magichome(bulbList, passed[2:])
                report(message)
            else:
                self.send_error(501)  # send format error response
                self.end_headers()
                time.sleep(0.001) # allow I/O time slice
                report("NRbridge: Type undefined. Received:" + str(type) + " ***")

        else:   # wasn't a GET
            self.send_error(400)  # 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)
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
message, bulbList = magichome(bulbList, ['initialise'])
report(message)


# Main loop
while(True):
    # Check for incoming command
    httpd.timeout = 2.0
    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()

Here is some code to receive a NR GET command as well as a handshake back into ISY's REST protocol to flip a variable back and forth, so ISY knows if my NRbridge.py is alive. This was originally on a RPi but I moved it onto polyisy until polyglot v2 came out which caused it to stall randomly. It now is back on an RPi working fine again.

The NR looks like this http://192.168.0.xx:8000/magichome/set/bulb#/hue/RGBlevel/Wlevel/..... You can parse as many parameters as you like. I just pass them through to my MagicHome handler to sort out the specifics for that protocol. Parameters received via Ethernet are all Ascii text and numbers need to be type converted.

The NR is mostly ISY Integer variables so that one Effect, one Set, one On, and one Off NR (total of 4 NRs) handle everything you can do to my thirty five bulbs and 10 RGBWW strip controllers.

 

 

 

Edited by larryllix
  • Like 1
Posted

Larry.  Thanks very much. I will give this a try.  Much more than I expected but looking at the code, not sure I am smart enough to develop on my own.

i will let you know my progress and what I am trying to do at a later time. 

Thanks again

Posted (edited)
1 hour ago, LarryCRetired said:

Larry.  Thanks very much. I will give this a try.  Much more than I expected but looking at the code, not sure I am smart enough to develop on my own.

i will let you know my progress and what I am trying to do at a later time. 

Thanks again

Look it over and extract what you can. Ask pointed questions and I will try to help smaller pieces at a time. This took a long time to extract from documents and I found most of the "official" documents still don't work as they state. Python3 is still a zoo and there doesn't ever seem to be any manual that 100% works.

BTW: There is free python3 app for windows 10 and they work very well, along with better editors that analyse, format your code, and give good hints for problems. Some Win 10 python3 code is slightly different but not likely you will ever find the differences. One snag is the EOL codes are different between linux and Win 10 editors.

Edited by larryllix
Posted

Larry

I just added the code to my RPI.  I will be looking at it this weekend when I have time in between my honeydo projects.  Once again, thanks for giving me a running start.  What I have now is an on/off module which turns my Arduino on and off based on what is happening in the ISY.  With your help above, I want the Pi to distinguish which message track to play on my Arduino Sketch and the amplifier connected to it.  Now it just plays "Unauthorized Entrance", Police have been called".   

Thanks again my Canadian Friend.

Posted

Larry

Thanks very much for your help.  I will be honest, the code above was a little beyond me, however I found a post and some code you provided to the forum a couple of years ago.  I was able to modify that code and I now can receive a pushed variable from the ISY. 

Where I ran into complications was trying to incorporate some python code to make a serial connection to the Arduino.  I ended up with two python programs.  The second program is initiated and also terminated from the first program.  I had to do this to close the serial connection to the Arduino.  I also had to start and stop the serial connection in the Arduino to get it to respond to different values of the variable.  Flushing the connection did not work for me.

Got everything finalized today and I set up the ISY to push a variable so the Arduino wishes my wife a Happy Birthday in a few days.

I am sure there are more elegant ways to do this but being a novice at python, I am happy I could get this to work.

Thanks again for your guidance.

Posted
4 hours ago, LarryCRetired said:

Larry

Thanks very much for your help.  I will be honest, the code above was a little beyond me, however I found a post and some code you provided to the forum a couple of years ago.  I was able to modify that code and I now can receive a pushed variable from the ISY. 

Where I ran into complications was trying to incorporate some python code to make a serial connection to the Arduino.  I ended up with two python programs.  The second program is initiated and also terminated from the first program.  I had to do this to close the serial connection to the Arduino.  I also had to start and stop the serial connection in the Arduino to get it to respond to different values of the variable.  Flushing the connection did not work for me.

Got everything finalized today and I set up the ISY to push a variable so the Arduino wishes my wife a Happy Birthday in a few days.

I am sure there are more elegant ways to do this but being a novice at python, I am happy I could get this to work.

Thanks again for your guidance.

AWESOME!!! Research away and ask questions. I would be happy to guide you along the way, with what I can. I have experimented with different response program lines and fought through a lot of confusion from online "manuals". I hassled with errors for a few years that were actually bugs in the Chrome browser. Python3 s a mess for info, but love the format.

  • Like 1
Posted

Larry. Thanks for your willingness to get me educated.   I had to stop for a while.  My wife is undergoing treatments and I am helping her get through them.  Where I had to stop is in getting the Arduino serial to accept a two digit integer.  Talk about confusing sources on the web.  Unbelievable 

But thanks again.  

My wife knows I converse with a forum member from Ontario   She showed me this link and said the stay at home was meant for everyone.   Hope you enjoy the video, not the staying home  part  

 

https://m.youtube.com/watch?v=oRgGAKnbWQc

 

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

×
×
  • Create New...