Author Topic: R-Pi with node.js & socket.io for home automation  (Read 6670 times)

jbeale

  • Jr. Member
  • **
  • Posts: 62
R-Pi with node.js & socket.io for home automation
« on: October 08, 2013, 12:19:25 PM »
Good to see the LowPowerLab blog posts on this subject! Just yesterday I got a R-Pi  working with "server push" using node.js and socket.io as mentioned here: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=36&t=57539&p=434008#p434008
It is very cool to have an event on the R-Pi show up immediately in the browser on my phone, without having to periodically reload.
I appreciate your notes on security. I haven't dealt with that yet since my Pi currently has no WAN link, only a self-hosted wifi access point with WPA2.
« Last Edit: October 08, 2013, 12:21:14 PM by jbeale »

dargs

  • NewMember
  • *
  • Posts: 4
Re: R-Pi with node.js & socket.io for home automation
« Reply #1 on: October 15, 2013, 09:03:58 AM »
Sorry for the newbie linux questions, hoping to learn some JS and socket skills, and this example looked great for getting sensors/controls going via web control.

trying the chat.html example at http://lowpowerlab.com/blog/2013/10/07/raspberrypi-home-automation-gateway-setup/ and got the first steps, the php and ssl working! cool...

But when I go to 192.168.0.22/chat.html .. the html appears, but no server action.. just "USERS" and spot to enter text  it's the socket.io doesn't work. The Pi doesn't show any connections..

pi@raspberrypi ~/moteino $ node chat.js
   info  - socket.io started


when i 'inspect element' in chrome, it shows an error, ""Uncaught ReferenceError: io is not defined"

To put the "socket.io-client.js" file in my /var/www/default directory, i used "wget https://raw.github.com/LearnBoost/socket.io-client/master/socket.io-client.js"

Do I need to do anything else at that point ?

Noticed that the chat.html refers to "<script src="socket.io.js"></script>"  So tried changing it to src="socket.io-client.js". now chrome doesn't report a 404 for socket.io.js

Now chrome element inspector looks better, but under Resources, scripts, socket.io-client.js shows a "Uncaught Error: failed to require "socket.io" from "root"" ?! seems to be some debugging in the socket.io-client.js, but not sure what it's telling me!

I've tried changing the file permissions for socket.io-client.js

Tried renaming the socket.io-client.js file to socket.io.js and get "Uncaught error ReferenceError: io is not defined" in chrome.

where it said "YOUR_PI_ADDRESS" i put     

   var socket = io.connect('192.168.0.22', {'connect timeout': 1000});



running out of ideas.. have tried installing everything from scratch and still end up at this point.. any clues would be great!

thanks
dargs

tomsk31

  • NewMember
  • *
  • Posts: 1
Re: R-Pi with node.js & socket.io for home automation
« Reply #2 on: October 15, 2013, 05:02:34 PM »
In the blog post, Felix installs socket.io using npm. I would do the same to get the same result.

Hope that helps.

Tom

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6620
  • Country: us
    • LowPowerLab
Re: R-Pi with node.js & socket.io for home automation
« Reply #3 on: October 15, 2013, 10:21:52 PM »
Ok so after you sort out the socket-io-client.js script (which is trivial), it almost sounds to me like your socket is not setup right. You're pointing to the same address as your webserver (which is just the Pi's IP) which implies that you proxied the websocket through NGINX. Did you actually do that?

If not, you could hit the websocket at port 8080 or whatever port you are running it (see chat.js).

dargs

  • NewMember
  • *
  • Posts: 4
Re: R-Pi with node.js & socket.io for home automation
« Reply #4 on: October 18, 2013, 11:18:56 PM »
I had used npm install socket.io, but had to use 'sudo /opt/node/bin/npm install socket.io' as root doesn't seem to have the same Path as the Pi user maybe?

Also 'service nginx restart' i had to run as 'sudo service nginx restart', as it was giving me permissions errors.

Maybe these were causing problems..

Otherwise I was following the blog word for word, so had the proxy lines in the default sites-available section... going to try again today.. learning lots anyway!

Thanks!



Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6620
  • Country: us
    • LowPowerLab
Re: R-Pi with node.js & socket.io for home automation
« Reply #5 on: October 19, 2013, 11:43:23 AM »
Definitely a steep learning curve...  it can be frustrating when it doesn't work, don't give up, it will eventually come to fruition.

juris.g

  • NewMember
  • *
  • Posts: 2
  • Country: lv
Re: R-Pi with node.js & socket.io for home automation
« Reply #6 on: October 21, 2013, 02:19:16 AM »
Try copying the socket.io client library installed with npm:
Code: [Select]
cd /var/www/default
cp -R ~/moteino/node_modules/socket.io/node_modules/socket.io-client/dist ./
mv dist socket.io
and edit the chat.html so that it uses copied files:
Code: [Select]
<script src="socket.io/socket.io.js"></script>

billchurch

  • NewMember
  • *
  • Posts: 12
Re: R-Pi with node.js & socket.io for home automation
« Reply #7 on: October 28, 2013, 06:42:33 PM »
So are there any plans to formalize a home automation system based on Node.js/socket.io?

I'm really liking what I see with Node.js so far, I've got a Universal Devices 994i right now interfaced into my Elk M1 Gold and an Insteon HA network.

Would like to replace it with a rPi powered unit that I can do a little more tinkering with, and add in my growing collection of Moteinos!

I'd be happy to contribute wherever I can in this regard.  Would like to start gathering some todos and some rough design ideas.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6620
  • Country: us
    • LowPowerLab
Re: R-Pi with node.js & socket.io for home automation
« Reply #8 on: October 29, 2013, 08:09:57 AM »
I've started thinking of how to standardize the Moteino communications, so that any new sensor/mote/device can report data or receive simple ASCII commands. Things like TMP:123.45 or GRG:CLS. But need that precious time to implement everything and make it all come together on the Pi node.js and the UI installation. Looks like EmonCMS is attempting a shift towards TimeStore which is interesting so I'll keep an eye on that progress.
UPDATE: EmonCMS folks have added a PHP-ported Timestore feed mechanism, this should be much faster than traditional straight MySQL logging, see this for details: http://openenergymonitor.org/emon/node/3868
« Last Edit: February 03, 2015, 09:08:34 PM by Felix »