├── mydb.db ├── .gitignore ├── .gitattributes ├── dbSetup.py ├── templates └── index.html ├── sender.py ├── README.md ├── DHT_Sensor_data_SENDER.py └── reciever.py /mydb.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ranajoy-dutta/IoT-MQTT/master/mydb.db -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows thumbnail cache files 2 | Thumbs.db 3 | ehthumbs.db 4 | ehthumbs_vista.db 5 | 6 | # Folder config file 7 | Desktop.ini 8 | 9 | # Recycle Bin used on file shares 10 | $RECYCLE.BIN/ 11 | 12 | # Windows Installer files 13 | *.cab 14 | *.msi 15 | *.msm 16 | *.msp 17 | 18 | # Windows shortcuts 19 | *.lnk 20 | 21 | # ========================= 22 | # Operating System Files 23 | # ========================= 24 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /dbSetup.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | 3 | try: 4 | conn = sqlite3.connect("mydb.db") 5 | cur = conn.cursor() 6 | cur.execute("CREATE TABLE dataTable (" 7 | "id INTEGER PRIMARY KEY AUTOINCREMENT, state TEXT(5), timestamp INTEGER)") 8 | 9 | conn.commit() 10 | cur.close() 11 | conn.close() 12 | print("Table Successfully Created!") 13 | except Exception as e: 14 | print("An error occurred!\n", 15 | "Reason : ",e) 16 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Welcome|Dashboard 5 | 6 | 11 | 12 | 13 |
14 |
15 |
16 |

Welcome {{user}}!

17 |

18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | {% for row in rows %} 31 | 32 | 33 | 34 | 35 | 36 | {% endfor %} 37 | 38 |
SNoNew StateTime
{{row["id"]}}{{row["new_state"]}} {{ row["timestamps"]}}
39 |
40 | 41 |
42 |

