Raspberry Pi IOT Server

From ProjectPages
Jump to: navigation, search

UNDER CONSTRUCTION

$ sudo apt-get update
$ sudo apt-get install python-requests
$ sudo pip install Flask
$ sudo pip install -U flask-cors


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  iot_server.py
#  
#  Copyright 2016  <pi@raspberrypi>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

import requests
from flask import Flask
from flask.ext.cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/")
def hello():
    return "Hello World!"
  
@app.route('/getstatus/<node_id>')
def getstatus(node_id):
	url = 'http://192.168.1.' + node_id + '/'
	print "url %s" % url
	r = requests.get(url)
	print r.text
	return r.text	

@app.route('/turnoff/<node_id>')
def turnoff(node_id):
	url = 'http://192.168.1.' + node_id + '/turnoff'
	r = requests.get(url)
	print r.text
	return r.text
	
@app.route('/turnon/<node_id>')
def turnon(node_id):
	url = 'http://192.168.1.' + node_id + '/turnon'
	r = requests.get(url)
	print r.text
	return r.text
	   
if __name__ == "__main__":
    app.debug = True
    app.run(host='0.0.0.0')


$ sudo nano /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

python '/home/pi/python/iot_server.py' > /dev/null 2>/tmp/upload.log &

exit 0

MQTT

On a raspberry pi

These instructions are taken from here - Mosquitto Debian repository

To use the new repository you should first import the repository package signing key:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key

Then make the repository available to apt:

 cd /etc/apt/sources.list.d/

Then one of the following, depending on which version of debian you are using:

sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list


Then update apt information:


sudo apt-get update

And discover what mosquitto packages are available:

apt-cache search mosquitto

Or just install:

sudo apt-get install mosquitto

Links