Add python monitor

master
esensar 2017-12-26 02:14:51 +01:00
parent fffbe99d4d
commit ae1e5f0bcd
2 changed files with 56 additions and 2 deletions

View File

@ -2,8 +2,10 @@
Simple MQTT example made for university project with [Dino Dizdarević](https://github.com/dizda13). Simple MQTT example made for university project with [Dino Dizdarević](https://github.com/dizda13).
Relies on a wifi connection which needs to be configured in code (ssid and password are hardcoded) and on [Energia IDE](http://energia.nu/). If everything is set up properly, device will try to connect to the MQTT server. If it fails, secondary LED will light up red. This project is just a simple representation of a very simple control/monitoring via lightweight MQTT protocol.
Relies on a wifi connection which needs to be configured in code (ssid and password are hardcoded) and on [Energia IDE](http://energia.nu/). If everything is set up properly, device will try to connect to the MQTT server. If it fails, secondary LED will light up red. For a simple MQTT broker, [Mosquitto](https://mosquitto.org/) was used.
After connection to MQTT server is made, device will publish rssi to the server every 5 seconds and it will also publish button presses. It will subscribe to topics which allow turning on 3 leds (red, green and blue). It will also subscribe to topic which allows the error led to be cleared. After connection to MQTT server is made, device will publish rssi to the server every 5 seconds and it will also publish button presses. It will subscribe to topics which allow turning on 3 leds (red, green and blue). It will also subscribe to topic which allows the error led to be cleared.
This project is just a simple representation of a very simple control/monitoring via lightweight MQTT protocol. Simple MQTT monitor written in Python is included in the repository to connect to broker and monitor the device. Monitor will automatically publish led controls based on button presses received from the device. It is written in for Python2.x and relies on [paho-mqtt library](https://pypi.python.org/pypi/paho-mqtt/1.1).

52
mqtt_monitor.py 100644
View File

@ -0,0 +1,52 @@
import paho.mqtt.client as mqtt
import time
TOPIC_NAME = 'devices/#'
LIGHTUP_LED = "devices/red/controls/led"
color=""
client = mqtt.Client()
def setColor(number):
color+=number
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(TOPIC_NAME)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
topicArray = msg.topic.split("/")
global color
if topicArray[3] == "left":
color+="1"
if topicArray[3] == "right":
color+="0"
if topicArray[3] == "wifi":
print("Wifi strenght is "+str(msg.payload))
if len(color) == 3:
i=3
while i>0:
print(i)
time.sleep(1)
i-=1
client.publish(LIGHTUP_LED, color, 0, False)
print("Message sent \n")
color=""
client.on_connect = on_connect
client.on_message = on_message
ip = raw_input("Enter brooker ip address \n")
client.connect(ip, 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()