43 | 44 | 45 | -------------------------------------------------------------------------------- /sender.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | How this file works : 4 | sender.py is flashed into the micro controller along with the required code for fetching data from necessary sensors. 5 | This file then publishes(sends) the data to the broker. Broker can be a public broker like iot.eclipse.org or 6 | test.mosquitto.org or broker.hivemq.com. Or it can be a custom server that supports MQTT protocols. 7 | 8 | Required libraries (dependencies): 9 | python v3 10 | paho-mqtt 11 | """ 12 | # currently this test file sends a toggle value of on/off along with a timestamp. This can be changed with your sensor values 13 | 14 | import paho.mqtt.client as mqtt #import the client1 15 | import time,json 16 | 17 | 18 | # broker_address="192.168.1.184" 19 | broker_address="iot.eclipse.org" 20 | print("creating new instance") 21 | client = mqtt.Client("akrdClient") # create new instance 22 | try: 23 | print("connecting to broker") 24 | client.connect(broker_address) # connect to broker 25 | client.loop_start() # start the loop 26 | 27 | # test toggle program sends value only 10 times. This can be changed to work infinite times or upto desired limit 28 | val = 'ON' 29 | i=0 30 | while i<10: 31 | if val == 'ON': 32 | val = 'OFF' 33 | else: 34 | val ='ON' 35 | data = json.dumps({'state':val,'timestamp':time.time()}) # json is a recommended data format 36 | print("Publishing message to topic","sigmaway/akrd/db1") 37 | client.publish("sigmaway/akrd/db1" 38 | "",data) 39 | print('data = ',data,i+1) 40 | time.sleep(4) # wait 41 | i+=1 42 | except Exception as e: 43 | print("Error occured : ", e) 44 | 45 | client.loop_stop() #stop the loop 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IoT - MQTT using Python 2 | ### This is a Prototype model for IoT. This model is suitable for any IoT device that supports Python. The system can be integrated to send data from the device to the cloud using MQTT protocol, which is a lightweight protocol for machine to machine communication.
On the other end, the system can be integrated for receiving the data over cloud for further analysis.

3 | 4 | ### How the system works :
5 | > The sender.py file is flashed into any micro controller like Arduino/Raspberry/Onion.
6 | The sender.py file is responsible for sending/publishing the data/values/parameter as a payload to cloud/broker. 7 | This data can collection of data fetched from the sensors. Broker is responsible for forwarding the same to all the 8 | subscribed listeners. The receiver.py is one of the subscribed listener. Once the receiver receives this 9 | payload/data, it can manage this data as per its needs like for just storing into data or for further analysis and 10 | machine learning and many more purposes.

11 | 12 | ### FILE LIST :- 13 | > IoT1 14 | |- templates 15 | >- index.html 16 | |- dbSetup.py 17 | |- README.md 18 | |- mydb.db 19 | |- receiver.py 20 | |- sender.py 21 | 22 | ### Dependencies in receiving server :-
23 | > python v3
24 | flask
25 | flask-mqtt
26 | flask-socketio
27 | eventlet

28 | 29 | ### Dependencies in Sending equipment :-
30 | > python v3
31 | paho-mqtt

32 | 33 | > Suitable Public Brokers for MQTT :-
34 | 1. Eclipse - iot.eclipse.org
35 | 2. Mosquitto - test.mosquitto.org
36 | 3. Hive - broker.hivemq.com

37 | 38 | > You can also create your own private Broker for MQTT by using the software provided by hive at https://www.hivemq.com/downloads/ 39 |


40 | 41 | ### The project is still under its initial stages of development and we welcome any kind of suggestions or improvements. 42 | Project being improved by me and [@anantkaushik](https://github.com/anantkaushik) 43 | -------------------------------------------------------------------------------- /DHT_Sensor_data_SENDER.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This file is responsible for collecting humidity and temperature from DHT-11 sensor at regular intervals and send the same 4 | to MQTT broker in json format. 5 | 6 | How this file works : 7 | sender.py is flashed into the micro controller along with the required code for fetching data from necessary sensors. 8 | This file then publishes(sends) the data to the broker. Broker can be a public broker like iot.eclipse.org or 9 | test.mosquitto.org or broker.hivemq.com. Or it can be a custom server that supports MQTT protocols. 10 | 11 | Required libraries (dependencies): 12 | python v3 13 | paho-mqtt 14 | """ 15 | # currently this test file sends a toggle value of on/off along with a timestamp. This can be changed with your sensor values 16 | 17 | import subprocess 18 | import paho.mqtt.client as mqtt # import the client1 19 | import time, json 20 | 21 | # broker_address="192.168.1.184" 22 | broker_address = "iot.eclipse.org" 23 | print("creating new instance") 24 | client = mqtt.Client("akrdClient") # create new instance 25 | try: 26 | print("connecting to broker") 27 | client.connect(broker_address) # connect to broker 28 | client.loop_start() # start the loop 29 | i=0 30 | while i < 25: 31 | pinNumber = 0 32 | sensorModel = 'DHT11' 33 | proc = subprocess.Popen(['dht-sensor ' + str(pinNumber) + ' ' + sensorModel], stdout=subprocess.PIPE, 34 | shell=True) 35 | (out, err) = proc.communicate() 36 | sensor_data = out.split('\n') 37 | humidity = sensor_data[0] 38 | temperature = sensor_data[1] 39 | # print "Humidity:" 40 | print 41 | str(humidity) + "%" 42 | # print "Temperature:" 43 | print 44 | str(temperature) + "°C\n" 45 | data = json.dumps({'humidity': humidity, 'temperature': temperature, 46 | 'timestamp': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( 47 | time.time()))}) # json is a recommended data format 48 | 49 | print("Publishing message to topic", "sigmaway/akrd/db1") 50 | client.publish("sigmaway/akrd/db1" 51 | "", data) 52 | print('data = ', data, i + 1) 53 | time.sleep(3.5) # wait 54 | i += 1 55 | except Exception as e: 56 | print("Error occured : ", e) 57 | 58 | client.loop_stop() # stop the loop 59 | -------------------------------------------------------------------------------- /reciever.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file, along with the HTML page, needs to be uploaded on cloud service which supports web sockets like AWS. 3 | 4 | How this file works : 5 | This file first subscribes to a topic when the HTML page is opened. Then it will receive any message with same topic 6 | over the MQTT broker. Once the message is received, this file stored the value into the SQLite3 Database. Every time 7 | the page is reloaded, the displayed data is refreshed(again fetched from the database). 8 | 9 | Required python libraries(dependencies): (use pip install) 10 | python v3 11 | flask 12 | flask-mqtt 13 | flask-socketio 14 | eventlet 15 | 16 | References : 17 | MQTT - https://mqtt.org/tag/paho (lightweight connectivity protocol for M2M communication) 18 | flask - http://flask.pocoo.org/ (Micro web framework written in python) 19 | flask-socket - https://flask-socketio.readthedocs.io/en/latest/ (web sockets compatible with flask) 20 | SQLite3 - https://www.sqlite.org/index.html (lightweight relational database) 21 | custom broker - https://www.hivemq.com/downloads/ (Creating your own custom broker) 22 | """ 23 | 24 | # this is a test program which receives the the timestamp when the light is turned on or off and stores it into the database 25 | 26 | from flask import Flask, render_template, redirect 27 | from flask_socketio import SocketIO 28 | from flask_mqtt import Mqtt 29 | import eventlet, sqlite3 as sql,json 30 | 31 | # initialising flask 32 | app = Flask(__name__, static_url_path='') 33 | eventlet.monkey_patch() 34 | 35 | # configuring app for required MQTT parameters 36 | app.secret_key = 'anantranajoykey' #keep it secret 37 | app.config['SECRET'] = '121202' #keep it secret 38 | app.config['TEMPLATES_AUTO_RELOAD'] = True 39 | app.config['MQTT_BROKER_URL'] = 'iot.eclipse.org' # suitable free brokers - iot.eclipse.org / test.mosquitto.org / broker.hivemq.com 40 | app.config['MQTT_BROKER_PORT'] = 1883 41 | app.config['MQTT_USERNAME'] = 'Ranajoy' 42 | app.config['MQTT_PASSWORD'] = '123456' 43 | app.config['MQTT_KEEPALIVE'] = 10 44 | app.config['MQTT_TLS_ENABLED'] = False 45 | app.config['MQTT_LAST_WILL_TOPIC'] = 'sigmaway/akrd/lastwill' 46 | app.config['MQTT_LAST_WILL_MESSAGE'] = 'bye' 47 | app.config['MQTT_LAST_WILL_QOS'] = 2 48 | 49 | 50 | # initialising MQTT 51 | mqtt = Mqtt(app) 52 | 53 | # initialising socket 54 | socketio = SocketIO(app) 55 | 56 | # Function for storing data into database table 57 | def toDatabase(data): 58 | try: 59 | conn = sql.connect('mydb.db') # connecting to database 60 | cur = conn.cursor() # setting the cursor 61 | # SQL query for insertion into table 62 | cur.execute("INSERT INTO dataTable (new_state, timestamps) VALUES ('%s','%f')"%(data['state'],data['timestamp'])) 63 | conn.commit() # saving the changes 64 | cur.close() # closing cursor 65 | conn.close() # closing the connection 66 | # print("Successfully stored into Database") 67 | except Exception as e: 68 | print("An error occurred\n" 69 | "Error : ", e) 70 | 71 | # function for fetching data from database table 72 | def al(): 73 | con = sql.connect("mydb.db") # creating connection with database 74 | con.row_factory = sql.Row 75 | cur = con.cursor() # setting the cursor 76 | cur.execute("select * from dataTable") # SQL query for fetching all of the data from the table 77 | rows = cur.fetchall(); # fetching the data and storing it into a variable 78 | cur.close() # closing the cursor 79 | con.close() # closing the connection 80 | return rows 81 | 82 | # routing home page (HTML page) 83 | @app.route('/') 84 | def index(): 85 | handle_subscribe() # Function Call to subscribe to the topic 86 | rows = al() # function call to fetch values from the Table (Database) 87 | return render_template('index.html', rows = rows) # rendering the HTML page and passing the fetched values 88 | 89 | # function to subscribe to the topic 90 | @socketio.on('subscribe') 91 | def handle_subscribe(): 92 | topic = 'sigmaway/akrd/db1' # topic name, must be same as the topic name of publisher 93 | qos = 0 94 | mqtt.subscribe(topic, qos) # MQTT function call to subscribe to the topic 95 | # print("*********Susbscibed !! *************") 96 | 97 | # function to print the log data 98 | @mqtt.on_log() 99 | def handle_logging(client, userdata, level, buf): 100 | print(level, buf) 101 | 102 | # function to receive the message from the broker 103 | @mqtt.on_message() # triggered when any message is sent from publisher (prior subscription is must) 104 | def handle_mqtt_message(client, userdata, message): 105 | payload=message.payload.decode() # decoding the message 106 | # print("***** message received = \n", payload, "*****") 107 | if payload != "": # if payload is not empty 108 | json_acceptable_string = payload.replace("'", "\"") 109 | data = json.loads(json_acceptable_string) # converting data into json format 110 | toDatabase(data) # function call for storing data into database 111 | 112 | # function to clear the data in table (triggered on button click from the HTML page) 113 | @app.route('/clearData') 114 | def clearData(): 115 | try: 116 | conn = sql.connect('mydb.db') 117 | cur = conn.cursor() 118 | cur.execute("DELETE FROM dataTable") 119 | cur.execute("DELETE FROM sqlite_sequence WHERE name = 'dataTable';") # resetting the autoincrement field 120 | conn.commit() 121 | cur.close() 122 | conn.close() 123 | # print("Database Cleared") 124 | except Exception as e: 125 | print("An error occurred\n" 126 | "Error : ", e) 127 | finally: 128 | return redirect('/') # return Home Page 129 | 130 | # running the main thread 131 | if __name__ == '__main__': 132 | # running the socket at localhost at port 5000 133 | # set debug = False before deploying it for commercial use 134 | socketio.run(app, host='127.0.0.1', port=5000, use_reloader=True, debug=True) 135 | --------------------------------------------------------------------------------