Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome Programming
 PowerHome Messageboard : PowerHome Programming
Subject Topic: Finally, a good Pi communications method Post ReplyPost New Topic
Message << Prev Topic | Next Topic >>
Senior Member
Senior Member

Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: June 07 2022 at 16:18 | IP Logged Quote gg102

Finally, I have figured out a good way to communicate with my Raspberry Pi, to and back from PH to the Pi.

This is the code in the Pi:
        import     requests # import URL for requests

#-------------------------------------------------------- --------------------------------
def write_server_data(passed_macro_name, passed_string1, passed_string2, passed_string3, passed_string4, passed_string5):
    #passed_macro_name        #you can configure this as you desire, you can either pass it in, or hard code it in the string below.
    #string1  = Will become [LOCAL1] in PH
    #string2  = will become [LOCAL2] in PH
    #string3  = will become [LOCAL3] in PH
    #string4  = will become [LOCAL4] in PH
    #string5  = will become [LOCAL5] in PH
    # You will need to have PH_USER, PH_PASSWORD pre-defined or pass it in the function.
    full_string = ""
    # YOU HAVE TO CHANGE: x.y:z    # x.y is the PH server address and z is your HTTP port in your PH settings (settings: web: web server port)
    # Be very careful of all the " and ' characters. THEY MATTER!
    # I did it this way so it's much easier to understand.
    header_string = "http://192.168.x.y:z/ph-cgi/eval?formula=ph_macroparmret( "
    data_string   =  ' "YOUR_ PH_MACRO_NAME_HERE" ' + " ," + \
                          '"' + passed_string1 + '"' + "," + \
                          '"' + passed_string2 + '"' + "," + \
                          '"' + passed_string3 + '"' + "," + \
                          '"' + passed_string4 + '"' + "," + \
                          '"' + passed_string5 + '"' + ")"
    full_string = header_string + data_string        & nbsp;         & nbsp;           # combine the header with the data
    write_local_log_file( "Sent to Server: " + data_string + "\r\n")       #
     try:    &n bsp;         &n bsp;                                 ;           ;  #
        response = -1         &nbs p;         &nbs p;                         &nbs p; # If   the  &nbs p;'request' fails, you might not get a 'response' causing the 'except' to fail.
      response = requests.get( full_string, auth=(PH_USER, PH_PASSWORD) ) # response is a structure
        write_local_log_file("Server Response: " + response.text + "\r\n")# Log the server response
        #returned_text = response.text         ;           ;              # Returned response.text for what WAS in [LOCAL1] when the PH macro ended.
        #returned_text = returned_text        #   for   debugger           #
        except:               ;           ;                            &nb sp;#
                 write_local_log_file("Error.  &nbs p;Line: " + get_program_line_number() + " Can't write to server data file." + \
                                                                     " Response is: "+ str(response) + "\r\n")
    #print (response) # for convenience while programming

The code above is a function in your Python program.

The way it works is it sends by an http URL, the name of the PH macro you wish to run, and it passes the strings1-5 into [LOCAL1-5] respectively.
Warning: YOU MUST HAVE ALL STRINGS. You can't 'shorten' the function or you'll get a syntax response error from PH.
If necessary, make them blank. ( "" ), but they must exist.

Your PH macro can use [LOCAL1-5] to determine what the Pi is sending
and PH will return to the Pi whatever the PH macro has in [LOCAL1] when the macro ends.
The PH response from [LOCAL1] can be extracted from the response structure in "response.text".
Your Pi can use the data in response.txt as a response from PH.

I send for example:
Macro name = "GARAGE_PI_DATA" (your_ph_macro_name_here) as shown above
string1 = "GARAGE"
string2 = "OPENED" / "CLOSED"
string3 = time
string4 = ""
string5 = ""

In PH then I have a case statement to parse things.

I have some of my local Python functions in the function, but you get the idea.

( I have to keep editing this to remove the ---;   &n bsp;---    that keeps getting inserted.
ok, I can't get rid of all the -- ;     &n bsp;-- so, just ignore them....sorry.

Edited by gg102 - June 07 2022 at 17:36
Back to Top View gg102's Profile Search for other posts by gg102

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum