基于Iotgo的物联网云端环境搭建

Iotgo平台简介

IoTgo项目的初衷是想让用户可以像部署zencart搭建博客,安装 phpbb创建论坛一样,通过部署IoTgo建立自己的云端服务器,服务器不仅仅是接口开放,而是全部代码开源


github链接
物联网平台及webUI
https://github.com/itead/IoTgo
手机端app demo
https://github.com/itead/IoTgo_Android_App
Arduino库
https://github.com/itead/ITEADLIB_Arduino_IoTgo
树莓派,CB等卡片式电脑SDK
https://github.com/itead/Segnix/tree/master/libraries/itead_IoTgo

通信协议层支持HTTP Websocket,MQTT, CoAP

其基本架构如下图所示


服务器搭建

服务器环境

主机:x86_64
操作系统: Ubuntu 16.04.3 LTS xenial
开发语言:html  javascript  python  go 
Web engine:nodejs
关系型数据库:mariaDB
NoSQL: mongoDB
开发依赖工具: git npm bower forever

平台依赖环境安装

NoSQL数据库mongoDB安装与配置

1
2
3
tar xvf ubuntu 16.04 Linux 64-bit.tar`
sudo mkdir /usr/local/mongodb `
cp ./ mongodb-linux-ubuntu 16.04 -x64/* /usr/local/mongodb -rf
  • 将mongodb的bin目录添加到PATH
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
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
export PATH=$PATH:/usr/local/mongodb/bin

source /etc/profile 配置立即生效

  • 编辑mongod.conf
    创建/etc/mongod.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vim /etc/mongod.conf
#详细记录输出
verbose = true
# 指定服务端口号,默认端口27017
port = 27017
# 指定MongoDB日志文件,注意是指定文件不是目录
logpath = /var/log/mongodb/logs/mongodb.log
# 使用追加的方式写日志
logappend = true
# 指定数据库路径
dbpath = /data/db/
#设置每个数据库将被保存在一个单独的目录
directoryperdb = true
# 启用验证
auth = false
#以守护进程的方式运行MongoDB,创建服务器进程
fork = true
#安静输出
quiet = true
  • 创建配置文件中提到的用来保存文件的数据库目录,日志目录,日志文件
1
2
3
sudo mkdir -p /data/db/
sudo mkdir -p /var/log/mongodb/logs/
sudo touch /var/log/mongodb/logs/mongodb.log
  • 编写脚本,注册开机启动
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
31
32
33
34
35
36
37
38
39
40
vim /etc/init.d/mongodb
#!/bin/sh
#BEGIN INIT INFO
# Provides: mongodb
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO
. /lib/lsb/init-functions
PROGRAM=/usr/local/mongodb/bin/mongod
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
start)
ulimit -n 3000
log_begin_msg "Starting MongoDB server"
$PROGRAM -f /etc/mongod.conf
log_end_msg 0
;;
stop)
log_begin_msg "Stopping MongoDB server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
log_end_msg 0
;;
status)
;;
*)
log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"
exit 1
esac
exit 0
  • 添加可执行权限
1
sudo chmod +x /etc/init.d/mongodb
  • 注册开机脚本
1
sudo update-rc.d mongodb defaults(移除用update-rc.d -f mongodb remove)
  • 开启服务并测试连接
1
2
sudo service mongodb start(停止服务用sudo service mongodb stop)
sudo mongo

Node.js安装与配置

执行以下命令安装

1
2
3
4
5
sudo apt-get update
sudo apt-get install -y python-software-properties software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

node版本管理,升级到最新版本,否则后面编译会报错

npm ERR! Make sure you have the latest version of node.js and npm installed.
Npm更新到最新版本

node的模块n是专门用来管理node.js的版本的
首先安装n模块

1
npm install -g n

升级node.js到最新稳定版

1
n stable

测试

1
nodejs -v

v4.2.6

表明已成功安装nodejs最新版本


安装nodejs包管理工具

1
sudo apt-get install npm

安装forever,让node应用作为系统服务使用

1
npm install forever -g

安装bower,front-end包管理工具

1
npm install -g bower

iotgo环境构建

  • 从github导入代码
1
git clone https://github.com/itead/IoTgo.git
  • 安装python依赖
1
cd IoTgo && npm install
  • 进入web app frontend 安装依赖
1
cd public/frontend && sudo bower install –allow-root
  • 进入web app backend 安装依赖
1
cd ../backend && sudo bower install –allow-root
  • 配置iot, config.js为iotgo启动配置文件
1
2
cd ../..
cp config.js.sample config.js
  • 更改config.js
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
31
module.exports = {
host: 'localhost', // Hostname of IoTgo platform
db: {
uri: 'mongodb://localhost/iotgo', // MongoDB database address
options: {
user: 'iotgo', // MongoDB database username
pass: 'iotgo' // MongoDB database password
}
},
jwt: {
secret: 'jwt_secret' // Shared secret to encrypt JSON Web Token
},
admin:{
'iotgo': 'iotgo' // Administrator account of IoTgo platform
},
page: {
limit: 50, // Default query page limit
sort: -1 // Default query sort order
},
recaptcha: {
secret: '', // Google reCAPTCHA serect
url: 'https://www.google.com/recaptcha/api/siteverify'
},
pendingRequestTimeout: 3000,
mailgun: {
api_key:'', //Mailgun API Key
domain:'', //Mailgun Domain Name
from:'' //Mailgun Default SMTP Login Email
},
upgradeUrl:"http://v.itead.cc/api/upgrade"
};
  • 让Iotgo作为系统服务使用

创建开机启动项,编辑脚本

1
sudo vim /etc/init.d/iotgo

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
#
# An init.d script for running a Node.js process as a service using Forever as
# the process monitor. For more configuration options associated with Forever,
# see: https://github.com/nodejitsu/forever
#
# This was written for Debian distributions such as Ubuntu, but should still
# work on RedHat, Fedora, or other RPM-based distributions, since none of the
# built-in service functions are used. So information is provided for both.
#
NAME="ITEAD IoTgo"
NODE_BIN_DIR="/usr/bin:/usr/local/bin"
NODE_PATH="/home/utopiaor/develop/IotGo/IoTgo/node_modules"
APPLICATION_PATH="/home/utopiaor/develop/IotGo/IoTgo/bin/www"
PIDFILE="/var/run/iotgo.pid"
LOGFILE="/var/log/iotgo.log"
MIN_UPTIME="5000"
SPIN_SLEEP_TIME="2000"
PATH=$NODE_BIN_DIR:$PATH
export NODE_PATH=$NODE_PATH
start() {
echo "Starting $NAME"
forever \
--pidFile $PIDFILE \
-a \
-l $LOGFILE \
--minUptime $MIN_UPTIME \
--spinSleepTime $SPIN_SLEEP_TIME \
start $APPLICATION_PATH 2>&1 > /dev/null &
RETVAL=$?
}
stop() {
if [ -f $PIDFILE ]; then
echo "Shutting down $NAME"
forever stop $APPLICATION_PATH 2>&1 > /dev/null
rm -f $PIDFILE
RETVAL=$?
else
echo "$NAME is not running."
RETVAL=0
fi
}
restart() {
stop
start
}
status() {
echo `forever list` | grep -q "$APPLICATION_PATH"
if [ "$?" -eq "0" ]; then
echo "$NAME is running."
RETVAL=0
else
echo "$NAME is not running."
RETVAL=3
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
  • 添加可执行权限
1
sudo chmod 755 /etc/init.d/iotgo
  • 注册启动脚本
1
sudo update-rc.d iotgo defaults
  • 测试
1
2
sudo systemctl daemon-reload
sudo service iotgo start

测试iotgo

1
DEBUG="iotgo" ./bin/www
  • 报错

1 Error: Cannot find module ‘../build/Release/bson’
2 Error: listen EACCES 0.0.0.0:80

对于1,没有安装bson,执行

1
npm install bson

,并修改

(/home/utopiaor/develop/iot/IoTgo/node_modules/bson/ext/index.js:15:10)
bson = require(‘../build/Release/bson’); 为
bson = require(‘bson’);

对于2,80端口被其他应用占用,kill 掉其应用

1
sudo lsof -n -P | grep 80

然后kill掉相关进程
或者更改端口号为8080

1
sudo PORT="8080" DEBUG="iotgo" ./bin/www

出现打印

iotgo Express server listening on port 80 +0ms

证明iotgo环境已搭建成功

本机浏览器访问:localhost,局域网内访问192.168.0.10,发现进入的是nodejs的欢迎界面
更换端口号为8080,node默认占用80端口,或者kill掉node服务
关闭nodejs 服务

1
systemctl stop nodejs.service

重新启动iotgo

1
sudo PORT="8080" DEBUG="iotgo" ./bin/www

出现iotgo定制页面,have a fun !

文章目录
  1. 1. Iotgo平台简介
  2. 2. 服务器搭建
    1. 2.1. 服务器环境
    2. 2.2. 平台依赖环境安装
      1. 2.2.1. NoSQL数据库mongoDB安装与配置
      2. 2.2.2. Node.js安装与配置
      3. 2.2.3. 安装nodejs包管理工具
      4. 2.2.4. 安装forever,让node应用作为系统服务使用
      5. 2.2.5. 安装bower,front-end包管理工具
    3. 2.3. iotgo环境构建
      1. 2.3.1. 测试iotgo