UniPi - Websocket

Fragen zu Raspberry, FritzBox, Microcontroller, UART, etc...
Antworten
Seecosmea
Foriker
Beiträge: 11
Registriert: So 17. Sep 2017, 15:05
Wohnort: Leer (Ostfriesland)
Kontaktdaten:

UniPi - Websocket

Beitrag von Seecosmea » So 15. Okt 2017, 13:47

Pls revert with a Gambas code to control the UniPi board via websocket. Up to now, I can control the board only via HTTP POST and the Gambas Shell command as follows:

Dim sContents1 As String
Shell "wget -qO- http://127.0.0.1/rest/all" To sContents1
Print Content1

The Evok ReadMe informations is giving the following information:

#### Websocket
Register your client at ws://your.unipi.ip.address/ws to receive status messages. Once it is connected, you can also send various commands to the UniPi
All messages in websocket are sent in JSON string format, eg. {"dev":"relay", "circuit":"1", "value":"1"} to set Relay 1 On.
Check the wsbase.js in www/js/ folder to see example of controlling the UniPi using websocket.

I tried already so much to put it into a Gambas code but not sucsessfully until now. I' am a beginner and need somehow a bit help in this matter.

Benutzeravatar
gambi
Moderator
Beiträge: 249
Registriert: Mi 8. Mai 2013, 21:46
Kontaktdaten:

Re: UniPi - Websocket

Beitrag von gambi » So 15. Okt 2017, 17:55

Hello Seecosmea,
welcome to the forum.
As I understood you want to use the POST-Method to communicate with a PI.

Take a look at these: http://gambas.8142.n7.nabble.com/reques ... tml#a47686

There is an example how to use POST with gambas httpClient.
It's just for XML, but maybe it gives you an idea how to use JSON instead.

gg

Seecosmea
Foriker
Beiträge: 11
Registriert: So 17. Sep 2017, 15:05
Wohnort: Leer (Ostfriesland)
Kontaktdaten:

Re: UniPi - Websocket

Beitrag von Seecosmea » Di 24. Okt 2017, 15:49

Hello Gambi,

tks for you msg but POST & GET ist not the problem.
I would like to use the follwing Python websocket program under Gambas:

-------------------------------------------------------------------------------------------------------------------------------------------
import websocket
import json

url = "ws://127.0.0.1:88/ws"

def on_message(ws, message):
obj = json.loads(message)
dev = obj['dev']
circuit = obj['circuit']
value = obj['value']
print message

def on_error(ws, error):
print error

def on_close(ws):
print "Connection closed"

#receiving messages
ws = websocket.WebSocketApp(url, on_message = on_message, on_error = on_error, on_close = on_close)
ws.run_forever()

#sending messages
ws = websocket.WebSocket()
ws.connect(url)
ws.send('{"cmd":"set","dev":"relay","circuit":"3","value":"1"}')
ws.close()
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
I tried already both methods unsuccessfully:
1) starting via Shell from Gambas and to get the informations into the Gambas program
2) tanslated the upper code into Gambas

Anyhow, I'am somehow new in Linux and Gambas :)

I hope that sombody may could help me out with both approaches :)

Tks in advance and greetings from Cochin - India

Benutzeravatar
gambi
Moderator
Beiträge: 249
Registriert: Mi 8. Mai 2013, 21:46
Kontaktdaten:

Re: UniPi - Websocket

Beitrag von gambi » Di 24. Okt 2017, 20:15

Hallo Seecosmea,
da muss ich leider passen. :(

gg

Seecosmea
Foriker
Beiträge: 11
Registriert: So 17. Sep 2017, 15:05
Wohnort: Leer (Ostfriesland)
Kontaktdaten:

Re: UniPi - Websocket

Beitrag von Seecosmea » So 12. Nov 2017, 02:42

Problem geloest via "Prozess Management" - "EXEC" Tux hat es geschaft:

Public Sub Form_Open()
Exec ["python", "/home/pi/py4.py"] For Input As "py4"
End

Public Sub py4_Read()
TextArea1.Insert(Last.Readline() & gb.Newline)
End

Vielen Dank nochmal; auch an Gambi :D

Antworten

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast