@sarthak444
@KathyReid would be a good place to start as she is up to speed on all things Mycroft, if she doesn’t know she will probably know who will.
I don’t have a lot of experience with using websockets with Mycroft, however I am documenting the MessageBus at the moment. It operates on port 8181 if that is any help?
Here is a working websocket code sample, If it is helpful for your documents. I will be interested too in your documents once they are pulled together.
I do it like this:
from websocket import create_connection, WebSocket
from mycroft.messagebus.message import Message
def connectToWebsocket(self):
uri = 'ws://localhost:8181/core'
ws = create_connection(uri)
utt = '{"context": null, "type": "recognizer_loop:utterance", "data": {"lang": "en-us", "utterances": ["hello human, how are you?"]}}'
ws.send(utt)
ws.close()
We now have the Message Bus documented here;
Yeah, got it working. The answer was in the config file…
Thank you anyway!
But I still get replies in json format(on the webserver) while my goal is to attain a UI similar to this (https://www.youtube.com/watch?v=J8NGy9UwkPI)
you need to register an event handler:
self.add_event('speak', self.yourHandler) # this will call the function/method yourHandler everytime a speak-event is triggered
def yourHandler(self, message):
response = message.data.get("utterance") # this extracts the utterance from the event
print(response) # prints the utterance part from the Event in CLI
hope this helps
I have a couple of what are probably very stupid questions.
-
Does this have to run on the Mark I itself? I tried running the code that @pcwii provided, and it seems to work, but fails in the end. A quick nmap shows that port 8181 (from @KathyReid) isn’t open. Only port 22.
-
On the Mark I, doing a straight up pip install of the websocket library fails because it doesn’t think pip is installed. pip is there, but in the Mycroft core virtual environment. If I just do an apt-get install python-pip, it’s wanting me to install a host of things, and I’m trying not to hose up my Mark I. Is it safe to do an apt-get install pip on these things?
I apologize in advance for these questions. I admit that I’m mucking around in things that are outside of my comfort zone and over my head for the most part. I appreciate your patience.
Never apologise for questions!
Are you able to post the error messages you get and we can dig a little deeper?
Sure, but the errors are pretty uninteresting. I’m using the code @pcwii posted last week exactly other than updating IP information to actually point to my Mark I. That’s here:
When I execute that code, I get this output after quite some time:
Traceback (most recent call last):
File “test.py”, line 22, in
send_message(‘speak, hello’)
File “test.py”, line 17, in send_message
ws = create_connection(url)
File “/usr/local/lib/python2.7/dist-packages/websocket/_core.py”, line 494, in create_connection
websock.connect(url, **options)
File “/usr/local/lib/python2.7/dist-packages/websocket/_core.py”, line 217, in connect
options.pop(‘socket’, None))
File “/usr/local/lib/python2.7/dist-packages/websocket/_http.py”, line 120, in connect
sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
File “/usr/local/lib/python2.7/dist-packages/websocket/_http.py”, line 186, in _open_socket
raise error
socket.error: [Errno 110] Connection timed out
I don’t think it’s anything to do with the code, which seems pretty straight forward. I did do an nmap of the system, and the only open port is showing as 22, and if the MessageBus operates on 8181 I’m not sure how it would establish a connection from outside of the Mark I.
From what I can see here, the code is executing under Python 2.7 - is this on Linux or another type of Mycroft device?
It’s on my main computer, which is running Mint 19. It is running under Python 2.7, but the results are the same if I run it with Python 3.6, which I also have installed.
Traceback (most recent call last):
File "test.py", line 22, in <module>
send_message('speak, hello')
File "test.py", line 17, in send_message
ws = create_connection(url)
File "/home/mike/.local/lib/python3.6/site-packages/websocket/_core.py", line 494, in create_connection
websock.connect(url, **options)
File "/home/mike/.local/lib/python3.6/site-packages/websocket/_core.py", line 217, in connect
options.pop('socket', None))
File "/home/mike/.local/lib/python3.6/site-packages/websocket/_http.py", line 120, in connect
sock = _open_socket(addrinfo_list, options.sockopt, options.timeout)
File "/home/mike/.local/lib/python3.6/site-packages/websocket/_http.py", line 186, in _open_socket
raise error
File "/home/mike/.local/lib/python3.6/site-packages/websocket/_http.py", line 170, in _open_socket
sock.connect(address)
TimeoutError: [Errno 110] Connection timed out
I’m trying to connect to a Mark I unit, but port 8181 doesn’t seem open.
Silly question, is ssh enabled on the Device - ie have you used the button on the top to enable ssh - it seems like a silly question but I often forget to do it after a reimage.
Yea, ssh is life. There is no living without ssh. 
Hearing you!
OK, I’m not sure what the issue is here. What does a traceroute trace show?
Traceroute is also uninteresting because it provides no information.
traceroute to mark1.local (Real IP Address Appears Here), 30 hops max, 60 byte packets
1 * * *
2 * * *
3 * * *
4 * * *
5 * * *
6 * * *
7 * * *
8 * * *
9 * * *
10 * * *
11 * * *
12 * * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
I’m able to connect to the Mark I via ssh without difficulty.
Darn I thought that would have shown something. No firewall? Nothing like that? I wonder if it’s a ufw command that’s needed, like
sudo ufw allow 8081
Nope, my laptop and the Mark I are on the same internal network. No firewall between them. The only slight variation (and this sounds weird) is that my laptop is wired to the network, and the Mark I is wireless. They’re connected to the exact same router though.
Aaaaaaaah!
OK, check to see whether your router has something called “wireless isolation” turned on. It’s kinda like a firewall between wired and wireless devices.