Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Satyavrat Wagle
Published © GPL3+

CyberSafe: Your Personal Cloud IoT Platform

Turn your old Raspberry Pi into a Private, Cloud connected IoT platform like Thingspeak or PubNub! Stream your sensor data seamlessly!

AdvancedFull instructions provided5 hours6,709
CyberSafe: Your Personal Cloud IoT Platform

Things used in this project

Hardware components

Raspberry Pi 1 Model B
Raspberry Pi 1 Model B
×1
Wifi Adapter for Raspberry Pi
×1
HDMI to VGA Converter
×1
Temperature Sensor
Temperature Sensor
×1
Photo resistor
Photo resistor
×1
Resistor 10k ohm
Resistor 10k ohm
×2

Software apps and online services

PuTTY Terminal
FileZilla File Client

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Digital Multimeter

Story

Read more

Schematics

Sensor Node

Schematic for the sensor board to be connected to the Intel Edison

Code

createtables.py

Python
Used to create tables within native installation of MYSQL server
import MySQLdb
db = MySQLdb.connect('localhost','root',*MYSQL_PASSWORD_HERE*,*DATABASE_NAME_HERE*)
cursor = db.cursor()
cursor.execute('CREATE TABLE EDISONTEMP (READING INT(50) auto_increment primary key, VALUE INT)')
cursor.execute('CREATE TABLE EDISONLIGHT (READING INT(50) auto_increment primary key, VALUE INT)')
db.commit()
db.close()

listen.py

Python
Run in the background on Raspberry Pi to listen for MySQL messages and update the databases
import socket
import MySQLdb
db = MySQLdb.connect('localhost','root',*MYSQL_PASSWORD_HERE*,*DATABASE_NAME_HERE*)
cursor = db.cursor()

TCP_IP = *RASPBERRY PI IP ADDRESS HERE*
TCP_PORT = 5005
BUFFER_SIZE = 25
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((TCP_IP,TCP_PORT))

while 1:
    s.listen(2)
    conn,addr = s.accept()
    data = conn.recv(BUFFER_SIZE)
    recdata = str(data)
    typ,val = recdata.split('/')
    print (val)
    val = str(val)
    print (typ)
    typ = str(typ)

    if(typ=='Temperature'):
        com = 'INSERT INTO EDISONTEMP (VALUE) VALUES ('+val+')'
        cursor.execute(com)
        com = 'UPDATE SENSORDATA SET VALUE ='+val+' WHERE SOURCE = "Edison" AND TYPE ="'+typ+'"'
        cursor.execute(com)
        db.commit()
        
    if(typ=='Light'):
        com = 'INSERT INTO EDISONLIGHT (VALUE) VALUES ('+val+')'
        cursor.execute(com)
        com = 'UPDATE SENSORDATA SET VALUE ='+val+' WHERE SOURCE = "Edison" AND TYPE ="'+typ+'"'
        cursor.execute(com)
        db.commit()    

nodesens.py

Python
Run on sensor node to acquire and transport data frame over TCP to Raspberry Pi
import socket
import mraa
import sys
import time

TCP_IP = *RASPBERRY PI IP ADDRESS HERE*
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Edison Here"
hum = mraa.Aio(0)
lig = mraa.Aio(1)

humval = int(hum.read())
ligval = int(lig.read())

time.sleep(10)

while(1):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((TCP_IP, TCP_PORT))

    humval = int(hum.read())
    humval = str(humval)
    print "Temperature : "+humval
    sendis = "Temperature/"+humval
    bytes = str.encode(sendis)
    s.send(bytes)
    time.sleep(2)

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((TCP_IP, TCP_PORT))
    ligval = int(lig.read())
    ligval = str(ligval)
    print "Light : "+ligval
    sendis = "Light/"+ligval
    bytes = str.encode(sendis)
    s.send(bytes)

    print ("Done!")
    print (" ")
    time.sleep(60)

raspi_paas.py

Python
The main code running on the Raspberry Pi for GUI and Graph plotting
from Tkinter import *
import MySQLdb
import numpy as np
import matplotlib.pyplot as plt

def Pressed():
        #function
        db = MySQLdb.connect('localhost','root',*MYSQL_PASSWORD_HERE*,*DATABASE_NAME_HERE*)
        cursor = db.cursor()

        com = 'SELECT VALUE FROM EDISONLIGHT'
        cursor.execute(com)
        op = cursor.fetchall()
        a = np.array(op)
        x = np.arange(0,a.size,1)
        y = a
        plt.plot(x,y)
        plt.show()

        db.commit()
        db.close()

