octoblu / meshblu

Meshblu is a cross-protocol IoT machine-to-machine messaging system.
https://meshblu.readme.io/
MIT License
816 stars 182 forks source link

Raspberry Pi help getting started #54

Closed gadgethome closed 10 years ago

gadgethome commented 10 years ago

Hi, I have skynet.im and also skynet hub installed on my raspberry pi. Having a little difficulty on how to get my home automation talking to each other.

I have a usb tellstick attached to the Pi and 4 power sockets around the house. To turn on socket 1, I need to issue the following command:

tdtool --on 1

Please could someone give some advice/walk-through on how and where to get started. It would be much appreciated.

Thanks

monteslu commented 10 years ago

I think the easiest route would be to install the hub shell plugin: npm install skynet-gw-shell

Then there's a few ways to configure a subdevice, but a simple one is if you have a websocket

socket.emit('gatewayConfig', {
  uuid:  'xxxxxx----my hub uuid----xxxxxx',
  token: 'xxxx----my hub token----xxxx',
  method: 'createSubdevice',
  type: 'skynet-gw-shell',
  name: 'tellstick',
  options: {shellCommand: 'tdtool'}
}, function(results){ console.log(results); });

after that you can message the gatway:


socket.emit('message', {
  devices:  'xxxxxx----my hub uuid----xxxxxx',
  subdevice: 'tellstick',
  payload: ['--on', '1']
});
gadgethome commented 10 years ago

Thank you. I'm looking forward to getting it working.

gadgethome commented 10 years ago

Just trying the websocket. Is the socket I connect to, the hub socket? (8889 in my case) or the local copy of skynet.im (3000) ?

I tried the following code. There are no errors but I think its because its not connecting at all.


var io = require('socket.io-client') socket = io.connect('raspberrypi', { port: 8889 });

socket.on('connect', function(){ console.log('Requesting websocket connection to SkyNet');

socket.on('identify', function(data){ console.log('Websocket connecting to SkyNet with socket id: ' + data.socketid); console.log('Sending device uuid'); socket.emit('identity', {uuid: 'xxxxxxxxxxxxxxxxxx0b1', socketid: data.socketid, token: 'xxxxxxxxxxxxxxxxxx287'}); });

socket.on('notReady', function(data){ if (data.status == 401){ console.log('Device not authenticated with SkyNet'); } }); socket.on('ready', function(data){ if (data.status == 201){ console.log('Device authenticated with SkyNet');

  // Send/Receive messages
  socket.emit('gatewayConfig', {
    uuid:  'xxxxxxxxxxxxxxxxxxxxx0b1',
    token: 'xxxxxxxxxxxxxxxxxxxx287',
    method: 'createSubdevice',
    type: 'skynet-gw-shell',
    name: 'tellstick',
    options: {shellCommand: 'tdtool'}
  }, function(results){ console.log(results); });

}}); });

octoblu commented 10 years ago

You need to connect to skynet (port 3000).

gadgethome commented 10 years ago

Had to give up in the end. Couldn't get it to work at all :(

octoblu commented 10 years ago

We just pushed an update that lets you deploy SkyNet without Redis or MongoDB! Could you try to pull the code again and remove the redis and mongo sections from your config.js and re-run? Note: you will need to also run npm install before running.

gadgethome commented 10 years ago

Thanks for the update. I will give it a try this evening.

gadgethome commented 10 years ago

"We just pushed an update that lets you deploy SkyNet without Redis or MongoDB! Could you try to pull the code again and remove the redis and mongo sections from your config.js and re-run? Note: you will need to also run npm install before running."

I did a fresh install as above and removed redis and mongo from the config. Still having issue either getting the hub on the Pi to communicate with skynet on my Pi and registering a subdevice so that I can issue commands to turn things on/off for home automation.

1) The hub has its own UUID and token. Are these registered on skynet.im or are they registered on my local version running on the pi?

2) Is there a command such as curl to registered the subdevice and the parameters to turn on/off device. Trying through a websocket, I'm either doing it wrong not understanding the correct way of doing it.

I think once I have one subdevice configured and active, adding more should be easier

3) Or is there a way to do all of this either through NodeBlu or Octoblu?

Thanks

monteslu commented 10 years ago

Might have been a bit of confusion here. If you simply want to add a subdevice to your hub, I've added a clarification to the hub documentation on how to do that with a curl command: https://github.com/skynetim/hub

gadgethome commented 10 years ago

Thanks. With your help managed to add the subdevice using curl and its now appearing under the subdevices on the hub. Whats is the easiest way to send a message to the subdevice to check its receiving it correctly?

monteslu commented 10 years ago

There's several ways to talk to it. All you need to make sure is that your message has a subdevice property.

You might want to give it a shot with NodeBlu: https://chrome.google.com/webstore/detail/nodeblu/aanmmiaepnlibdlobmbhmfemjioahilm

Just need an inject button, then a javascript function block that adds the correct subdevice name to the msg object, then finally output to a skynet block that has the uuid of your hub.

monteslu commented 10 years ago

Here's an example screenshot of using NodeBlu for this purpose: http://azprogrammer.com/nodeblu.png

gadgethome commented 10 years ago

My hub subdevices are set up as follows:

Skynet Gateway Sub-devices Name Plugin Type Options remove greeting skynet-greeting [object Object] X living_Room_Lights skynet-gw-shell [object Object] X

I created the following function as in the screenshot

msg.subdevice = 'living_Room_Lights'; msg.payload = { setState: {'--on', '2'} }; return msg;

And then the command should run as tdtool --on 2 (this should turn a powersocket on)

In Nodeblue, it shows this {"topic":"","payload":"--on 2","subdevice":"living_Room_Lights"}

and then followed by

{"topic":"","payload":{"error":"request timeout"},"subdevice":"living_Room_Lights","devices":["xxxxx-my UUID-xxxxxxx"]}

I'm getting close but still not there yet.

monteslu commented 10 years ago

Uncheck the forward response checkbox in nodeBlu. The hub shell plugin doesn't return anything when called.

Also I think they payload can just be:

msg.payload = ['--on', '2'];

you might want to connect a debug node directly to the output of your javascript function as well as the skynet output block

gadgethome commented 10 years ago

Excellent. Thanks both your suggestions worked and now able to turn on and off powersockets.

If I want to run this outside of my LAN, is it just port 8888 that needs to be enabled and this will allow nodeblu to communicate with the Hub on my Pi?

gadgethome commented 10 years ago

Sorry. Also how to get the Hub to send a message back if the On/Off was successful or not?

monteslu commented 10 years ago

You shouldn't need to do any port forwarding at all. As long as the hub is connected to skynet and you know its uuid, you can message it from anywhere.

The current shell plugin is pretty basic and doesn't do anything with the out of the shell command. In order to get it to return a result, you'd likely need to write a custom plugin. Other plugins such as the skynet-hue do give responses on commands.

You could however call the greeting plugin just to make sure that the gateway is up and running.

gadgethome commented 10 years ago

Thanks for all your help and support :)