• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python script for direct socket connection
#1
Hi,

Because I sometimes have the need to directly trigger a light from a different computer than the raspberryPi, I made a little python script to do so. Maybe someone else has use for it as well or they can modify it to their needs.

Usage is like this: script.py "protocol" "dimlevel" "unit" "id"

So: python script.py kaku_dimmer 15 1 120
sets kaku_dimmer dimlevel 15 to unit 1 with id 120

It's real simple and perhaps it only works for kaku_dimmer because i didn't look into the other protocols, but anyways:

Code:
import socket
import json
import sys

try:
   a = sys.argv[1:]

except ValueError:
   print 'Vul een andere waarde in'

data1 = {"message":"client sender"}

data2 = {}
data2["protocol"] = a[0]
data2["dimlevel"] = a[1]
data2["unit"] =     a[2]
data2["id"] =       a[3]

data3 = {}
data3["message"] = "send"
data3["code"] = data2

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 5000))
s.send(json.dumps(data1))
result1 = json.loads(s.recv(1024))
print result1
s.send(json.dumps(data3))
s.close()

Edit: oh yeah, remember to change the ip-address if you run it from a different computer and point it to the raspberry.
 
Reply
#2
What operating system is that computer running?
 
Reply
#3
Quote:What operating system is that computer running?


Different kinds of Linux (Arch, Mint and Slack), but I guess it should work in windows/mac too.
 
Reply
#4
You most probably can just use pilight-send natively on those distibutions. I've tested it on FreeBSD, Ubuntu, and Windows.
 
Reply
#5
Well, on my pogoplug (armv5te with soft float) I was not able to compile because of libwebsocket.o (it's already armv6) and I think this was also the case on a 64bit distribution. Maybe it would have been a better idea to tackle that problem instead of working around it like I did now, eh? Smile I will look into it some more. Thanks.
 
Reply
#6
Yes, you're right. I disabled that possibility once i introduced that libwebsocket.o file. I shall look if i can make that file compile on the fly.

I've added the libwebsockets source file so the libwebsockets include file is now compiled on the fly. Can you check if a cross-compiled pilight-send works again?
 
Reply
#7
Good morning,

I don't see the changes on the github yet...
 
Reply
#8
Check the memory branch.
 
Reply
#9
Aha, yep it compiles on x86_64 (although the protocols folder seems to be in the wrong place).

pilight-send does not quite work yet from that pc.

When, from the raspberry pi itself, i do:
Code:
sudo pilight-send -p kaku_dimmer -i 247 -u 0 -f

i get:

Code:
[Aug 28 09:55:57] pilight-daemon: INFO: new client, ip: 127.0.0.1, port: 54986
[Aug 28 09:55:57] pilight-daemon: DEBUG: client fd: 14
[Aug 28 09:55:57] pilight-daemon: DEBUG: client id: 1
[Aug 28 09:55:57] pilight-daemon: DEBUG: socket recv: {"message":"client sender"}

[Aug 28 09:55:57] pilight-daemon: DEBUG: socket write succeeded: {"message":"accept client"}

[Aug 28 09:55:57] pilight-daemon: INFO: client recognized as sender
[Aug 28 09:55:57] pilight-daemon: DEBUG: socket recv: {"message":"send","code":{"protocol":"kaku_dimmer","unit":$0","id":"247","off":"1"}}

[Aug 28 09:55:57] pilight-daemon: INFO: client disconnected, ip 127.0.0.1, port 54986
[Aug 28 09:55:58] pilight-daemon: DEBUG: successfully send archtech_dimmers code
[Aug 28 09:55:58] pilight-daemon: DEBUG: socket write succeeded: {"origin":"config","type":2,"devices":{"beneden$
:["bar"]},"values":{"state":"off"}}

But when i do this from the other computer (x86_64):
Code:
pilight-send -S 10.0.0.22 -P 5000 -p kaku_dimmer -i 247 -u 0 -f

I get:
Code:
[Aug 28 09:59:09] pilight-daemon: INFO: new client, ip: 10.0.0.3, port: 57122
[Aug 28 09:59:09] pilight-daemon: DEBUG: client fd: 14
[Aug 28 09:59:09] pilight-daemon: DEBUG: client id: 1
[Aug 28 09:59:09] pilight-daemon: DEBUG: socket recv: {"message":"client sender"}

[Aug 28 09:59:09] pilight-daemon: DEBUG: socket write succeeded: {"message":"accept client"}

[Aug 28 09:59:09] pilight-daemon: INFO: client recognized as sender
[Aug 28 09:59:09] pilight-daemon: INFO: client disconnected, ip 10.0.0.3, port 57122

So, it does connect, but does not switch anything.
 
Reply
#10
I will investigate this later.

I've tried it myself and it works perfectly...
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Already fixed in 8.1.2] Loss of webserver-connection Ulrich.Arnold 34 2,641 08-02-2019, 02:25 PM
Last Post: curlymo
  Using pilight-daemon socket API def_meggi 4 385 04-29-2019, 05:58 PM
Last Post: curlymo
  deleyCON remote plug socket (arctec_screen_old protocol) itsMe 6 1,178 04-01-2018, 08:35 PM
Last Post: itsMe
  WebSocket connection failed: One or more reserved bits gregnau 8 2,153 02-16-2018, 10:02 PM
Last Post: curlymo
Thumbs Up Newbie problems, usbnano connection and rsl366 pukington 1 498 02-14-2018, 07:36 PM
Last Post: pukington
  Windows HTTP connection andbehtls 1 1,082 01-14-2018, 02:24 PM
Last Post: bnk72
  API, receive output, socket Twain 6 1,937 06-27-2017, 06:07 PM
Last Post: pilino1234
  Illumina, cannot obtain connection hansrijn2 5 1,334 05-31-2017, 04:29 PM
Last Post: hansrijn2
  Installing WiringX Python Module on Radxa Rock 8BitBob 5 1,266 04-20-2017, 07:20 PM
Last Post: curlymo
  Home Easy 109 double socket pconnor 1 1,002 03-15-2017, 12:19 AM
Last Post: IzzyWizzy

Forum Jump:


Browsing: 1 Guest(s)