def TPressed():
        #function
        db = MySQLdb.connect('localhost','root','9okm8ijn76','TESTDB')  
        cursor = db.cursor()

        com = 'SELECT VALUE FROM EDISONTEMP'
        cursor.execute(com)
        op = cursor.fetchall()
        a = np.array(op)
        x = np.arange(0,a.size,1)
        y = a
        plt.plot(x,y)
        plt.show()

        db.commit()
        db.close()

        
root = Tk()                             #main window
root.title("Satya's IoT Platform")

org = PanedWindow(root,orient=VERTICAL)
org.pack(fill=BOTH,expand=1)

m1 = PanedWindow(org,orient=HORIZONTAL)
m2 = PanedWindow(org,orient=HORIZONTAL)
m3 = PanedWindow(org,orient=HORIZONTAL)
org.add(m1)
org.add(m2)
org.add(m3)

temp = Label(m1,width=15)
mois = Label(m1,width=15)
light = Label(m1,width=15)
m1.add(temp)
m1.add(mois)
m1.add(light)

tempval = Label(m2)
moisval = Label(m2)
ligval = Label(m2)
m2.add(tempval)
m2.add(moisval)
m2.add(ligval)

tempplot = Label(m3)
moisplot = Label(m3)
ligplot = Label(m3)
m3.add(tempplot)
m3.add(moisplot)
m3.add(ligplot)

this = Text(temp, bg = 'Light Grey', height=1, width = 15,relief=FLAT)
yay = 'Temperature'
this.insert(INSERT,yay)
this.pack()

this = Text(mois, bg = 'Light Grey', height=1, width = 15,relief=FLAT)
yay = 'Moisture'
this.insert(INSERT,yay)
this.pack()

this = Text(light, bg = 'Light Grey', height=1, width = 15,relief=FLAT)
yay = 'Light'
this.insert(INSERT,yay)
this.pack()

this = Text(tempval, bg = 'White', height=1, width = 15)
yay = 'Loading...'
this.insert(INSERT,yay)
this.pack()

moisv = Text(moisval, bg = 'White', height=1, width = 15)
yay = 'Loading...'
moisv.insert(INSERT,yay)
moisv.pack()

lightv = Text(ligval, bg = 'White', height=1, width = 15)
yay = 'Loading...'
lightv.insert(INSERT,yay)
lightv.pack()

button = Button(tempplot, text = 'Plot T', command = TPressed, height=1, width = 10)
button.pack(padx = 2)

button = Button(moisplot, text = 'Plot M', command = Pressed, height=1, width = 10)
button.pack(padx = 2)

button = Button(ligplot, text = 'Plot L', command = Pressed, height=1, width = 10)
button.pack(padx = 2)

def task():
        db = MySQLdb.connect('localhost','root',*MYSQL_PASSWORD_HERE*,*DATABASE_NAME_HERE*)
        cursor = db.cursor()
        
        com = 'SELECT VALUE FROM SENSORDATA WHERE SOURCE = "Edison" AND TYPE ="Temperature"'
        cursor.execute(com)
        why = cursor.fetchone()
        why = str(why[0])
        this.delete('1.0','2.0')
        this.insert(INSERT,why)

        com = 'SELECT VALUE FROM SENSORDATA WHERE SOURCE = "Edison" AND TYPE ="Moisture"'
        cursor.execute(com)
        why = cursor.fetchone()
        why = str(why[0])
        moisv.delete('1.0','2.0')
        moisv.insert(INSERT,why)

        com = 'SELECT VALUE FROM SENSORDATA WHERE SOURCE = "Edison" AND TYPE ="Light"'
        cursor.execute(com)
        why = cursor.fetchone()
        why = str(why[0])
        lightv.delete('1.0','2.0')
        lightv.insert(INSERT,why) 
        
        db.commit()
        db.close()
        root.after(2000,task)

root.after(10000,task)
root.mainloop()

Credits

Satyavrat Wagle
6 projects • 64 followers
I have an active interest in IoT, Wireless Sensor Networks, Single Board Computing Implementations, Embedded Systems, and Robotics.

Comments