Component list
- Raspberry Pi
- DHT11/22 or AM2302 temperature and humidity sensor
- 4.7K or 10K resister
- Breadboard
- Jumper wires
1. RPI and sensor Hardware preparation
First of all follow the tutorial on this url to prepare the RPI and sensor hardware.Temperature monitoring with Raspberry Pi and DHT11/22 temperature and humidity sensor
You only need to follow the steps from 1 to 4. Assuming that now we have the working scripts read_temperature.sh
and read_humidity.sh
you can follow the instructions given below.
2. GadgetKeeper configuration
Follow the Instructions in this url and implement the remote property. Implement Remote Property
Use 10000 like bigger value instead of 1000 as the timeout value in the step 2.
3. MQTT client preparation
-
create a directory MQTT
sudo
mkdir
MQTT
cd
MQTT/
-
Download the mosquitto from this site or simply use the following command. http://mosquitto.org/download/
sudo
wget http:
//mosquitto
.org
/files/source/mosquitto-1
.0.2.
tar
.gz
- I used this mosquitto-1.0.2.tar.gz version for this tutorial
-
Follow these commands to install mosquitto
sudo
apt-get
install
libwrap0-dev
tar
zxf mosquitto-1.0.2.
tar
.gz
cd
mosquitto-1.0.2/
make
sudo
make
install
sudo
ldconfig
sudo
make
clean
sudo
iptables -A INPUT -p tcp -m tcp --dport 1883 -j ACCEPT
-
If it fails the compile process try the following supportive commands on the terminal to install the dependency libraries. Then compile again
sudo
apt-get
install
libssl-dev
sudo
apt-get
install
python-mosquitto
-
Next step is to prepare the MQTT client script to communicate with gadgetkeeper MQTT server. Following Python script can be used to do it. First go to the gadgetkeeper directory we created on the Temperature monitoring with Raspberry Pi and DHT11/22 temperature and humidity sensor tutorial. Then create a file as follows
cd
/root/gadgetkeeper/
sudo
nano mqtt_listner.py
Then paste the following Python code on it.
#!/usr/bin/python
import
time
import
mosquitto
import
random
import
commands
###############################################
# change this thing ID as required
THING_ID
=
"c91d2bdbbf3911e3ab9651a372c40aa5"
BROKER_NAME
=
"api.gadgetkeeper.com"
###############################################
topic
=
'thing.'
+
THING_ID
broker
=
BROKER_NAME
client_name
=
"device-1"
last_id
=
""
def
on_message(mosq, obj, msg):
global
last_id
print
'Request: '
+
msg.payload
id
,jsn,cmd
=
msg.payload.split(
','
,
2
);
#print id
#print jsn
#print cmd
if
last_id
=
=
id
:
print
'Update successful'
return
last_id
=
id
#data_val = 20.00
#data_val = random.uniform(1.5, 100.9) #genarate random value for testing
#read temperature from the sensor
shl_cmd
=
'/root/gadgetkeeper/read_temperature.sh'
#read humidity from the sensor
#shl_cmd = '/root/gadgetkeeper/read_humidity.sh'
data_val
=
commands.getoutput(shl_cmd)
if
data_val:
print
'Sensor reading: '
+
data_val
else
:
time.sleep(
2
)
#delay 2s and try again
data_val
=
commands.getoutput(shl_cmd)
if
data_val:
print
'Sensor reading: '
+
data_val
else
:
data_val
=
-
1000.0
# If sensor reading fails assign this value
print
'Sensor read error'
reply_msg
=
id
+
','
+
jsn
+
','
+
"\"result\":"
+
str
(data_val)
+
'}'
print
'Response: '
+
reply_msg
#client_rply = mosquitto.Mosquitto("device-1")
#client_rply.connect(broker)
#client_rply.publish(topic, reply_msg ,0)
reply_msg_new
=
reply_msg.replace(
'"', '\\"')
shl_cmd = '/usr/local/bin/mosquitto_pub -h ' + broker + ' -p 1883 -t ' + topic + ' -m "' + reply_msg_new + '"'
#print shl_cmd
reply_prog
=
commands.getoutput(shl_cmd)
client
=
mosquitto.Mosquitto(client_name)
client.connect(broker)
client.subscribe(topic,
0
)
client.on_message
=
on_message
while
True
:
client.loop(
15
)
time.sleep(
2
)
-
Now simply run this script as follows
sudo
python mqtt_listner.py
- Make sure that port 1883 is not blocked by the firewall.
4. Test the communication
Now you can use the "Get property value" step on the Implement Remote Property .Simply click on "Get" button and it should fetch the new value (Temperature or Humidity) from the RPI via the MQTT protocol.
Sensor read error will be shown as below (If the sensor is not available or fetching the temperature or humidity value from the sensor is failed, then the following response will be received from the MQTT client. You can assign any value for this as the error code on the python script. I have used -1000 to indicate error readings)
This window shows the communication between the client and server.
Following window shows a successful sensor reading upload to the server. Here the sensor value is 62.33
This window shows the communication between the client and server.