Sunteți pe pagina 1din 4

2017822 RealtimetemperaturemonitoringwithBeagleBoneBlackandMQTTprotocolDocumentationDocumentation

Documentation / / BeagleBone

RealtimetemperaturemonitoringwithBeagleBoneBlack
andMQTTprotocol
CreatedbyKaterynaPietukhova,lastmodifiedbyArturDenysenkoonSep30,2014

Componentlist
1.BBBandsensorHardwarepreparation
2.GadgetKeeperconfiguration
3.MQTTclientpreparation
4.Testcommunication
Relatedarticles

Componentlist
1.BeagleBoneBlack
2.DHT11/22orAM2302temperatureandhumiditysensor
3.4.7Kor10Kresistor
4.Breadboard
5.Jumperwires

1.BBBandsensorHardwarepreparation
ToprepareBBBhardwareandsoftwareforsendorusetutorialunderlinkTemperaturemonitoringwithBeagleBoneBlackandDHT11/22
andAM2302temperatureandhumiditysensor.
Youonlyneedtofollowthestepsfrom1to5.Assumingthatnowwehavetheworkingscriptsread_temperature.pyand
read_humidity.pyyoucanfollowtheinstructionsgivenbelow.

2.GadgetKeeperconfiguration
FollowtheInstructionsinImplementRemotePropertytoimplementtheremoteproperty.

Onstep2."CreateMQTTconnectiontodevice",set"Response,ms"to60000ms

3.MQTTclientpreparation
1.createadirectoryMQTT

sudomkdirMQTT
cdMQTT/

2.Downloadthemosquittofromthissiteorsimplyusethefollowingcommand.http://mosquitto.org/download/

sudowgethttp://mosquitto.org/files/source/mosquitto1.0.2.tar.gz

3.Iusedthismosquitto1.0.2.tar.gzversionforthistutorial
4.Followthesecommandstoinstallmosquitto

http://docs.gadgetkeeper.com/display/docs/Realtime+temperature+monitoring+with+BeagleBone+Black+and+MQTT+protocol 1/4
2017822 RealtimetemperaturemonitoringwithBeagleBoneBlackandMQTTprotocolDocumentationDocumentation

sudoaptgetinstalllibwrap0dev
tarzxfmosquitto1.0.2.tar.gz
cdmosquitto1.0.2/
make
sudomakeinstall
sudoldconfig
sudomakeclean
sudoiptablesAINPUTptcpmtcpdport1883jACCEPT

5.Ifitfailsthecompileprocesstrythefollowingsupportivecommandsontheterminaltoinstallthedependencylibraries.Then
compileagain

sudoaptgetinstalllibssldev
sudoaptgetinstallpythonmosquitto

6.NextstepistopreparetheMQTTclientscripttocommunicatewithgadgetkeeperMQTTserver.FollowingPythonscriptcanbe
usedtodoit.FirstgotothegadgetkeeperdirectorywecreatedontheTemperaturemonitoringwithBeagleBoneBlackand
DHT11/22andAM2302temperatureandhumiditysensortutorial.Thencreateafileasfollows

cd/root/gadgetkeeper/
sudonanomqtt_listner.py

ThenpastethefollowingPythoncodeonit.

http://docs.gadgetkeeper.com/display/docs/Realtime+temperature+monitoring+with+BeagleBone+Black+and+MQTT+protocol 2/4
2017822 RealtimetemperaturemonitoringwithBeagleBoneBlackandMQTTprotocolDocumentationDocumentation

#!/usr/bin/python
importtime
importmosquitto
importrandom
importcommands
###############################################

#changethisthingIDasrequired
THING_ID="ecf5ceb002d111e49e72fb53d9d5a540"
BROKER_NAME="api.gadgetkeeper.com"
READ_TEMP="python/root/gadgetkeeper/read_temperature.py1130"
READ_HUMID="python/root/gadgetkeeper/read_humidity.py1130"
###############################################

topic='thing.'+THING_ID
broker=BROKER_NAME
client_name="device1"
last_id=""
defon_message(mosq,obj,msg):
globallast_id
print'Request:'+msg.payload
id,jsn,cmd=msg.payload.split(',',2);
#printid
#printjsn
#printcmd
iflast_id==id:
print'Updatesuccessful'
return
last_id=id
#data_val=20.00
#data_val=random.uniform(1.5,100.9)#genaraterandomvaluefortesting
#readtemperaturefromthesensor
data_value=commands.getoutput(READ_TEMP)
#data_value=commands.getoutput(READ_HUMID)
if'Failed'notindata_value:
data_val=data_value[26:]
#data_val=data_value[23:]
print'Sensorreading:'+data_val
else:
time.sleep(2)#delay2sandtryagain
data_val=commands.getoutput(READ_TEMP)
#data_val=commands.getoutput(READ_HUMID)
if'Failed'indata_value:
data_val=1000.0#Ifsensorreadingfailsassignthisvalue
print'Sensorreaderror'
reply_msg=id+','+jsn+','+"\"result\":"+str(data_val)+'}'
print'Response:'+reply_msg
client_rply=mosquitto.Mosquitto("device1")
client_rply.connect(broker)
client_rply.publish(topic,reply_msg,0)
client=mosquitto.Mosquitto(client_name)
client.connect(broker)
client.subscribe(topic,0)
client.on_message=on_message
whileTrue:
client.loop(15)
time.sleep(2)

http://docs.gadgetkeeper.com/display/docs/Realtime+temperature+monitoring+with+BeagleBone+Black+and+MQTT+protocol 3/4
2017822 RealtimetemperaturemonitoringwithBeagleBoneBlackandMQTTprotocolDocumentationDocumentation

7.Nowsimplyrunthisscriptasfollows

sudopythonmqtt_listner.py

8.Makesurethatport1883isnotblockedbythefirewall.

4.Testcommunication
Nowyoucanusethe"Getpropertyvalue"stepontheImplementRemoteProperty.Simplyclickon"Get"buttonanditshouldfetchthe
newvalue(TemperatureorHumidity)fromtheBBBviatheMQTTprotocol.
Sensorreaderrorwillbeshownasbelow(Ifthesensorisnotavailableorfetchingthetemperatureorhumidityvaluefromthesensoris
failed,thenthefollowingresponsewillbereceivedfromtheMQTTclient.Youcanassignanyvalueforthisastheerrorcodeonthe
pythonscript.Ihaveused1000toindicateerrorreadings)

Successfulcommunicationbetweenuserandserveryoucanseebelow.

Relatedarticles
TemperaturemonitoringwithBeagleBoneBlackandEvent/Trigger
RealtimetemperaturemonitoringwithBeagleBoneBlackandMQTTprotocol
TemperaturemonitoringwithBeagleBoneBlackandDHT11/22andAM2302temperatureandhumiditysensor

http://docs.gadgetkeeper.com/display/docs/Realtime+temperature+monitoring+with+BeagleBone+Black+and+MQTT+protocol 4/4