Raspberry Pi Gateway Setup

Started by Rick, November 10, 2013, 11:33:01 PM

Rick

I am having a blast setting up my first Raspberry Pi with the GarageMote and a bunch of Moteinos in the mail.  I am no stranger to building things and IT projects, but Linux is a rusty skill for me.  With a lot of help from Google and a friend that plays with this stuff regularly, I have made some pretty good progress in setting up the Pi home automation gateway.

I closely followed the third part of the gateway series.  After building the default site config file ( /etc/nginx/sites-available/default), I got errors restarting nginx, "unexpected end of file, expecting "}" in /etc/nginx/sites-enabled/default:72".  I found a missing } at the end of the file after the ) on line 71.  I later found the file same config file posted on the Github site which confirmed correct closing }.

Another problem I encountered was testing the NGINX setup.  I created the index.php file that should generate the PHP information splash.  I was prompted for credentials, proceeded through the cert error page, and loaded index.php, but received a blank screen in the browser.  I confirmed that the page had loaded by viewing the source, and there were no errors and the logs confirmed that the page had loaded with no errors.  I was able to confirm the PHP information by running a command line phpinfo, So the nginx-PHP combo is apparently ok.

Moving on, I had a little problem getting through the addition of the path statement in /etc/profile.  I added

NODE_JS_HOME="/opt/node"
PATH="$PATH:$NODE_JS_HOME/bin"


just above the existing

export PATH

I guessed that is where it goes, seems to work.


I am now stuck installing serialport from npm.  The current version is now v 1.2.4.  I am getting an error:

pi@TropicalPi ~/moteino $ npm install serialport
npm http GET https://registry.npmjs.org/serialport
npm http 304 https://registry.npmjs.org/serialport
npm ERR! Error: EACCES, mkdir '/home/pi/moteino/node_modules'
npm ERR!  { [Error: EACCES, mkdir '/home/pi/moteino/node_modules']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/home/pi/moteino/node_modules',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/home/pi/moteino/node_modules/serialport',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack:
npm ERR!    [ '/opt/node/lib/node_modules/npm/node_modules/fstream/lib/writer.js:171:23',
npm ERR!      '/opt/node/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR!      'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Linux 3.6.11+
npm ERR! command "/opt/node/bin/node" "/opt/node/bin/npm" "install" "serialport"
npm ERR! cwd /home/pi/moteino
npm ERR! node -v v0.10.12
npm ERR! npm -v 1.2.32
npm ERR! path /home/pi/moteino/node_modules
npm ERR! fstream_path /home/pi/moteino/node_modules/serialport
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/home/pi/moteino/node_modules'
npm ERR! fstream_stack /opt/node/lib/node_modules/npm/node_modules/fstream/lib/writer.js:171:23
npm ERR! fstream_stack /opt/node/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: EACCES, open 'npm-debug.log'
npm ERR!  { [Error: EACCES, open 'npm-debug.log'] errno: 3, code: 'EACCES', path: 'npm-debug.log' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Linux 3.6.11+
npm ERR! command "/opt/node/bin/node" "/opt/node/bin/npm" "install" "serialport"
npm ERR! cwd /home/pi/moteino
npm ERR! node -v v0.10.12
npm ERR! npm -v 1.2.32
npm ERR! path npm-debug.log
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, open 'npm-debug.log'
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /home/pi/moteino/npm-debug.log
npm ERR! not ok code 0



I tried sudo:

pi@TropicalPi ~/moteino $ sudo npm install serialport
sudo: npm: command not found


Any ideas? 

RonD

Rick,

You beat me to it, came to comment on the missing "}" as well as the questions on where NODE_JS_HOME="/opt/node" should go. I put it in the same place you did.

As far as getting serialport and socket.io installed I also had the same error. Using sudo /opt/node/bin/npm install serialport and sudo /opt/node/bin/npm install socket.io worked for me as suggested by another forum user. Reference post #4 http://lowpowerlab.com/forum/index.php/topic,154.0.html

Everything seems to be working now.

Ron

RonD

One more change that I needed to get everything working correctly.

In the chat.html file I changed <script src="socket.io.js"></script> to <script src="/socket.io/socket.io.js"></script> this post http://stackoverflow.com/questions/17757728/getting-the-right-client-side-socket-io-js-file discusses why. Also if you do this you do not need to add the socket.io-client.js file to the /var/www/default directory

Hope that helps.

Ron

Felix

Thanks for the heads up, I added the missing "}" at the end in the blog post. The files are also available at github: https://github.com/LowPowerLab/RaspberryPi-Gateway/tree/master/server_config_files

For socket.io.js - that is just a static script which comes with installing socket.io. You can include it without installing socket.io, and then on the client include the static client file socket.io-client.js

Serialport might be a tricky one because of mismatching versions. Not really sure if the creator keeps it up to date or if the Pi repositories keep a good version, I had troubles with that too and had to install a specific node version that ended up accepting serialport, I included a short script which can check for you if serialport installed correctly.

Rick

Thanks, Ron and especially you, Felix!  It is working.  The real fun will start when the parts arrive.

The installs from npm went ok, but there was a warning during socket.io

http 200 https://registry.npmjs.org/redis/-/redis-0.7.3.tgz
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field

It seems to work ok.

Rick

Felix

Hm, I believe that's got something to do with redis, if you got that installed...

Rick

Picking up on this project again, having a couple of problems.
First, I can start gateway.js start with
node gateway.js &.
I created the startup script:
sudo nano /etc/init.d/nodejs-moteino.sh
I then executed the following commands
chmod 755 nodejs.sh
the filename should be nodejs-moteino.sh instead of nodejs.sh, right?  Root is the owner, so I had to add sudo.
sudo cp nodejs.sh /etc/init.d  It was already in init.d
sudo update-rc.d nodejs.sh defaults
When I run sudo update-rc.d nodejs-moteino.sh defaults I get an error:
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'nodejs-moteino.sh' missing LSB tags and overrides
insserv: There is a loop between service nginx and nodejs-moteino.sh if stopped
insserv:  loop involving service nodejs-moteino.sh at depth 2
insserv:  loop involving service nginx at depth 1
insserv: Stopping nodejs-moteino.sh depends on nginx and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header


Also, when I load the default page after manually starting gateway.js,  I get

Garage is ... OPN/CLS Clear Log
(date); getting status

and the chrome JavaScript Console shows an error:
GET https://192.168.1.12/socket.io/1/?t=1386564747642 502 (Bad Gateway) socket.io.js:1659
Socket.handshake socket.io.js:1659
Socket.connect socket.io.js:1699
maybeReconnect socket.io.js:2040


Also, I am not getting any LED activity on the base moteino.

Rick

Felix

Hey Rick, sorry for ignoring this. Really not sure what to suggest.

I think i've seen various types of errors in my own trials of setting this up. I started from scratch before preparing the instructions and that time I ran into far fewer issues (learning from first time around). However other people have told me they pulled hairs trying to get this setup working on their Pis, and it always seems to be an issue of permissions.
What I'm really hoping for is that the Pi platform will mature and the repositories will start adopting some of the latest versions of the software packages involved in this demo. Things like nginx, node, node-serial, socket-io. Those should all work together out of the box by running their npm-installs etc.

Looking at your errors, I've seen that first error sometime (missing LSB tags ...), I think it has to do with the init script not being formatted correctly, but I did provide a good script so it should not give you that, unless something else happened in between or maybe you are running a different distribution or there's something I'm missing.
However you should still be able to start the node js socket server manually or by alternative means.

The other error probably has to do with your socket.io client/server js files being in the right places. I placed my client socketio-client.js file manually where it will be found by the HTML page so double check that.

Let us know if you have updates or if you found solutions to these issues..

wnance

I am trying to install the Raspberry PI gateway.  So far, I made it to the command "service nginx restart' and I get the error "unknown user directive user www-data in /etc/nginx/nginx.conf".  I am new to Linux and don't quite understand all the steps, but everything went well up to this point.  Any idea what may be wrong?

Felix

Hmm, sounds like theres some missing user entry or permissions issue somewhere. Make sure to follow the guide to the iota.
It's still possible I might have missed something in the guide at some point but from what I can tell the guide works for most people.
Please let me know if you still can't get past this and I will look into more detail where that might come from.

wnance

#10
Thanks Felix
I reloaded the PI from scratch and carefully went through each step and got the same error. I will try again this weekend.  I was thinking about just trying to stop after the sudo make install command and try testing Nginx with a very simple install script and simple index.htm page just to verify that Nginx is working properly before I proceed with the more complicated setup with SSL, PHP, etc.  I figure I could just access the PI from a browser on another machine on my home network and verify that Nginx is at least serving simple web pages.
Did you originally try something simple like that when you loaded Nginx the first time on the PI?  If so, if you have any simple configuration files or advice you could share I would appreciate it.

Update:
I tried to install again and I get this output:
pi@raspberrypi ~/sources/nginx-1.4.2 $ sudo update-rc.d -f nginx defaults
update-rc.d: using dependency based boot sequencing
update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match nginx Default-Start values (     2 3 4 5)
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match nginx Default-Stop values (      0 1 6)
insserv: Script nginx is broken: incomplete LSB comment.
insserv: missing valid name for `Provides:' please add.
insserv: warning: script 'mathkernel' missing LSB tags and overrides

Final update:
On.my third attempt I finally got Nginx installed. Apparently, I caused errors in the config files when I cut and pasted code from the listings on the setup page.  Once I typed all the code things started working as expected and now Nginx is working fine.

Felix

Oh great! Happy to hear that it started working.
I pretty much had the same experience, took me 2-3 times from scratch to get all of the components working. It's mostly because at the time (and perhaps still true) the software packages were not all compatible with each other so I had to get specific versions, do some building, and a lot of settings tweaking.
But when it works ... it makes it worth it :)
Be sure to save an image with a working setup. You don't want to go through all that again!

wnance

Thanks Felix. Good advice -- I saved an image and now I am trying to get the chat app to work.  Hopefully this won't take long and now I can fall back on a known good image if I have trouble.  Hopefully I can soon start installing some moteinos!

I have three Moteinos in hand and have ordered the SwitchMote and the ATXRaspi.  Once I get a small Moteino network going I hope to install a complete home automation system using your system.  Your system seems to me the best combination of low cost and flexible design that I can find so I will be supporting your work by buying your products.  Keep up the good work and keep those new products coming so amateurs like me can learn how this stuff works.